]> git.sesse.net Git - vlc/blob - src/playlist/sort.c
Improvements to the playlist core
[vlc] / src / playlist / sort.c
1 /*****************************************************************************
2  * sort.c : Playlist sorting functions
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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_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  * \param p_playlist the playlist
85  * \param p_node the node to sort
86  * \param i_mode: SORT_ID, SORT_TITLE, SORT_AUTHOR, SORT_RANDOM
87  * \param i_type: ORDER_NORMAL or ORDER_REVERSE (reversed order)
88  * \return VLC_SUCCESS on success
89  */
90 int playlist_NodeSort( playlist_t * p_playlist , playlist_item_t *p_node,
91                        int i_mode, int i_type )
92 {
93     int i_id;
94     vlc_value_t val;
95     val.b_bool = VLC_TRUE;
96
97     vlc_mutex_lock( &p_playlist->object_lock );
98
99     playlist_ItemArraySort( p_playlist,p_node->i_children,
100                             p_node->pp_children, i_mode, i_type );
101
102     p_node->i_serial++;
103
104     vlc_mutex_unlock( &p_playlist->object_lock );
105
106     /* Notify the interfaces */
107     var_Set( p_playlist, "intf-change", val );
108
109     return VLC_SUCCESS;
110 }
111
112
113 int playlist_ItemArraySort( playlist_t *p_playlist, int i_items,
114                 playlist_item_t **pp_items, int i_mode,
115                 int i_type )
116 {
117     int i , i_small , i_position;
118     playlist_item_t *p_temp;
119     vlc_value_t val;
120     val.b_bool = VLC_TRUE;
121
122     if( i_mode == SORT_RANDOM )
123     {
124         for( i_position = 0; i_position < i_items ; i_position ++ )
125         {
126             int i_new  = rand() % (i_items - 1);
127
128             p_temp = pp_items[i_position];
129             pp_items[i_position] = pp_items[i_new];
130             pp_items[i_new] = p_temp;
131         }
132
133         return VLC_SUCCESS;
134     }
135
136     for( i_position = 0; i_position < i_items -1 ; i_position ++ )
137     {
138         i_small  = i_position;
139         for( i = i_position + 1 ; i< i_items ; i++)
140         {
141             int i_test = 0;
142
143             if( i_mode == SORT_TITLE )
144             {
145                 i_test = strcasecmp( pp_items[i]->input.psz_name,
146                                          pp_items[i_small]->input.psz_name );
147             }
148             else if( i_mode == SORT_DURATION )
149             {
150                 i_test = pp_items[i]->input.i_duration -
151                              pp_items[i_small]->input.i_duration;
152             }
153             else if( i_mode == SORT_AUTHOR )
154             {
155                 msg_Err( p_playlist,"META SORT not implemented" );
156             }
157
158             if( ( i_type == ORDER_NORMAL  && i_test < 0 ) ||
159                 ( i_type == ORDER_REVERSE && i_test > 0 ) )
160             {
161                 i_small = i;
162             }
163         }
164         p_temp = pp_items[i_position];
165         pp_items[i_position] = pp_items[i_small];
166         pp_items[i_small] = p_temp;
167     }
168     return VLC_SUCCESS;
169 }
170
171
172 int playlist_NodeGroup( playlist_t * p_playlist , int i_view,
173                         playlist_item_t *p_root,
174                         playlist_item_t **pp_items,int i_item,
175                         int i_mode, int i_type )
176 {
177     char *psz_search = NULL;
178     int i_nodes = 0;
179     playlist_item_t **pp_nodes = NULL;
180     playlist_item_t *p_node;
181     vlc_bool_t b_found;
182     int i,j;
183     for( i = 0; i< i_item ; i++ )
184     {
185         if( psz_search ) free( psz_search );
186         if( i_mode == SORT_TITLE )
187         {
188             psz_search = strdup( pp_items[i]->input.psz_name );
189         }
190         else if ( i_mode == SORT_AUTHOR )
191         {
192             psz_search = playlist_ItemGetInfo( pp_items[i],
193                             _("Meta-information"), _( "Artist" ) );
194         }
195
196         if( psz_search && !strcmp( psz_search, "" ) )
197         {
198             free( psz_search );
199             psz_search = strdup( _("Undefined") );
200         }
201
202         b_found = VLC_FALSE;
203         for( j = 0 ; j< i_nodes; j++ )
204         {
205            if( !strcasecmp( psz_search, pp_nodes[j]->input.psz_name ) )
206            {
207                 playlist_NodeAppend( p_playlist, i_view,
208                                      pp_items[i], pp_nodes[j] );
209                 b_found = VLC_TRUE;
210                 break;
211            }
212         }
213         if( !b_found )
214         {
215             p_node = playlist_NodeCreate( p_playlist, i_view,psz_search,
216                                           NULL );
217             INSERT_ELEM( pp_nodes, i_nodes, i_nodes, p_node );
218             playlist_NodeAppend( p_playlist, i_view,
219                                  pp_items[i],p_node );
220         }
221     }
222
223     /* Now, sort the nodes by name */
224     playlist_ItemArraySort( p_playlist, i_nodes, pp_nodes, SORT_TITLE,
225                             i_type );
226
227     /* Now, sort each node and append it to the root node*/
228     for( i = 0 ; i< i_nodes ; i++ )
229     {
230         playlist_ItemArraySort( p_playlist, pp_nodes[i]->i_children,
231                                 pp_nodes[i]->pp_children, SORT_TITLE, i_type );
232
233         playlist_NodeAppend( p_playlist, i_view,
234                              pp_nodes[i], p_root );
235     }
236     return VLC_SUCCESS;
237 }