video_view.c 8.26 KB
Newer Older
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
1 2 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 28
/*****************************************************************************
 * Copyright © 2015 VideoLAN, VideoLabs SAS
 *****************************************************************************
 *
 * Authors: Nicolas Rechatin [nicolas@videolabs.io]
 *
 * 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"

29
#include "controller/video_controller.h"
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
30
#include "ui/interface.h"
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
31 32 33
#include "video_view.h"
#include "video_player.h"

34 35
#include "ui/utils.h"

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
36
#include "media/media_item.h"
37

38 39
#include <Elementary.h>

40
struct view_sys
41
{
42 43
    interface *p_intf;

44
    video_controller* p_controller;
45 46 47

    Evas_Object *p_video_list;
    Elm_Genlist_Item_Class* p_default_item_class;
48
};
49

50 51
typedef struct video_list_item
{
52
    view_sys *videoview;
53

54
    media_item *p_media_item;
55

56 57
    const Elm_Genlist_Item_Class *itc;

58 59
    //For refresh purposes.
    Elm_Object_Item* p_object_item;
60
} video_list_item;
61

Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
62
void
63
genlist_item_selected_cb(void *data, Evas_Object *obj, void *event_info)
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
64
{
65
    video_list_item *vli = (video_list_item*)data;
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
66

67
    intf_video_player_play(vli->videoview->p_intf, vli->p_media_item->psz_path);
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
68 69 70
}

static void
71
free_list_item(void *data, Evas_Object *obj, void *event_info)
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
72
{
73
    video_list_item *vli = data;
74
    media_item_destroy(vli->p_media_item);
75
    free(vli);
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
76 77 78
}

static char *
79
genlist_text_get_cb(void *data, Evas_Object *obj, const char *part)
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
80
{
81 82
    video_list_item *vli = data;
    const Elm_Genlist_Item_Class *itc = vli->itc;
Thomas Guillem's avatar
Thomas Guillem committed
83 84 85 86 87

    /* Check the item class style and put the current folder or file name as a string */
    /* Then put this string as the genlist item label */
    if (itc->item_style && !strcmp(itc->item_style, "2line.top.3")) {
        if (part && !strcmp(part, "elm.text.main.left.top")) {
88 89 90
            char *buff;
            asprintf(&buff, "<b>%s</b>", media_item_title(vli->p_media_item));
            return buff;
Thomas Guillem's avatar
Thomas Guillem committed
91 92
        }
        else if (!strcmp(part, "elm.text.sub.left.bottom")) {
93
            if(vli->p_media_item->i_duration < 0)
94 95
                return NULL;
            else
96
                return media_timetostr(vli->p_media_item->i_duration/1000);
Thomas Guillem's avatar
Thomas Guillem committed
97 98
        }
        else if (!strcmp(part, "elm.text.sub.right.bottom")) {
99
            if (vli->p_media_item->i_w <= 0 || vli->p_media_item->i_h <= 0)
100 101
                return NULL;
            char *str_resolution;
102
            asprintf( &str_resolution, "%dx%d", vli->p_media_item->i_w, vli->p_media_item->i_h);
103
            return str_resolution;
Thomas Guillem's avatar
Thomas Guillem committed
104 105 106
        }
    }
    return NULL;
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
107 108
}

109 110 111 112 113 114 115
const media_item*
video_list_item_get_media_item(video_list_item* p_item)
{
    return p_item->p_media_item;
}

void
116
video_list_item_set_media_item(video_list_item* p_item, media_item* p_media_item)
117
{
118
    p_item->p_media_item = p_media_item;
119
    ecore_main_loop_thread_safe_call_async((Ecore_Cb)elm_genlist_item_update, p_item->p_object_item);
120 121
}

Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
122
static Evas_Object*
123
genlist_content_get_cb(void *data, Evas_Object *obj, const char *part)
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
124
{
125 126 127 128
    video_list_item *vli = data;
    const Elm_Genlist_Item_Class *itc = vli->itc;

    Evas_Object *layout = NULL;
Thomas Guillem's avatar
Thomas Guillem committed
129 130 131 132 133

    /* Check the item class style and add the object needed in the item class*/
    /* Here, puts the icon in the item class to add it to genlist items */
    if (itc->item_style && !strcmp(itc->item_style, "2line.top.3")) {
        if (part && !strcmp(part, "elm.icon.1")) {
134 135
            layout = elm_layout_add(obj);
            elm_layout_theme_set(layout, "layout", "list/B/type.1", "default");
136 137 138 139 140
            Evas_Object *icon;
            if (vli->p_media_item->psz_snapshot != NULL)
                icon = create_image(layout, vli->p_media_item->psz_snapshot);
            else
                icon = create_image(layout, ICON_DIR "background_cone.png");
141
            elm_layout_content_set(layout, "elm.swallow.content", icon);
Thomas Guillem's avatar
Thomas Guillem committed
142 143 144
        }
    }

145
    return layout;
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
146 147 148
}

