]> git.sesse.net Git - vlc/blobdiff - modules/gui/qt4/dialogs/vlm.cpp
Rework a bit the OpenDialog calls in order to fix the double-click issue when transco...
[vlc] / modules / gui / qt4 / dialogs / vlm.cpp
index 4f892793f9819c91e4ab676c5d74397e096f1820..12ad9bf4e4411750dd8266dee0557cd307bea3e4 100644 (file)
@@ -1,11 +1,12 @@
 /*****************************************************************************
  * vlm.cpp : VLM Management
  ****************************************************************************
- * Copyright ( C ) 2006 the VideoLAN team
- * $Id: sout.cpp 21875 2007-09-08 16:01:33Z jb $
+ * Copyright © 2008 the VideoLAN team
+ * $Id$
  *
  * Authors: Jean-Baptiste Kempf <jb@videolan.org>
  *          Jean-François Massol <jf.massol -at- gmail.com>
+ *          Clément Sténac <zorglub@videolan.org>
  *
  * 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
  * Foundation, Inc., 51 Franklin street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
 #include "dialogs/vlm.hpp"
 
+#ifdef ENABLE_VLM
+#include "dialogs/open.hpp"
+#include "dialogs/sout.hpp"
+
 #include <QString>
 #include <QComboBox>
 #include <QVBoxLayout>
 #include <QPushButton>
 #include <QHBoxLayout>
 #include <QDateTimeEdit>
+#include <QDateTime>
 #include <QSpinBox>
 #include <QHeaderView>
 #include <QScrollArea>
+#include <QFileDialog>
 
 static const char *psz_type[] = { "Broadcast", "Schedule", "VOD" };
 
 VLMDialog *VLMDialog::instance = NULL;
 
-VLMDialog::VLMDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
-{   
+VLMDialog::VLMDialog( QWidget *parent, intf_thread_t *_p_intf ) : QVLCDialog( parent, _p_intf )
+{
     p_vlm = vlm_New( p_intf );
 
     if( !p_vlm )
     {
         msg_Warn( p_intf, "Couldn't build VLM object ");
-        return;   
+        return;
     }
     vlmWrapper = new VLMWrapper( p_vlm );
 
@@ -69,9 +80,9 @@ VLMDialog::VLMDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
 
     /* Schedule Stuffs */
     QGridLayout *schetimelayout = new QGridLayout( ui.schedBox );
-    QLabel *schetimelabel = new QLabel( qtr( "Hours/Minutes/Seconds:" ) );
+    QLabel *schetimelabel = new QLabel( qtr( "Hours / Minutes / Seconds:" ) );
     schetimelayout->addWidget( schetimelabel, 0, 0 );
-    QLabel *schedatelabel = new QLabel( qtr( "Day Month Year:" ) );
+    QLabel *schedatelabel = new QLabel( qtr( "Day / Month / Year:" ) );
     schetimelayout->addWidget( schedatelabel, 1, 0 );
     QLabel *scherepeatLabel = new QLabel( qtr( "Repeat:" ) );
     schetimelayout->addWidget( scherepeatLabel, 2, 0 );
@@ -80,6 +91,7 @@ VLMDialog::VLMDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
 
     time = new QDateTimeEdit( QTime::currentTime() );
     time->setAlignment( Qt::AlignRight );
+    time->setDisplayFormat( "hh:mm:ss" );
     schetimelayout->addWidget( time, 0, 1, 1, 3 );
 
     date = new QDateTimeEdit( QDate::currentDate() );
@@ -118,9 +130,16 @@ VLMDialog::VLMDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
         new QSpacerItem( 10, 10, QSizePolicy::Minimum, QSizePolicy::Expanding);
     vlmItemLayout->addItem( spacer );
 
