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 );
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
90 int playlist_NodeSort( playlist_t * p_playlist , playlist_item_t *p_node,
91 int i_mode, int i_type )
94 val.b_bool = VLC_TRUE;
96 vlc_mutex_lock( &p_playlist->object_lock );
98 playlist_ItemArraySort( p_playlist,p_node->i_children,
99 p_node->pp_children, i_mode, i_type );
103 vlc_mutex_unlock( &p_playlist->object_lock );
105 /* Notify the interfaces */
106 var_Set( p_playlist, "intf-change", val );
112 int playlist_ItemArraySort( playlist_t *p_playlist, int i_items,
113 playlist_item_t **pp_items, int i_mode,
116 int i , i_small , i_position;
117 playlist_item_t *p_temp;
119 val.b_bool = VLC_TRUE;
121 if( i_mode == SORT_RANDOM )
123 for( i_position = 0; i_position < i_items ; i_position ++ )
125 int i_new = rand() % (i_items - 1);
127 p_temp = pp_items[i_position];
128 pp_items[i_position] = pp_items[i_new];
129 pp_items[i_new] = p_temp;
135 for( i_position = 0; i_position < i_items -1 ; i_position ++ )
137 i_small = i_position;
138 for( i = i_position + 1 ; i< i_items ; i++)
142 if( i_mode == SORT_TITLE )
144 i_test = strcasecmp( pp_items[i]->input.psz_name,
145 pp_items[i_small]->input.psz_name );
147 else if( i_mode == SORT_DURATION )
149 i_test = pp_items[i]->input.i_duration -
150 pp_items[i_small]->input.i_duration;
152 else if( i_mode == SORT_AUTHOR )
154 msg_Err( p_playlist,"META SORT not implemented" );
156 else if( i_mode == SORT_TITLE_NODES_FIRST )
158 /* Alphabetic sort, all nodes first */
160 if( pp_items[i]->i_children == -1 &&
161 pp_items[i_small]->i_children >= 0 )
165 else if( pp_items[i]->i_children >= 0 &&
166 pp_items[i_small]->i_children == -1 )
172 i_test = strcasecmp( pp_items[i]->input.psz_name,
173 pp_items[i_small]->input.psz_name );
177 if( ( i_type == ORDER_NORMAL && i_test < 0 ) ||
178 ( i_type == ORDER_REVERSE && i_test > 0 ) )
183 p_temp = pp_items[i_position];
184 pp_items[i_position] = pp_items[i_small];
185 pp_items[i_small] = p_temp;
191 int playlist_NodeGroup( playlist_t * p_playlist , int i_view,
192 playlist_item_t *p_root,
193 playlist_item_t **pp_items,int i_item,
194 int i_mode, int i_type )
196 char *psz_search = NULL;
198 playlist_item_t **pp_nodes = NULL;
199 playlist_item_t *p_node;
202 for( i = 0; i< i_item ; i++ )
204 if( psz_search ) free( psz_search );
205 if( i_mode == SORT_TITLE )
207 psz_search = strdup( pp_items[i]->input.psz_name );
209 else if ( i_mode == SORT_AUTHOR )
211 psz_search = playlist_ItemGetInfo( pp_items[i],
212 _("Meta-information"), _( "Artist" ) );
215 if( psz_search && !strcmp( psz_search, "" ) )
218 psz_search = strdup( _("Undefined") );
222 for( j = 0 ; j< i_nodes; j++ )
224 if( !strcasecmp( psz_search, pp_nodes[j]->input.psz_name ) )
226 playlist_NodeAppend( p_playlist, i_view,
227 pp_items[i], pp_nodes[j] );
234 p_node = playlist_NodeCreate( p_playlist, i_view,psz_search,
236 INSERT_ELEM( pp_nodes, i_nodes, i_nodes, p_node );
237 playlist_NodeAppend( p_playlist, i_view,
238 pp_items[i],p_node );
242 /* Now, sort the nodes by name */
243 playlist_ItemArraySort( p_playlist, i_nodes, pp_nodes, SORT_TITLE,
246 /* Now, sort each node and append it to the root node*/
247 for( i = 0 ; i< i_nodes ; i++ )
249 playlist_ItemArraySort( p_playlist, pp_nodes[i]->i_children,
250 pp_nodes[i]->pp_children, SORT_TITLE, i_type );
252 playlist_NodeAppend( p_playlist, i_view,
253 pp_nodes[i], p_root );