stream_view.c 11.3 KB
Newer Older
Ludovic Fauvet's avatar
Ludovic Fauvet committed
1
/*****************************************************************************
Ludovic Fauvet's avatar
Ludovic Fauvet committed
2
 * Copyright © 2015-2016 VideoLAN, VideoLabs SAS
Ludovic Fauvet's avatar
Ludovic Fauvet committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *****************************************************************************
 *
 * Authors: Ludovic Fauvet <etix@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
/*
 * By committing to this project, you allow VideoLAN and VideoLabs to relicense
 * the code to a different OSI approved license, in case it is required for
 * compatibility with the Store
 *****************************************************************************/

#include "common.h"
Ludovic Fauvet's avatar
Ludovic Fauvet committed
28
#include "stream_view.h"
Ludovic Fauvet's avatar
Ludovic Fauvet committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

#include "ui/interface.h"
#include "ui/utils.h"
#include "ui/menu/popup_menu.h"

#include <sqlite3.h>

struct view_sys
{
    interface           *p_intf;
    Elm_Object_Item     *current_item;

    /* Widgets */
    Evas_Object *layout;
    Evas_Object *uri;
    Evas_Object *history;
    Evas_Object *current_popup;

    /* Database */
    sqlite3 *db;
};

typedef struct {
    char *uri;
    view_sys *p_sys;
} item_data;

void
stream_view_load_history(view_sys *p_sys);

static void
stream_view_play(view_sys *p_sys, const char *psz_path)
{
    int rc;
    sqlite3_stmt *stmt;

    rc = sqlite3_prepare_v2(p_sys->db, "REPLACE INTO stream_history (URI) VALUES (?);", -1, &stmt, 0);
    if (rc != SQLITE_OK)
    {
        LOGE("Unable to prepare statement");
        goto play;
    }

    rc = sqlite3_bind_text(stmt, 1, psz_path, -1, SQLITE_STATIC);
    if (rc != SQLITE_OK)
    {
        LOGE("Unable to bind text");
        goto play;
    }

    rc = sqlite3_step(stmt);
    if (rc == SQLITE_ERROR)
    {
        LOGE("Unable to execute statement: %s", sqlite3_errmsg(p_sys->db));
        goto play;
    }

play:
87
    intf_video_player_play(p_sys->p_intf, psz_path, 0);
Ludovic Fauvet's avatar
Ludovic Fauvet committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

    if (rc == SQLITE_DONE || rc == SQLITE_OK)
    {
        // Reload the list for reordering
        stream_view_load_history(p_sys);
    }
}

static void
stream_view_uri_activated(void *data, Evas_Object *obj, void *event_info)
{
    view_sys *p_sys = data;

    const char *psz_path = elm_entry_entry_get(p_sys->uri);

    if (strlen(psz_path) == 0)
        return;

    stream_view_play(p_sys, psz_path);
    elm_entry_entry_set(p_sys->uri, "");
}

static bool
stream_view_has_menu(view_sys *p_sys)
{
    return false;
}

void
clicked_reuse(void *data, Evas_Object *obj, void *event_info)
{
    view_sys *p_sys = data;
    item_data *item_data = elm_object_item_data_get(p_sys->current_item);

    elm_entry_entry_set(p_sys->uri, item_data->uri);
123
    popup_close(p_sys->current_popup);
Ludovic Fauvet's avatar
Ludovic Fauvet committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    p_sys->current_popup = NULL;
}

void
clicked_remove(void *data, Evas_Object *obj, void *event_info)
{
    int rc;
    sqlite3_stmt *stmt;
    view_sys *p_sys = data;
    item_data *item_data = elm_object_item_data_get(p_sys->current_item);

    rc = sqlite3_prepare_v2(p_sys->db, "DELETE FROM stream_history WHERE URI=?;", -1, &stmt, 0);
    if (rc != SQLITE_OK)
    {
        LOGE("Unable to prepare statement");
        return;
    }

    rc = sqlite3_bind_text(stmt, 1, item_data->uri, -1, SQLITE_STATIC);
    if (rc != SQLITE_OK)
    {
        LOGE("Unable to bind text");
        return;
    }

    rc = sqlite3_step(stmt);
    if (rc == SQLITE_ERROR)
    {
        LOGE("Unable to execute statement: %s", sqlite3_errmsg(p_sys->db));
        return;
    }

    elm_object_item_del(p_sys->current_item);
157
    popup_close(p_sys->current_popup);
Ludovic Fauvet's avatar
Ludovic Fauvet committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
    p_sys->current_popup = NULL;
}

