]> git.sesse.net Git - vlc/commitdiff
* Merged sort functions
authorClément Stenac <zorglub@videolan.org>
Wed, 26 Nov 2003 10:45:21 +0000 (10:45 +0000)
committerClément Stenac <zorglub@videolan.org>
Wed, 26 Nov 2003 10:45:21 +0000 (10:45 +0000)
* Added a randomize playlist function

include/vlc_playlist.h
modules/gui/wxwindows/playlist.cpp
src/playlist/sort.c

index 38a10b6a16aaa75db5c333393d583adfe1b7fd70..78ecbeaa7bc739c68b6b899c8e4e419a541989fe 100644 (file)
@@ -2,7 +2,7 @@
  * vlc_playlist.h : Playlist functions
  *****************************************************************************
  * Copyright (C) 1999, 2000, 2001, 2002 VideoLAN
- * $Id: vlc_playlist.h,v 1.16 2003/11/12 08:10:21 zorglub Exp $
+ * $Id: vlc_playlist.h,v 1.17 2003/11/26 10:45:21 zorglub Exp $
  *
  * Authors: Samuel Hocevar <sam@zoy.org>
  *
@@ -96,6 +96,11 @@ struct playlist_t
     /*@}*/
 };
 
+#define SORT_TITLE 0
+#define SORT_AUTHOR 1
+#define SORT_GROUP 2
+#define SORT_RANDOM 3
+
 #define SORT_NORMAL 0
 #define SORT_REVERSE 1
 
@@ -132,9 +137,11 @@ VLC_EXPORT( int, playlist_DeleteGroup, (playlist_t *, int ) );
 VLC_EXPORT( char *, playlist_FindGroup, (playlist_t *, int ) );
 VLC_EXPORT( int, playlist_GroupToId, (playlist_t *, char * ) );
 
-VLC_EXPORT( int,  playlist_SortTitle, ( playlist_t *, int) );
-VLC_EXPORT( int,  playlist_SortAuthor, ( playlist_t *, int) );
-VLC_EXPORT( int,  playlist_SortGroup, ( playlist_t *, int) );
+#define playlist_SortTitle(p, i) playlist_Sort( p, SORT_TITLE, i)
+#define playlist_SortAuthor(p, i) playlist_Sort( p, SORT_AUTHOR, i)
+#define playlist_SortGroup(p, i) playlist_Sort( p, SORT_GROUP, i)
+
+VLC_EXPORT( int,  playlist_Sort, ( playlist_t *, int, int) );
 
 VLC_EXPORT( int,  playlist_Move, ( playlist_t *, int, int ) );
 VLC_EXPORT( int,  playlist_LoadFile, ( playlist_t *, const char * ) );
index 77db72b19d01ef34de2779a40aa07ffa53785858..ee5bd10b0214faeb4d8f73ef5ce44e4da0582821 100644 (file)
@@ -2,7 +2,7 @@
  * playlist.cpp : wxWindows plugin for vlc
  *****************************************************************************
  * Copyright (C) 2000-2001 VideoLAN
- * $Id: playlist.cpp,v 1.26 2003/11/21 18:55:40 gbazin Exp $
+ * $Id: playlist.cpp,v 1.27 2003/11/26 10:45:21 zorglub Exp $
  *
  * Authors: Olivier Teulière <ipkiss@via.ecp.fr>
  *
@@ -59,6 +59,7 @@ enum
     RSortAuthor_Event,
     SortGroup_Event,
     RSortGroup_Event,
+    Randomize_Event,
 
     EnableSelection_Event,
     DisableSelection_Event,
@@ -99,6 +100,8 @@ BEGIN_EVENT_TABLE(Playlist, wxFrame)
     EVT_MENU(SortGroup_Event, Playlist::OnSort)
     EVT_MENU(RSortGroup_Event, Playlist::OnSort)
 
+    EVT_MENU(Randomize_Event, Playlist::OnSort)
+
     EVT_MENU(EnableSelection_Event, Playlist::OnEnableSelection)
     EVT_MENU(DisableSelection_Event, Playlist::OnDisableSelection)
     EVT_MENU(InvertSelection_Event, Playlist::OnInvertSelection)
@@ -180,14 +183,16 @@ Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
 
     /* Create our "Sort" menu */
     wxMenu *sort_menu = new wxMenu;
-    sort_menu->Append( SortTitle_Event, wxU(_("&Sort by title")) );
+    sort_menu->Append( SortTitle_Event, wxU(_("Sort by &title")) );
     sort_menu->Append( RSortTitle_Event, wxU(_("&Reverse sort by title")) );
     sort_menu->AppendSeparator();
-    sort_menu->Append( SortAuthor_Event, wxU(_("&Sort by author")) );
+    sort_menu->Append( SortAuthor_Event, wxU(_("Sort by &author")) );
     sort_menu->Append( RSortAuthor_Event, wxU(_("&Reverse sort by author")) );
     sort_menu->AppendSeparator();
-    sort_menu->Append( SortGroup_Event, wxU(_("&Sort by group")) );
+    sort_menu->Append( SortGroup_Event, wxU(_("Sort by &group")) );
     sort_menu->Append( RSortGroup_Event, wxU(_("&Reverse sort by group")) );
+    sort_menu->AppendSeparator();
+    sort_menu->Append( Randomize_Event, wxU(_("&Randomize Playlist")) );
 
     /* Create our "Selection" menu */
     wxMenu *selection_menu = new wxMenu;