-    QPushButton *closeButton = new QPushButton( qtr( "Close" ) );
+    QPushButton *importButton = new QPushButton( qtr( "Import" ) );
+    ui.buttonBox->addButton( importButton, QDialogButtonBox::ActionRole );
+
+    QPushButton *exportButton = new QPushButton( qtr( "Export" ) );
+    ui.buttonBox->addButton( exportButton, QDialogButtonBox::ActionRole );
+
+    QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
     ui.buttonBox->addButton( closeButton, QDialogButtonBox::AcceptRole );
 
+
     showScheduleWidget( QVLM_Broadcast );
 
     /* Connect the comboBox to show the right Widgets */
@@ -132,17 +151,25 @@ VLMDialog::VLMDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
              this, selectVLMItem( int ) );
 
     BUTTONACT( closeButton, close() );
+    BUTTONACT( exportButton, exportVLMConf() );
+    BUTTONACT( importButton, importVLMConf() );
     BUTTONACT( ui.addButton, addVLMItem() );
     BUTTONACT( ui.clearButton, clearWidgets() );
     BUTTONACT( ui.saveButton, saveModifications() );
+    BUTTONACT( ui.inputButton, selectInput() );
+    BUTTONACT( ui.outputButton, selectOutput() );
 }
 
 VLMDialog::~VLMDialog()
 {
+    delete vlmWrapper;
+
    /* FIXME :you have to destroy vlm here to close
     * but we shouldn't destroy vlm here in case somebody else wants it */
     if( p_vlm )
+    {
         vlm_Delete( p_vlm );
+    }
 }
 
 void VLMDialog::showScheduleWidget( int i )
@@ -154,7 +181,8 @@ void VLMDialog::showScheduleWidget( int i )
 
 void VLMDialog::selectVLMItem( int i )
 {
-    ui.vlmItemScroll->ensureWidgetVisible( vlmItems.at( i ) );
+    if( i >= 0 )
+        ui.vlmItemScroll->ensureWidgetVisible( vlmItems.at( i ) );
 }
 
 bool VLMDialog::isNameGenuine( QString name )
@@ -175,7 +203,7 @@ void VLMDialog::addVLMItem()
     QString name = ui.nameLedit->text();
     if( name.isEmpty() || !isNameGenuine( name ) )
     {
-        msg_Dbg( p_intf, "VLM Name is empty or already exists, I can't do it" );
+        msg_Err( p_intf, "VLM Name is empty or already exists, I can't do it" );
         return;
     }
 
@@ -186,7 +214,10 @@ void VLMDialog::addVLMItem()
     QString outputText = ui.outputLedit->text();
     bool b_checked = ui.enableCheck->isChecked();
     bool b_looped = ui.loopBCast->isChecked();
-
+    QDateTime schetime = time->dateTime();
+    QDateTime schedate = date->dateTime();
+    int repeatnum = scherepeatnumber->value();
+    int repeatdays = repeatDays->value();
     VLMAWidget * vlmAwidget;
 
     switch( type )
@@ -194,19 +225,23 @@ void VLMDialog::addVLMItem()
     case QVLM_Broadcast:
         typeShortName = "Bcast";
         vlmAwidget = new VLMBroadcast( name, inputText, outputText,
-                                  b_checked, b_looped, this );
-        //VLMWrapper::AddBroadcast( vlmWrapper->p_vlc, name, inputText, outputText, b_checked, b_looped ); 
+                                       b_checked, b_looped, this );
+        VLMWrapper::AddBroadcast( name, inputText, outputText, b_checked,
+                                  b_looped );
     break;
     case QVLM_VOD:
         typeShortName = "VOD";
         vlmAwidget = new VLMVod( name, inputText, outputText,
                                  b_checked, ui.muxLedit->text(), this );
-        ///VLMWrapper::AddVod( vlmWrapper->GetVLM(), name, inputText, outputText, b_checked );
+        VLMWrapper::AddVod( name, inputText, outputText, b_checked );
         break;
     case QVLM_Schedule:
         typeShortName = "Sched";
         vlmAwidget = new VLMSchedule( name, inputText, outputText,
-                                      b_checked, this );
+                                      schetime, schedate, repeatnum,
+                                      repeatdays, b_checked, this );
+        VLMWrapper::AddSchedule( name, inputText, outputText, schetime,
+                                 schedate, repeatnum, repeatdays, b_checked);
         break;
     default:
         msg_Warn( p_intf, "Something bad happened" );
