1 /*****************************************************************************
2 * open.cpp : Advanced open dialog
3 *****************************************************************************
4 * Copyright © 2006-2011 the VideoLAN team
6 * Authors: Jean-Baptiste Kempf <jb@videolan.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
27 #include "dialogs/open.hpp"
28 #include "dialogs_provider.hpp"
29 #include "recents.hpp"
30 #include "util/qt_dirs.hpp"
40 OpenDialog *OpenDialog::instance = NULL;
42 OpenDialog* OpenDialog::getInstance( QWidget *parent, intf_thread_t *p_intf,
43 bool b_rawInstance, int _action_flag, bool b_selectMode, bool _b_pl )
47 instance = new OpenDialog( parent, p_intf, b_selectMode,
48 _action_flag, _b_pl );
49 else if( !b_rawInstance )
51 /* Request the instance but change small details:
54 _action_flag = SELECT; /* This should be useless, but we never know
55 if the call is correct */
56 instance->setWindowModality( Qt::WindowModal );
57 instance->i_action_flag = _action_flag;
58 instance->b_pl = _b_pl;
59 instance->setMenuAction();
64 OpenDialog::OpenDialog( QWidget *parent,
65 intf_thread_t *_p_intf,
68 bool _b_pl) : QVLCDialog( parent, _p_intf )
70 i_action_flag = _action_flag;
73 if( b_selectMode ) /* Select mode */
74 i_action_flag = SELECT;
76 /* Basic Creation of the Window */
78 setWindowTitle( qtr( "Open Media" ) );
79 setWindowRole( "vlc-open-media" );
80 setWindowModality( Qt::WindowModal );
82 /* Tab definition and creation */
83 fileOpenPanel = new FileOpenPanel( this, p_intf );
84 discOpenPanel = new DiscOpenPanel( this, p_intf );
85 netOpenPanel = new NetOpenPanel( this, p_intf );
86 captureOpenPanel = new CaptureOpenPanel( this, p_intf );
89 ui.Tab->insertTab( OPEN_FILE_TAB, fileOpenPanel, QIcon( ":/type/file-asym" ),
91 ui.Tab->insertTab( OPEN_DISC_TAB, discOpenPanel, QIcon( ":/type/disc" ),
93 ui.Tab->insertTab( OPEN_NETWORK_TAB, netOpenPanel, QIcon( ":/type/network" ),
95 ui.Tab->insertTab( OPEN_CAPTURE_TAB, captureOpenPanel,
96 QIcon( ":/type/capture-card" ), qtr( "Capture &Device" ) );
98 /* Hide the Slave input widgets */
99 ui.slaveLabel->hide();
100 ui.slaveText->hide();
101 ui.slaveBrowseButton->hide();
103 /* Buttons Creation */
105 playButton = ui.playButton;
108 cancelButton = new QPushButton( qtr( "&Cancel" ) );
111 selectButton = new QPushButton( qtr( "&Select" ) );
113 /* Menu for the Play button */
114 QMenu * openButtonMenu = new QMenu( "Open" );
115 openButtonMenu->addAction( qtr( "&Enqueue" ), this, SLOT( enqueue() ),
116 QKeySequence( "Alt+E" ) );
117 openButtonMenu->addAction( qtr( "&Play" ), this, SLOT( play() ),
118 QKeySequence( "Alt+P" ) );
119 openButtonMenu->addAction( qtr( "&Stream" ), this, SLOT( stream() ) ,
120 QKeySequence( "Alt+S" ) );
121 openButtonMenu->addAction( qtr( "C&onvert" ), this, SLOT( transcode() ) ,
122 QKeySequence( "Alt+O" ) );
124 playButton->setMenu( openButtonMenu );
126 /* Add the three Buttons */
127 ui.buttonsBox->addButton( selectButton, QDialogButtonBox::AcceptRole );
128 ui.buttonsBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
130 /* At creation time, modify the default buttons */
133 /* Force MRL update on tab change */
134 CONNECT( ui.Tab, currentChanged( int ), this, signalCurrent( int ) );
136 CONNECT( fileOpenPanel, mrlUpdated( const QStringList&, const QString& ),
137 this, updateMRL( const QStringList&, const QString& ) );
138 CONNECT( netOpenPanel, mrlUpdated( const QStringList&, const QString& ),
139 this, updateMRL( const QStringList&, const QString& ) );
140 CONNECT( discOpenPanel, mrlUpdated( const QStringList&, const QString& ),
141 this, updateMRL( const QStringList&, const QString& ) );
142 CONNECT( captureOpenPanel, mrlUpdated( const QStringList&, const QString& ),
143 this, updateMRL( const QStringList&, const QString& ) );
145 CONNECT( fileOpenPanel, methodChanged( const QString& ),
146 this, newCachingMethod( const QString& ) );
147 CONNECT( netOpenPanel, methodChanged( const QString& ),
148 this, newCachingMethod( const QString& ) );
149 CONNECT( discOpenPanel, methodChanged( const QString& ),
150 this, newCachingMethod( const QString& ) );
151 CONNECT( captureOpenPanel, methodChanged( const QString& ),
152 this, newCachingMethod( const QString& ) );
154 /* Advanced frame Connects */
155 CONNECT( ui.slaveCheckbox, toggled( bool ), this, updateMRL() );
156 CONNECT( ui.slaveText, textChanged( const QString& ), this, updateMRL() );
157 CONNECT( ui.cacheSpinBox, valueChanged( int ), this, updateMRL() );
158 CONNECT( ui.startTimeTimeEdit, timeChanged ( const QTime& ), this, updateMRL() );
159 BUTTONACT( ui.advancedCheckBox, toggleAdvancedPanel() );
160 BUTTONACT( ui.slaveBrowseButton, browseInputSlave() );
163 BUTTONACT( playButton, selectSlots() );
164 BUTTONACT( selectButton, close() );
165 BUTTONACT( cancelButton, cancel() );
167 /* Hide the advancedPanel */
168 if( !getSettings()->value( "OpenDialog/advanced", false ).toBool())
170 ui.advancedFrame->hide();
171 ui.advancedFrame->setEnabled( false );
174 ui.advancedCheckBox->setChecked( true );
176 /* Initialize caching */
178 newCachingMethod( "file-caching" );
180 /* enforce section due to .ui bug */
181 ui.startTimeTimeEdit->setCurrentSection( QDateTimeEdit::SecondSection );
183 setMinimumSize( sizeHint() );
184 setMaximumWidth( 900 );
185 resize( getSettings()->value( "OpenDialog/size", QSize( 500, 400 ) ).toSize() );
188 /* Finish the dialog and decide if you open another one after */
189 void OpenDialog::setMenuAction()
191 if( i_action_flag == SELECT )
194 selectButton->show();
195 selectButton->setDefault( true );
199 switch ( i_action_flag )
201 case OPEN_AND_STREAM:
202 playButton->setText( qtr( "&Stream" ) );
205 playButton->setText( qtr( "C&onvert / Save" ) );
207 case OPEN_AND_ENQUEUE:
208 playButton->setText( qtr( "&Enqueue" ) );
212 playButton->setText( qtr( "&Play" ) );
215 selectButton->hide();
219 OpenDialog::~OpenDialog()
221 getSettings()->setValue( "OpenDialog/size", size() -
222 ( ui.advancedFrame->isEnabled() ?
223 QSize(0, ui.advancedFrame->height()) : QSize(0, 0) ) );
224 getSettings()->setValue( "OpenDialog/advanced", ui.advancedFrame->isVisible() );
227 /* Used by VLM dialog and inputSlave selection */
228 QString OpenDialog::getMRL( bool b_all )
230 if( itemsMRL.count() == 0 ) return "";
231 return b_all ? itemsMRL[0] + getOptions()
235 QString OpenDialog::getOptions()
237 return ui.advancedLineInput->text();
240 void OpenDialog::showTab( int i_tab )
242 if( i_tab == OPEN_CAPTURE_TAB ) captureOpenPanel->initialize();
243 ui.Tab->setCurrentIndex( i_tab );
245 if( ui.Tab->currentWidget() != NULL )
247 OpenPanel *panel = qobject_cast<OpenPanel *>( ui.Tab->currentWidget() );
253 void OpenDialog::toggleAdvancedPanel()
255 if( ui.advancedFrame->isVisible() )
257 ui.advancedFrame->hide();
258 ui.advancedFrame->setEnabled( false );
259 if( size().isValid() )
260 resize( size().width(), size().height()
261 - ui.advancedFrame->height() );
265 ui.advancedFrame->show();
266 ui.advancedFrame->setEnabled( true );
267 if( size().isValid() )
268 resize( size().width(), size().height()
269 + ui.advancedFrame->height() );
273 void OpenDialog::browseInputSlave()
275 OpenDialog *od = new OpenDialog( this, p_intf, true, SELECT );
277 ui.slaveText->setText( od->getMRL( false ) );
281 /* Function called on signal currentChanged triggered */
282 void OpenDialog::signalCurrent( int i_tab )
284 if( i_tab == OPEN_CAPTURE_TAB ) captureOpenPanel->initialize();
285 if( ui.Tab->currentWidget() != NULL )
287 OpenPanel *panel = qobject_cast<OpenPanel *>( ui.Tab->currentWidget() );
297 /* If Cancel is pressed or escaped */
298 void OpenDialog::cancel()
300 /* Clear the panels */
301 for( int i = 0; i < OPEN_TAB_MAX; i++ )
302 qobject_cast<OpenPanel*>( ui.Tab->widget( i ) )->clear();
304 /* Clear the variables */
308 /* If in Select Mode, reject instead of hiding */
309 if( i_action_flag == SELECT ) reject();
313 /* If EnterKey is pressed */
314 void OpenDialog::close()
316 /* If in Select Mode, accept instead of selecting a Slot */
317 if( i_action_flag == SELECT )
324 void OpenDialog::selectSlots()
326 switch ( i_action_flag )
328 case OPEN_AND_STREAM:
334 case OPEN_AND_ENQUEUE:
343 /* Play Action, called from selectSlots or play Menu */
344 void OpenDialog::play()
349 /* Enqueue Action, called from selectSlots or enqueue Menu */
350 void OpenDialog::enqueue( bool b_enqueue )
354 if( i_action_flag == SELECT )
360 for( int i = 0; i < OPEN_TAB_MAX; i++ )
361 qobject_cast<OpenPanel*>( ui.Tab->widget( i ) )->onAccept();
363 /* Sort alphabetically */
366 /* Go through the item list */
367 for( int i = 0; i < itemsMRL.count(); i++ )
369 bool b_start = !i && !b_enqueue;
371 input_item_t *p_input_item;
372 p_input_item = input_item_New( qtu( itemsMRL[i] ), NULL );
374 /* Take options from the UI, not from what we stored */
375 QStringList optionsList = getOptions().split( " :" );
378 for( int j = 0; j < optionsList.count(); j++ )
380 QString qs = colon_unescape( optionsList[j] );
383 input_item_AddOption( p_input_item, qtu( qs ),
384 VLC_INPUT_OPTION_TRUSTED );
386 msg_Warn( p_intf, "Input option: %s", qtu( qs ) );
391 /* Switch between enqueuing and starting the item */
392 /* FIXME: playlist_AddInput() can fail */
393 playlist_AddInput( THEPL, p_input_item,
394 PLAYLIST_APPEND | ( b_start ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
395 PLAYLIST_END, b_pl ? true : false, pl_Unlocked );
396 vlc_gc_decref( p_input_item );
398 /* Do not add the current MRL if playlist_AddInput fail */
399 RecentsMRL::getInstance( p_intf )->addRecent( itemsMRL[i] );
403 void OpenDialog::transcode()
408 void OpenDialog::stream( bool b_transcode_only )
410 QString soutMRL = getMRL( false );
411 if( soutMRL.isEmpty() ) return;
414 /* Dbg and send :D */
415 msg_Dbg( p_intf, "MRL passed to the Sout: %s", qtu( soutMRL ) );
416 THEDP->streamingDialog( this, soutMRL, b_transcode_only,
417 getOptions().split( " :" ) );
420 /* Update the MRL items from the panels */
421 void OpenDialog::updateMRL( const QStringList& item, const QString& tempMRL )
423 optionsMRL = tempMRL;
428 /* Update the complete MRL */
429 void OpenDialog::updateMRL() {
430 QString mrl = optionsMRL;
431 if( ui.slaveCheckbox->isChecked() ) {
432 mrl += " :input-slave=" + ui.slaveText->text();
434 mrl += QString( " :%1=%2" ).arg( storedMethod ).
435 arg( ui.cacheSpinBox->value() );
436 if( ui.startTimeTimeEdit->time() != ui.startTimeTimeEdit->minimumTime() ) {
437 mrl += QString( " :start-time=%1.%2" )
438 .arg( QString::number(
439 ui.startTimeTimeEdit->minimumTime().secsTo(
440 ui.startTimeTimeEdit->time()
442 .arg( ui.startTimeTimeEdit->time().msec(), 3, 10, QChar('0') );
444 ui.advancedLineInput->setText( mrl );
445 ui.mrlLine->setText( itemsMRL.join( " " ) );
446 /* Only allow action without valid items */
447 playButton->setEnabled( !itemsMRL.isEmpty() );
448 selectButton->setEnabled( !itemsMRL.isEmpty() );
451 /* Change the caching combobox */
452 void OpenDialog::newCachingMethod( const QString& method )
454 if( method != storedMethod ) {
455 storedMethod = method;
456 int i_value = var_InheritInteger( p_intf, qtu( storedMethod ) );
457 ui.cacheSpinBox->setValue( i_value );
463 QStringList OpenDialog::SeparateEntries( const QString& entries )
465 bool b_quotes_mode = false;
467 QStringList entries_array;
471 while( index < entries.count() )
473 int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
474 if( delim_pos < 0 ) delim_pos = entries.count() - 1;
475 entry += entries.mid( index, delim_pos - index + 1 );
476 index = delim_pos + 1;
478 if( entry.isEmpty() ) continue;
480 if( !b_quotes_mode && entry.endsWith( "\"" ) )
482 /* Enters quotes mode */
483 entry.truncate( entry.count() - 1 );
484 b_quotes_mode = true;
486 else if( b_quotes_mode && entry.endsWith( "\"" ) )
488 /* Finished the quotes mode */
489 entry.truncate( entry.count() - 1 );
490 b_quotes_mode = false;
492 else if( !b_quotes_mode && !entry.endsWith( "\"" ) )
494 /* we found a non-quoted standalone string */
495 if( index < entries.count() ||
496 entry.endsWith( " " ) || entry.endsWith( "\t" ) ||
497 entry.endsWith( "\r" ) || entry.endsWith( "\n" ) )
498 entry.truncate( entry.count() - 1 );
499 if( !entry.isEmpty() ) entries_array.append( entry );
506 if( !entry.isEmpty() ) entries_array.append( entry );
508 return entries_array;