From 981f9d4066c41c9c8e4827fb9392060886302c8b Mon Sep 17 00:00:00 2001
From: Fabio Ritrovato <exsephiroth87@gmail.com>
Date: Wed, 22 Jul 2009 00:21:25 +0200
Subject: [PATCH] [GSoC] luasd: tentative interface to pass data to sd

---
 include/vlc_interface.h                            |    1 +
 modules/gui/qt4/Modules.am                         |    3 +
 modules/gui/qt4/components/playlist/panels.hpp     |    1 +
 .../gui/qt4/components/playlist/standardpanel.cpp  |   26 +++-
 modules/gui/qt4/dialogs/services_discovery.cpp     |  149 ++++++++++++++++++++
 modules/gui/qt4/dialogs/services_discovery.hpp     |   69 +++++++++
 modules/gui/qt4/dialogs_provider.cpp               |   25 ++++
 modules/gui/qt4/dialogs_provider.hpp               |    1 +
 8 files changed, 271 insertions(+), 4 deletions(-)
 create mode 100644 modules/gui/qt4/dialogs/services_discovery.cpp
 create mode 100644 modules/gui/qt4/dialogs/services_discovery.hpp

diff --git a/include/vlc_interface.h b/include/vlc_interface.h
index 824fedf..f641924 100644
--- a/include/vlc_interface.h
+++ b/include/vlc_interface.h
@@ -149,6 +149,7 @@ typedef enum vlc_dialog {
     INTF_DIALOG_PREFS,
     INTF_DIALOG_BOOKMARKS,
     INTF_DIALOG_EXTENDED,
+    INTF_DIALOG_SD,
 
     INTF_DIALOG_POPUPMENU = 20,
     INTF_DIALOG_AUDIOPOPUPMENU,
diff --git a/modules/gui/qt4/Modules.am b/modules/gui/qt4/Modules.am
index af12d8e..9222f07 100644
--- a/modules/gui/qt4/Modules.am
+++ b/modules/gui/qt4/Modules.am
@@ -39,6 +39,7 @@ nodist_SOURCES_qt4 = \
 		dialogs/open.moc.cpp \
 		dialogs/openurl.moc.cpp \
 		dialogs/podcast_configuration.moc.cpp \
+		dialogs/services_discovery.moc.cpp \
 		dialogs/vlm.moc.cpp \
 		components/extended_panels.moc.cpp \
 		components/info_panels.moc.cpp \
@@ -215,6 +216,7 @@ SOURCES_qt4 = 	qt4.cpp \
 		dialogs/openurl.cpp \
 		dialogs/vlm.cpp \
 		dialogs/podcast_configuration.cpp \
+		dialogs/services_discovery.cpp \
 		components/extended_panels.cpp \
 		components/info_panels.cpp \
 		components/preferences_widgets.cpp \
@@ -262,6 +264,7 @@ noinst_HEADERS = \
 	dialogs/openurl.hpp \
 	dialogs/vlm.hpp \
 	dialogs/podcast_configuration.hpp \
+	dialogs/services_discovery.hpp \
 	components/extended_panels.hpp \
 	components/info_panels.hpp \
 	components/preferences_widgets.hpp \
diff --git a/modules/gui/qt4/components/playlist/panels.hpp b/modules/gui/qt4/components/playlist/panels.hpp
index 1891564..18f0e53 100644
--- a/modules/gui/qt4/components/playlist/panels.hpp
+++ b/modules/gui/qt4/components/playlist/panels.hpp
@@ -90,6 +90,7 @@ private slots:
     void setCurrentRootId( int );
     void popupAdd();
     void popupSelectColumn( QPoint );
+    void callSDAppendDialog();
 };
 
 #endif