@@ -221,8 +256,106 @@ void VLMDialog::addVLMItem()
 
     vlmItemLayout->insertWidget( vlmItemCount, vlmAwidget );
     vlmItems.append( vlmAwidget );
+    clearWidgets();
+}
 
-    /* HERE BE DRAGONS VLM REQUEST */
+// FIXME : VOD are not exported to the file
+bool VLMDialog::exportVLMConf()
+{
+    QString saveVLMConfFileName = QFileDialog::getSaveFileName(
+            this, qtr( "Choose a filename to save the VLM configuration..." ),
+            qfu( config_GetHomeDir() ),
+            qtr( "VLM conf (*.vlm) ;; All (*.*)" ) );
+
+    if( !saveVLMConfFileName.isEmpty() )
+    {
+        vlm_message_t *message;
+        QString command = "save \"" + saveVLMConfFileName + "\"";
+        vlm_ExecuteCommand( p_vlm , qtu( command ) , &message );
+        vlm_MessageDelete( message );
+        return true;
+    }
+    return false;
+}
+
+void VLMDialog::mediasPopulator()
+{
+    if( p_vlm )
+    {
+        int i_nMedias;
+        QString typeShortName;
+        int vlmItemCount;
+        vlm_media_t ***ppp_dsc = (vlm_media_t ***)malloc( sizeof( vlm_media_t ) );
+
+        /* Get medias informations and numbers */
+        vlm_Control( p_vlm, VLM_GET_MEDIAS, ppp_dsc, &i_nMedias );
+
+        /* Loop on all of them */
+        for( int i = 0; i < i_nMedias; i++ )
+        {
+            VLMAWidget * vlmAwidget;
+            vlmItemCount = vlmItems.size();
+
+            QString mediaName = qfu( (*ppp_dsc)[i]->psz_name );
+            /* It may have several inputs, we take the first one by default
+                 - an evolution will be to manage these inputs in the gui */
+            QString inputText = qfu( (*ppp_dsc)[i]->ppsz_input[0] );
+
+            QString outputText = qfu( (*ppp_dsc)[i]->psz_output );
+
+            /* Schedule media is a quite especial, maybe there is another way to grab informations */
+            if( (*ppp_dsc)[i]->b_vod )
+            {
+                typeShortName = "VOD";
+                QString mux = qfu( (*ppp_dsc)[i]->vod.psz_mux );
+                vlmAwidget = new VLMVod( mediaName, inputText, outputText,
+                                    (*ppp_dsc)[i]->b_enabled, mux, this );
+            }
+            else
+            {
+                typeShortName = "Bcast";
+                vlmAwidget = new VLMBroadcast( mediaName, inputText, outputText,
+                                  (*ppp_dsc)[i]->b_enabled, (*ppp_dsc)[i]->broadcast.b_loop, this );
+            }
+            /* Add an Item of the Side List */
+            ui.vlmListItem->addItem( typeShortName + " : " + mediaName );
+            ui.vlmListItem->setCurrentRow( vlmItemCount - 1 );
+
+            /* Add a new VLMAWidget on the main List */
+            vlmItemLayout->insertWidget( vlmItemCount, vlmAwidget );
+            vlmItems.append( vlmAwidget );
+            clearWidgets();
+        }
+        free( ppp_dsc );
+    }
+}
+
+bool VLMDialog::importVLMConf()
+{
+    QString openVLMConfFileName = QFileDialog::getOpenFileName(
+            this, qtr( "Open a VLM Configuration File" ),
+            qfu( config_GetHomeDir() ),
+            qtr( "VLM conf (*.vlm) ;; All (*.*)" ) );
+
+    if( !openVLMConfFileName.isEmpty() )
+    {
+        vlm_message_t *message;
+        int status;
+        QString command = "load \"" + openVLMConfFileName + "\"";
+        status = vlm_ExecuteCommand( p_vlm, qtu( command ) , &message );
+        vlm_MessageDelete( message );
+        if( status == 0 )
+        {
+            mediasPopulator();
+        }
+        else
+        {
+            msg_Warn( p_intf, "Failed to import vlm configuration file : %s", qtu( command ) );
+            return false;
+        }
+        return true;
+    }
+    return false;
 }
 
 void VLMDialog::clearWidgets()
