]> git.sesse.net Git - vlc/blob - src/control/hierarchical_media_list_view.c
84b8e85dfb8438b23268ec9112e5f4b0b928860c
[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 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc/libvlc.h>
29 #include <vlc/libvlc_media.h>
30 #include <vlc/libvlc_media_list.h>
31 #include <vlc/libvlc_media_list_view.h>
32
33 #include "media_list_internal.h"
34 #include "media_list_view_internal.h"
35
36 //#define DEBUG_HIERARCHICAL_VIEW
37
38 #ifdef DEBUG_HIERARCHICAL_VIEW
39 # define trace( fmt, ... ) printf( "%s(): " fmt, __FUNCTION__, ##__VA_ARGS__ )
40 #else
41 # define trace( ... )
42 #endif
43
44 /*
45  * Private functions
46  */
47
48 /**************************************************************************
49  *       flat_media_list_view_count  (private)
50  * (called by media_list_view_count)
51  **************************************************************************/
52 static int
53 hierarch_media_list_view_count( libvlc_media_list_view_t * p_mlv,
54                                 libvlc_exception_t * p_e )
55 {
56     VLC_UNUSED(p_e);
57     return libvlc_media_list_count( p_mlv->p_mlist );
58 }
59
60 /**************************************************************************
61  *       flat_media_list_view_item_at_index  (private)
62  * (called by flat_media_list_view_item_at_index)
63  **************************************************************************/
64 static libvlc_media_t *
65 hierarch_media_list_view_item_at_index( libvlc_media_list_view_t * p_mlv,
66                                     int index,
67                                     libvlc_exception_t * p_e )
68 {
69     return libvlc_media_list_item_at_index( p_mlv->p_mlist, index, p_e );
70 }
71
72 /**************************************************************************
73  *       flat_media_list_view_item_at_index  (private)
74  * (called by flat_media_list_view_item_at_index)
75  **************************************************************************/
76 static libvlc_media_list_view_t *
77 hierarch_media_list_view_children_at_index( libvlc_media_list_view_t * p_mlv,
78                                         int index,
79                                         libvlc_exception_t * p_e )
80 {
81     libvlc_media_t * p_md;
82     libvlc_media_list_t * p_submlist;
83     libvlc_media_list_view_t * p_ret;
84     p_md = libvlc_media_list_item_at_index( p_mlv->p_mlist, index, p_e );
85     if( !p_md ) return NULL;
86     p_submlist = libvlc_media_subitems( p_md );
87     libvlc_media_release( p_md );
88     if( !p_submlist ) return NULL;
89     p_ret = libvlc_media_list_hierarchical_view( p_submlist, p_e );
90     libvlc_media_list_release( p_submlist );
91
92     return p_ret;
93 }
94
95 /**************************************************************************
96  *       media_list_(item|will)_* (private) (Event callback)
97  **************************************************************************/
98 static void
99 media_list_item_added( const libvlc_event_t * p_event, void * user_data )
100 {
101     libvlc_media_t * p_md;
102     libvlc_media_list_view_t * p_mlv = user_data;
103     int index = p_event->u.media_list_item_added.index;
104     p_md = p_event->u.media_list_item_added.item;
105     libvlc_media_list_view_item_added( p_mlv, p_md, index );
106 }
107 static void
108 media_list_will_add_item( const libvlc_event_t * p_event, void * user_data )
109 {
110     libvlc_media_t * p_md;
111     libvlc_media_list_view_t * p_mlv = user_data;
112     int index = p_event->u.media_list_will_add_item.index;
113     p_md = p_event->u.media_list_will_add_item.item;
114     libvlc_media_list_view_will_add_item( p_mlv, p_md, index );
115 }
116 static void
117 media_list_item_deleted( const libvlc_event_t * p_event, void * user_data )
118 {
119     libvlc_media_t * p_md;
120     libvlc_media_list_view_t * p_mlv = user_data;
121     int index = p_event->u.media_list_item_deleted.index;
122     p_md = p_event->u.media_list_item_deleted.item;
123     libvlc_media_list_view_item_deleted( p_mlv, p_md, index );
124 }
125 static void
126 media_list_will_delete_item( const libvlc_event_t * p_event, void * user_data )
127 {
128     libvlc_media_t * p_md;
129     libvlc_media_list_view_t * p_mlv = user_data;
130     int index = p_event->u.media_list_will_delete_item.index;
131     p_md = p_event->u.media_list_will_delete_item.item;
132     libvlc_media_list_view_will_delete_item( p_mlv, p_md, index );
133 }
134
135 /*
136  * Public libvlc functions
137  */
138
139
140 /**************************************************************************
141  *       flat_media_list_view_release (private)
142  * (called by media_list_view_release)
143  **************************************************************************/
144 static void
145 hierarch_media_list_view_release( libvlc_media_list_view_t * p_mlv )
146 {
147     libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
148                          libvlc_MediaListItemAdded,
149                          media_list_item_added, p_mlv );
150     libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
151                          libvlc_MediaListWillAddItem,
152                          media_list_will_add_item, p_mlv );
153     libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
154                          libvlc_MediaListItemDeleted,
155                          media_list_item_deleted, p_mlv );
156     libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
157                          libvlc_MediaListWillDeleteItem,
158                          media_list_will_delete_item, p_mlv );
159 }
160
161 /**************************************************************************
162  *       libvlc_media_list_flat_view (Public)
163  **************************************************************************/
164 libvlc_media_list_view_t *
165 libvlc_media_list_hierarchical_view( libvlc_media_list_t * p_mlist,
166                                      libvlc_exception_t * p_e )
167 {
168     trace("\n");
169     libvlc_media_list_view_t * p_mlv;
170     p_mlv = libvlc_media_list_view_new( p_mlist,
171                                         hierarch_media_list_view_count,
172                                         hierarch_media_list_view_item_at_index,
173                                         hierarch_media_list_view_children_at_index,
174                                         libvlc_media_list_hierarchical_view,
175                                         hierarch_media_list_view_release,
176                                         NULL,
177                                         p_e );
178     libvlc_media_list_lock( p_mlist );
179     libvlc_event_attach( p_mlv->p_mlist->p_event_manager,
180                          libvlc_MediaListItemAdded,
181                          media_list_item_added, p_mlv, NULL );
182     libvlc_event_attach( p_mlv->p_mlist->p_event_manager,
183                          libvlc_MediaListWillAddItem,
184                          media_list_will_add_item, p_mlv, NULL );
185     libvlc_event_attach( p_mlv->p_mlist->p_event_manager,
186                          libvlc_MediaListItemDeleted,
187                          media_list_item_deleted, p_mlv, NULL );
188     libvlc_event_attach( p_mlv->p_mlist->p_event_manager,
189                          libvlc_MediaListWillDeleteItem,
190                          media_list_will_delete_item, p_mlv, NULL );
191     libvlc_media_list_unlock( p_mlist );
192     return p_mlv;
193 }