audio_view.c 7.91 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
/*****************************************************************************
 * 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"
28
#include "system_storage.h"
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
29

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
30 31
#include "ui/interface.h"
#include "ui/views/audio_view.h"
32
#include "ui/views/audio_list_song_view.h"
33
#include "ui/views/audio_list_artist_view.h"
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
34
#include "ui/audio_player.h"
35
#include "ui/menu/popup_menu.h"
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
36

37 38
#include "ui/utils.h"

39
#include "media/media_item.h"
40
#include "media/library/media_library.hpp"
41

42 43
#include <Elementary.h>

44 45 46 47 48 49 50 51 52 53
typedef enum audio_view_type
{
    AUDIO_VIEW_ARTIST,
    AUDIO_VIEW_ALBUM,
    AUDIO_VIEW_SONG,
    AUDIO_VIEW_GENRE,
    //AUDIO_VIEW_PLAYLIST,
    AUDIO_VIEW_MAX,
} audio_view_type;

54
struct view_sys
55
{
56 57
    interface*              p_intf;
    Evas_Object*            nf_toolbar;
58 59
    Evas_Object*            p_parent;
    Evas_Object*            p_overflow_menu;
60
    list_view*              p_lists[AUDIO_VIEW_MAX];
61
    audio_view_type         p_current_tab;
62
};
63

Ludovic Fauvet's avatar
Ludovic Fauvet committed
64
typedef struct toolbar_tab {
65 66
    audio_view_type type;
    void *data;
Ludovic Fauvet's avatar
Ludovic Fauvet committed
67
} toolbar_tab;
68

69
static list_view*
70
create_audio_list_type(view_sys *av, audio_view_type type )
71
{
72
    application* p_app = intf_get_application(av->p_intf);
73 74
    list_view* p_view = av->p_lists[type];
    if(p_view == NULL)
75
    {
76 77 78 79
        switch (type)
        {
        case AUDIO_VIEW_SONG:
        default:
80
            p_view = audio_list_song_view_create(p_app, av->p_intf, av->nf_toolbar);
81 82
            break;
        case AUDIO_VIEW_ARTIST:
83
            p_view = audio_list_artist_view_create(p_app, av->p_intf, av->nf_toolbar);
84
            break;
85 86
        }
        av->p_lists[type] = p_view;
87 88 89
    }
    else
    {
90
        LOGD("Recycling View %i", type);
91 92
    }

93 94
    p_view->pf_show(p_view->p_sys, av->nf_toolbar);
    return p_view;
95 96
}

Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
97
static void
98
tabbar_item_cb(void *data, Evas_Object *obj, void *event_info)
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
99
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
100
    toolbar_tab *item = data;
101
    view_sys *av = item->data;
Thomas Guillem's avatar
Thomas Guillem committed
102

103
    if (av->p_current_tab == item->type)
104 105
        return;

106
    av->p_current_tab = item->type;
107

108 109 110 111 112 113
    create_audio_list_type(av, item->type);
}

void
tabbar_item_del(void *data, Evas_Object *obj, void *event_info)
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
114
    toolbar_tab *item = data;
115 116 117 118 119 120 121
    free(item);
}

static Elm_Object_Item*
toolbar_item_append(Evas_Object *obj, audio_view_type type, const char *label, Evas_Smart_Cb func, void *data)
{
    Elm_Object_Item* it;
Ludovic Fauvet's avatar
Ludovic Fauvet committed
122
    toolbar_tab *it_data = malloc(sizeof(*it_data));
123 124 125 126 127 128 129
    it_data->type = type;
    it_data->data = data;

    it = elm_toolbar_item_append(obj, NULL, label, func, it_data);
    elm_object_item_del_cb_set(it, tabbar_item_del);

    return it;
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
130 131 132
}

static Evas_Object*
133
create_toolbar(view_sys *av, Evas_Object *parent)
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
134
{
Thomas Guillem's avatar
Thomas Guillem committed
135
    /* Create and set the toolbar */
136
    Evas_Object *tabbar = elm_toolbar_add(parent);
Thomas Guillem's avatar
Thomas Guillem committed
137 138 139 140 141 142

    /* Set the toolbar shrink mode to NONE */
    elm_toolbar_shrink_mode_set(tabbar, ELM_TOOLBAR_SHRINK_SCROLL);
    /* Expand the content to fill the toolbar */
    elm_toolbar_transverse_expanded_set(tabbar, EINA_TRUE);
    /* Items will only call their selection func and callback when first becoming selected*/
143
    elm_toolbar_select_mode_set(tabbar, ELM_OBJECT_SELECT_MODE_ALWAYS);
Thomas Guillem's avatar
Thomas Guillem committed
144

145 146 147
    evas_object_size_hint_weight_set(tabbar, EVAS_HINT_EXPAND, 0.0);
    evas_object_size_hint_align_set(tabbar, EVAS_HINT_FILL, EVAS_HINT_FILL);