@@ -240,13 +373,25 @@ void VLMDialog::clearWidgets()
     ui.addButton->show();
 }
 
+void VLMDialog::selectInput()
+{
+    OpenDialog *o = OpenDialog::getInstance( this, p_intf, false, SELECT, true );
+    o->exec();
+    ui.inputLedit->setText( o->getMRL() );
+}
+
+void VLMDialog::selectOutput()
+{
+    SoutDialog *s = SoutDialog::getInstance( this, p_intf, false );
+    if( s->exec() == QDialog::Accepted )
+        ui.outputLedit->setText( s->getMrl() );
+}
+
 /* Object Modification */
 void VLMDialog::removeVLMItem( VLMAWidget *vlmObj )
 {
     int index = vlmItems.indexOf( vlmObj );
     if( index < 0 ) return;
-
-    //FIXME, this is going to segfault if the focus in on the ListWidget
     delete ui.vlmListItem->takeItem( index );
     vlmItems.removeAt( index );
     delete vlmObj;
@@ -259,7 +404,6 @@ void VLMDialog::startModifyVLMItem( VLMAWidget *vlmObj )
     currentIndex = vlmItems.indexOf( vlmObj );
     if( currentIndex < 0 ) return;
 
-    msg_Dbg( p_intf, "Type: %i", vlmObj->type );
     ui.vlmListItem->setCurrentRow( currentIndex );
     ui.nameLedit->setText( vlmObj->name );
     ui.inputLedit->setText( vlmObj->input );
@@ -275,7 +419,8 @@ void VLMDialog::startModifyVLMItem( VLMAWidget *vlmObj )
         ui.muxLedit->setText( (qobject_cast<VLMVod *>(vlmObj))->mux );
         break;
     case QVLM_Schedule:
-        //(qobject_cast<VLMSchedule *>)
+        time->setDateTime( ( qobject_cast<VLMSchedule *>(vlmObj))->schetime );
+        date->setDateTime( ( qobject_cast<VLMSchedule *>(vlmObj))->schedate );
         break;
     }
 
@@ -302,6 +447,10 @@ void VLMDialog::saveModifications()
             (qobject_cast<VLMVod *>(vlmObj))->mux = ui.muxLedit->text();
             break;
         case QVLM_Schedule:
+            (qobject_cast<VLMSchedule *>(vlmObj))->schetime = time->dateTime();
+            (qobject_cast<VLMSchedule *>(vlmObj))->schedate = date->dateTime();
+            (qobject_cast<VLMSchedule *>(vlmObj))->rNumber = scherepeatnumber->value();
+            (qobject_cast<VLMSchedule *>(vlmObj))->rDays = repeatDays->value();
             break;
            //           vlmObj->
         }
@@ -352,6 +501,7 @@ VLMAWidget::VLMAWidget( QString _name,
 
     BUTTONACT( modifyButton, modify() );
     BUTTONACT( deleteButton, del() );
+    CONNECT( this, clicked( bool ), this, toggleEnabled( bool ) );
 }
 
 void VLMAWidget::modify()
@@ -370,8 +520,13 @@ void VLMAWidget::enterEvent( QEvent *event )
     printf( "test" );
 }
 