diff --git a/modules/gui/qt4/components/playlist/standardpanel.cpp b/modules/gui/qt4/components/playlist/standardpanel.cpp
index 7b41816..a849a0a 100644
--- a/modules/gui/qt4/components/playlist/standardpanel.cpp
+++ b/modules/gui/qt4/components/playlist/standardpanel.cpp
@@ -33,6 +33,7 @@
 #include "util/customwidgets.hpp"
 
 #include <vlc_intf_strings.h>
+#include <vlc_services_discovery.h>
 
 #include <QPushButton>
 #include <QHBoxLayout>
@@ -231,7 +232,15 @@ void StandardPLPanel::setCurrentRootId( int _new )
         addButton->setToolTip( qtr(I_PL_ADDML) );
     }
     else
-        addButton->setEnabled( false );
+    {
+
+        services_discovery_t *p_sd = playlist_ServiceDiscoveryGetById( THEPL,
+                                                                currentRootId );
+        if( p_sd && p_sd->b_can_put )
+            addButton->setEnabled( true );
+        else
+            addButton->setEnabled( false );
+    }
 }
 
 /* PopupAdd Menu for the Add Menu */
@@ -254,7 +263,10 @@ void StandardPLPanel::popupAdd()
         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
         popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
     }
-
+    else
+    {
+        popup.addAction( qtr( "Add..." ), this, SLOT( callSDAppendDialog() ) );
+    }
     popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
                         + QPoint( 0, addButton->height() ) );
 }
@@ -334,11 +346,17 @@ void StandardPLPanel::deleteSelection()
     model->doDelete( list );
 }
 
+void StandardPLPanel::callSDAppendDialog()
+{
+    services_discovery_t *p_sd = playlist_ServiceDiscoveryGetById( THEPL, currentRootId );
+    intf_dialog_args_t *p_arg = new intf_dialog_args_t;
+    p_arg->p_arg = p_sd;
+    THEDP->SDAppendDialog( p_arg );
+}
+
 StandardPLPanel::~StandardPLPanel()
 {
     getSettings()->beginGroup("Playlist");
     getSettings()->setValue( "headerState", view->header()->saveState() );
     getSettings()->endGroup();
 }
