1 /*****************************************************************************
2 * sort.c : Playlist sorting functions
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VideoLAN
7 * Authors: Clément Stenac <zorglub@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., 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() */
28 #include <vlc/input.h>
32 #include "vlc_playlist.h"
35 int playlist_ItemArraySort( playlist_t *p_playlist, int i_items,
36 playlist_item_t **pp_items, int i_mode,
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
47 int playlist_Sort( playlist_t * p_playlist , int i_mode, int i_type )
51 val.b_bool = VLC_TRUE;
53 vlc_mutex_lock( &p_playlist->object_lock );
55 p_playlist->i_sort = i_mode;
56 p_playlist->i_order = i_type;
58 if( p_playlist->i_index >= 0 )
60 i_id = p_playlist->pp_items[p_playlist->i_index]->input.i_id;
63 playlist_ItemArraySort( p_playlist, p_playlist->i_size,
64 p_playlist->pp_items, i_mode, i_type );
68 p_playlist->i_index = playlist_GetPositionById( p_playlist, i_id );
71 /* ensure we are in no-view mode */
72 p_playlist->status.i_view = -1;
74 vlc_mutex_unlock( &p_playlist->object_lock );
76 /* Notify the interfaces */
77 var_Set( p_playlist, "intf-change", val );
85 * This function must be entered with the playlist lock !
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_RANDOM
90 * \param i_type: ORDER_NORMAL or ORDER_REVERSE (reversed order)
91 * \return VLC_SUCCESS on success
93 int playlist_NodeSort( playlist_t * p_playlist , playlist_item_t *p_node,
94 int i_mode, int i_type )
97 playlist_ItemArraySort( p_playlist,p_node->i_children,
98 p_node->pp_children, i_mode, i_type );
107 * Sort a node recursively.
109 * This function must be entered with the playlist lock !
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_RANDOM
114 * \param i_type: ORDER_NORMAL or ORDER_REVERSE (reversed order)
115 * \return VLC_SUCCESS on success
117 int playlist_RecursiveNodeSort( playlist_t *p_playlist, playlist_item_t *p_node,
118 int i_mode, int i_type )
122 playlist_NodeSort( p_playlist, p_node, i_mode, i_type );
123 for( i = 0 ; i< p_node->i_children; i++ )
125 if( p_node->pp_children[i]->i_children != -1 )
127 playlist_RecursiveNodeSort( p_playlist, p_node->pp_children[i],
137 int playlist_ItemArraySort( playlist_t *p_playlist, int i_items,
138 playlist_item_t **pp_items, int i_mode,
141 int i , i_small , i_position;
142 playlist_item_t *p_temp;
144 val.b_bool = VLC_TRUE;
146 if( i_mode == SORT_RANDOM )
148 for( i_position = 0; i_position < i_items ; i_position ++ )
150 int i_new = rand() % (i_items - 1);
152 p_temp = pp_items[i_position];
153 pp_items[i_position] = pp_items[i_new];
154 pp_items[i_new] = p_temp;
160 for( i_position = 0; i_position < i_items -1 ; i_position ++ )
162 i_small = i_position;
163 for( i = i_position + 1 ; i< i_items ; i++)
167 if( i_mode == SORT_TITLE )
169 i_test = strcasecmp( pp_items[i]->input.psz_name,
170 pp_items[i_small]->input.psz_name );
172 else if( i_mode == SORT_DURATION )
174 i_test = pp_items[i]->input.i_duration -
175 pp_items[i_small]->input.i_duration;
177 else if( i_mode == SORT_AUTHOR )
179 msg_Err( p_playlist,"META SORT not implemented" );
181 else if( i_mode == SORT_TITLE_NODES_FIRST )
183 /* Alphabetic sort, all nodes first */
185 if( pp_items[i]->i_children == -1 &&
186 pp_items[i_small]->i_children >= 0 )
190 else if( pp_items[i]->i_children >= 0 &&
191 pp_items[i_small]->i_children == -1 )
197 i_test = strcasecmp( pp_items[i]->input.psz_name,
198 pp_items[i_small]->input.psz_name );
202 if( ( i_type == ORDER_NORMAL && i_test < 0 ) ||
203 ( i_type == ORDER_REVERSE && i_test > 0 ) )
208 p_temp = pp_items[i_position];
209 pp_items[i_position] = pp_items[i_small];
210 pp_items[i_small] = p_temp;
216 int playlist_NodeGroup( playlist_t * p_playlist , int i_view,
217 playlist_item_t *p_root,
218 playlist_item_t **pp_items,int i_item,
219 int i_mode, int i_type )
221 char *psz_search = NULL;
223 playlist_item_t **pp_nodes = NULL;
224 playlist_item_t *p_node;
227 for( i = 0; i< i_item ; i++ )
229 if( psz_search ) free( psz_search );
230 if( i_mode == SORT_TITLE )
232 psz_search = strdup( pp_items[i]->input.psz_name );
234 else if ( i_mode == SORT_AUTHOR )
236 psz_search = playlist_ItemGetInfo( pp_items[i],
237 _("Meta-information"), _( "Artist" ) );
240 if( psz_search && !strcmp( psz_search, "" ) )
243 psz_search = strdup( _("Undefined") );
247 for( j = 0 ; j< i_nodes; j++ )
249 if( !strcasecmp( psz_search, pp_nodes[j]->input.psz_name ) )
251 playlist_NodeAppend( p_playlist, i_view,
252 pp_items[i], pp_nodes[j] );
259 p_node = playlist_NodeCreate( p_playlist, i_view,psz_search,
261 INSERT_ELEM( pp_nodes, i_nodes, i_nodes, p_node );
262 playlist_NodeAppend( p_playlist, i_view,
263 pp_items[i],p_node );
267 /* Now, sort the nodes by name */
268 playlist_ItemArraySort( p_playlist, i_nodes, pp_nodes, SORT_TITLE,
271 /* Now, sort each node and append it to the root node*/
272 for( i = 0 ; i< i_nodes ; i++ )
274 playlist_ItemArraySort( p_playlist, pp_nodes[i]->i_children,
275 pp_nodes[i]->pp_children, SORT_TITLE, i_type );
277 playlist_NodeAppend( p_playlist, i_view,
278 pp_nodes[i], p_root );