]> git.sesse.net Git - vlc/blobdiff - src/playlist/sort.c
Improvements to the playlist core
[vlc] / src / playlist / sort.c
index 2d0a0f9a8dad13833a228cc0662c34cdc2c10e11..092b01ddab083d811c0c195168418ca12f57bb7c 100644 (file)
 
 #include "vlc_playlist.h"
 
+
+int playlist_ItemArraySort( playlist_t *p_playlist, int i_items,
+                playlist_item_t **pp_items, int i_mode,
+                int i_type );
+
+
 /**
- * Sort the playlist
+ * Sort the playlist.
  * \param p_playlist the playlist
- * \param i_mode: SORT_ID, SORT_TITLE, SORT_GROUP, SORT_AUTHOR, SORT_RANDOM
+ * \param i_mode: SORT_ID, SORT_TITLE, SORT_AUTHOR, SORT_RANDOM
  * \param i_type: ORDER_NORMAL or ORDER_REVERSE (reversed order)
  * \return VLC_SUCCESS on success
  */
 int playlist_Sort( playlist_t * p_playlist , int i_mode, int i_type )
 {
-    int i , i_small , i_position;
-    playlist_item_t *p_temp;
+    int  i_id = -1;
     vlc_value_t val;
     val.b_bool = VLC_TRUE;
 
@@ -49,76 +54,105 @@ int playlist_Sort( playlist_t * p_playlist , int i_mode, int i_type )
 
     p_playlist->i_sort = i_mode;
     p_playlist->i_order = i_type;
-    /* playlist with one or less items are allways sorted in all
-       manners, quit fast. */
-    if( p_playlist->i_size <= 1 )
+
+    if( p_playlist->i_index >= 0 )
     {
-        vlc_mutex_unlock( &p_playlist->object_lock );
+        i_id = p_playlist->pp_items[p_playlist->i_index]->input.i_id;
+    }
 
-        /* Notify the interfaces, is this necessary? */
-        var_Set( p_playlist, "intf-change", val );
+    playlist_ItemArraySort( p_playlist, p_playlist->i_size,
+                    p_playlist->pp_items, i_mode, i_type );
 
-        return VLC_SUCCESS;
+    if( i_id != -1 )
+    {
+        p_playlist->i_index = playlist_GetPositionById( p_playlist, i_id );
     }
 
+    /* ensure we are in no-view mode */
+    p_playlist->status.i_view = -1;
+
+    vlc_mutex_unlock( &p_playlist->object_lock );
+
+    /* Notify the interfaces */
+    var_Set( p_playlist, "intf-change", val );
+
+    return VLC_SUCCESS;
+}
+
+/**
+ * Sort a node.
+ * \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_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 )
+{
+    int i_id;
+    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 );
+
+    /* Notify the interfaces */
+    var_Set( p_playlist, "intf-change", val );
+
+    return VLC_SUCCESS;
+}
+
+
+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;
+    playlist_item_t *p_temp;
+    vlc_value_t val;
+    val.b_bool = VLC_TRUE;
+
     if( i_mode == SORT_RANDOM )
     {
-        for( i_position = 0; i_position < p_playlist->i_size ; i_position ++ )
+        for( i_position = 0; i_position < i_items ; i_position ++ )
         {
-            int i_new  = rand() % (p_playlist->i_size - 1);
-
-            /* Keep the correct current index */
-            if( i_new == p_playlist->i_index )
-                p_playlist->i_index = i_position;
-            else if( i_position == p_playlist->i_index )
-                p_playlist->i_index = i_new;
+            int i_new  = rand() % (i_items - 1);
 
-            p_temp = p_playlist->pp_items[i_position];
-            p_playlist->pp_items[i_position] = p_playlist->pp_items[i_new];
-            p_playlist->pp_items[i_new] = p_temp;
+            p_temp = pp_items[i_position];
+            pp_items[i_position] = pp_items[i_new];
+            pp_items[i_new] = p_temp;
         }
-        vlc_mutex_unlock( &p_playlist->object_lock );
-
-        /* Notify the interfaces */
-        var_Set( p_playlist, "intf-change", val );
 
         return VLC_SUCCESS;
     }
 