-
-
diff --git a/modules/gui/qt4/dialogs/services_discovery.cpp b/modules/gui/qt4/dialogs/services_discovery.cpp
new file mode 100644
index 0000000..2f8c51a
--- /dev/null
+++ b/modules/gui/qt4/dialogs/services_discovery.cpp
@@ -0,0 +1,149 @@
+/*****************************************************************************
+ * services_discovery.cpp: Send data to a service discovery
+ *****************************************************************************
+ * Copyright © 2009 the VideoLAN team
+ *
+ * Authors: Fabio Ritrovato <exsephiroth87@gmail.com>
+ *
+ * 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include "dialogs/services_discovery.hpp"
+#include "util/customwidgets.hpp"
+
+#include <vlc_services_discovery.h>
+
+#include <QPushButton>
+#include <QDialogButtonBox>
+#include <QApplication>
+#include <QClipboard>
+#include <QMimeData>
+#include <QList>
+#include <QUrl>
+#include <QFile>
+#include <QLabel>
+#include <QComboBox>
+
+#include <assert.h>
+
+SDDialog *SDDialog::instance = NULL;
+
+SDDialog* SDDialog::getInstance( QWidget *parent,
+                                           intf_thread_t *p_intf,
+                                           services_discovery_t *p_sd )
+{
+    /* Creation */
+    if( !instance )
+        instance = new SDDialog( parent, p_intf, p_sd );
+    instance->p_sd = p_sd;
+    instance->setWindowTitle( qtr( services_discovery_GetLocalizedName( p_sd ) ) );
+    instance->parameter->clear();
+    instance->value->clear();
+    instance->edit->clear();
+    instance->custom->clear();
+    for( int i = 0; i < p_sd->i_parameters; i++ )
+        instance->parameter->addItem( qtr( p_sd->pp_parameters[i]->psz_name ), NULL );
+    return instance;
+}
+
+SDDialog::SDDialog( QWidget *parent,
+                              intf_thread_t *_p_intf,
+                              services_discovery_t *_p_sd ) :
+        QVLCDialog( parent, _p_intf ), p_sd( _p_sd )
+{
+    /* Buttons */
+    QPushButton *but;
+
+    QDialogButtonBox *box = new QDialogButtonBox( this );
+    but = box->addButton( QDialogButtonBox::Ok );
+    CONNECT( but, clicked(), this, send() );
+    but = box->addButton( QDialogButtonBox::Cancel );
+    but = box->addButton( qtr( "&Add" ), QDialogButtonBox::AcceptRole );
+    CONNECT( but, clicked(), this, add() );
+
+    CONNECT( box, rejected(), this, reject() );
+
+    /* Info label and line edit */
+    custom = new ClickLineEdit( qtr( "Enter the custom value..." ), this );
+    edit = new ClickLineEdit( qtr( "Enter parameters here..." ), this );
+
+    QLabel *info = new QLabel( qtr( "Enter what you want to add to the SD " ),
+                               this );
+
+    parameter = new QComboBox( this );
+    value = new QComboBox( this );
+    CONNECT( parameter, currentIndexChanged( int ), this, parameter_changed( int ) );
+
+    /* Layout */
+    QVBoxLayout *vlay = new QVBoxLayout( this );
+
+    vlay->addWidget( info );
+    vlay->addWidget( parameter );
+    vlay->addWidget( value );
+    vlay->addWidget( custom );
+    vlay->addWidget( edit );
+    vlay->addWidget( box );
+}
+
+void SDDialog::add()
+{
+    if( parameter->currentIndex() > -1 )
+    {   if( p_sd->pp_parameters[parameter->currentIndex()]->i_values > 0 )
+        {
+            edit->insert( qtr( p_sd->pp_parameters[parameter->currentIndex()]->pp_values[value->currentIndex()]->psz_value ).append( ' ' ) );
+        }
+        else
+        {
+            QString *string = new QString( qtr( p_sd->pp_parameters[parameter->currentIndex()]->psz_name ).toLower() );
+            string->replace( QChar( ' ' ), QChar( '_' ) );
+            edit->insert( string->append( "=\"" ).append( custom->text() ).append( "\" " ) );
+        }
+    }
+    edit->setFocus();
+}
+
+void SDDialog::send()
+{
+    parameterString = edit->text();
+    accept();
+}
+
+QString SDDialog::string() const
+{
+    return parameterString;
+}
+
+void SDDialog::parameter_changed( int index )
+{
+    if( index > -1 )
+    {   if( p_sd->pp_parameters[index]->i_values > 0 )
+        {
+            custom->setEnabled( false );
+            value->setEnabled( true );
+            value->clear();
+            for( int i = 0; i < p_sd->pp_parameters[index]->i_values; i++ )
+                value->addItem( qtr( p_sd->pp_parameters[index]->pp_values[i]->psz_name ), NULL );
+        }
+        else
+        {
+            value->setEnabled( false );
+            custom->setEnabled( true );
+        }
+    }
+}
diff --git a/modules/gui/qt4/dialogs/services_discovery.hpp b/modules/gui/qt4/dialogs/services_discovery.hpp
new file mode 100644
index 0000000..bcbc470
--- /dev/null
+++ b/modules/gui/qt4/dialogs/services_discovery.hpp
@@ -0,0 +1,69 @@
+/*****************************************************************************
+ * services_discovery.hpp: Send data to a service discovery
+ ****************************************************************************
+ * Copyright © 2009 the VideoLAN team
+ *
+ * Authors: Fabio Ritrovato <exsephiroth87@gmail.com>
+ *
+ * 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ ******************************************************************************/
+
+#ifndef QVLC_SD_H_
+#define QVLC_SD_ 1
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include "util/qvlcframe.hpp"
+
+class ClickLineEdit;
+class QComboBox;
+
+class SDDialog : public QVLCDialog
+{
+    Q_OBJECT
+
+private:
+    SDDialog( QWidget *, intf_thread_t *, services_discovery_t * );
+    QString parameterString;
+    services_discovery_t *p_sd;
+    ClickLineEdit *edit;
+    QComboBox *parameter;
+    QComboBox *value;
+    ClickLineEdit *custom;
+
+    static SDDialog *instance;
+
+private slots:
+    void add();
+    void send();
+    void parameter_changed( int );
+
+public:
+    virtual ~SDDialog() {}
+
+    static SDDialog* getInstance( QWidget *parent,
+                                  intf_thread_t *p_intf,
+                                  services_discovery_t *p_sd );
+
+    QString string() const;
+
+public slots:
+    virtual void close() { send(); };
+};
+
+#endif
diff --git a/modules/gui/qt4/dialogs_provider.cpp b/modules/gui/qt4/dialogs_provider.cpp
index 2641b8b..0affc70 100644
--- a/modules/gui/qt4/dialogs_provider.cpp
+++ b/modules/gui/qt4/dialogs_provider.cpp
@@ -27,6 +27,7 @@
 #endif
 
 #include <vlc_intf_strings.h>
