]> git.sesse.net Git - vlc/blobdiff - src/playlist/sort.c
Merge branch '1.0-bugfix'
[vlc] / src / playlist / sort.c
index c81a2ecb49dad8e1914a4831b095a67c95d53447..5cbf2ac370fc1bd7444326b0976a3f3311281b4b 100644 (file)
@@ -5,6 +5,7 @@
  * $Id$
  *
  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
+ *          Ilkka Ollakka <ileoo@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
@@ -24,7 +25,7 @@
 # include "config.h"
 #endif
 
-#include <vlc/vlc.h>
+#include <vlc_common.h>
 #include "vlc_playlist.h"
 #include "playlist_internal.h"
 
@@ -32,6 +33,7 @@
 static int playlist_ItemArraySort( playlist_t *p_playlist, int i_items,
                                    playlist_item_t **pp_items, int i_mode,
                                    int i_type );
+static int playlist_cmp(const void *, const void *);
 
 /**
  * Sort a node.
@@ -52,41 +54,65 @@ static int playlist_NodeSort( playlist_t * p_playlist , playlist_item_t *p_node,
 }
 
 /**
- * Sort a node recursively.
- *
- * This function must be entered with the playlist lock !
- *
+ * Sort a node recursively
  * \param p_playlist the playlist
  * \param p_node the node to sort
  * \param i_mode: SORT_ID, SORT_TITLE, SORT_ARTIST, 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 )
+static int recursiveNodeSort( playlist_t *p_playlist, playlist_item_t *p_node,
+                              int i_mode, int i_type )
 {
     int i;
+    /* Sort the current node */
     playlist_NodeSort( p_playlist, p_node, i_mode, i_type );
+
+    /* And all the children */
     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 );
+            recursiveNodeSort( p_playlist, p_node->pp_children[i],
+                               i_mode, i_type );
         }
     }
     return VLC_SUCCESS;
 }
 
 
+/**
+ * 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_ARTIST, 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 )
+{
+    /* Ask the playlist to reset as we are changing the order */
+    pl_priv(p_playlist)->b_reset_currently_playing = true;
+
+    /* Do the real job recursively */
+    return recursiveNodeSort( p_playlist, p_node, i_mode, i_type );
+}
+
+static int sort_mode = 0;
+static int sort_type = 0;
+
 static int playlist_ItemArraySort( playlist_t *p_playlist, int i_items,
                                    playlist_item_t **pp_items, int i_mode,
                                    int i_type )
 {
-    int i , i_small , i_position;
+    int i_position;
     playlist_item_t *p_temp;
-    vlc_value_t val;
-    val.b_bool = VLC_TRUE;
+    sort_mode = i_mode;
+    sort_type = i_type;
 
     (void)p_playlist; // a bit surprising we don't need p_playlist!
 
@@ -107,30 +133,39 @@ static int playlist_ItemArraySort( playlist_t *p_playlist, int i_items,
 
         return VLC_SUCCESS;
     }
+    qsort(pp_items,i_items,sizeof(pp_items[0]),playlist_cmp);
+    return VLC_SUCCESS;
+}
+
+static int playlist_cmp(const void *first, const void *second)
+{
 
-#define META_STRCASECMP_NAME( i, i_small ) { \
-    char *psz_i = input_item_GetName( pp_items[i]->p_input ); \
-    char *psz_ismall = input_item_GetName( pp_items[i_small]->p_input ); \
-    i_test = strcasecmp( psz_i, psz_ismall ); \
+#define META_STRCASECMP_NAME( ) { \
+    char *psz_i = input_item_GetTitleFbName( (*(playlist_item_t **)first)->p_input ); \
+    char *psz_ismall = input_item_GetTitleFbName( (*(playlist_item_t **)second)->p_input ); \
+    if( psz_i != NULL && psz_ismall != NULL ) i_test = strcasecmp( psz_i, psz_ismall ); \
+    else if ( psz_i == NULL && psz_ismall != NULL ) i_test = 1; \
+    else if ( psz_ismall == NULL && psz_i != NULL ) i_test = -1; \
+    else i_test = 0; \
     free( psz_i ); \
     free( psz_ismall ); \
 }
 
 
 #define DO_META_SORT_ADV( node, integer ) { \