-    for( i_position = 0; i_position < p_playlist->i_size -1 ; i_position ++ )
+    for( i_position = 0; i_position < i_items -1 ; i_position ++ )
     {
         i_small  = i_position;
-        for( i = i_position + 1 ; i<  p_playlist->i_size ; i++)
+        for( i = i_position + 1 ; i< i_items ; i++)
         {
             int i_test = 0;
 
-            if( i_mode == SORT_ID )
+            if( i_mode == SORT_TITLE )
             {
-                i_test = p_playlist->pp_items[i]->i_id -
-                                 p_playlist->pp_items[i_small]->i_id;
-            }
-            else if( i_mode == SORT_TITLE )
-            {
-                i_test = strcasecmp( p_playlist->pp_items[i]->input.psz_name,
-                             p_playlist->pp_items[i_small]->input.psz_name );
-            }
-            else if( i_mode == SORT_GROUP )
-            {
-                i_test = p_playlist->pp_items[i]->i_group -
-                             p_playlist->pp_items[i_small]->i_group;
+                i_test = strcasecmp( pp_items[i]->input.psz_name,
+                                         pp_items[i_small]->input.psz_name );
             }
             else if( i_mode == SORT_DURATION )
             {
-                i_test = p_playlist->pp_items[i]->input.i_duration -
-                             p_playlist->pp_items[i_small]->input.i_duration;
+                i_test = pp_items[i]->input.i_duration -
+                             pp_items[i_small]->input.i_duration;
             }
             else if( i_mode == SORT_AUTHOR )
             {
-                 i_test = strcasecmp(
-                          playlist_GetInfo( p_playlist, i,
-                                            _("General") , _("Author") ),
-                          playlist_GetInfo( p_playlist, i_small,
-                                            _("General") , _("Author") ) );
+                msg_Err( p_playlist,"META SORT not implemented" );
             }
 
             if( ( i_type == ORDER_NORMAL  && i_test < 0 ) ||
@@ -127,20 +161,77 @@ int playlist_Sort( playlist_t * p_playlist , int i_mode, int i_type )
                 i_small = i;
             }
         }
-        /* Keep the correct current index */
-        if( i_small == p_playlist->i_index )
-            p_playlist->i_index = i_position;
-        else if( i_position == p_playlist->i_index )
-            p_playlist->i_index = i_small;
-
-        p_temp = p_playlist->pp_items[i_position];
-        p_playlist->pp_items[i_position] = p_playlist->pp_items[i_small];
-        p_playlist->pp_items[i_small] = p_temp;
+        p_temp = pp_items[i_position];
+        pp_items[i_position] = pp_items[i_small];
+        pp_items[i_small] = p_temp;
     }
-    vlc_mutex_unlock( &p_playlist->object_lock );
+    return VLC_SUCCESS;
+}
 
-    /* Notify the interfaces  */
-    var_Set( p_playlist, "intf-change", val );
 
+int playlist_NodeGroup( playlist_t * p_playlist , int i_view,
+                        playlist_item_t *p_root,
+                        playlist_item_t **pp_items,int i_item,
+                        int i_mode, int i_type )
+{
+    char *psz_search = NULL;
+    int i_nodes = 0;
+    playlist_item_t **pp_nodes = NULL;
+    playlist_item_t *p_node;
+    vlc_bool_t b_found;
+    int i,j;
+    for( i = 0; i< i_item ; i++ )
+    {
+        if( psz_search ) free( psz_search );
+        if( i_mode == SORT_TITLE )
+        {
+            psz_search = strdup( pp_items[i]->input.psz_name );
+        }
+        else if ( i_mode == SORT_AUTHOR )
+        {
+            psz_search = playlist_ItemGetInfo( pp_items[i],
+                            _("Meta-information"), _( "Artist" ) );
+        }
+
+        if( psz_search && !strcmp( psz_search, "" ) )
+        {
+            free( psz_search );
+            psz_search = strdup( _("Undefined") );
+        }
+
+        b_found = VLC_FALSE;
+        for( j = 0 ; j< i_nodes; j++ )
+        {
+           if( !strcasecmp( psz_search, pp_nodes[j]->input.psz_name ) )
+           {
+                playlist_NodeAppend( p_playlist, i_view,
+                                     pp_items[i], pp_nodes[j] );
+                b_found = VLC_TRUE;
+                break;
+           }
+        }
+        if( !b_found )
+        {
+            p_node = playlist_NodeCreate( p_playlist, i_view,psz_search,
+                                          NULL );
+            INSERT_ELEM( pp_nodes, i_nodes, i_nodes, p_node );
+            playlist_NodeAppend( p_playlist, i_view,
+                                 pp_items[i],p_node );
+        }
+    }
+
+    /* Now, sort the nodes by name */
+    playlist_ItemArraySort( p_playlist, i_nodes, pp_nodes, SORT_TITLE,
+                            i_type );
+
+    /* Now, sort each node and append it to the root node*/
+    for( i = 0 ; i< i_nodes ; i++ )
+    {
+        playlist_ItemArraySort( p_playlist, pp_nodes[i]->i_children,
+                                pp_nodes[i]->pp_children, SORT_TITLE, i_type );
+
+        playlist_NodeAppend( p_playlist, i_view,
+                             pp_nodes[i], p_root );
+    }
     return VLC_SUCCESS;
 }