static popup_menu longpress_menu[] =
{
        {"Reuse",   NULL,   clicked_reuse},
        {"Remove",  NULL,   clicked_remove},
        {0}
};

void stream_view_clear_popup_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
{
    view_sys *p_sys = data;
    p_sys->current_item = NULL;
    p_sys->current_popup = NULL;
}

static void
stream_view_history_longpress_callback(void *data, Evas_Object *obj, void *event_info)
{
    view_sys *p_sys = data;
    Elm_Object_Item *it = event_info;

    p_sys->current_item = it;

    Evas_Object *popup = p_sys->current_popup = popup_menu_orient_add(longpress_menu, ELM_POPUP_ORIENT_CENTER, p_sys, p_sys->layout);
    evas_object_show(popup);
    evas_object_event_callback_add(popup, EVAS_CALLBACK_FREE, stream_view_clear_popup_cb, p_sys);
}

static void
stream_view_item_clicked_cb(void *data, Evas_Object *obj, void *event_info)
{
    item_data *item_data = data;
    view_sys *p_sys = item_data->p_sys;

    if (p_sys->current_popup != NULL)
    {
        // A popup is open, discard event.
        return;
    }

    stream_view_play(p_sys, item_data->uri);
}

void
stream_view_item_del_cb(void *data, Evas_Object *obj, void *event_info)
{
    item_data *item_data = data;
    free(item_data->uri);
    free(item_data);
}

int
stream_view_history_result_cb(
        void* data,    /* Data provided in the 4th argument of sqlite3_exec() */
        int argc,      /* The number of columns in row */
        char** argv,   /* An array of strings representing fields in the row */
        char** colname    /* An array of strings representing column names */
        )
{
    view_sys *p_sys = data;

    if (argc <= 0)
        return 1;

    item_data *item_data = malloc(sizeof(*item_data));
    item_data->p_sys = p_sys;
    item_data->uri = strdup(argv[0]);

    Elm_Object_Item *it = elm_list_item_append(p_sys->history, item_data->uri, NULL, NULL, stream_view_item_clicked_cb, item_data);

    // Add a callback to free the item when deleted
    elm_object_item_del_cb_set(it, stream_view_item_del_cb);

    return 0;
}

void
stream_view_load_history(view_sys *p_sys)
{
    int rc;
    char *error;

    elm_list_clear(p_sys->history);
    p_sys->current_item = NULL;

    const char *req = "SELECT URI FROM stream_history ORDER BY datetime(Timestamp) DESC";

    rc = sqlite3_exec(p_sys->db, req, stream_view_history_result_cb, p_sys, &error);
    if( rc != SQLITE_OK )
    {
        LOGE("SQL Error: %s", error);
        sqlite3_free(error);
    }

    elm_list_go(p_sys->history);
}

void
stream_view_prepare_database(view_sys *p_sys)
{
    int rc;
    char *error;

    char *appdata = system_storage_appdata_get();
    char *dbpath;
    asprintf(&dbpath, "%s/streams.db", appdata);
    rc = sqlite3_open(dbpath, &p_sys->db);
    free(appdata);
    free(dbpath);

    if (rc != SQLITE_OK)
    {
        LOGE("Unable to open the database");
        return;
    }

    const char* req = "CREATE TABLE IF NOT EXISTS stream_history (" \
            "URI PRIMARY KEY NOT NULL," \
            "Timestamp DATETIME DEFAULT CURRENT_TIMESTAMP" \
            ");";

    rc = sqlite3_exec(p_sys->db, req, NULL, NULL, &error);
    if (rc != SQLITE_OK)
    {
        LOGE("SQL Error: %s", error);
        sqlite3_free(error);
    }
}

static bool
stream_view_callback(view_sys *p_view_sys, interface_view_event event)
{
    switch (event) {
    case INTERFACE_VIEW_EVENT_BACK:
            if (p_view_sys->current_popup) {
295
                popup_close(p_view_sys->current_popup);
Ludovic Fauvet's avatar
Ludovic Fauvet committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
                p_view_sys->current_popup = NULL;
                return true;
            }
        return false;
    default:
        break;
    }
    return true;
}