-    char *psz_a = input_item_GetMeta( pp_items[i]->p_input, vlc_meta_##node ); \
-    char *psz_b = input_item_GetMeta( pp_items[i_small]->p_input, vlc_meta_##node ); \
+    char *psz_a = input_item_GetMeta( (*(playlist_item_t **)first)->p_input, vlc_meta_##node ); \
+    char *psz_b = input_item_GetMeta( (*(playlist_item_t **)second)->p_input, vlc_meta_##node ); \
     /* Nodes go first */ \
-    if( pp_items[i]->i_children == -1 && pp_items[i_small]->i_children >= 0 ) \
+    if( (*(playlist_item_t **)first)->i_children == -1 && (*(playlist_item_t **)second)->i_children >= 0 ) \
         i_test = 1;\
-    else if( pp_items[i]->i_children >= 0 &&\
-             pp_items[i_small]->i_children == -1 ) \
+    else if( (*(playlist_item_t **)first)->i_children >= 0 &&\
+             (*(playlist_item_t **)second)->i_children == -1 ) \
        i_test = -1; \
     /* Both are nodes, sort by name */ \
-    else if( pp_items[i]->i_children >= 0 && \
-               pp_items[i_small]->i_children >= 0 ) \
+    else if( (*(playlist_item_t **)first)->i_children >= 0 && \
+               (*(playlist_item_t **)second)->i_children >= 0 ) \
     { \
-        META_STRCASECMP_NAME( i, i_small ) \
+        META_STRCASECMP_NAME( ) \
     } \
     /* Both are items */ \
     else if( psz_a == NULL && psz_b != NULL ) \
@@ -140,104 +175,104 @@ static int playlist_ItemArraySort( playlist_t *p_playlist, int i_items,
     /* No meta, sort by name */ \
     else if( psz_a == NULL && psz_b == NULL ) \
     { \
-        META_STRCASECMP_NAME( i, i_small ); \
+        META_STRCASECMP_NAME( ); \
     } \
     else \
     { \
-        if( !integer ) i_test = strcmp( psz_a, psz_b ); \
+        if( !integer ) i_test = strcasecmp( psz_a, psz_b ); \
         else           i_test = atoi( psz_a ) - atoi( psz_b ); \
     } \
     free( psz_a ); \
     free( psz_b ); \
 }
-#define DO_META_SORT( node ) DO_META_SORT_ADV( node, VLC_FALSE )
+#define DO_META_SORT( node ) DO_META_SORT_ADV( node, false )
+
+    int i_test = 0;
 
-    for( i_position = 0; i_position < i_items -1 ; i_position ++ )
+    if( sort_mode == SORT_TITLE )
+    {
+        META_STRCASECMP_NAME( );
+    }
+    else if( sort_mode == SORT_TITLE_NUMERIC )
+    {
+        char *psz_i = input_item_GetTitleFbName(
+                                (*(playlist_item_t **)first)->p_input );
+        char *psz_ismall = input_item_GetTitleFbName(
+                                (*(playlist_item_t **)second)->p_input );
+        i_test = atoi( psz_i ) - atoi( psz_ismall );
+        free( psz_i );
+        free( psz_ismall );
+    }
+    else if( sort_mode == SORT_DURATION )
+    {
+        i_test = input_item_GetDuration( (*(playlist_item_t **)first)->p_input ) -
+                 input_item_GetDuration( (*(playlist_item_t **)second)->p_input );
+    }
+    else if( sort_mode == SORT_ARTIST )
     {
-        i_small  = i_position;
-        for( i = i_position + 1 ; i< i_items ; i++)
+        DO_META_SORT( Artist );
+        /* sort by artist, album, tracknumber */
+        if( i_test == 0 )
+            DO_META_SORT( Album );
+        if( i_test == 0 )
+            DO_META_SORT_ADV( TrackNumber, true );
+    }
+    else if( sort_mode == SORT_GENRE )
+    {
+        DO_META_SORT( Genre );
+    }
+    else if( sort_mode == SORT_ALBUM )
+    {
+        DO_META_SORT( Album );
+        /* Sort by tracknumber if albums are the same */
+        if( i_test == 0 )
+            DO_META_SORT_ADV( TrackNumber, true );
+    }
+    else if( sort_mode == SORT_TRACK_NUMBER )
+    {
+        DO_META_SORT_ADV( TrackNumber, true );
+    }
+    else if( sort_mode == SORT_DESCRIPTION )
+    {
+        DO_META_SORT( Description );
+    }
+    else if( sort_mode == SORT_ID )
+    {
+        i_test = (*(playlist_item_t **)first)->i_id - (*(playlist_item_t **)second)->i_id;
+    }
+    else if( sort_mode == SORT_TITLE_NODES_FIRST )
+    {
+        /* Alphabetic sort, all nodes first */
+
+        if( (*(playlist_item_t **)first)->i_children == -1 &&
+            (*(playlist_item_t **)second)->i_children >= 0 )
+        {
+            i_test = 1;
+        }
+        else if( (*(playlist_item_t **)first)->i_children >= 0 &&
+                 (*(playlist_item_t **)second)->i_children == -1 )
+        {
+            i_test = -1;
+        }
+        else
         {
-            int i_test = 0;
-
-            if( i_mode == SORT_TITLE )
-            {
-                META_STRCASECMP_NAME( i, i_small );
-            }
-            else if( i_mode == SORT_TITLE_NUMERIC )
-            {
-                char *psz_i = input_item_GetName( pp_items[i]->p_input );
-                char *psz_ismall =
-                        input_item_GetName( pp_items[i_small]->p_input );
-                i_test = atoi( psz_i ) - atoi( psz_ismall );
-                free( psz_i );
-                free( psz_ismall );
-            }
-            else if( i_mode == SORT_DURATION )
-            {
-                i_test = input_item_GetDuration( pp_items[i]->p_input ) -
-                         input_item_GetDuration( pp_items[i_small]->p_input );
-            }
-            else if( i_mode == SORT_ARTIST )
-            {
-                DO_META_SORT( Artist );
-            }
-            else if( i_mode == SORT_GENRE )
-            {
-                DO_META_SORT( Genre );
-            }
-            else if( i_mode == SORT_ALBUM )
-            {
-                DO_META_SORT( Album );
-            }
-            else if( i_mode == SORT_TRACK_NUMBER )
-            {
-                DO_META_SORT_ADV( TrackNumber, VLC_TRUE );
-            }
-            else if( i_mode == SORT_DESCRIPTION )
-            {
-                DO_META_SORT( Description );
-            }
-            else if( i_mode == SORT_RATING )
-            {
-                DO_META_SORT( Rating );
-            }
-            else if( i_mode == SORT_ID )
-            {
-                i_test = pp_items[i]->i_id - pp_items[i_small]->i_id;
-            }
-            else if( i_mode == SORT_TITLE_NODES_FIRST )
-            {
-                /* Alphabetic sort, all nodes first */
-
-                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;
-                }
-                else
-                {
-                    i_test = strcasecmp( pp_items[i]->p_input->psz_name,
-                                         pp_items[i_small]->p_input->psz_name );
-                }
-            }
-
-            if( ( i_type == ORDER_NORMAL  && i_test < 0 ) ||
-                ( i_type == ORDER_REVERSE && i_test > 0 ) )
-            {
-                i_small = i;
-            }
+            META_STRCASECMP_NAME();
         }
-        p_temp = pp_items[i_position];
-        pp_items[i_position] = pp_items[i_small];
-        pp_items[i_small] = p_temp;
     }
+    else if( sort_mode == SORT_URI )
+    {
+        char *psz_i = input_item_GetURI( (*(playlist_item_t **)first)->p_input );
+        char *psz_ismall =
+                input_item_GetURI( (*(playlist_item_t **)second)->p_input );
+        i_test = strcasecmp( psz_i, psz_ismall );
+        free( psz_i );
+        free( psz_ismall );
+    }
+
+    if ( sort_type == ORDER_REVERSE )
+        i_test = i_test * -1;
 #undef DO_META_SORT
 #undef DO_META_SORT_ADV
 
-    return VLC_SUCCESS;
+    return i_test;
 }