+#include <vlc_services_discovery.h>
 
 #include "qt4.hpp"
 #include "dialogs_provider.hpp"
@@ -53,6 +54,7 @@
 #include "dialogs/toolbar.hpp"
 #include "dialogs/plugins.hpp"
 #include "dialogs/external.hpp"
+#include "dialogs/services_discovery.hpp"
 
 #include <QEvent>
 #include <QApplication>
@@ -152,6 +154,8 @@ void DialogsProvider::customEvent( QEvent *event )
         case INTF_DIALOG_WIZARD:
         case INTF_DIALOG_STREAMWIZARD:
             openAndStreamingDialogs(); break;
+        case INTF_DIALOG_SD:
+            SDAppendDialog( de->p_arg ); break;
 #ifdef UPDATE_CHECK
         case INTF_DIALOG_UPDATEVLC:
             updateDialog(); break;
@@ -370,6 +374,27 @@ void DialogsProvider::MLAppendDialog( int tab )
                                     ->showTab( tab );
 }
 
+void DialogsProvider::SDAppendDialog( intf_dialog_args_t *p_arg )
+{
+    services_discovery_t *p_sd = (services_discovery_t *)p_arg->p_arg;
+    SDDialog *sdd = SDDialog::getInstance( p_intf->p_sys->p_mi, p_intf, p_sd );
+    if( sdd->exec() == QDialog::Accepted )
+    {
+        QString string = sdd->string();
+        if( !string.isEmpty() )
+        {
+            vlc_event_t event;
+            event.type = vlc_ServicesDiscoveryItemPut;
+            if( !( event.u.services_discovery_item_put.psz_string =
+                                                       strdup( qtu( string ) ) ) )
+                return;
+            vlc_event_send( &p_sd->event_manager, &event );
+            free( event.u.services_discovery_item_put.psz_string );
+        }
+    }
+    free( p_arg );
+}
+
 /**
  * Simple open
  ***/
diff --git a/modules/gui/qt4/dialogs_provider.hpp b/modules/gui/qt4/dialogs_provider.hpp
index d65de88..9a0c2e9 100644
--- a/modules/gui/qt4/dialogs_provider.hpp
+++ b/modules/gui/qt4/dialogs_provider.hpp
@@ -174,6 +174,7 @@ public slots:
 
     void PLAppendDialog( int tab = OPEN_FILE_TAB );
     void MLAppendDialog( int tab = OPEN_FILE_TAB );
+    void SDAppendDialog( intf_dialog_args_t * );
 
     void PLOpenDir();
     void PLAppendDir();
-- 
1.6.0.4