@@ -661,6 +666,9 @@ void Playlist::OnSort( wxCommandEvent& event )
         case RSortGroup_Event:
            playlist_SortGroup( p_playlist , 1 );
            break;
+        case Randomize_Event:
+           playlist_Sort( p_playlist , SORT_RANDOM, SORT_NORMAL );
+           break;
     }
     vlc_object_release( p_playlist );
 
index 8f97980a75bba022e62a1983d3e2bbb1662a7bc0..92ef5e9422a6eee8b966376f2c9655ac7f296f6d 100644 (file)
@@ -2,7 +2,7 @@
  * sort.c : Playlist sorting functions
  *****************************************************************************
  * Copyright (C) 1999-2001 VideoLAN
- * $Id: sort.c,v 1.1 2003/10/29 18:00:46 zorglub Exp $
+ * $Id: sort.c,v 1.2 2003/11/26 10:45:21 zorglub Exp $
  *
  * Authors: Clément Stenac <zorglub@videolan.org>
  *
 #include "vlc_playlist.h"
 
 /**
- * Sort the playlist by title
+ * Sort the playlist
  * \param p_playlist the playlist
+ * \param i_mode: SORT_TITLE, SORT_GROUP, SORT_AUTHOR, SORT_RANDOM
  * \param i_type: SORT_NORMAL or SORT_REVERSE (reversed order)
  * \return 0 on success
  */
-int playlist_SortTitle( playlist_t * p_playlist , int i_type )
+int playlist_Sort( playlist_t * p_playlist , int i_mode, int i_type )
 {
     int i , i_small , i_position;
     playlist_item_t *p_temp;
 
     vlc_mutex_lock( &p_playlist->object_lock );
 
-    for( i_position = 0; i_position < p_playlist->i_size -1 ; i_position ++ )
+    if( i_mode == SORT_RANDOM )
     {
-        i_small  = i_position;
-        for( i = i_position + 1 ; i<  p_playlist->i_size ; i++)
+        for( i_position = 0; i_position < p_playlist->i_size ; i_position ++ )
         {
-            int i_test;
+            int i_new  = rand() % (p_playlist->i_size - 1);
 
-            i_test = strcasecmp( p_playlist->pp_items[i]->psz_name,
-                                 p_playlist->pp_items[i_small]->psz_name );
+            /* 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;
 
-            if( ( i_type == SORT_NORMAL  && i_test < 0 ) ||
-                ( i_type == SORT_REVERSE && i_test > 0 ) )
-            {
-                i_small = i;
-            }
+            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;
         }
-        /* 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;
+        vlc_mutex_unlock( &p_playlist->object_lock );
 
-        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;
+        return 0;
     }
-    vlc_mutex_unlock( &p_playlist->object_lock );
-
-    return 0;
-}
-
-/**
- * Sort the playlist by author
- * \param p_playlist the playlist
- * \param i_type: SORT_NORMAL or SORT_REVERSE (reversed order)
- * \return 0 on success
- */
-int playlist_SortAuthor( playlist_t * p_playlist , int i_type )
-{
-    int i , i_small , i_position;
-    playlist_item_t *p_temp;
-
-    vlc_mutex_lock( &p_playlist->object_lock );
 
     for( i_position = 0; i_position < p_playlist->i_size -1 ; i_position ++ )
     {
         i_small  = i_position;
         for( i = i_position + 1 ; i<  p_playlist->i_size ; i++)
         {
-            int i_test;
+            int i_test = 0;
 
-            i_test = strcasecmp( p_playlist->pp_items[i]->psz_author,
-                                 p_playlist->pp_items[i_small]->psz_author );
-
-            if( ( i_type == SORT_NORMAL  && i_test < 0 ) ||
-                ( i_type == SORT_REVERSE && i_test > 0 ) )
+            if( i_mode == SORT_TITLE )
             {
-                i_small = i;
+                i_test = strcasecmp( p_playlist->pp_items[i]->psz_name,
+                                 p_playlist->pp_items[i_small]->psz_name );
             }
-        }
-        /* 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;
-    }
-    vlc_mutex_unlock( &p_playlist->object_lock );
-
-    return 0;
-}
-
-int playlist_SortGroup( playlist_t * p_playlist , int i_type )
-{
-    int i , i_small , i_position;
-    playlist_item_t *p_temp;
-
-    vlc_mutex_lock( &p_playlist->object_lock );
-
-    for( i_position = 0; i_position < p_playlist->i_size -1 ; i_position ++ )
-    {
-        i_small  = i_position;
-        for( i = i_position + 1 ; i<  p_playlist->i_size ; i++)
-        {
-            int i_test;
-
-            i_test = p_playlist->pp_items[i]->i_group -
+            else if( i_mode == SORT_GROUP )
+            {
+                i_test = p_playlist->pp_items[i]->i_group -
                                  p_playlist->pp_items[i_small]->i_group;
+            }
+            else if( i_mode == SORT_AUTHOR )
+            {
+                 i_test = strcasecmp( p_playlist->pp_items[i]->psz_author,
+                                 p_playlist->pp_items[i_small]->psz_author );
+            }
 
             if( ( i_type == SORT_NORMAL  && i_test < 0 ) ||
                 ( i_type == SORT_REVERSE && i_test > 0 ) )