qt: playlist: use item title if available
[vlc.git] / lib / media_list_path.h
blob6534a98e0475eb0d231e6e071bfc1c81e7caebc2
1 /*****************************************************************************
2 * media_list_path.h : Some inlined function that allows media_list_path
3 * manipulation. This is internal and used only by media_list_player.
4 *****************************************************************************
5 * Copyright (C) 2005 VLC authors and VideoLAN
7 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #ifndef _LIBVLC_MEDIA_LIST_PATH_H
25 #define _LIBVLC_MEDIA_LIST_PATH_H 1
27 typedef int * libvlc_media_list_path_t; /* (Media List Player Internal) */
29 /**************************************************************************
30 * path_dump (Media List Player Internal)
31 **************************************************************************/
32 static inline void libvlc_media_list_path_dump( const libvlc_media_list_path_t path )
34 if(!path)
36 printf("NULL path\n");
37 return;
40 for(int i = 0; path[i] != -1; i++)
41 printf("%s%d", i > 0 ? "/" : "", path[i]);
42 printf("\n");
45 /**************************************************************************
46 * path_empty (Media List Player Internal)
47 **************************************************************************/
48 static inline libvlc_media_list_path_t libvlc_media_list_path_empty( void )
50 libvlc_media_list_path_t ret = xmalloc(sizeof(int));
51 ret[0] = -1;
52 return ret;
55 /**************************************************************************
56 * path_with_root_index (Media List Player Internal)
57 **************************************************************************/
58 static inline libvlc_media_list_path_t libvlc_media_list_path_with_root_index( int index )
60 libvlc_media_list_path_t ret = xmalloc(sizeof(int)*2);
61 ret[0] = index;
62 ret[1] = -1;
63 return ret;
66 /**************************************************************************
67 * path_depth (Media List Player Internal)
68 **************************************************************************/
69 static inline int libvlc_media_list_path_depth( const libvlc_media_list_path_t path )
71 int i;
72 for( i = 0; path[i] != -1; i++ );
73 return i;
76 /**************************************************************************
77 * path_append (Media List Player Internal)
78 **************************************************************************/
79 static inline void libvlc_media_list_path_append( libvlc_media_list_path_t * p_path, int index )
81 int old_depth = libvlc_media_list_path_depth( *p_path );
82 *p_path = xrealloc( *p_path, sizeof(int)*(old_depth+2));
83 *p_path[old_depth] = index;
84 *p_path[old_depth+1] = -1;
87 /**************************************************************************
88 * path_copy_by_appending (Media List Player Internal)
89 **************************************************************************/
90 static inline libvlc_media_list_path_t libvlc_media_list_path_copy_by_appending( const libvlc_media_list_path_t path, int index )
92 libvlc_media_list_path_t ret;
93 int old_depth = libvlc_media_list_path_depth( path );
94 ret = xmalloc( sizeof(int) * (old_depth + 2) );
95 memcpy( ret, path, sizeof(int) * old_depth );
96 ret[old_depth] = index;
97 ret[old_depth+1] = -1;
98 return ret;
101 /**************************************************************************
102 * path_copy (Media List Player Internal)
103 **************************************************************************/
104 static inline libvlc_media_list_path_t libvlc_media_list_path_copy( const libvlc_media_list_path_t path )
106 libvlc_media_list_path_t ret;
107 int depth = libvlc_media_list_path_depth( path );
108 ret = xmalloc( sizeof(int)*(depth+1) );
109 memcpy( ret, path, sizeof(int)*(depth+1) );
110 return ret;
113 /**************************************************************************
114 * get_path_rec (Media List Player Internal)
115 **************************************************************************/
116 static libvlc_media_list_path_t
117 get_path_rec( const libvlc_media_list_path_t path, libvlc_media_list_t * p_current_mlist, libvlc_media_t * p_searched_md )
119 int count = libvlc_media_list_count( p_current_mlist );
121 for( int i = 0; i < count; i++ )
123 libvlc_media_t * p_md = libvlc_media_list_item_at_index( p_current_mlist, i );
125 if( p_md == p_searched_md )
126 return libvlc_media_list_path_copy_by_appending( path, i ); /* Found! */
128 libvlc_media_list_t * p_subitems = libvlc_media_subitems( p_md );
129 libvlc_media_release( p_md );
130 if( p_subitems )
132 libvlc_media_list_path_t new_path = libvlc_media_list_path_copy_by_appending( path, i );
133 libvlc_media_list_lock( p_subitems );
134 libvlc_media_list_path_t ret = get_path_rec( new_path, p_subitems, p_searched_md );
135 libvlc_media_list_unlock( p_subitems );
136 free( new_path );
137 libvlc_media_list_release( p_subitems );
138 if( ret )
139 return ret; /* Found in sublist! */
142 return NULL;
145 /**************************************************************************
146 * path_of_item (Media List Player Internal)
147 **************************************************************************/
148 static inline libvlc_media_list_path_t libvlc_media_list_path_of_item( libvlc_media_list_t * p_mlist, libvlc_media_t * p_md )
150 libvlc_media_list_path_t path = libvlc_media_list_path_empty();
151 libvlc_media_list_path_t ret;
152 ret = get_path_rec( path, p_mlist, p_md );
153 free( path );
154 return ret;
157 /**************************************************************************
158 * item_at_path (Media List Player Internal)
159 **************************************************************************/
160 static libvlc_media_t *
161 libvlc_media_list_item_at_path( libvlc_media_list_t * p_mlist, const libvlc_media_list_path_t path )
163 libvlc_media_list_t * p_current_mlist = p_mlist;
165 for( int i = 0; path[i] != -1; i++ )
167 libvlc_media_t* p_md = libvlc_media_list_item_at_index( p_current_mlist, path[i] );
169 if( p_current_mlist != p_mlist )
170 libvlc_media_list_release( p_current_mlist );
172 if( path[i+1] == -1 )
173 return p_md;
175 p_current_mlist = libvlc_media_subitems( p_md );
176 libvlc_media_release( p_md );
178 if( !p_current_mlist )
179 return NULL;
181 /* Fetch next one */
183 /* Not found, shouldn't happen if the p_path is not empty */
184 if( p_current_mlist != p_mlist )
185 libvlc_media_list_release( p_current_mlist );
186 return NULL;
189 /**************************************************************************
190 * parentlist_at_path (Media List Player Internal)
191 **************************************************************************/
192 static libvlc_media_list_t *
193 libvlc_media_list_parentlist_at_path( libvlc_media_list_t * p_mlist, const libvlc_media_list_path_t path )
195 libvlc_media_list_t * p_current_mlist = p_mlist;
197 for( int i = 0; path[i] != -1; i++ )
199 if( p_current_mlist != p_mlist )
200 libvlc_media_list_release( p_current_mlist );
202 if( path[i+1] == -1 )
204 libvlc_media_list_retain(p_current_mlist);
205 return p_current_mlist;
208 libvlc_media_t* p_md = libvlc_media_list_item_at_index( p_current_mlist, path[i] );
210 p_current_mlist = libvlc_media_subitems( p_md );
211 libvlc_media_release( p_md );
213 if( !p_current_mlist )
214 return NULL;
216 /* Fetch next one */
218 /* Not found, shouldn't happen if the p_path is not empty */
219 if( p_current_mlist != p_mlist )
220 libvlc_media_list_release( p_current_mlist );
221 return NULL;
224 /**************************************************************************
225 * sublist_at_path (Media List Player Internal)
226 **************************************************************************/
227 static libvlc_media_list_t *
228 libvlc_media_list_sublist_at_path( libvlc_media_list_t * p_mlist, const libvlc_media_list_path_t path )
230 libvlc_media_list_t * ret;
231 libvlc_media_t * p_md = libvlc_media_list_item_at_path( p_mlist, path );
232 if( !p_md )
233 return NULL;
235 ret = libvlc_media_subitems( p_md );
236 libvlc_media_release( p_md );
238 return ret;
241 #endif