interface_view *
create_stream_view(interface *intf, Evas_Object *parent)
{
    interface_view *view = calloc(1, sizeof(*view));

    /* Setup the audio_view */
    view_sys *stream_view_sys = calloc(1, sizeof(*stream_view_sys));
    stream_view_sys->p_intf = intf;

    //view->pf_event = stream_view_callback;
    view->p_view_sys = stream_view_sys;
    view->pf_has_menu = stream_view_has_menu;
    view->pf_event = stream_view_callback;

    /* Open/prepare database */
    stream_view_prepare_database(stream_view_sys);

    /* Create layout and set the theme */
    Evas_Object *layout = stream_view_sys->layout = elm_layout_add(parent);
    elm_layout_theme_set(layout, "layout", "application", "default");

    /* Create the background */
    Evas_Object *bg = elm_bg_add(layout);
    elm_bg_color_set(bg, 255, 255, 255);
    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
    evas_object_size_hint_align_set(bg, EVAS_HINT_FILL, EVAS_HINT_FILL);
    evas_object_show(bg);

    /* Set the background to the theme */
    elm_object_part_content_set(layout, "elm.swallow.bg", bg);

    /* Content table */
    Evas_Object *table = elm_table_add(layout);
    evas_object_size_hint_weight_set(table, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
    evas_object_size_hint_align_set(table, EVAS_HINT_FILL, EVAS_HINT_FILL);
    evas_object_show(table);

    /* Set the content to the theme */
    elm_object_part_content_set(layout, "elm.swallow.content", table);

    /* */
    Evas_Object *entry = stream_view_sys->uri = elm_entry_add(table);
    elm_entry_single_line_set(entry, EINA_TRUE);
    elm_entry_scrollable_set(entry, EINA_TRUE);
    elm_entry_input_panel_layout_set(entry, ELM_INPUT_PANEL_LAYOUT_URL);
    elm_entry_input_panel_return_key_type_set(entry, ELM_INPUT_PANEL_RETURN_KEY_TYPE_GO);
    elm_entry_prediction_allow_set(entry, EINA_FALSE);
    evas_object_size_hint_weight_set(entry, 0.9, EVAS_HINT_EXPAND);
    evas_object_size_hint_align_set(entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
    elm_object_part_text_set(entry, "guide", "e.g. http://, mms:// or rtsp://");
    evas_object_show(entry);

    Evas_Textblock_Style *style = evas_textblock_style_new();
    evas_textblock_style_set(style, "color=#ff0000");

    evas_object_textblock_style_set(entry, style);

    elm_table_pack(table, entry, 0, 0, 1, 1);

    /* */
    Evas_Object *button = elm_button_add(table);
    elm_object_text_set(button, "Go");
    evas_object_size_hint_weight_set(button, 0.1, EVAS_HINT_EXPAND);
    evas_object_size_hint_align_set(button, EVAS_HINT_FILL, EVAS_HINT_FILL);
    evas_object_show(button);

    evas_object_smart_callback_add(button, "clicked", stream_view_uri_activated, stream_view_sys);

    elm_table_pack(table, button, 1, 0, 1, 1);

    /* */
    Evas_Object *history = stream_view_sys->history = elm_list_add(table);
    elm_scroller_policy_set(history, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_ON);
    elm_scroller_bounce_set(history, EINA_TRUE, EINA_TRUE);

    stream_view_load_history(stream_view_sys);

    evas_object_smart_callback_add(history, "longpressed", stream_view_history_longpress_callback, stream_view_sys);
    evas_object_size_hint_weight_set(history, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
    evas_object_size_hint_align_set(history, EVAS_HINT_FILL, EVAS_HINT_FILL);
    evas_object_show(history);

    elm_table_pack(table, history, 0, 1, 2, 1);

    /*  */
    evas_object_show(layout);
    view->view = layout;

    return view;
}

void
destroy_stream_view(interface_view *view)
{
    view_sys* p_sys = view->p_view_sys;
    sqlite3_close(p_sys->db);
Ludovic Fauvet's avatar
Ludovic Fauvet committed
402
    evas_object_del(view->view);
Ludovic Fauvet's avatar
Ludovic Fauvet committed
403 404 405 406
    free(p_sys);
    free(view);
}