video_controller.c 5.84 KB
Newer Older
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 29 30
/*****************************************************************************
 * Copyright © 2015 VideoLAN, VideoLabs SAS
 *****************************************************************************
 *
 * Authors: Hugo Beauzée-Luyssen <hugo@beauzee.fr>
 *
 * 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"
#include "video_controller.h"

#include "application.h"
31
#include "media/library/media_library.hpp"
32 33 34
#include "ui/views/video_view.h"
#include "ui/interface.h"

35 36
#include <assert.h>

37 38
struct video_controller
{
39 40 41
    /**
     * Content Management
     */
42
    application*    p_app;
43
    view_sys*     p_view;
44
    // This is the content as a video_list_item list.
45
    Eina_List*      p_content;
46 47 48 49 50 51 52 53 54 55

    /**
     * Callbacks & settings
     */
    video_list_item*    (*pf_view_append_media_item)( view_sys* p_view, media_item* p_item );
    void                (*pf_view_clear)( view_sys* videoview );
    const media_item*   (*pf_get_media_item)( video_list_item* p_view );
    void                (*pf_set_media_item)( video_list_item* p_view, const media_item* p_item );
    void                (*pf_media_library_get_content)( media_library* p_ml, media_library_list_cb cb, void* p_user_data );

56 57
};

58 59 60 61 62 63
static void
video_controller_add_item(video_controller* ctrl, const media_item* p_item)
{
    media_item* p_new_item = media_item_copy( p_item );
    if (p_new_item == NULL)
        return;
64
    video_list_item* p_view_item = ctrl->pf_view_append_media_item( ctrl->p_view, p_new_item );
65 66 67 68 69 70 71 72
    if (p_view_item == NULL)
    {
        media_item_destroy(p_new_item);
        return;
    }
    ctrl->p_content = eina_list_append(ctrl->p_content, p_view_item);
}

73 74 75 76 77 78 79 80 81 82 83
bool video_controller_file_update( video_controller* ctrl, const media_item* p_new_media_item )
{
    if ( p_new_media_item->i_type != MEDIA_ITEM_TYPE_VIDEO )
        return false;

    if ( ctrl->p_content != NULL )
    {
        Eina_List* it;
        video_list_item* p_item;
        EINA_LIST_FOREACH( ctrl->p_content, it, p_item )
        {
84
            const media_item* p_media_item = ctrl->pf_get_media_item(p_item);
85 86 87

            if (!strcmp( p_media_item->psz_path, p_new_media_item->psz_path))
            {
88
                ctrl->pf_set_media_item(p_item, p_new_media_item);
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
                return true;
            }
        }
    }
    video_controller_add_item( ctrl, p_new_media_item );
    return true;
}

static bool
video_controller_file_updated_cb(void* p_data, const media_item* p_new_media_item, bool b_added )
{
    (void)b_added;
    video_controller* ctrl = (video_controller*)p_data;
    return video_controller_file_update(ctrl, p_new_media_item);
}

105 106 107 108 109 110
/* Called by the Media Library with updated video list
 * Guaranteed to be called from the main loop
 */
void
video_controller_content_update_cb(Eina_List* p_content, void* p_data)
{
111 112
    if (p_content == NULL)
        return;
113
    video_controller* ctrl = (video_controller*)p_data;
114 115 116 117 118
    Eina_List* it;
    media_item* p_item;

    EINA_LIST_FOREACH( p_content, it, p_item )
    {
119
        video_controller_file_update(ctrl, p_item);
120
    }
121 122
}

123
/*
124
 * Called when media library signals a content change (currently, only after reload)
125 126
 * Guaranteed to be called from the main loop
 */
127 128 129
void
video_controller_content_changed_cb(void* p_data)
{
130 131
    video_controller* ctrl = (video_controller*)p_data;

132 133
    // Discard previous content if any, and ask ML for the new content
    if (ctrl->p_content != NULL)
134
    {
135
        eina_list_free(ctrl->p_content);
136
        ctrl->pf_view_clear(ctrl->p_view);
137
        ctrl->p_content = NULL;
138
    }
139
    media_library* p_ml = (media_library*)application_get_media_library( ctrl->p_app );
140
    ctrl->pf_media_library_get_content(p_ml, &video_controller_content_update_cb, ctrl);
141 142
}

143
video_controller*
144
video_controller_create(application* p_app, view_sys* p_view )
145 146 147 148 149 150
{
   video_controller* ctrl = calloc(1, sizeof(*ctrl));
   if ( ctrl == NULL )
       return NULL;
   ctrl->p_app = p_app;
   ctrl->p_view = p_view;
151 152 153 154 155
   ctrl->pf_view_append_media_item = &video_view_append_item;
   ctrl->pf_get_media_item = &video_list_item_get_media_item;
   ctrl->pf_set_media_item = &video_list_item_set_media_item;
   ctrl->pf_media_library_get_content = &media_library_get_video_files;
   ctrl->pf_view_clear = &video_view_clear;
156 157

   /* Populate it */
158
   media_library* p_ml = (media_library*)application_get_media_library(p_app);
159
   media_library_register_on_change(p_ml, video_controller_content_changed_cb, ctrl);
160
   media_library_register_item_updated(p_ml, video_controller_file_updated_cb, ctrl);
161 162
   return ctrl;
}
163 164 165 166 167

void
video_controller_destroy(video_controller *ctrl)
{
    eina_list_free(ctrl->p_content);
168 169 170
    media_library* p_ml = (media_library*)application_get_media_library(ctrl->p_app);
    media_library_unregister_on_change(p_ml, &video_controller_content_changed_cb, ctrl);
    media_library_unregister_item_updated(p_ml, &video_controller_file_updated_cb, ctrl);
171 172
    free(ctrl);
}