From 80eb9273f5fcd426c9e33bea0a1620ed5ee4654f Mon Sep 17 00:00:00 2001
From: Fabio Ritrovato <exsephiroth87@gmail.com>
Date: Wed, 3 Sep 2008 14:01:39 +0200
Subject: [PATCH] Skins2: add support for the new SDs

---
 modules/gui/skins2/commands/cmd_playtree.cpp |   52 ++++++++++++++++++++++
 modules/gui/skins2/commands/cmd_playtree.hpp |   27 ++++++++++++
 modules/gui/skins2/src/dialogs.cpp           |   29 +++++++++++-
 modules/gui/skins2/src/dialogs.hpp           |    2 +
 modules/gui/skins2/vars/playtree.cpp         |   59 ++++++++++++++++++++------
 5 files changed, 153 insertions(+), 16 deletions(-)

diff --git a/modules/gui/skins2/commands/cmd_playtree.cpp b/modules/gui/skins2/commands/cmd_playtree.cpp
index 9d61617..50e9e35 100644
--- a/modules/gui/skins2/commands/cmd_playtree.cpp
+++ b/modules/gui/skins2/commands/cmd_playtree.cpp
@@ -46,3 +46,55 @@ void CmdPlaytreeSort::execute()
     Playtree &rVar = VlcProc::instance( getIntf() )->getPlaytreeVar();
     rVar.onChange();
 }
+
+void CmdPlaytreeAdd::execute()
+{
+    int i, id;
+    Playtree &rVar = VlcProc::instance( getIntf() )->getPlaytreeVar();
+    playlist_t *p_playlist = getIntf()->p_sys->p_playlist;
+    VarTree *parent = rVar.getLastSelected();
+    if( parent != NULL )
+    {
+        while( parent->parent() != rVar.root() )
+            parent = parent->parent();
+        id = parent->m_id;
+    }
+    else
+        id = p_playlist->p_local_category->i_id;
+    if( ( id == p_playlist->p_local_category->i_id ) || ( id == p_playlist->p_local_onelevel->i_id ) )
+    {
+        for( i = 0; i < i_num; i++ )
+            playlist_Add( p_playlist, psz_list[i], NULL, ( PLAYLIST_APPEND | ( i ? 0 : PLAYLIST_GO ) |
+                                                                             ( i ? PLAYLIST_PREPARSE : 0 ) ),
+                                                         PLAYLIST_END, true, false );
+    }
+    else if ( ( id == p_playlist->p_ml_category->i_id ) || ( id == p_playlist->p_ml_onelevel->i_id ) )
+    {
+        for( i = 0; i < i_num; i++ )
+            playlist_Add( p_playlist, psz_list[i], NULL, ( PLAYLIST_APPEND | ( i ? 0 : PLAYLIST_GO ) |
+                                                                             ( i ? PLAYLIST_PREPARSE : 0 ) ),
+                                                         PLAYLIST_END, false, false );
+    }
+    else
+    {
+        int current_sd = -1;
+        for( i = 0; i < p_playlist->i_sds; i++ )
+        {
+            if( ( id == p_playlist->pp_sds[i]->p_cat->i_id || id == p_playlist->pp_sds[i]->p_one->i_id ) && p_playlist->pp_sds[i]->p_sd->pf_item_put && !p_playlist->pp_sds[i]->p_sd->b_read_only )
+            {
+                current_sd = i;
+                i = p_playlist->i_sds;
+            }
+        }
+        if( current_sd != -1 )
+        {
+            for( i = 0; i < i_num; i++ )
+            {
+                if( p_playlist->pp_sds[current_sd]->p_sd->pf_item_put( p_playlist->pp_sds[current_sd]->p_sd, psz_list[i] ) != VLC_SUCCESS )
+                    msg_Err( p_playlist, "Error adding %s", psz_list[i] );
+            }
+        }
+        else
+            msg_Err( p_playlist, "The selected service discovery does not support the add operation" );
+    }
+}
diff --git a/modules/gui/skins2/commands/cmd_playtree.hpp b/modules/gui/skins2/commands/cmd_playtree.hpp
index 3309174..fa6c5a0 100644
--- a/modules/gui/skins2/commands/cmd_playtree.hpp
+++ b/modules/gui/skins2/commands/cmd_playtree.hpp
@@ -52,4 +52,31 @@ class CmdPlaytreeDel: public CmdGeneric
 /// Command to sort the playtree
 DEFINE_COMMAND( PlaytreeSort, "playtree sort" )
 