+void VLMAWidget::toggleEnabled( bool b_enable )
+{
+    VLMWrapper::EnableItem( name, b_enable );
+}
+
 /****************
- * VLMBroadcast 
+ * VLMBroadcast
  ****************/
 VLMBroadcast::VLMBroadcast( QString _name, QString _input, QString _output,
                             bool _enabled, bool _looped, VLMDialog *_parent)
@@ -397,13 +552,13 @@ VLMBroadcast::VLMBroadcast( QString _name, QString _input, QString _output,
     BUTTONACT( playButton, togglePlayPause() );
     BUTTONACT( stopButton, stop() );
     BUTTONACT( loopButton, toggleLoop() );
-    
+
     update();
 }
 
 void VLMBroadcast::update()
 {
-    //VLMWrapper::EditBroadcast( VLMWrapper::p_vlm, name, input, output, b_enabled, b_looped );
+    VLMWrapper::EditBroadcast( name, input, output, b_enabled, b_looped );
     if( b_looped )
         loopButton->setIcon( QIcon( QPixmap( ":/pixmaps/playlist_repeat_all.png" ) ) );
     else
@@ -414,12 +569,12 @@ void VLMBroadcast::togglePlayPause()
 {
     if( b_playing = true )
     {
-      //  VLMWrapper::ControlBroadcast( VLMWrapper::p_vlm, name, ControlBroadcastPause );
+        VLMWrapper::ControlBroadcast( name, ControlBroadcastPause );
         playButton->setIcon( QIcon( QPixmap( ":/pixmaps/pause_16px.png" ) ) );
     }
     else
     {
-        //VLMWrapper::ControlBroadcast( VLMWrapper::p_vlm, name, ControlBroadcastPlay );
+        VLMWrapper::ControlBroadcast( name, ControlBroadcastPlay );
         playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
     }
     b_playing = !b_playing;
@@ -433,7 +588,7 @@ void VLMBroadcast::toggleLoop()
 
 void VLMBroadcast::stop()
 {
-    //VLMWrapper::ControlBroadcast( VLMWrapper::p_vlm, name, ControlBroadcastStop );
+    VLMWrapper::ControlBroadcast( name, ControlBroadcastStop );
     playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
 }
 
@@ -441,14 +596,24 @@ void VLMBroadcast::stop()
  * VLMSchedule
  ****************/
 VLMSchedule::VLMSchedule( QString name, QString input, QString output,
-                            bool enabled, VLMDialog *parent) 
+                          QDateTime _schetime, QDateTime _schedate,
+                          int _scherepeatnumber, int _repeatDays,
+                          bool enabled, VLMDialog *parent )
             : VLMAWidget( name, input, output, enabled, parent, QVLM_Schedule )
 {
     nameLabel->setText( "Schedule: " + name );
+    schetime = _schetime;
+    schedate = _schedate;
+    rNumber = _scherepeatnumber;
+    rDays = _repeatDays;
+    type = QVLM_Schedule;
+    update();
 }
 
 void VLMSchedule::update()
 {
+   VLMWrapper::EditSchedule( name, input, output, schetime, schedate,
+                             rNumber, rDays, b_enabled);
 }
 
 /****************
@@ -463,14 +628,14 @@ VLMVod::VLMVod( QString name, QString input, QString output,
     mux = _mux;
     muxLabel = new QLabel;
     objLayout->addWidget( muxLabel, 1, 0 );
-    
+
     update();
 }
 
 void VLMVod::update()
 {
     muxLabel->setText( mux );
-    //VLMWrapper::EditVod( p_vlm, name, input, output, b_enabled, mux );
+    VLMWrapper::EditVod( name, input, output, b_enabled, mux );
 }
 
 
@@ -485,7 +650,9 @@ VLMWrapper::VLMWrapper( vlm_t *_p_vlm )
 }
 
 VLMWrapper::~VLMWrapper()
-{}
+{
+    p_vlm = NULL;
+}
 
 void VLMWrapper::AddBroadcast( const QString name, QString input,
                                QString output,
@@ -504,7 +671,7 @@ void VLMWrapper::EditBroadcast( const QString name, const QString input,
 {
     vlm_message_t *message;
     QString command;
-    
+
     command = "setup \"" + name + "\" inputdel all";
     vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
     vlm_MessageDelete( message );
@@ -531,12 +698,18 @@ void VLMWrapper::EditBroadcast( const QString name, const QString input,
     }
 }
 
-void VLMWrapper::ControlBroadcast( const QString name, int BroadcastStatus, 
+void VLMWrapper::EnableItem( const QString name, bool b_enable )
+{
+    vlm_message_t *message;
+    QString command = "setup \"" + name + ( b_enable ? " enable" : " disable" );
+}
+
+void VLMWrapper::ControlBroadcast( const QString name, int BroadcastStatus,
                                    unsigned int seek )
 {
     vlm_message_t *message;
 
-    QString command = "setup \"" + name;
+    QString command = "control \"" + name;
     switch( BroadcastStatus )
     {
     case ControlBroadcastPlay:
@@ -568,7 +741,7 @@ void VLMWrapper::AddVod( const QString name, const QString input,
 }
 
 void VLMWrapper::EditVod( const QString name, const QString input,
-                          const QString output, 
+                          const QString output,
                           bool b_enabled,
                           const QString mux )
 {
@@ -576,12 +749,14 @@ void VLMWrapper::EditVod( const QString name, const QString input,
     QString command = "setup \"" + name + "\" input \"" + input + "\"";
     vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
     vlm_MessageDelete( message );
+
     if( !output.isEmpty() )
     {
         command = "setup \"" + name + "\" output \"" + output + "\"";
         vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
         vlm_MessageDelete( message );
     }
+
     if( b_enabled )
     {
         command = "setup \"" + name + "\" enabled";
@@ -595,3 +770,65 @@ void VLMWrapper::EditVod( const QString name, const QString input,
         vlm_MessageDelete( message );
     }
 }
+
+void VLMWrapper::AddSchedule( const QString name, const QString input,
+                              const QString output, QDateTime _schetime,
+                              QDateTime _schedate,
+                              int _scherepeatnumber, int _repeatDays,
+                              bool b_enabled, const QString mux )
+{
+    vlm_message_t *message;
+    QString command = "new \"" + name + "\" schedule";
+    vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
+    vlm_MessageDelete( message );
+    EditSchedule(  name, input, output, _schetime, _schedate,
+            _scherepeatnumber, _repeatDays, b_enabled, mux );
+}
+
+void VLMWrapper::EditSchedule( const QString name, const QString input,
+                          const QString output, QDateTime _schetime,
+                          QDateTime _schedate, int _scherepeatnumber,
+                          int _repeatDays, bool b_enabled,
+                          const QString mux )
+{
+    vlm_message_t *message;
+    QString command = "setup \"" + name + "\" input \"" + input + "\"";
+    vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
+    vlm_MessageDelete( message );
+
+    if( !output.isEmpty() )
+    {
+        command = "setup \"" + name + "\" output \"" + output + "\"";
+        vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
+        vlm_MessageDelete( message );
+    }
+
+    if( b_enabled )
+    {
+        command = "setup \"" + name + "\" enabled";
+        vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
+        vlm_MessageDelete( message );
+    }
+
+    if( !mux.isEmpty() )
+    {
+        command = "setup \"" + name + "\" mux \"" + mux + "\"";
+        vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
+        vlm_MessageDelete( message );
+    }
+
+    command = "setup \"" + name + "\" date \"" +
+        _schedate.toString( "yyyy/MM/dd" )+ "-" +
+        _schetime.toString( "hh:mm:ss" ) + "\"";
+    vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
+    vlm_MessageDelete( message );
+    if( _scherepeatnumber > 0 )
+    {
+       command = "setup \"" + name + "\" repeat \"" + _scherepeatnumber + "\"";
+       vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
+       vlm_MessageDelete( message );
+    }
+}
+
+
+#endif