Thomas Guillem's avatar
Thomas Guillem committed
148 149 150 151
    evas_object_size_hint_min_set(tabbar, 450, 400);
    evas_object_size_hint_max_set(tabbar, 450, 400);

    /* Append new entry in the toolbar with the Icon & Label wanted */
152 153 154 155
    toolbar_item_append(tabbar, AUDIO_VIEW_ARTIST,  "Artists",  tabbar_item_cb, av);
    toolbar_item_append(tabbar, AUDIO_VIEW_ALBUM,   "Albums",   tabbar_item_cb, av);
    toolbar_item_append(tabbar, AUDIO_VIEW_SONG,    "Songs",    tabbar_item_cb, av);
    toolbar_item_append(tabbar, AUDIO_VIEW_GENRE,   "Genre",    tabbar_item_cb, av);
Thomas Guillem's avatar
Thomas Guillem committed
156 157

    return tabbar;
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
158 159
}

160 161
static void
audio_view_refresh_cb(void *data, Evas_Object *obj, void *event_info)
162 163 164 165 166 167
{
    view_sys *p_sys = data;

    if (!p_sys)
        return;

168 169
    application* p_app = intf_get_application(p_sys->p_intf);
    media_library* p_ml = (media_library*)application_get_media_library(p_app);
170 171 172 173 174 175 176 177
    if (p_ml != NULL)
        media_library_reload(p_ml);

    /* */
    evas_object_del(obj);
    p_sys->p_overflow_menu = NULL;
}

178
static popup_menu audio_view_popup_menu[] =
179 180 181 182 183
{
        {"Refresh", NULL, audio_view_refresh_cb},
        {0}
};

184
static bool
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
audio_view_callback(view_sys *p_view_sys, interface_view_event event)
{
    switch (event) {
    case INTERFACE_VIEW_EVENT_MENU:
    {
        p_view_sys->p_overflow_menu = popup_menu_add(audio_view_popup_menu, p_view_sys, p_view_sys->p_parent);
        evas_object_show(p_view_sys->p_overflow_menu);
        return true;
    }
    case INTERFACE_VIEW_EVENT_BACK:
        if (p_view_sys->p_overflow_menu) {
            evas_object_del(p_view_sys->p_overflow_menu);
            p_view_sys->p_overflow_menu = NULL;
            return true;
        }
        return false;
    default:
        break;
    }

    return false;
}

208
static bool
209 210 211 212
audio_view_has_menu(view_sys *p_view_sys)
{
    return true;
}
213

214
interface_view *
Thomas Guillem's avatar
Thomas Guillem committed
215
create_audio_view(interface *intf, Evas_Object *parent)
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
216
{
217 218
    interface_view *view = calloc(1, sizeof(*view));

219
    /* Setup the audio_view */
220 221
    view_sys *audio_view_sys = calloc(1, sizeof(*audio_view_sys));
    audio_view_sys->p_intf = intf;
222
    audio_view_sys->p_parent = parent;
223

224
    view->pf_event = audio_view_callback;
225
    view->p_view_sys = audio_view_sys;
226
    view->pf_has_menu = audio_view_has_menu;
227

228 229 230 231 232 233
    /* Content box */
    Evas_Object *audio_box = elm_box_add(parent);
    evas_object_size_hint_weight_set(audio_box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
    evas_object_size_hint_align_set(audio_box, EVAS_HINT_FILL, EVAS_HINT_FILL);

    /* Create the toolbar in the view */
234
    Evas_Object *tabbar = create_toolbar(audio_view_sys, audio_box);
235 236 237
    elm_box_pack_end(audio_box, tabbar);
    evas_object_show(tabbar);

Thomas Guillem's avatar
Thomas Guillem committed
238
    /* Toolbar Naviframe */
239 240 241 242 243
    audio_view_sys->nf_toolbar = elm_naviframe_add(audio_box);
    evas_object_size_hint_weight_set(audio_view_sys->nf_toolbar, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
    evas_object_size_hint_align_set(audio_view_sys->nf_toolbar, EVAS_HINT_FILL, EVAS_HINT_FILL);
    elm_box_pack_end(audio_box, audio_view_sys->nf_toolbar );
    evas_object_show(audio_view_sys->nf_toolbar);
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
244

Thomas Guillem's avatar
Thomas Guillem committed
245
    /* Set the first item in the toolbar */
246 247 248 249
    elm_toolbar_item_selected_set(elm_toolbar_first_item_get(tabbar), EINA_TRUE);

    /*  */
    evas_object_show(audio_box);
250
    view->view = audio_box;
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
251

252
    return view;
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
253
}
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
254 255 256 257

void
destroy_audio_view(interface_view *view)
{
258 259 260
    view_sys* p_sys = view->p_view_sys;
    for ( unsigned int i = 0; i < AUDIO_VIEW_MAX; ++i )
    {
261 262 263 264 265
        if (p_sys->p_lists[i] != NULL)
        {
            p_sys->p_lists[i]->pf_del(p_sys->p_lists[i]->p_sys);
            free(p_sys->p_lists[i]);
        }
266 267 268
    }
    free(p_sys);
    free(view);
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
269
}