From 7791a27e900bac1051fadcab1b0d737617c11223 Mon Sep 17 00:00:00 2001
From: Fabio Ritrovato <exsephiroth87@gmail.com>
Date: Wed, 3 Sep 2008 01:53:29 +0200
Subject: [PATCH] Qt4: add support for the new SDs

---
 .../gui/qt4/components/playlist/playlist_model.cpp |  112 ++++++++++++++++++--
 .../gui/qt4/components/playlist/standardpanel.cpp  |   30 +++++
 modules/gui/qt4/dialogs_provider.cpp               |   63 +++++++++++
 modules/gui/qt4/dialogs_provider.hpp               |    2 +
 4 files changed, 200 insertions(+), 7 deletions(-)

diff --git a/modules/gui/qt4/components/playlist/playlist_model.cpp b/modules/gui/qt4/components/playlist/playlist_model.cpp
index f09a4a7..e11637c 100644
--- a/modules/gui/qt4/components/playlist/playlist_model.cpp
+++ b/modules/gui/qt4/components/playlist/playlist_model.cpp
@@ -39,6 +39,7 @@
 #include <QMenu>
 #include <QApplication>
 #include <QSettings>
+#include <QFileDialog>
 
 #include "sorting.h"
 
@@ -159,9 +160,24 @@ bool PLModel::dropMimeData( const QMimeData *data, Qt::DropAction action,
         QDataStream stream( &encodedData, QIODevice::ReadOnly );
 
         PLItem *newParentItem;
+        int current_sd = -1;
+        int i;
+        for( i = 0; i < THEPL->i_sds; i++ )
+        {
+            if( targetItem->i_id == THEPL->pp_sds[i]->p_cat->i_id || targetItem->i_id == THEPL->pp_sds[i]->p_one->i_id )
+            {
+                if( THEPL->pp_sds[i]->p_sd->pf_item_put && !THEPL->pp_sds[i]->p_sd->b_read_only )
+                {
+                    current_sd = i;
+                    i = THEPL->i_sds;
+                }
+                else
+                    return false;
+            }
+        }
+
         while( !stream.atEnd() )
         {
-            int i;
             int srcId;
             stream >> srcId;
 
@@ -198,9 +214,37 @@ bool PLModel::dropMimeData( const QMimeData *data, Qt::DropAction action,
             else
             {
                 /* \todo: if we drop on a top-level node, use copy instead ? */
-                playlist_TreeMove( p_playlist, p_src, p_target, 0 );
-                i = 0;
-                newParentItem = targetItem;
+                if( current_sd == -1 )
+                {
+                    playlist_TreeMove( p_playlist, p_src, p_target, 0 );
+                    i = 0;
+                    newParentItem = targetItem;
+                }
+                else
+                {
+                    if( THEPL->pp_sds[current_sd]->p_sd->pf_item_put( THEPL->pp_sds[current_sd]->p_sd, p_src->p_input->psz_uri ) != VLC_SUCCESS )
+                        msg_Err( THEPL, "Error adding %s", p_src->p_input->psz_uri );
+                }
+            }
+
+            if( current_sd == -1 )
+            {
+                /* Remove from source */
+                PLItem *srcItem = FindById( rootItem, p_src->i_id );
+                // We dropped on the source selector. Ask the dialog to forward
+                // to the main view
+                if( !srcItem )
+                {
+                    emit shouldRemove( p_src->i_id );
+                }
+                else
+                    srcItem->remove( srcItem );
+                /* Display at new destination */
+                PLItem *newItem = new PLItem( p_src, newParentItem, this );
+                newParentItem->insertChild( newItem, i, true );
+                UpdateTreeItem( p_src, newItem, true );
+                if( p_src->i_children != -1 )
+                    UpdateNodeChildren( newItem );
             }
             PL_UNLOCK;
         }
@@ -711,6 +755,8 @@ void PLModel::doDeleteItem( PLItem *item, QModelIndexList *fullList )
 {
     QModelIndex deleteIndex = index( item, 0 );
     fullList->removeAll( deleteIndex );
+    int i;
+    int current_sd = -1;
 
     PL_LOCK;
     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id,
@@ -719,6 +765,22 @@ void PLModel::doDeleteItem( PLItem *item, QModelIndexList *fullList )
     {
         PL_UNLOCK; return;
     }
+    for( i = 0; i < THEPL->i_sds; i++ )
+    {
+        if( ( p_item->p_parent->i_id == THEPL->pp_sds[i]->p_cat->i_id || p_item->p_parent->i_id == THEPL->pp_sds[i]->p_one->i_id ) &&
+            THEPL->pp_sds[i]->p_sd->pf_item_delete && !THEPL->pp_sds[i]->p_sd->b_read_only )
+        {
+            current_sd = i;
+            i = THEPL->i_sds;
+        }
+    }
+    if( current_sd != -1 )
+    {
+        if( THEPL->pp_sds[current_sd]->p_sd->pf_item_delete( THEPL->pp_sds[current_sd]->p_sd, p_item->p_input->i_id ) != VLC_SUCCESS )
+            msg_Err( p_playlist, "Error deleting: %s (id:%d)", p_item->p_input->psz_name, p_item->p_input->i_id );
+        PL_UNLOCK;
+        return;
+    }
     if( p_item->i_children == -1 )
         playlist_DeleteFromInput( p_playlist, item->i_input_id, pl_Locked );
     else
@@ -913,9 +975,45 @@ void PLModel::popupStream()
 
 void PLModel::popupSave()
 {
-    QStringList mrls = selectedURIs();
-    if( !mrls.isEmpty() )
-        THEDP->streamingDialog( NULL, mrls[0], true );
+    int i;
+    int current_sd = -1;
+    for( i = 0; i < THEPL->i_sds; i++ )
+    {
+        if( ( i_popup_parent == THEPL->pp_sds[i]->p_cat->i_id || i_popup_parent == THEPL->pp_sds[i]->p_one->i_id ) &&
+            THEPL->pp_sds[i]->p_sd->pf_item_get && !THEPL->pp_sds[i]->p_sd->b_read_only )
+        {
+            current_sd = i;
+            i = THEPL->i_sds;
+        }
+    }
+    if( current_sd != -1 )
+    {
+        QString dir = QFileDialog::getExistingDirectory( 0, qtr(I_OP_OPDIR) );
+        if( !dir.isEmpty() )
+        {
+            playlist_item_t *p_item;
+            for( i = current_selection.size() -1; i >= 0; i-- )
+            {
+                QModelIndex index = current_selection[i];
+                if( index.column() != 0 ) continue;
+                PLItem *item = static_cast<PLItem*>(index.internalPointer());
+                assert( item );
+                if( ( p_item = playlist_ItemGetById( p_playlist, item->i_id, false ) ) != NULL )
+                {
+                    if( THEPL->pp_sds[current_sd]->p_sd->pf_item_get( THEPL->pp_sds[current_sd]->p_sd, qtu( dir ), p_item->p_input->i_id ) != VLC_SUCCESS )
+                        msg_Err( p_playlist, "Error saving: %s (id:%d)", p_item->p_input->psz_name, p_item->p_input->i_id );
+                }
+                else
+                    msg_Err( p_playlist, "Some error occourred with item %d", item->i_id );
+            }
+        }
+    }
+    else
+    {
+        QStringList mrls = selectedURIs();
+        if( !mrls.isEmpty() )
+            THEDP->streamingDialog( NULL, mrls[0], true );
+    }
 }
 
 #include <QUrl>
diff --git a/modules/gui/qt4/components/playlist/standardpanel.cpp b/modules/gui/qt4/components/playlist/standardpanel.cpp
index de809f2..3d88ed7 100644
--- a/modules/gui/qt4/components/playlist/standardpanel.cpp
+++ b/modules/gui/qt4/components/playlist/standardpanel.cpp
@@ -227,6 +227,8 @@ void StandardPLPanel::handleExpansion( const QModelIndex &index )
 
 void StandardPLPanel::setCurrentRootId( int _new )
 {
+    int i;
+
     currentRootId = _new;
     if( currentRootId == THEPL->p_local_category->i_id ||
         currentRootId == THEPL->p_local_onelevel->i_id  )
@@ -243,13 +245,27 @@ void StandardPLPanel::setCurrentRootId( int _new )
         addButton->setToolTip( qtr(I_PL_ADDML) );
     }
     else
+    {
         addButton->setEnabled( false );
+        for( i = 0; i < THEPL->i_sds; i++ )
+        {
+            if( ( currentRootId == THEPL->pp_sds[i]->p_cat->i_id ||
+                        currentRootId == THEPL->pp_sds[i]->p_one->i_id ) &&
+                THEPL->pp_sds[i]->p_sd->pf_item_put && !THEPL->pp_sds[i]->p_sd->b_read_only )
+            {
+                addButton->setEnabled( true );
+                /* TODO addButton->setToolTip( qtr(I_PL_ADDSD) ); */
+                i = THEPL->i_sds;
+            }
+        }
+    }
 }
 
 /* PopupAdd Menu for the Add Menu */
 void StandardPLPanel::popupAdd()
 {
     QMenu popup;
+    int i;
     if( currentRootId == THEPL->p_local_category->i_id ||
         currentRootId == THEPL->p_local_onelevel->i_id )
     {
@@ -264,6 +280,20 @@ void StandardPLPanel::popupAdd()
         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( MLAppendDialog() ) );
         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
     }
+    else
+    {
+        for( i = 0; i < THEPL->i_sds; i++ )
+        {
+            if( ( currentRootId == THEPL->pp_sds[i]->p_cat->i_id ||
+                        currentRootId == THEPL->pp_sds[i]->p_one->i_id ) &&
+                THEPL->pp_sds[i]->p_sd->pf_item_put && !THEPL->pp_sds[i]->p_sd->b_read_only )
+            {
+                popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simpleSDAppendDialog() ) );
+                popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( SDAppendDir() ) );
+                i = THEPL->i_sds;
+            }
+        }
+    }
     popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
                         + QPoint( 0, addButton->height() ) );
 }