+class CmdPlaytreeAdd: public CmdGeneric
+{
+    public:
+        CmdPlaytreeAdd( intf_thread_t *pIntf, int num, char **list ):
+            CmdGeneric( pIntf )
+        {
+            psz_list = (char **)malloc( num * sizeof( char * ) );
+            for( i_num = 0; i_num < num; i_num++ )
+                 psz_list[i_num] = strdup( list[i_num] );
+        }
+        virtual ~CmdPlaytreeAdd()
+        {
+            for( int i = 0; i < i_num; i++ )
+                free( psz_list[i] );
+            free( psz_list );
+        }
+
+        /// This method does the real job of the command
+        virtual void execute();
+
+        /// Return the type of the command
+        virtual string getType() const { return "playtree add"; }
+    private:
+        int i_num;
+        char **psz_list;
+};
+
 #endif
diff --git a/modules/gui/skins2/src/dialogs.cpp b/modules/gui/skins2/src/dialogs.cpp
index 60d961c..7df5ad3 100644
--- a/modules/gui/skins2/src/dialogs.cpp
+++ b/modules/gui/skins2/src/dialogs.cpp
@@ -90,6 +90,22 @@ void Dialogs::showPlaylistSaveCB( intf_dialog_args_t *pArg )
     }
 }
 
+void Dialogs::showPlaytreeAddCB( intf_dialog_args_t *pArg )
+{
+    intf_thread_t *pIntf = (intf_thread_t *)pArg->p_arg;
+
+    if( pArg->i_results )
+    {
+        // Create a Playtree Add command
+        CmdPlaytreeAdd *pCmd =
+            new CmdPlaytreeAdd( pIntf, pArg->i_results, pArg->psz_results );
+
+        // Push the command in the asynchronous command queue
+        AsyncQueue *pQueue = AsyncQueue::instance( pIntf );
+        pQueue->push( CmdGenericPtr( pCmd ) );
+    }
+}
+
 
 /// Callback called when the popup menu is requested
 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
@@ -262,10 +278,17 @@ void Dialogs::showFileSimple( bool play )
 
 void Dialogs::showFile( bool play )
 {
-    if( m_pProvider && m_pProvider->pf_show_dialog )
+    char *extensions;
+    if( asprintf( &extensions, "%s|%s|%s|%s|%s|%s|%s|%s|%s|%s", _("Media Files"), EXTENSIONS_MEDIA,
+                                                            _("Video Files"), EXTENSIONS_VIDEO,
+                                                            _("Audio Files"), EXTENSIONS_AUDIO,
+                                                            _("Playlist Files"), EXTENSIONS_PLAYLIST,
+                                                            _("All Files"), "*.*" ) != -1 )
     {
-        m_pProvider->pf_show_dialog( m_pProvider, INTF_DIALOG_FILE,
-                                     (int)play, 0 );
+        showFileGeneric( _("Open"),
+                         extensions,
+                         showPlaytreeAddCB, kOPEN );
+        free( extensions );
     }
 }
 
diff --git a/modules/gui/skins2/src/dialogs.hpp b/modules/gui/skins2/src/dialogs.hpp
index 94e36c8..a97e6b5 100644
--- a/modules/gui/skins2/src/dialogs.hpp
+++ b/modules/gui/skins2/src/dialogs.hpp
@@ -130,6 +130,8 @@ class Dialogs: public SkinObject
         /// Callback for the Save Playlist dialog
         static void showPlaylistSaveCB( intf_dialog_args_t *pArg );
 
+        static void showPlaytreeAddCB( intf_dialog_args_t *pArg );
+
         /// Dialogs provider module
         intf_thread_t *m_pProvider;
         module_t *m_pModule;
