1 /*****************************************************************************
2 * media_list.c: libvlc tree functions
3 *****************************************************************************
4 * Copyright (C) 2007 the VideoLAN team
7 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
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.
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.
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 *****************************************************************************/
24 #include "libvlc_internal.h"
25 #include <vlc/libvlc.h>
27 #include "vlc_arrays.h"
30 * Private libvlc functions
33 /**************************************************************************
34 * notify_subtree_addition (private)
36 * Do the appropriate action when a subtree is added.
37 **************************************************************************/
39 notify_subtree_addition( libvlc_tree_t * p_tree,
40 libvlc_tree_t * p_subtree,
45 /* Construct the event */
46 event.type = libvlc_TreeSubtreeAdded;
47 event.u.tree_subtree_added.subtree = p_subtree;
48 event.u.tree_subtree_added.index = index;
51 libvlc_event_send( p_tree->p_event_manager, &event );
54 /**************************************************************************
55 * notify_subtree_deletion (private)
57 * Do the appropriate action when a subtree is deleted.
58 **************************************************************************/
60 notify_subtree_deletion( libvlc_tree_t * p_tree,
61 libvlc_tree_t * p_subtree,
66 /* Construct the event */
67 event.type = libvlc_TreeSubtreeDeleted;
68 event.u.tree_subtree_deleted.subtree = p_subtree;
69 event.u.tree_subtree_deleted.index = index;
72 libvlc_event_send( p_tree->p_event_manager, &event );
76 /**************************************************************************
77 * notify_tree_item_value_changed (private)
79 * Do the appropriate action when a tree's item changes.
80 **************************************************************************/
82 notify_tree_item_value_changed( libvlc_tree_t * p_tree,
83 libvlc_tree_t * p_subtree,
88 /* Construct the event */
89 event.type = libvlc_TreeItemValueChanged;
90 event.u.tree_item_value_changed.new_value = p_tree->p_item;
93 libvlc_event_send( p_tree->p_event_manager, &event );
97 /**************************************************************************
99 **************************************************************************/
100 static libvlc_tree_t *
101 libvlc_tree_new( libvlc_retain_function item_retain,
102 libvlc_retain_function item_release,
104 libvlc_exception_t * p_e )
107 libvlc_tree_t * p_tree;
109 p_tree = malloc(sizeof(libvlc_tree_t));
114 p_tree->i_refcount = 1;
115 p_tree->p_item = item;
116 ARRAY_INIT( p_tree->subtrees );
118 p_tree->p_event_manager = libvlc_event_manager_new( p_tree, NULL, p_e );
119 libvlc_event_manager_register_event_type( p_tree->p_event_manager,
120 libvlc_TreeSubtreeAdded, p_e );
121 libvlc_event_manager_register_event_type( p_tree->p_event_manager,
122 libvlc_TreeSubtreeDeleted, p_e );
123 libvlc_event_manager_register_event_type( p_tree->p_event_manager,
124 libvlc_TreeItemValueChanged, p_e );
129 /**************************************************************************
131 **************************************************************************/
133 libvlc_tree_item( libvlc_tree_t * p_tree,
134 libvlc_exception_t * p_e )
136 if( p_tree->pf_item_retain ) p_tree->pf_item_retain( p_tree->p_item );
137 return p_tree->p_item;
142 * Public libvlc functions
146 /**************************************************************************
147 * new_with_media_list (Public)
148 **************************************************************************/
150 libvlc_tree_new_with_media_list_as_item( libvlc_media_list_t * p_mlist,
151 libvlc_exception_t * p_e )
154 libvlc_tree_t * p_tree = libvlc_tree_new(
155 (libvlc_retain_function)libvlc_media_list_retain,
156 (libvlc_release_function)libvlc_media_list_release,
163 /**************************************************************************
164 * new_with_string (Public)
165 **************************************************************************/
167 libvlc_tree_new_with_string_as_item( const char * psz,
168 libvlc_exception_t * p_e )
171 libvlc_tree_t * p_tree = libvlc_tree_new( NULL,
172 (libvlc_release_function)free,
173 psz ? strdup( psz ): NULL,
179 /**************************************************************************
181 **************************************************************************/
182 void libvlc_tree_release( libvlc_tree_t * p_tree )
184 libvlc_tree_t * p_subtree;
186 p_tree->i_refcount--;
188 if( p_tree->i_refcount > 0 )
191 if( p_tree->pf_item_release && p_tree->p_item )
192 p_tree->pf_item_release( p_tree->p_item );
194 FOREACH_ARRAY( p_subtree, p_tree->subtrees )
195 libvlc_tree_release( p_subtree );
201 /**************************************************************************
203 **************************************************************************/
204 void libvlc_tree_retain( libvlc_tree_t * p_tree )
206 p_tree->i_refcount++;
209 /**************************************************************************
210 * item_as_string (Public)
211 **************************************************************************/
213 libvlc_tree_item_as_string( libvlc_tree_t * p_tree,
214 libvlc_exception_t * p_e )
217 return p_tree->p_item ? strdup( p_tree->p_item ) : NULL;
220 /**************************************************************************
221 * item_as_media_list (Public)
222 **************************************************************************/
223 libvlc_media_list_t *
224 libvlc_tree_item_as_media_list( libvlc_tree_t * p_tree,
225 libvlc_exception_t * p_e )
227 /* Automatically retained */
228 return libvlc_tree_item( p_tree, p_e );
231 /**************************************************************************
233 **************************************************************************/
235 libvlc_tree_subtree_count( libvlc_tree_t * p_tree, libvlc_exception_t * p_e )
238 return p_tree->subtrees.i_size;
241 /**************************************************************************
242 * subtree_at_index (Public)
244 * Note: The subtree won't be retained
245 **************************************************************************/
247 libvlc_tree_subtree_at_index( libvlc_tree_t * p_tree,
249 libvlc_exception_t * p_e )
252 libvlc_tree_t * p_subtree;
254 p_subtree = ARRAY_VAL( p_tree->subtrees, index );
255 libvlc_tree_retain( p_subtree );
260 /**************************************************************************
261 * insert_subtree_at_index (Public)
263 * Note: The subtree won't be retained
264 **************************************************************************/
266 libvlc_tree_insert_subtree_at_index( libvlc_tree_t * p_tree,
267 libvlc_tree_t * p_subtree,
269 libvlc_exception_t * p_e )
272 libvlc_tree_retain( p_tree );
274 ARRAY_INSERT( p_tree->subtrees, p_subtree, index);
275 notify_subtree_addition( p_tree, p_subtree, index );
278 /**************************************************************************
279 * remove_subtree_at_index (Public)
280 **************************************************************************/
282 libvlc_tree_remove_subtree_at_index( libvlc_tree_t * p_tree,
284 libvlc_exception_t * p_e )
287 libvlc_tree_t * p_subtree;
289 p_subtree = ARRAY_VAL( p_tree->subtrees, index );
291 ARRAY_REMOVE( p_tree->subtrees, index );
292 notify_subtree_deletion( p_tree, p_subtree, index );
294 libvlc_tree_release( p_subtree );