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