]> git.sesse.net Git - vlc/blobdiff - src/playlist/sort.c
Uniformize source files encoding
[vlc] / src / playlist / sort.c
index 2833f396c060b88953fb6a94ddeaa9c6042c5ae3..83db0db77c5f2ee1f1fd2144d8cb9a2631a093be 100644 (file)
@@ -1,10 +1,10 @@
 /*****************************************************************************
  * sort.c : Playlist sorting functions
  *****************************************************************************
- * Copyright (C) 1999-2004 VideoLAN
+ * Copyright (C) 1999-2004 the VideoLAN team
  * $Id$
  *
- * Authors: Clément Stenac <zorglub@videolan.org>
+ * Authors: Clément Stenac <zorglub@videolan.org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -18,7 +18,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 #include <stdlib.h>                                      /* free(), strtol() */
 #include <stdio.h>                                              /* sprintf() */
@@ -40,7 +40,7 @@ int playlist_ItemArraySort( playlist_t *p_playlist, int i_items,
 /**
  * Sort the playlist.
  * \param p_playlist the playlist
- * \param i_mode: SORT_ID, SORT_TITLE, SORT_AUTHOR, SORT_RANDOM
+ * \param i_mode: SORT_ID, SORT_TITLE, SORT_AUTHOR, SORT_ALBUM, SORT_RANDOM
  * \param i_type: ORDER_NORMAL or ORDER_REVERSE (reversed order)
  * \return VLC_SUCCESS on success
  */
@@ -81,31 +81,56 @@ int playlist_Sort( playlist_t * p_playlist , int i_mode, int i_type )
 
 /**
  * Sort a node.
+ *
+ * This function must be entered with the playlist lock !
+ *
  * \param p_playlist the playlist
  * \param p_node the node to sort
- * \param i_mode: SORT_ID, SORT_TITLE, SORT_AUTHOR, SORT_RANDOM
+ * \param i_mode: SORT_ID, SORT_TITLE, SORT_AUTHOR, SORT_ALBUM, SORT_RANDOM
  * \param i_type: ORDER_NORMAL or ORDER_REVERSE (reversed order)
  * \return VLC_SUCCESS on success
  */
 int playlist_NodeSort( playlist_t * p_playlist , playlist_item_t *p_node,
                        int i_mode, int i_type )
 {
-    vlc_value_t val;
-    val.b_bool = VLC_TRUE;
-
-    vlc_mutex_lock( &p_playlist->object_lock );
 
     playlist_ItemArraySort( p_playlist,p_node->i_children,
                             p_node->pp_children, i_mode, i_type );
 
     p_node->i_serial++;
 
-    vlc_mutex_unlock( &p_playlist->object_lock );
+    return VLC_SUCCESS;
+}
 
-    /* Notify the interfaces */
-    var_Set( p_playlist, "intf-change", val );
+/**
+ *
+ * Sort a node recursively.
+ *
+ * This function must be entered with the playlist lock !
+ *
+ * \param p_playlist the playlist
+ * \param p_node the node to sort
+ * \param i_mode: SORT_ID, SORT_TITLE, SORT_AUTHOR, SORT_ALBUM, SORT_RANDOM
+ * \param i_type: ORDER_NORMAL or ORDER_REVERSE (reversed order)
+ * \return VLC_SUCCESS on success
+ */
+int playlist_RecursiveNodeSort( playlist_t *p_playlist, playlist_item_t *p_node,
+                                int i_mode, int i_type )
+{
+    int i;
+
+    playlist_NodeSort( p_playlist, p_node, i_mode, i_type );
+    for( i = 0 ; i< p_node->i_children; i++ )
+    {
+        if( p_node->pp_children[i]->i_children != -1 )
+        {
+            playlist_RecursiveNodeSort( p_playlist, p_node->pp_children[i],
+                                        i_mode,i_type );
+        }
+    }
 
     return VLC_SUCCESS;
+
 }
 
 
@@ -122,8 +147,12 @@ int playlist_ItemArraySort( playlist_t *p_playlist, int i_items,
     {
         for( i_position = 0; i_position < i_items ; i_position ++ )
         {
-            int i_new  = rand() % (i_items - 1);
+            int i_new;
 
+            if( i_items > 1 )
+                i_new = rand() % (i_items - 1);
+            else
+                i_new = 0;
             p_temp = pp_items[i_position];
             pp_items[i_position] = pp_items[i_new];
             pp_items[i_new] = p_temp;
@@ -144,6 +173,11 @@ int playlist_ItemArraySort( playlist_t *p_playlist, int i_items,
                 i_test = strcasecmp( pp_items[i]->input.psz_name,
                                          pp_items[i_small]->input.psz_name );
             }
+            else if( i_mode == SORT_TITLE_NUMERIC )
+            {
+                i_test = atoi( pp_items[i]->input.psz_name ) -
+                         atoi( pp_items[i_small]->input.psz_name );
+            }
             else if( i_mode == SORT_DURATION )
             {
                 i_test = pp_items[i]->input.i_duration -
@@ -151,8 +185,90 @@ int playlist_ItemArraySort( playlist_t *p_playlist, int i_items,
             }
             else if( i_mode == SORT_AUTHOR )
             {
-                msg_Err( p_playlist,"META SORT not implemented" );
+                char *psz_a = vlc_input_item_GetInfo(
+                                 &pp_items[i]->input,
+                                 _( "Meta-information"), _("Artist") );
+                char *psz_b = vlc_input_item_GetInfo(
+                                 &pp_items[i_small]->input,
+                                 _( "Meta-information"), _("Artist") );
+                if( pp_items[i]->i_children == -1 &&
+                    pp_items[i_small]->i_children >= 0 )
+                {
+                    i_test = 1;
+                }
+                else if( pp_items[i]->i_children >= 0 &&
+                         pp_items[i_small]->i_children == -1 )
+                {
+                    i_test = -1;
+                }
+                // both are nodes
+                else if( pp_items[i]->i_children >= 0 &&
+                         pp_items[i_small]->i_children >= 0 )
+                {
+                    i_test = strcasecmp( pp_items[i]->input.psz_name,
+                                         pp_items[i_small]->input.psz_name );
+                }
+                else if( psz_a == NULL && psz_b != NULL )
+                {
+                    i_test = 1;
+                }
+                else if( psz_a != NULL && psz_b == NULL )
+                {
+                    i_test = -1;
+                }
+                else if( psz_a == NULL && psz_b == NULL )
+                {
+                    i_test = strcasecmp( pp_items[i]->input.psz_name,
+                                         pp_items[i_small]->input.psz_name );
+                }
+                else
+                {
+                    i_test = strcmp( psz_b, psz_a );
+                }
             }
+            else if( i_mode == SORT_ALBUM )
+           {
+                char *psz_a = vlc_input_item_GetInfo(
+                                 &pp_items[i]->input,
+                                 _( "Meta-information"), _("Album/movie/show title") );
+                char *psz_b = vlc_input_item_GetInfo(
+                                 &pp_items[i_small]->input,
+                                 _( "Meta-information"), _("Album/movie/show title") );
+                if( pp_items[i]->i_children == -1 &&
+                    pp_items[i_small]->i_children >= 0 )
+                {
+                    i_test = 1;
+                }
+                else if( pp_items[i]->i_children >= 0 &&
+                         pp_items[i_small]->i_children == -1 )
+                {
+                    i_test = -1;
+                }
+                // both are nodes
+                else if( pp_items[i]->i_children >= 0 &&
+                         pp_items[i_small]->i_children >= 0 )
+                {
+                    i_test = strcasecmp( pp_items[i]->input.psz_name,
+                                         pp_items[i_small]->input.psz_name );
+                }
+                else if( psz_a == NULL && psz_b != NULL )
+                {
+                    i_test = 1;
+                }
+                else if( psz_a != NULL && psz_b == NULL )
+                {
+                    i_test = -1;
+                }
+                else if( psz_a == NULL && psz_b == NULL )
+                {
+                    i_test = strcasecmp( pp_items[i]->input.psz_name,
+                                         pp_items[i_small]->input.psz_name );
+                }
+                else
+                {
+                    i_test = strcmp( psz_b, psz_a );
+                }
+           }
             else if( i_mode == SORT_TITLE_NODES_FIRST )
             {
                 /* Alphabetic sort, all nodes first */
@@ -208,9 +324,19 @@ int playlist_NodeGroup( playlist_t * p_playlist , int i_view,
         }
         else if ( i_mode == SORT_AUTHOR )
         {
-            psz_search = playlist_ItemGetInfo( pp_items[i],
+            psz_search = vlc_input_item_GetInfo( &pp_items[i]->input,
                             _("Meta-information"), _( "Artist" ) );
         }
+        else if ( i_mode == SORT_ALBUM )
+        {
+            psz_search = vlc_input_item_GetInfo( &pp_items[i]->input,
+                            _("Meta-information"), _( "Album/movie/show title" ) );
+        }
+        else if ( i_mode == SORT_GENRE )
+        {
+            psz_search = vlc_input_item_GetInfo( &pp_items[i]->input,
+                            _("Meta-information"), _( "Genre" ) );
+        }
 
         if( psz_search && !strcmp( psz_search, "" ) )
         {