diff --git a/modules/gui/qt4/dialogs_provider.cpp b/modules/gui/qt4/dialogs_provider.cpp
index fe37d85..a2055ad 100644
--- a/modules/gui/qt4/dialogs_provider.cpp
+++ b/modules/gui/qt4/dialogs_provider.cpp
@@ -406,6 +406,34 @@ void DialogsProvider::simpleMLAppendDialog()
     addFromSimple( false, false );
 }
 
+void DialogsProvider::simpleSDAppendDialog()
+{
+    int i;
+    int current_sd = -1;
+    int id = PlaylistDialog::getInstance( p_intf )->getRootId();
+    for( i = 0; i < THEPL->i_sds; i++ )
+    {
+        if( ( id == THEPL->pp_sds[i]->p_cat->i_id || id == THEPL->pp_sds[i]->p_one->i_id ) &&
+            THEPL->pp_sds[i]->p_sd->pf_item_put && !THEPL->pp_sds[i]->p_sd->b_read_only )
+        {
+            current_sd = i;
+            i = THEPL->i_sds;
+        }
+    }
+    if( current_sd != -1 )
+    {
+        QStringList files = DialogsProvider::showSimpleOpen( NULL, EXT_FILTER_MEDIA );
+        foreach( QString file, files )
+        {
+            const char * psz_utf8 = qtu( file );
+            if( THEPL->pp_sds[current_sd]->p_sd->pf_item_put( THEPL->pp_sds[current_sd]->p_sd, psz_utf8 ) != VLC_SUCCESS )
+                msg_Err( THEPL, "Error adding %s", psz_utf8 );
+        }
+    }
+    else
+        msg_Err( THEPL, "The selected service discovery does not support the add operation" );
+}
+
 /* Directory */
 /**
  * Open a directory,
@@ -445,6 +473,41 @@ void DialogsProvider::MLAppendDir()
     openDirectory( p_intf, false , false );
 }
 
+void DialogsProvider::SDAppendDir()
+{
+    int i;
+    int current_sd = -1;
+    int id = PlaylistDialog::getInstance( p_intf )->getRootId();
+    for( i = 0; i < THEPL->i_sds; i++ )
+    {
+        if( ( id == THEPL->pp_sds[i]->p_cat->i_id || id == THEPL->pp_sds[i]->p_one->i_id ) &&
+            THEPL->pp_sds[i]->p_sd->pf_item_put && !THEPL->pp_sds[i]->p_sd->b_read_only )
+        {
+            current_sd = i;
+            i = THEPL->i_sds;
+        }
+    }
+    if( current_sd != -1 )
+    {
+        QString dir_path = QFileDialog::getExistingDirectory( 0, qtr(I_OP_OPDIR) );
+        if (!dir_path.isEmpty())
+        {
+            QDir dir( dir_path );
+            dir.setFilter( QDir::Files | QDir::NoSymLinks );
+            QFileInfoList list = dir.entryInfoList();
+            for( i = 0; i < list.size(); i++ )
+            {
+                QFileInfo fileInfo = list.at(i);
+                const char * psz_utf8 = qtu( fileInfo.absoluteFilePath() );
+                if( THEPL->pp_sds[current_sd]->p_sd->pf_item_put( THEPL->pp_sds[current_sd]->p_sd, psz_utf8 ) != VLC_SUCCESS )
+                    msg_Err( THEPL, "Error adding %s", psz_utf8 );
+            }
+        }
+    }
+    else
+        msg_Err( THEPL, "The selected service discovery does not support the add operation" );
+}
+
 /****************
  * Playlist     *
  ****************/
diff --git a/modules/gui/qt4/dialogs_provider.hpp b/modules/gui/qt4/dialogs_provider.hpp
index d69e9f5..a32e5eb 100644
--- a/modules/gui/qt4/dialogs_provider.hpp
+++ b/modules/gui/qt4/dialogs_provider.hpp
@@ -157,6 +157,7 @@ public slots:
     void simpleOpenDialog();
     void simplePLAppendDialog();
     void simpleMLAppendDialog();
+    void simpleSDAppendDialog();
 
     void openDialog();
     void openDialog( int );
@@ -171,6 +172,7 @@ public slots:
     void PLOpenDir();
     void PLAppendDir();
     void MLAppendDir();
+    void SDAppendDir();
 
     void streamingDialog( QWidget *parent, QString mrl = "",
             bool b_stream = true );
-- 
1.5.4.3

