]> git.sesse.net Git - vlc/blob - src/playlist/sort.c
[patch] unifying meta-information access, the 2nd by Daniel Stränger
[vlc] / src / playlist / sort.c
1 /*****************************************************************************
2  * sort.c : Playlist sorting functions
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Clément Stenac <zorglub@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 #include <stdlib.h>                                      /* free(), strtol() */
24 #include <stdio.h>                                              /* sprintf() */
25 #include <string.h>                                            /* strerror() */
26
27 #include <vlc/vlc.h>
28 #include <vlc/input.h>
29 #include <vlc/vout.h>
30 #include <vlc/sout.h>
31
32 #include "vlc_playlist.h"
33
34
35 int playlist_ItemArraySort( playlist_t *p_playlist, int i_items,
36                 playlist_item_t **pp_items, int i_mode,
37                 int i_type );
38
39
40 /**
41  * Sort the playlist.
42  * \param p_playlist the playlist
43  * \param i_mode: SORT_ID, SORT_TITLE, SORT_AUTHOR, SORT_ALBUM, SORT_RANDOM
44  * \param i_type: ORDER_NORMAL or ORDER_REVERSE (reversed order)
45  * \return VLC_SUCCESS on success
46  */
47 int playlist_Sort( playlist_t * p_playlist , int i_mode, int i_type )
48 {
49     int  i_id = -1;
50     vlc_value_t val;
51     val.b_bool = VLC_TRUE;
52
53     vlc_mutex_lock( &p_playlist->object_lock );
54
55     p_playlist->i_sort = i_mode;
56     p_playlist->i_order = i_type;
57
58     if( p_playlist->i_index >= 0 )
59     {
60         i_id = p_playlist->pp_items[p_playlist->i_index]->input.i_id;
61     }
62
63     playlist_ItemArraySort( p_playlist, p_playlist->i_size,
64                     p_playlist->pp_items, i_mode, i_type );
65
66     if( i_id != -1 )
67     {
68         p_playlist->i_index = playlist_GetPositionById( p_playlist, i_id );
69     }
70
71     /* ensure we are in no-view mode */
72     p_playlist->status.i_view = -1;
73
74     vlc_mutex_unlock( &p_playlist->object_lock );
75
76     /* Notify the interfaces */
77     var_Set( p_playlist, "intf-change", val );
78
79     return VLC_SUCCESS;
80 }
81
82 /**
83  * Sort a node.
84  *
85  * This function must be entered with the playlist lock !
86  *
87  * \param p_playlist the playlist
88  * \param p_node the node to sort
89  * \param i_mode: SORT_ID, SORT_TITLE, SORT_AUTHOR, SORT_ALBUM, SORT_RANDOM
90  * \param i_type: ORDER_NORMAL or ORDER_REVERSE (reversed order)
91  * \return VLC_SUCCESS on success
92  */
93 int playlist_NodeSort( playlist_t * p_playlist , playlist_item_t *p_node,
94                        int i_mode, int i_type )
95 {
96
97     playlist_ItemArraySort( p_playlist,p_node->i_children,
98                             p_node->pp_children, i_mode, i_type );
99
100     p_node->i_serial++;
101
102     return VLC_SUCCESS;
103 }
104
105 /**
106  *
107  * Sort a node recursively.
108  *
109  * This function must be entered with the playlist lock !
110  *
111  * \param p_playlist the playlist
112  * \param p_node the node to sort
113  * \param i_mode: SORT_ID, SORT_TITLE, SORT_AUTHOR, SORT_ALBUM, SORT_RANDOM
114  * \param i_type: ORDER_NORMAL or ORDER_REVERSE (reversed order)
115  * \return VLC_SUCCESS on success
116  */
117 int playlist_RecursiveNodeSort( playlist_t *p_playlist, playlist_item_t *p_node,
118                                 int i_mode, int i_type )
119 {
120     int i;
121
122     playlist_NodeSort( p_playlist, p_node, i_mode, i_type );
123     for( i = 0 ; i< p_node->i_children; i++ )
124     {
125         if( p_node->pp_children[i]->i_children != -1 )
126         {
127             playlist_RecursiveNodeSort( p_playlist, p_node->pp_children[i],
128                                         i_mode,i_type );
129         }
130     }
131
132     return VLC_SUCCESS;
133
134 }
135
136
137 int playlist_ItemArraySort( playlist_t *p_playlist, int i_items,
138                 playlist_item_t **pp_items, int i_mode,
139                 int i_type )
140 {
141     int i , i_small , i_position;
142     playlist_item_t *p_temp;
143     vlc_value_t val;
144     val.b_bool = VLC_TRUE;
145
146     if( i_mode == SORT_RANDOM )
147     {
148         for( i_position = 0; i_position < i_items ; i_position ++ )
149         {
150             int i_new;
151
152             if( i_items > 1 )
153                 i_new = rand() % (i_items - 1);
154             else
155                 i_new = 0;
156             p_temp = pp_items[i_position];
157             pp_items[i_position] = pp_items[i_new];
158             pp_items[i_new] = p_temp;
159         }
160
161         return VLC_SUCCESS;
162     }
163
164     for( i_position = 0; i_position < i_items -1 ; i_position ++ )
165     {
166         i_small  = i_position;
167         for( i = i_position + 1 ; i< i_items ; i++)
168         {
169             int i_test = 0;
170
171             if( i_mode == SORT_TITLE )
172             {
173                 i_test = strcasecmp( pp_items[i]->input.psz_name,
174                                          pp_items[i_small]->input.psz_name );
175             }
176             else if( i_mode == SORT_TITLE_NUMERIC )
177             {
178                 i_test = atoi( pp_items[i]->input.psz_name ) -
179                          atoi( pp_items[i_small]->input.psz_name );
180             }
181             else if( i_mode == SORT_DURATION )
182             {
183                 i_test = pp_items[i]->input.i_duration -
184                              pp_items[i_small]->input.i_duration;
185             }
186             else if( i_mode == SORT_AUTHOR )
187             {
188                 char *psz_a = vlc_input_item_GetInfo(
189                                  &pp_items[i]->input,
190                                  _(VLC_META_INFO_CAT), _(VLC_META_ARTIST) );
191                 char *psz_b = vlc_input_item_GetInfo(
192                                  &pp_items[i_small]->input,
193                                  _(VLC_META_INFO_CAT), _(VLC_META_ARTIST) );
194                 if( pp_items[i]->i_children == -1 &&
195                     pp_items[i_small]->i_children >= 0 )
196                 {
197                     i_test = 1;
198                 }
199                 else if( pp_items[i]->i_children >= 0 &&
200                          pp_items[i_small]->i_children == -1 )
201                 {
202                     i_test = -1;
203                 }
204                 // both are nodes
205                 else if( pp_items[i]->i_children >= 0 &&
206                          pp_items[i_small]->i_children >= 0 )
207                 {
208                     i_test = strcasecmp( pp_items[i]->input.psz_name,
209                                          pp_items[i_small]->input.psz_name );
210                 }
211                 else if( psz_a == NULL && psz_b != NULL )
212                 {
213                     i_test = 1;
214                 }
215                 else if( psz_a != NULL && psz_b == NULL )
216                 {
217                     i_test = -1;
218                 }
219                 else if( psz_a == NULL && psz_b == NULL )
220                 {
221                     i_test = strcasecmp( pp_items[i]->input.psz_name,
222                                          pp_items[i_small]->input.psz_name );
223                 }
224                 else
225                 {
226                     i_test = strcmp( psz_b, psz_a );
227                 }
228             }
229             else if( i_mode == SORT_ALBUM )
230             {
231                 char *psz_a = vlc_input_item_GetInfo(
232                                  &pp_items[i]->input,
233                                  _(VLC_META_INFO_CAT), _(VLC_META_COLLECTION) );
234                 char *psz_b = vlc_input_item_GetInfo(
235                                  &pp_items[i_small]->input,
236                                  _(VLC_META_INFO_CAT), _(VLC_META_COLLECTION) );
237                 if( pp_items[i]->i_children == -1 &&
238                     pp_items[i_small]->i_children >= 0 )
239                 {
240                     i_test = 1;
241                 }
242                 else if( pp_items[i]->i_children >= 0 &&
243                          pp_items[i_small]->i_children == -1 )
244                 {
245                     i_test = -1;
246                 }
247                 // both are nodes
248                 else if( pp_items[i]->i_children >= 0 &&
249                          pp_items[i_small]->i_children >= 0 )
250                 {
251                     i_test = strcasecmp( pp_items[i]->input.psz_name,
252                                          pp_items[i_small]->input.psz_name );
253                 }
254                 else if( psz_a == NULL && psz_b != NULL )
255                 {
256                     i_test = 1;
257                 }
258                 else if( psz_a != NULL && psz_b == NULL )
259                 {
260                     i_test = -1;
261                 }
262                 else if( psz_a == NULL && psz_b == NULL )
263                 {
264                     i_test = strcasecmp( pp_items[i]->input.psz_name,
265                                          pp_items[i_small]->input.psz_name );
266                 }
267                 else
268                 {
269                     i_test = strcmp( psz_b, psz_a );
270                 }
271             }
272             else if( i_mode == SORT_TITLE_NODES_FIRST )
273             {
274                 /* Alphabetic sort, all nodes first */
275
276                 if( pp_items[i]->i_children == -1 &&
277                     pp_items[i_small]->i_children >= 0 )
278                 {
279                     i_test = 1;
280                 }
281                 else if( pp_items[i]->i_children >= 0 &&
282                          pp_items[i_small]->i_children == -1 )
283                 {
284                     i_test = -1;
285                 }
286                 else
287                 {
288                     i_test = strcasecmp( pp_items[i]->input.psz_name,
289                                          pp_items[i_small]->input.psz_name );
290                 }
291             }
292
293             if( ( i_type == ORDER_NORMAL  && i_test < 0 ) ||
294                 ( i_type == ORDER_REVERSE && i_test > 0 ) )
295             {
296                 i_small = i;
297             }
298         }
299         p_temp = pp_items[i_position];
300         pp_items[i_position] = pp_items[i_small];
301         pp_items[i_small] = p_temp;
302     }
303     return VLC_SUCCESS;
304 }
305
306
307 int playlist_NodeGroup( playlist_t * p_playlist , int i_view,
308                         playlist_item_t *p_root,
309                         playlist_item_t **pp_items,int i_item,
310                         int i_mode, int i_type )
311 {
312     char *psz_search = NULL;
313     int i_nodes = 0;
314     playlist_item_t **pp_nodes = NULL;
315     playlist_item_t *p_node;
316     vlc_bool_t b_found;
317     int i,j;
318     for( i = 0; i< i_item ; i++ )
319     {
320         if( psz_search ) free( psz_search );
321         if( i_mode == SORT_TITLE )
322         {
323             psz_search = strdup( pp_items[i]->input.psz_name );
324         }
325         else if ( i_mode == SORT_AUTHOR )
326         {
327             psz_search = vlc_input_item_GetInfo( &pp_items[i]->input,
328                             _(VLC_META_INFO_CAT), _(VLC_META_ARTIST) );
329         }
330         else if ( i_mode == SORT_ALBUM )
331         {
332             psz_search = vlc_input_item_GetInfo( &pp_items[i]->input,
333                             _(VLC_META_INFO_CAT), _(VLC_META_COLLECTION) );
334         }
335         else if ( i_mode == SORT_GENRE )
336         {
337             psz_search = vlc_input_item_GetInfo( &pp_items[i]->input,
338                             _(VLC_META_INFO_CAT), _(VLC_META_GENRE) );
339         }
340
341         if( psz_search && !strcmp( psz_search, "" ) )
342         {
343             free( psz_search );
344             psz_search = strdup( _("Undefined") );
345         }
346
347         b_found = VLC_FALSE;
348         for( j = 0 ; j< i_nodes; j++ )
349         {
350            if( !strcasecmp( psz_search, pp_nodes[j]->input.psz_name ) )
351            {
352                 playlist_NodeAppend( p_playlist, i_view,
353                                      pp_items[i], pp_nodes[j] );
354                 b_found = VLC_TRUE;
355                 break;
356            }
357         }
358         if( !b_found )
359         {
360             p_node = playlist_NodeCreate( p_playlist, i_view,psz_search,
361                                           NULL );
362             INSERT_ELEM( pp_nodes, i_nodes, i_nodes, p_node );
363             playlist_NodeAppend( p_playlist, i_view,
364                                  pp_items[i],p_node );
365         }
366     }
367
368     /* Now, sort the nodes by name */
369     playlist_ItemArraySort( p_playlist, i_nodes, pp_nodes, SORT_TITLE,
370                             i_type );
371
372     /* Now, sort each node and append it to the root node*/
373     for( i = 0 ; i< i_nodes ; i++ )
374     {
375         playlist_ItemArraySort( p_playlist, pp_nodes[i]->i_children,
376                                 pp_nodes[i]->pp_children, SORT_TITLE, i_type );
377
378         playlist_NodeAppend( p_playlist, i_view,
379                              pp_nodes[i], p_root );
380     }
381     return VLC_SUCCESS;
382 }