diff --git a/modules/gui/skins2/vars/playtree.cpp b/modules/gui/skins2/vars/playtree.cpp
index 7a79025..460dc68 100644
--- a/modules/gui/skins2/vars/playtree.cpp
+++ b/modules/gui/skins2/vars/playtree.cpp
@@ -51,6 +51,7 @@ Playtree::~Playtree()
 
 void Playtree::delSelected()
 {
+    int id;
     Iterator it = begin();
     vlc_object_lock( getIntf()->p_sys->p_playlist );
     for( it = begin(); it != end(); it = getNextVisibleItem( it ) )
@@ -69,23 +70,55 @@ void Playtree::delSelected()
     {
         if( (*it).m_deleted )
         {
-            VarTree::Iterator it2;
-            playlist_item_t *p_item = (playlist_item_t *)(it->m_pData);
-            if( p_item->i_children == -1 )
+            VarTree *parent = &(*it);
+            while( parent->parent() != root() )
+                parent = parent->parent();
+            id = parent->m_id;
+            if( ( id == getIntf()->p_sys->p_playlist->p_local_category->i_id ) ||
+                ( id == getIntf()->p_sys->p_playlist->p_local_onelevel->i_id ) ||
+                ( id == getIntf()->p_sys->p_playlist->p_ml_category->i_id ) ||
+                ( id == getIntf()->p_sys->p_playlist->p_ml_onelevel->i_id ) )
             {
-                playlist_DeleteFromInput( getIntf()->p_sys->p_playlist,
-                                          p_item->p_input->i_id, pl_Locked );
-                it2 = getNextVisibleItem( it ) ;
-                it->parent()->removeChild( it );
-                it = it2;
+                VarTree::Iterator it2;
+                playlist_item_t *p_item = (playlist_item_t *)(it->m_pData);
+                if( p_item->i_children == -1 )
+                {
+                    playlist_DeleteFromInput( getIntf()->p_sys->p_playlist,
+                                              p_item->p_input->i_id, pl_Locked );
+                    it2 = getNextVisibleItem( it ) ;
+                    it->parent()->removeChild( it );
+                    it = it2;
+                }
+                else
+                {
+                    playlist_NodeDelete( getIntf()->p_sys->p_playlist, p_item,
+                                         true, false );
+                    it2 = getNextSibling( it );
+                    it->parent()->removeChild( it );
+                    it = it2;
+                }
             }
             else
             {
-                playlist_NodeDelete( getIntf()->p_sys->p_playlist, p_item,
-                                     true, false );
-                it2 = getNextSibling( it );
-                it->parent()->removeChild( it );
-                it = it2;
+                int i;
+                int current_sd = -1;
+                for( i = 0; i < getIntf()->p_sys->p_playlist->i_sds; i++ )
+                {
+                    if( ( id == getIntf()->p_sys->p_playlist->pp_sds[i]->p_cat->i_id || id == getIntf()->p_sys->p_playlist->pp_sds[i]->p_one->i_id ) && getIntf()->p_sys->p_playlist->pp_sds[i]->p_sd->pf_item_put && !getIntf()->p_sys->p_playlist->pp_sds[i]->p_sd->b_read_only )
+                    {
+                        current_sd = i;
+                        i = getIntf()->p_sys->p_playlist->i_sds;
+                    }
+                }
+                if( current_sd != -1 )
+                {
+                    playlist_item_t *p_item = (playlist_item_t *)(it->m_pData);
+                    if( getIntf()->p_sys->p_playlist->pp_sds[current_sd]->p_sd->pf_item_delete( getIntf()->p_sys->p_playlist->pp_sds[current_sd]->p_sd, p_item->p_input->i_id ) != VLC_SUCCESS )
+                        msg_Err( getIntf()->p_sys->p_playlist, "Error deleting %s (id:%d)", p_item->p_input->psz_name, p_item->p_input->i_id );
+                }
+                else
+                    msg_Err( getIntf()->p_sys->p_playlist, "The selected service discovery does not support the delete operation" );
+                it = getNextVisibleItem( it );
             }
         }
         else
-- 
1.5.4.3

