]> git.sesse.net Git - vlc/blob - src/control/hierarchical_media_list_view.c
4d345280ff3a8653fdf2b3b880c5e6d5f91c6997
[vlc] / src / control / hierarchical_media_list_view.c
1 /*****************************************************************************
2  * hierarchical_media_list_view.c: libvlc hierarchical media list view functs.
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include "libvlc_internal.h"
25 #include <vlc/libvlc.h>
26 #include <assert.h>
27 #include "vlc_arrays.h"
28
29 //#define DEBUG_HIERARCHICAL_VIEW
30
31 #ifdef DEBUG_HIERARCHICAL_VIEW
32 # define trace( fmt, ... ) printf( "%s(): " fmt, __FUNCTION__, ##__VA_ARGS__ )
33 #else
34 # define trace( ... )
35 #endif
36
37 /*
38  * Private functions
39  */
40
41 /**************************************************************************
42  *       flat_media_list_view_count  (private)
43  * (called by media_list_view_count)
44  **************************************************************************/
45 static int
46 hierarch_media_list_view_count( libvlc_media_list_view_t * p_mlv,
47                                 libvlc_exception_t * p_e )
48 {
49     return libvlc_media_list_count( p_mlv->p_mlist, p_e );
50 }
51
52 /**************************************************************************
53  *       flat_media_list_view_item_at_index  (private)
54  * (called by flat_media_list_view_item_at_index)
55  **************************************************************************/
56 static libvlc_media_t *
57 hierarch_media_list_view_item_at_index( libvlc_media_list_view_t * p_mlv,
58                                     int index,
59                                     libvlc_exception_t * p_e )
60 {
61     return libvlc_media_list_item_at_index( p_mlv->p_mlist, index, p_e );
62 }
63
64 /**************************************************************************
65  *       flat_media_list_view_item_at_index  (private)
66  * (called by flat_media_list_view_item_at_index)
67  **************************************************************************/
68 static libvlc_media_list_view_t *
69 hierarch_media_list_view_children_at_index( libvlc_media_list_view_t * p_mlv,
70                                         int index,
71                                         libvlc_exception_t * p_e )
72 {
73     libvlc_media_t * p_md;
74     libvlc_media_list_t * p_submlist;
75     libvlc_media_list_view_t * p_ret;
76     p_md = libvlc_media_list_item_at_index( p_mlv->p_mlist, index, p_e );
77     if( !p_md ) return NULL;
78     p_submlist = libvlc_media_subitems( p_md, p_e );
79     libvlc_media_release( p_md );
80     if( !p_submlist ) return NULL;
81     p_ret = libvlc_media_list_hierarchical_view( p_submlist, p_e );
82     libvlc_media_list_release( p_submlist );
83
84     return p_ret;
85 }
86
87 /**************************************************************************
88  *       media_list_(item|will)_* (private) (Event callback)
89  **************************************************************************/
90 static void
91 media_list_item_added( const libvlc_event_t * p_event, void * user_data )
92 {
93     libvlc_media_t * p_md;
94     libvlc_media_list_view_t * p_mlv = user_data;
95     int index = p_event->u.media_list_item_added.index;
96     p_md = p_event->u.media_list_item_added.item;
97     libvlc_media_list_view_item_added( p_mlv, p_md, index );
98 }
99 static void
100 media_list_will_add_item( const libvlc_event_t * p_event, void * user_data )
101 {
102     libvlc_media_t * p_md;
103     libvlc_media_list_view_t * p_mlv = user_data;
104     int index = p_event->u.media_list_will_add_item.index;
105     p_md = p_event->u.media_list_will_add_item.item;
106     libvlc_media_list_view_will_add_item( p_mlv, p_md, index );
107 }
108 static void
109 media_list_item_deleted( const libvlc_event_t * p_event, void * user_data )
110 {
111     libvlc_media_t * p_md;
112     libvlc_media_list_view_t * p_mlv = user_data;
113     int index = p_event->u.media_list_item_deleted.index;
114     p_md = p_event->u.media_list_item_deleted.item;
115     libvlc_media_list_view_item_deleted( p_mlv, p_md, index );
116 }
117 static void
118 media_list_will_delete_item( const libvlc_event_t * p_event, void * user_data )
119 {
120     libvlc_media_t * p_md;
121     libvlc_media_list_view_t * p_mlv = user_data;
122     int index = p_event->u.media_list_will_delete_item.index;
123     p_md = p_event->u.media_list_will_delete_item.item;
124     libvlc_media_list_view_will_delete_item( p_mlv, p_md, index );
125 }
126
127 /*
128  * Public libvlc functions
129  */
130
131
132 /**************************************************************************
133  *       flat_media_list_view_release (private)
134  * (called by media_list_view_release)
135  **************************************************************************/
136 static void
137 hierarch_media_list_view_release( libvlc_media_list_view_t * p_mlv )
138 {
139     libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
140                          libvlc_MediaListItemAdded,
141                          media_list_item_added, p_mlv, NULL );
142     libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
143                          libvlc_MediaListWillAddItem,
144                          media_list_will_add_item, p_mlv, NULL );
145     libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
146                          libvlc_MediaListItemDeleted,
147                          media_list_item_deleted, p_mlv, NULL );
148     libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
149                          libvlc_MediaListWillDeleteItem,
150                          media_list_will_delete_item, p_mlv, NULL );
151 }
152
153 /**************************************************************************
154  *       libvlc_media_list_flat_view (Public)
155  **************************************************************************/
156 libvlc_media_list_view_t *
157 libvlc_media_list_hierarchical_view( libvlc_media_list_t * p_mlist,
158                                      libvlc_exception_t * p_e )
159 {
160     trace("\n");
161     libvlc_media_list_view_t * p_mlv;
162     p_mlv = libvlc_media_list_view_new( p_mlist,
163                                         hierarch_media_list_view_count,
164                                         hierarch_media_list_view_item_at_index,
165                                         hierarch_media_list_view_children_at_index,
166                                         libvlc_media_list_hierarchical_view,
167                                         hierarch_media_list_view_release,
168                                         NULL,
169                                         p_e );
170     libvlc_media_list_lock( p_mlist );
171     libvlc_event_attach( p_mlv->p_mlist->p_event_manager,
172                          libvlc_MediaListItemAdded,
173                          media_list_item_added, p_mlv, NULL );
174     libvlc_event_attach( p_mlv->p_mlist->p_event_manager,
175                          libvlc_MediaListWillAddItem,
176                          media_list_will_add_item, p_mlv, NULL );
177     libvlc_event_attach( p_mlv->p_mlist->p_event_manager,
178                          libvlc_MediaListItemDeleted,
179                          media_list_item_deleted, p_mlv, NULL );
180     libvlc_event_attach( p_mlv->p_mlist->p_event_manager,
181                          libvlc_MediaListWillDeleteItem,
182                          media_list_will_delete_item, p_mlv, NULL );
183     libvlc_media_list_unlock( p_mlist );
184     return p_mlv;
185 }