static void
149
genlist_loaded_cb(void *data EINA_UNUSED, Evas_Object *obj, void *event_info EINA_UNUSED)
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
150
{
Thomas Guillem's avatar
Thomas Guillem committed
151
    /* Set the callbacks when one of the genlist item is loaded */
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
152 153 154
}

static void
155
genlist_realized_cb(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info)
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
156
{
Thomas Guillem's avatar
Thomas Guillem committed
157
    /* Set the callbacks when one of the genlist item is realized */
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
158 159 160
}

static void
161
genlist_longpressed_cb(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info)
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
162
{
Thomas Guillem's avatar
Thomas Guillem committed
163
    /* Set the callbacks when one of the genlist item is longpress */
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
164 165 166
}

static void
167
genlist_contracted_cb(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info)
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
168
{
Thomas Guillem's avatar
Thomas Guillem committed
169
    Elm_Object_Item *it = event_info;
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
170

Thomas Guillem's avatar
Thomas Guillem committed
171 172
    /* Free the genlist subitems when contracted */
    elm_genlist_item_subitems_clear(it);
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
173 174
}

175
video_list_item *
176
video_view_append_item(view_sys *videoview, media_item* p_item)
177 178 179
{
    /* */
    video_list_item *vli = calloc(1, sizeof(*vli));
180 181
    if (vli == NULL)
        return NULL;
182
    vli->videoview = videoview;
183
    vli->itc = videoview->p_default_item_class;
184 185

    /* Item instantiation */
186
    vli->p_media_item = p_item;
187
    /* Set and append new item in the genlist */
188
    vli->p_object_item = elm_genlist_item_append(videoview->p_video_list,
189
            vli->itc,                       /* genlist item class               */
190 191 192 193 194
            vli,                            /* genlist item class user data     */
            NULL,                           /* genlist parent item for trees    */
            ELM_GENLIST_ITEM_NONE,          /* genlist item type                */
            genlist_item_selected_cb,       /* genlist select smart callback    */
            vli);                           /* genlist smart callback user data */
195 196 197 198 199
    if (vli->p_object_item == NULL)
    {
        free(vli);
        return NULL;
    }
200
    /* */
201
    elm_object_item_del_cb_set(vli->p_object_item, free_list_item);
202
    return vli;
203 204 205
}

void
206
video_view_clear(view_sys* videoview)
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
207
{
208
    elm_genlist_clear(videoview->p_video_list);
209 210
}

211
interface_view*
212
create_video_view(interface *intf, Evas_Object *parent)
213
{
214
    interface_view *view = calloc(1, sizeof(*view));
215 216 217
    view_sys *p_sys = view->p_view_sys = malloc(sizeof(*p_sys));
    p_sys->p_intf = intf;
    p_sys->p_controller = video_controller_create(intf_get_application(intf), p_sys);
218

219
    /* Genlist class */
220 221 222 223
    p_sys->p_default_item_class = elm_genlist_item_class_new();
    p_sys->p_default_item_class->item_style = "2line.top.3";
    p_sys->p_default_item_class->func.text_get = genlist_text_get_cb;
    p_sys->p_default_item_class->func.content_get = genlist_content_get_cb;
224

225 226
    /* Create the Video Genlist */
    Evas_Object *genlist = p_sys->p_video_list = elm_genlist_add(parent);
227 228

    /* Set the genlist modes */
229 230 231 232
    elm_scroller_single_direction_set(genlist, ELM_SCROLLER_SINGLE_DIRECTION_HARD);
    elm_genlist_homogeneous_set(genlist, EINA_TRUE);
    elm_genlist_mode_set(genlist, ELM_LIST_COMPRESS);

233
    /* Set smart Callbacks on the list */
234 235 236 237
    evas_object_smart_callback_add(genlist, "realized", genlist_realized_cb, NULL);
    evas_object_smart_callback_add(genlist, "loaded", genlist_loaded_cb, NULL);
    evas_object_smart_callback_add(genlist, "longpressed", genlist_longpressed_cb, NULL);
    evas_object_smart_callback_add(genlist, "contracted", genlist_contracted_cb, NULL);
238

239 240
    view->view = genlist;

Thomas Guillem's avatar
Thomas Guillem committed
241
    /* */
242
    return view;
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
243
}
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
244 245 246 247

void
destroy_video_view(interface_view *view)
{
248
    elm_genlist_item_class_free(view->p_view_sys->p_default_item_class);
249
    video_controller_destroy(view->p_view_sys->p_controller);
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
250 251
    free(view->p_view_sys);
    free(view);
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
252
}