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"
36 * \param p_playlist the playlist
37 * \param i_mode: SORT_ID, SORT_TITLE, SORT_GROUP, SORT_AUTHOR, SORT_RANDOM
38 * \param i_type: ORDER_NORMAL or ORDER_REVERSE (reversed order)
39 * \return VLC_SUCCESS on success
41 int playlist_Sort( playlist_t * p_playlist , int i_mode, int i_type )
43 int i , i_small , i_position;
44 playlist_item_t *p_temp;
46 val.b_bool = VLC_TRUE;
48 vlc_mutex_lock( &p_playlist->object_lock );
50 p_playlist->i_sort = i_mode;
51 p_playlist->i_order = i_type;
52 /* playlist with one or less items are allways sorted in all
53 manners, quit fast. */
54 if( p_playlist->i_size <= 1 )
56 vlc_mutex_unlock( &p_playlist->object_lock );
58 /* Notify the interfaces, is this necessary? */
59 var_Set( p_playlist, "intf-change", val );
64 if( i_mode == SORT_RANDOM )
66 for( i_position = 0; i_position < p_playlist->i_size ; i_position ++ )
68 int i_new = rand() % (p_playlist->i_size - 1);
70 /* Keep the correct current index */
71 if( i_new == p_playlist->i_index )
72 p_playlist->i_index = i_position;
73 else if( i_position == p_playlist->i_index )
74 p_playlist->i_index = i_new;
76 p_temp = p_playlist->pp_items[i_position];
77 p_playlist->pp_items[i_position] = p_playlist->pp_items[i_new];
78 p_playlist->pp_items[i_new] = p_temp;
80 vlc_mutex_unlock( &p_playlist->object_lock );
82 /* Notify the interfaces */
83 var_Set( p_playlist, "intf-change", val );
88 for( i_position = 0; i_position < p_playlist->i_size -1 ; i_position ++ )
91 for( i = i_position + 1 ; i< p_playlist->i_size ; i++)
95 if( i_mode == SORT_ID )
97 i_test = p_playlist->pp_items[i]->i_id -
98 p_playlist->pp_items[i_small]->i_id;
100 else if( i_mode == SORT_TITLE )
102 i_test = strcasecmp( p_playlist->pp_items[i]->input.psz_name,
103 p_playlist->pp_items[i_small]->input.psz_name );
105 else if( i_mode == SORT_GROUP )
107 i_test = p_playlist->pp_items[i]->i_group -
108 p_playlist->pp_items[i_small]->i_group;
110 else if( i_mode == SORT_DURATION )
112 i_test = p_playlist->pp_items[i]->input.i_duration -
113 p_playlist->pp_items[i_small]->input.i_duration;
115 else if( i_mode == SORT_AUTHOR )
118 playlist_GetInfo( p_playlist, i,
119 _("General") , _("Author") ),
120 playlist_GetInfo( p_playlist, i_small,
121 _("General") , _("Author") ) );
124 if( ( i_type == ORDER_NORMAL && i_test < 0 ) ||
125 ( i_type == ORDER_REVERSE && i_test > 0 ) )
130 /* Keep the correct current index */
131 if( i_small == p_playlist->i_index )
132 p_playlist->i_index = i_position;
133 else if( i_position == p_playlist->i_index )
134 p_playlist->i_index = i_small;
136 p_temp = p_playlist->pp_items[i_position];
137 p_playlist->pp_items[i_position] = p_playlist->pp_items[i_small];
138 p_playlist->pp_items[i_small] = p_temp;
140 vlc_mutex_unlock( &p_playlist->object_lock );
142 /* Notify the interfaces */
143 var_Set( p_playlist, "intf-change", val );