]> git.sesse.net Git - vlc/blobdiff - modules/gui/qt4/dialogs/open.cpp
Fix potential segfault.
[vlc] / modules / gui / qt4 / dialogs / open.cpp
index 8cc64a40daed01b2ad0065fd07bb2f74055cfee0..3a13ca9e67ecbedc17f71a907cbd0f9dae43338c 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * open.cpp : Advanced open dialog
  *****************************************************************************
- * Copyright (C) 2006-2007 the VideoLAN team
+ * Copyright © 2006-2007 the VideoLAN team
  * $Id$
  *
  * Authors: Jean-Baptiste Kempf <jb@videolan.org>
@@ -20,6 +20,9 @@
  * 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 "input_manager.hpp"
 
 
 OpenDialog *OpenDialog::instance = NULL;
 
-OpenDialog::OpenDialog( QWidget *parent, intf_thread_t *_p_intf, bool modal,
+OpenDialog* OpenDialog::getInstance( QWidget *parent, intf_thread_t *p_intf,
+        bool b_rawInstance, int _action_flag, bool b_selectMode )
+{
+    /* Creation */
+    if( !instance )
+        instance = new OpenDialog( parent, p_intf, b_selectMode, _action_flag );
+    else if( !b_rawInstance )
+    {
+        /* Request the instance but change small details:
+           - Button menu
+           - Modality on top of the parent dialog */
+        if( b_selectMode )
+        {
+            instance->setWindowModality( Qt::WindowModal );
+            _action_flag = SELECT; /* This should be useless, but we never know
+                                      if the call is correct */
+        }
+        instance->i_action_flag = _action_flag;
+        instance->setMenuAction();
+    }
+    return instance;
+}
+
+OpenDialog::OpenDialog( QWidget *parent,
+                        intf_thread_t *_p_intf,
+                        bool b_selectMode,
                         int _action_flag )  :  QVLCDialog( parent, _p_intf )
 {
-    setModal( modal );
     i_action_flag = _action_flag;
 
+    if( b_selectMode ) /* Select mode */
+    {
+        setWindowModality( Qt::WindowModal );
+        i_action_flag = SELECT;
+    }
+
     /* Basic Creation of the Window */
     ui.setupUi( this );
-    setWindowTitle( qtr("Open" ) );
-    resize( 410, 300);
+    setWindowTitle( qtr( "Open" ) );
+    /* resize( 410, 600 ); */
+    setMinimumSize( 520, 460 );
 
     /* Tab definition and creation */
-    fileOpenPanel = new FileOpenPanel( ui.Tab, p_intf );
-    discOpenPanel = new DiscOpenPanel( ui.Tab, p_intf );
-    netOpenPanel = new NetOpenPanel( ui.Tab, p_intf );
+    fileOpenPanel    = new FileOpenPanel( ui.Tab, p_intf );
+    discOpenPanel    = new DiscOpenPanel( ui.Tab, p_intf );
+    netOpenPanel     = new NetOpenPanel( ui.Tab, p_intf );
     captureOpenPanel = new CaptureOpenPanel( ui.Tab, p_intf );
 
     /* Insert the tabs */
@@ -55,41 +89,27 @@ OpenDialog::OpenDialog( QWidget *parent, intf_thread_t *_p_intf, bool modal,
     ui.Tab->insertTab( OPEN_DISC_TAB, discOpenPanel, qtr( "&Disc" ) );
     ui.Tab->insertTab( OPEN_NETWORK_TAB, netOpenPanel, qtr( "&Network" ) );
     ui.Tab->insertTab( OPEN_CAPTURE_TAB, captureOpenPanel,
-                                qtr( "Capture &Device" ) );
+                       qtr( "Capture &Device" ) );
 
     /* Hide the Slave input widgets */
     ui.slaveLabel->hide();
     ui.slaveText->hide();
     ui.slaveBrowseButton->hide();
 
-    /* Hide the advancedPanel */
-    if(! config_GetInt( p_intf, "qt-adv-options") )
-        ui.advancedFrame->hide();
-    else
-        ui.advancedCheckBox->setChecked( true );
-
     /* Buttons Creation */
-    QSizePolicy buttonSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
-    buttonSizePolicy.setHorizontalStretch( 0 );
-    buttonSizePolicy.setVerticalStretch( 0 );
-
     /* Play Button */
-    playButton = new QToolButton( this );
-    playButton->setText( qtr( "&Play" ) );
-    playButton->setSizePolicy( buttonSizePolicy );
-    playButton->setMinimumSize( QSize(90, 0) );
-    playButton->setPopupMode( QToolButton::MenuButtonPopup );
-    playButton->setToolButtonStyle( Qt::ToolButtonTextOnly );
+    playButton = ui.playButton;
 
     /* Cancel Button */
-    cancelButton = new QPushButton();
-    cancelButton->setText( qtr( "&Cancel" ) );
-    cancelButton->setSizePolicy( buttonSizePolicy );
+    cancelButton = new QPushButton( qtr( "&Cancel" ) );
+
+    /* Select Button */
+    selectButton = new QPushButton( qtr( "&Select" ) );
 
     /* Menu for the Play button */
     QMenu * openButtonMenu = new QMenu( "Open" );
     openButtonMenu->addAction( qtr( "&Enqueue" ), this, SLOT( enqueue() ),
-                                    QKeySequence( "Alt+E") );
+                                    QKeySequence( "Alt+E" ) );
     openButtonMenu->addAction( qtr( "&Play" ), this, SLOT( play() ),
                                     QKeySequence( "Alt+P" ) );
     openButtonMenu->addAction( qtr( "&Stream" ), this, SLOT( stream() ) ,
@@ -97,105 +117,120 @@ OpenDialog::OpenDialog( QWidget *parent, intf_thread_t *_p_intf, bool modal,
     openButtonMenu->addAction( qtr( "&Convert" ), this, SLOT( transcode() ) ,
                                     QKeySequence( "Alt+C" ) );
 
-    playButton->setMenu( openButtonMenu );
+    ui.menuButton->setMenu( openButtonMenu );
 
-    ui.buttonsBox->addButton( playButton, QDialogButtonBox::AcceptRole );
+    /* Add the three Buttons */
+    ui.buttonsBox->addButton( selectButton, QDialogButtonBox::AcceptRole );
     ui.buttonsBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
 
+    /* At creation time, modify the default buttons */
+    setMenuAction();
+
     /* Force MRL update on tab change */
-    CONNECT( ui.Tab, currentChanged(int), this, signalCurrent() );
+    CONNECT( ui.Tab, currentChanged( int ), this, signalCurrent() );
 
-    CONNECT( fileOpenPanel, mrlUpdated( QString ), this, updateMRL(QString) );
-    CONNECT( netOpenPanel, mrlUpdated( QString ), this, updateMRL(QString) );
-    CONNECT( discOpenPanel, mrlUpdated( QString ), this, updateMRL(QString) );
-    CONNECT( captureOpenPanel, mrlUpdated( QString ), this, updateMRL(QString) );
+    CONNECT( fileOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
+    CONNECT( netOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
+    CONNECT( discOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
+    CONNECT( captureOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
 
     CONNECT( fileOpenPanel, methodChanged( QString ),
-                                                 this, newCachingMethod(QString) );
+             this, newCachingMethod( QString ) );
     CONNECT( netOpenPanel, methodChanged( QString ),
-                                                 this, newCachingMethod(QString) );
+             this, newCachingMethod( QString ) );
     CONNECT( discOpenPanel, methodChanged( QString ),
-                                                 this, newCachingMethod(QString) );
+             this, newCachingMethod( QString ) );
     CONNECT( captureOpenPanel, methodChanged( QString ),
-                                                 this, newCachingMethod(QString) );
+             this, newCachingMethod( QString ) );
 
     /* Advanced frame Connects */
-    CONNECT( ui.slaveText, textChanged(QString), this, updateMRL() );
-    CONNECT( ui.cacheSpinBox, valueChanged(int), this, updateMRL() );
-    CONNECT( ui.startTimeSpinBox, valueChanged(int), this, updateMRL() );
-    BUTTONACT( ui.advancedCheckBox , toggleAdvancedPanel() );
+    CONNECT( ui.slaveText, textChanged( QString ), this, updateMRL() );
+    CONNECT( ui.cacheSpinBox, valueChanged( int ), this, updateMRL() );
+    CONNECT( ui.startTimeSpinBox, valueChanged( int ), this, updateMRL() );
+    BUTTONACT( ui.advancedCheckBox, toggleAdvancedPanel() );
+    BUTTONACT( ui.slaveBrowseButton, browseInputSlave() );
 
     /* Buttons action */
     BUTTONACT( playButton, selectSlots() );
+    BUTTONACT( selectButton, close() );
     BUTTONACT( cancelButton, cancel() );
 
-    /* At creation time, modify the default buttons */
-    if ( i_action_flag ) setMenuAction();
+    /* Hide the advancedPanel */
+    if( !config_GetInt( p_intf, "qt-adv-options" ) )
+        ui.advancedFrame->hide();
+    else
+        ui.advancedCheckBox->setChecked( true );
 
     /* Initialize caching */
     storedMethod = "";
     newCachingMethod( "file-caching" );
-
-    mainHeight = advHeight = 0;
 }
 
 OpenDialog::~OpenDialog()
-{
-}
+{}
 
 /* Finish the dialog and decide if you open another one after */
 void OpenDialog::setMenuAction()
 {
-    switch ( i_action_flag )
+    if( i_action_flag == SELECT )
     {
-    case OPEN_AND_STREAM:
-        playButton->setText( qtr( "&Stream" ) );
-        break;
-    case OPEN_AND_SAVE:
-        playButton->setText( qtr( "&Convert / Save" ) );
-        break;
-    case OPEN_AND_ENQUEUE:
-        playButton->setText( qtr( "&Enqueue" ) );
-        break;
-    case OPEN_AND_PLAY:
-    default:
-        playButton->setText( qtr( "&Play" ) );
-   }
+        playButton->hide();
+        selectButton->show();
+        selectButton->setDefault( true );
+    }
+    else
+    {
+        switch ( i_action_flag )
+        {
+        case OPEN_AND_STREAM:
+            playButton->setText( qtr( "&Stream" ) );
+            break;
+        case OPEN_AND_SAVE:
+            playButton->setText( qtr( "&Convert / Save" ) );
+            break;
+        case OPEN_AND_ENQUEUE:
+            playButton->setText( qtr( "&Enqueue" ) );
+            break;
+        case OPEN_AND_PLAY:
+        default:
+            playButton->setText( qtr( "&Play" ) );
+        }
+        playButton->show();
+        selectButton->hide();
+        playButton->setDefault( true );
+    }
 }
 
-void OpenDialog::showTab( int i_tab=0 )
+void OpenDialog::showTab( int i_tab )
 {
-    this->show();
     ui.Tab->setCurrentIndex( i_tab );
+    show();
 }
 
-void OpenDialog::signalCurrent() {
-    if (ui.Tab->currentWidget() != NULL)
-        (dynamic_cast<OpenPanel *>( ui.Tab->currentWidget() ))->updateMRL();
+/* Function called on signal currentChanged triggered */
+void OpenDialog::signalCurrent()
+{
+    if( ui.Tab->currentWidget() != NULL )
+        ( dynamic_cast<OpenPanel *>( ui.Tab->currentWidget() ) )->updateMRL();
 }
 
 void OpenDialog::toggleAdvancedPanel()
 {
-    //FIXME does not work under Windows
-    if( ui.advancedFrame->isVisible() ) {
+    if( ui.advancedFrame->isVisible() )
+    {
         ui.advancedFrame->hide();
-#ifndef WIN32
-        setMinimumHeight( 1 );
-        resize( width(), mainHeight );
-#endif
-    } else {
-#ifndef WIN32
-        if( mainHeight == 0 )
-            mainHeight = height();
-#endif
-
+        //setMinimumSize( 520, 460 );
+        if( size().isValid() )
+            resize( size().width(), size().height()
+                    - ui.advancedFrame->height() );
+    }
+    else
+    {
         ui.advancedFrame->show();
-#ifndef WIN32
-        if( advHeight == 0 ) {
-            advHeight = height() - mainHeight;
-        }
-        resize( width(), mainHeight + advHeight );
-#endif
+        //setMinimumSize( 520, 460 + ui.advancedFrame->height() );
+        if( size().isValid() )
+            resize( size().width(), size().height()
+                    + ui.advancedFrame->height() );
     }
 }
 
@@ -205,16 +240,27 @@ void OpenDialog::toggleAdvancedPanel()
 /* If Cancel is pressed or escaped */
 void OpenDialog::cancel()
 {
+    /* Clear the panels */
     for( int i = 0; i < OPEN_TAB_MAX; i++ )
-        dynamic_cast<OpenPanel*>(ui.Tab->widget( i ))->clear();
-    toggleVisible();
-    if( isModal() ) reject();
+        dynamic_cast<OpenPanel*>( ui.Tab->widget( i ) )->clear();
+
+    /* Clear the variables */
+    mrl.clear();
+    mainMRL.clear();
+
+    /* If in Select Mode, reject instead of hiding */
+    if( i_action_flag == SELECT ) reject();
+    else hide();
 }
 
 /* If EnterKey is pressed */
 void OpenDialog::close()
 {
-    selectSlots();
+    /* If in Select Mode, accept instead of selecting a Slot */
+    if( i_action_flag == SELECT )
+        accept();
+    else
+        selectSlots();
 }
 
 /* Play button */
@@ -247,24 +293,13 @@ void OpenDialog::enqueue()
     finish( true );
 }
 
-void OpenDialog::transcode()
-{
-    stream( true );
-}
-
-void OpenDialog::stream( bool b_transcode_only )
-{
-    /* not finished FIXME */
-    /* Should go through the finish function */
-    THEDP->streamingDialog( mrl, b_transcode_only );
-}
 
 void OpenDialog::finish( bool b_enqueue = false )
 {
     toggleVisible();
     mrl = ui.advancedLineInput->text();
 
-    if( !isModal() )
+    if( i_action_flag != SELECT )
     {
         QStringList tempMRL = SeparateEntries( mrl );
         for( size_t i = 0; i < tempMRL.size(); i++ )
@@ -282,24 +317,30 @@ void OpenDialog::finish( bool b_enqueue = false )
             }
 
             /* Switch between enqueuing and starting the item */
-            if( b_start )
-            {
-                playlist_AddInput( THEPL, p_input,
-                                   PLAYLIST_APPEND | PLAYLIST_GO,
-                                   PLAYLIST_END, VLC_TRUE, VLC_FALSE );
-            }
-            else
-            {
-                playlist_AddInput( THEPL, p_input,
-                                   PLAYLIST_APPEND | PLAYLIST_PREPARSE,
-                                   PLAYLIST_END, VLC_TRUE, VLC_FALSE );
-            }
+            /* FIXME: playlist_AddInput() can fail */
+            playlist_AddInput( THEPL, p_input,
+                PLAYLIST_APPEND | ( b_start ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
+                PLAYLIST_END, true, pl_Unlocked );
+            vlc_gc_decref( p_input );
         }
     }
     else
         accept();
 }
 
+void OpenDialog::transcode()
+{
+    stream( true );
+}
+
+void OpenDialog::stream( bool b_transcode_only )
+{
+    mrl = ui.advancedLineInput->text();
+    toggleVisible();
+    QStringList listMRL = SeparateEntries( mrl );
+    if( listMRL.size() > 0 )
+    THEDP->streamingDialog( this, SeparateEntries( mrl )[0], b_transcode_only );
+}
 
 /* Update the MRL */
 void OpenDialog::updateMRL( QString tempMRL )
@@ -381,3 +422,11 @@ QStringList OpenDialog::SeparateEntries( QString entries )
 
     return entries_array;
 }
+
+void OpenDialog::browseInputSlave()
+{
+    OpenDialog *od = new OpenDialog( this, p_intf, true, SELECT );
+    od->exec();
+    ui.slaveText->setText( od->getMRL() );
+    delete od;
+}