1 /*****************************************************************************
2 * open.cpp : Advanced open dialog
3 *****************************************************************************
4 * Copyright © 2006-2009 the VideoLAN team
7 * Authors: Jean-Baptiste Kempf <jb@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
28 #include "dialogs/open.hpp"
30 #include "dialogs_provider.hpp"
32 #include "recents.hpp"
33 #include "util/qt_dirs.hpp"
36 #include <QGridLayout>
42 OpenDialog *OpenDialog::instance = NULL;
44 OpenDialog* OpenDialog::getInstance( QWidget *parent, intf_thread_t *p_intf,
45 bool b_rawInstance, int _action_flag, bool b_selectMode, bool _b_pl )
49 instance = new OpenDialog( parent, p_intf, b_selectMode,
50 _action_flag, _b_pl );
51 else if( !b_rawInstance )
53 /* Request the instance but change small details:
55 - Modality on top of the parent dialog */
58 instance->setWindowModality( Qt::WindowModal );
59 _action_flag = SELECT; /* This should be useless, but we never know
60 if the call is correct */
62 instance->i_action_flag = _action_flag;
63 instance->b_pl = _b_pl;
64 instance->setMenuAction();
69 OpenDialog::OpenDialog( QWidget *parent,
70 intf_thread_t *_p_intf,
73 bool _b_pl) : QVLCDialog( parent, _p_intf )
75 i_action_flag = _action_flag;
78 if( b_selectMode ) /* Select mode */
80 i_action_flag = SELECT;
81 setWindowModality( Qt::WindowModal );
84 /* Basic Creation of the Window */
86 setWindowTitle( qtr( "Open Media" ) );
88 /* Tab definition and creation */
89 fileOpenPanel = new FileOpenPanel( this, p_intf );
90 discOpenPanel = new DiscOpenPanel( this, p_intf );
91 netOpenPanel = new NetOpenPanel( this, p_intf );
92 captureOpenPanel = new CaptureOpenPanel( this, p_intf );
95 ui.Tab->insertTab( OPEN_FILE_TAB, fileOpenPanel, QIcon( ":/folder-grey" ),
97 ui.Tab->insertTab( OPEN_DISC_TAB, discOpenPanel, QIcon( ":/disc" ),
99 ui.Tab->insertTab( OPEN_NETWORK_TAB, netOpenPanel, QIcon( ":/network" ),
101 ui.Tab->insertTab( OPEN_CAPTURE_TAB, captureOpenPanel, QIcon( ":/capture-card" ),
102 qtr( "Capture &Device" ) );
104 /* Hide the Slave input widgets */
105 ui.slaveLabel->hide();
106 ui.slaveText->hide();
107 ui.slaveBrowseButton->hide();
109 /* Buttons Creation */
111 playButton = ui.playButton;
114 cancelButton = new QPushButton( qtr( "&Cancel" ) );
117 selectButton = new QPushButton( qtr( "&Select" ) );
119 /* Menu for the Play button */
120 QMenu * openButtonMenu = new QMenu( "Open" );
121 openButtonMenu->addAction( qtr( "&Enqueue" ), this, SLOT( enqueue() ),
122 QKeySequence( "Alt+E" ) );
123 openButtonMenu->addAction( qtr( "&Play" ), this, SLOT( play() ),
124 QKeySequence( "Alt+P" ) );
125 openButtonMenu->addAction( qtr( "&Stream" ), this, SLOT( stream() ) ,
126 QKeySequence( "Alt+S" ) );
127 openButtonMenu->addAction( qtr( "&Convert" ), this, SLOT( transcode() ) ,
128 QKeySequence( "Alt+C" ) );
130 ui.menuButton->setMenu( openButtonMenu );
131 ui.menuButton->setIcon( QIcon( ":/down_arrow" ) );
133 /* Add the three Buttons */
134 ui.buttonsBox->addButton( selectButton, QDialogButtonBox::AcceptRole );
135 ui.buttonsBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
137 /* At creation time, modify the default buttons */
140 /* Force MRL update on tab change */
141 CONNECT( ui.Tab, currentChanged( int ), this, signalCurrent( int ) );
143 CONNECT( fileOpenPanel, mrlUpdated( const QStringList&, const QString& ),
144 this, updateMRL( const QStringList&, const QString& ) );
145 CONNECT( netOpenPanel, mrlUpdated( const QStringList&, const QString& ),
146 this, updateMRL( const QStringList&, const QString& ) );
147 CONNECT( discOpenPanel, mrlUpdated( const QStringList&, const QString& ),
148 this, updateMRL( const QStringList&, const QString& ) );
149 CONNECT( captureOpenPanel, mrlUpdated( const QStringList&, const QString& ),
150 this, updateMRL( const QStringList&, const QString& ) );
152 CONNECT( fileOpenPanel, methodChanged( const QString& ),
153 this, newCachingMethod( const QString& ) );
154 CONNECT( netOpenPanel, methodChanged( const QString& ),
155 this, newCachingMethod( const QString& ) );
156 CONNECT( discOpenPanel, methodChanged( const QString& ),
157 this, newCachingMethod( const QString& ) );
158 CONNECT( captureOpenPanel, methodChanged( const QString& ),
159 this, newCachingMethod( const QString& ) );
161 /* Advanced frame Connects */
162 CONNECT( ui.slaveCheckbox, toggled( bool ), this, updateMRL() );
163 CONNECT( ui.slaveText, textChanged( const QString& ), this, updateMRL() );
164 CONNECT( ui.cacheSpinBox, valueChanged( int ), this, updateMRL() );
165 CONNECT( ui.startTimeDoubleSpinBox, valueChanged( double ), this, updateMRL() );
166 BUTTONACT( ui.advancedCheckBox, toggleAdvancedPanel() );
167 BUTTONACT( ui.slaveBrowseButton, browseInputSlave() );
170 BUTTONACT( playButton, selectSlots() );
171 BUTTONACT( selectButton, close() );
172 BUTTONACT( cancelButton, cancel() );
174 /* Hide the advancedPanel */
175 if( !config_GetInt( p_intf, "qt-adv-options" ) )
176 ui.advancedFrame->hide();
178 ui.advancedCheckBox->setChecked( true );
180 /* Initialize caching */
182 newCachingMethod( "file-caching" );
184 setMinimumSize( sizeHint() );
185 setMaximumWidth( 900 );
186 resize( getSettings()->value( "opendialog-size", QSize( 500, 490 ) ).toSize() );
189 OpenDialog::~OpenDialog()
191 getSettings()->setValue( "opendialog-size", size() );
194 /* Used by VLM dialog and inputSlave selection */
195 QString OpenDialog::getMRL( bool b_all )
197 if( itemsMRL.size() == 0 ) return "";
198 return b_all ? itemsMRL[0] + ui.advancedLineInput->text()
202 /* Finish the dialog and decide if you open another one after */
203 void OpenDialog::setMenuAction()
205 if( i_action_flag == SELECT )
208 selectButton->show();
209 selectButton->setDefault( true );
213 switch ( i_action_flag )
215 case OPEN_AND_STREAM:
216 playButton->setText( qtr( "&Stream" ) );
219 playButton->setText( qtr( "&Convert / Save" ) );
221 case OPEN_AND_ENQUEUE:
222 playButton->setText( qtr( "&Enqueue" ) );
226 playButton->setText( qtr( "&Play" ) );
229 selectButton->hide();
230 playButton->setDefault( true );
234 void OpenDialog::showTab( int i_tab )
236 if( i_tab == OPEN_CAPTURE_TAB ) captureOpenPanel->initialize();
237 ui.Tab->setCurrentIndex( i_tab );
241 /* Function called on signal currentChanged triggered */
242 void OpenDialog::signalCurrent( int i_tab )
244 if( i_tab == OPEN_CAPTURE_TAB ) captureOpenPanel->initialize();
246 if( ui.Tab->currentWidget() != NULL )
247 ( dynamic_cast<OpenPanel *>( ui.Tab->currentWidget() ) )->updateMRL();
250 void OpenDialog::toggleAdvancedPanel()
252 if( ui.advancedFrame->isVisible() )
254 ui.advancedFrame->hide();
255 if( size().isValid() )
256 resize( size().width(), size().height()
257 - ui.advancedFrame->height() );
261 ui.advancedFrame->show();
262 if( size().isValid() )
263 resize( size().width(), size().height()
264 + ui.advancedFrame->height() );
271 /* If Cancel is pressed or escaped */
272 void OpenDialog::cancel()
274 /* Clear the panels */
275 for( int i = 0; i < OPEN_TAB_MAX; i++ )
276 dynamic_cast<OpenPanel*>( ui.Tab->widget( i ) )->clear();
278 /* Clear the variables */
282 /* If in Select Mode, reject instead of hiding */
283 if( i_action_flag == SELECT ) reject();
287 /* If EnterKey is pressed */
288 void OpenDialog::close()
290 /* If in Select Mode, accept instead of selecting a Slot */
291 if( i_action_flag == SELECT )
298 void OpenDialog::selectSlots()
300 switch ( i_action_flag )
302 case OPEN_AND_STREAM:
308 case OPEN_AND_ENQUEUE:
317 void OpenDialog::play()
322 void OpenDialog::enqueue()
328 void OpenDialog::finish( bool b_enqueue = false )
332 if( i_action_flag == SELECT )
338 /* Sort alphabetically */
341 /* Go through the item list */
342 for( int i = 0; i < itemsMRL.size(); i++ )
344 bool b_start = !i && !b_enqueue;
346 input_item_t *p_input;
347 p_input = input_item_New( p_intf, qtu( itemsMRL[i] ), NULL );
349 /* Insert options only for the first element.
350 We don't know how to edit that anyway. */
353 /* Take options from the UI, not from what we stored */
354 QStringList optionsList = ui.advancedLineInput->text().split( " :" );
357 for( int j = 0; j < optionsList.size(); j++ )
359 QString qs = colon_unescape( optionsList[j] );
362 input_item_AddOption( p_input, qtu( qs ),
363 VLC_INPUT_OPTION_TRUSTED );
365 msg_Warn( p_intf, "Input option: %s", qtu( qs ) );
371 /* Switch between enqueuing and starting the item */
372 /* FIXME: playlist_AddInput() can fail */
373 playlist_AddInput( THEPL, p_input,
374 PLAYLIST_APPEND | ( b_start ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
375 PLAYLIST_END, b_pl ? true : false, pl_Unlocked );
376 vlc_gc_decref( p_input );
378 /* Do not add the current MRL if playlist_AddInput fail */
379 RecentsMRL::getInstance( p_intf )->addRecent( itemsMRL[i] );
383 void OpenDialog::transcode()
388 void OpenDialog::stream( bool b_transcode_only )
390 QString soutMRL = getMRL( false );
391 if( soutMRL.isEmpty() ) return;
394 /* Dbg and send :D */
395 msg_Dbg( p_intf, "MRL passed to the Sout: %s", qtu( soutMRL ) );
396 THEDP->streamingDialog( this, soutMRL, b_transcode_only,
397 ui.advancedLineInput->text().split( " :" ) );
401 void OpenDialog::updateMRL( const QStringList& item, const QString& tempMRL )
403 optionsMRL = tempMRL;
408 void OpenDialog::updateMRL() {
409 QString mrl = optionsMRL;
410 if( ui.slaveCheckbox->isChecked() ) {
411 mrl += " :input-slave=" + ui.slaveText->text();
413 int i_cache = config_GetInt( p_intf, qtu( storedMethod ) );
414 if( i_cache != ui.cacheSpinBox->value() ) {
415 mrl += QString( " :%1=%2" ).arg( storedMethod ).
416 arg( ui.cacheSpinBox->value() );
418 if( ui.startTimeDoubleSpinBox->value() ) {
419 mrl += " :start-time=" + QString::number( ui.startTimeDoubleSpinBox->value() );
421 ui.advancedLineInput->setText( mrl );
422 ui.mrlLine->setText( itemsMRL.join( " " ) );
425 void OpenDialog::newCachingMethod( const QString& method )
427 if( method != storedMethod ) {
428 storedMethod = method;
429 int i_value = config_GetInt( p_intf, qtu( storedMethod ) );
430 ui.cacheSpinBox->setValue( i_value );
434 QStringList OpenDialog::SeparateEntries( const QString& entries )
436 bool b_quotes_mode = false;
438 QStringList entries_array;
442 while( index < entries.size() )
444 int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
445 if( delim_pos < 0 ) delim_pos = entries.size() - 1;
446 entry += entries.mid( index, delim_pos - index + 1 );
447 index = delim_pos + 1;
449 if( entry.isEmpty() ) continue;
451 if( !b_quotes_mode && entry.endsWith( "\"" ) )
453 /* Enters quotes mode */
454 entry.truncate( entry.size() - 1 );
455 b_quotes_mode = true;
457 else if( b_quotes_mode && entry.endsWith( "\"" ) )
459 /* Finished the quotes mode */
460 entry.truncate( entry.size() - 1 );
461 b_quotes_mode = false;
463 else if( !b_quotes_mode && !entry.endsWith( "\"" ) )
465 /* we found a non-quoted standalone string */
466 if( index < entries.size() ||
467 entry.endsWith( " " ) || entry.endsWith( "\t" ) ||
468 entry.endsWith( "\r" ) || entry.endsWith( "\n" ) )
469 entry.truncate( entry.size() - 1 );
470 if( !entry.isEmpty() ) entries_array.append( entry );
477 if( !entry.isEmpty() ) entries_array.append( entry );
479 return entries_array;
482 void OpenDialog::browseInputSlave()
484 OpenDialog *od = new OpenDialog( this, p_intf, true, SELECT );
486 ui.slaveText->setText( od->getMRL( false ) );