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:
56 _action_flag = SELECT; /* This should be useless, but we never know
57 if the call is correct */
58 instance->setWindowModality( Qt::WindowModal );
59 instance->i_action_flag = _action_flag;
60 instance->b_pl = _b_pl;
61 instance->setMenuAction();
66 OpenDialog::OpenDialog( QWidget *parent,
67 intf_thread_t *_p_intf,
70 bool _b_pl) : QVLCDialog( parent, _p_intf )
72 i_action_flag = _action_flag;
75 if( b_selectMode ) /* Select mode */
76 i_action_flag = SELECT;
78 /* Basic Creation of the Window */
80 setWindowTitle( qtr( "Open Media" ) );
81 setWindowRole( "vlc-open-media" );
82 setWindowModality( Qt::WindowModal );
84 /* Tab definition and creation */
85 fileOpenPanel = new FileOpenPanel( this, p_intf );
86 discOpenPanel = new DiscOpenPanel( this, p_intf );
87 netOpenPanel = new NetOpenPanel( this, p_intf );
88 captureOpenPanel = new CaptureOpenPanel( this, p_intf );
91 ui.Tab->insertTab( OPEN_FILE_TAB, fileOpenPanel, QIcon( ":/type/folder-grey" ),
93 ui.Tab->insertTab( OPEN_DISC_TAB, discOpenPanel, QIcon( ":/type/disc" ),
95 ui.Tab->insertTab( OPEN_NETWORK_TAB, netOpenPanel, QIcon( ":/type/network" ),
97 ui.Tab->insertTab( OPEN_CAPTURE_TAB, captureOpenPanel,
98 QIcon( ":/type/capture-card" ), qtr( "Capture &Device" ) );
100 /* Hide the Slave input widgets */
101 ui.slaveLabel->hide();
102 ui.slaveText->hide();
103 ui.slaveBrowseButton->hide();
105 /* Buttons Creation */
107 playButton = ui.playButton;
110 cancelButton = new QPushButton( qtr( "&Cancel" ) );
113 selectButton = new QPushButton( qtr( "&Select" ) );
115 /* Menu for the Play button */
116 QMenu * openButtonMenu = new QMenu( "Open" );
117 openButtonMenu->addAction( qtr( "&Enqueue" ), this, SLOT( enqueue() ),
118 QKeySequence( "Alt+E" ) );
119 openButtonMenu->addAction( qtr( "&Play" ), this, SLOT( play() ),
120 QKeySequence( "Alt+P" ) );
121 openButtonMenu->addAction( qtr( "&Stream" ), this, SLOT( stream() ) ,
122 QKeySequence( "Alt+S" ) );
123 openButtonMenu->addAction( qtr( "&Convert" ), this, SLOT( transcode() ) ,
124 QKeySequence( "Alt+C" ) );
126 playButton->setMenu( openButtonMenu );
128 /* Add the three Buttons */
129 ui.buttonsBox->addButton( selectButton, QDialogButtonBox::AcceptRole );
130 ui.buttonsBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
132 /* At creation time, modify the default buttons */
135 /* Force MRL update on tab change */
136 CONNECT( ui.Tab, currentChanged( int ), this, signalCurrent( int ) );
138 CONNECT( fileOpenPanel, mrlUpdated( const QStringList&, const QString& ),
139 this, updateMRL( const QStringList&, const QString& ) );
140 CONNECT( netOpenPanel, mrlUpdated( const QStringList&, const QString& ),
141 this, updateMRL( const QStringList&, const QString& ) );
142 CONNECT( discOpenPanel, mrlUpdated( const QStringList&, const QString& ),
143 this, updateMRL( const QStringList&, const QString& ) );
144 CONNECT( captureOpenPanel, mrlUpdated( const QStringList&, const QString& ),
145 this, updateMRL( const QStringList&, const QString& ) );
147 CONNECT( fileOpenPanel, methodChanged( const QString& ),
148 this, newCachingMethod( const QString& ) );
149 CONNECT( netOpenPanel, methodChanged( const QString& ),
150 this, newCachingMethod( const QString& ) );
151 CONNECT( discOpenPanel, methodChanged( const QString& ),
152 this, newCachingMethod( const QString& ) );
153 CONNECT( captureOpenPanel, methodChanged( const QString& ),
154 this, newCachingMethod( const QString& ) );
156 /* Advanced frame Connects */
157 CONNECT( ui.slaveCheckbox, toggled( bool ), this, updateMRL() );
158 CONNECT( ui.slaveText, textChanged( const QString& ), this, updateMRL() );
159 CONNECT( ui.cacheSpinBox, valueChanged( int ), this, updateMRL() );
160 CONNECT( ui.startTimeDoubleSpinBox, valueChanged( double ), this, updateMRL() );
161 BUTTONACT( ui.advancedCheckBox, toggleAdvancedPanel() );
162 BUTTONACT( ui.slaveBrowseButton, browseInputSlave() );
165 BUTTONACT( playButton, selectSlots() );
166 BUTTONACT( selectButton, close() );
167 BUTTONACT( cancelButton, cancel() );
169 /* Hide the advancedPanel */
170 if( !var_InheritBool( p_intf, "qt-adv-options" ) )
172 ui.advancedFrame->hide();
173 ui.advancedFrame->setEnabled( false );
176 ui.advancedCheckBox->setChecked( true );
178 /* Initialize caching */
180 newCachingMethod( "file-caching" );
182 setMinimumSize( sizeHint() );
183 setMaximumWidth( 900 );
184 resize( getSettings()->value( "opendialog-size", QSize( 500, 490 ) ).toSize() );
187 OpenDialog::~OpenDialog()
189 getSettings()->setValue( "opendialog-size", size() -
190 ( ui.advancedFrame->isEnabled() ?
191 QSize(0, ui.advancedFrame->height()) : QSize(0, 0) ) );
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 QString OpenDialog::getOptions()
204 return ui.advancedLineInput->text();
207 /* Finish the dialog and decide if you open another one after */
208 void OpenDialog::setMenuAction()
210 if( i_action_flag == SELECT )
213 selectButton->show();
214 selectButton->setDefault( true );
218 switch ( i_action_flag )
220 case OPEN_AND_STREAM:
221 playButton->setText( qtr( "&Stream" ) );
224 playButton->setText( qtr( "&Convert / Save" ) );
226 case OPEN_AND_ENQUEUE:
227 playButton->setText( qtr( "&Enqueue" ) );
231 playButton->setText( qtr( "&Play" ) );
234 selectButton->hide();
238 void OpenDialog::showTab( int i_tab )
240 if( i_tab == OPEN_CAPTURE_TAB ) captureOpenPanel->initialize();
241 ui.Tab->setCurrentIndex( i_tab );
243 if( ui.Tab->currentWidget() != NULL )
245 OpenPanel *panel = dynamic_cast<OpenPanel *>( ui.Tab->currentWidget() );
251 /* Function called on signal currentChanged triggered */
252 void OpenDialog::signalCurrent( int i_tab )
254 if( i_tab == OPEN_CAPTURE_TAB ) captureOpenPanel->initialize();
255 if( ui.Tab->currentWidget() != NULL )
257 OpenPanel *panel = dynamic_cast<OpenPanel *>( ui.Tab->currentWidget() );
264 void OpenDialog::toggleAdvancedPanel()
266 if( ui.advancedFrame->isVisible() )
268 ui.advancedFrame->hide();
269 ui.advancedFrame->setEnabled( false );
270 if( size().isValid() )
271 resize( size().width(), size().height()
272 - ui.advancedFrame->height() );
276 ui.advancedFrame->show();
277 ui.advancedFrame->setEnabled( true );
278 if( size().isValid() )
279 resize( size().width(), size().height()
280 + ui.advancedFrame->height() );
287 /* If Cancel is pressed or escaped */
288 void OpenDialog::cancel()
290 /* Clear the panels */
291 for( int i = 0; i < OPEN_TAB_MAX; i++ )
292 dynamic_cast<OpenPanel*>( ui.Tab->widget( i ) )->clear();
294 /* Clear the variables */
298 /* If in Select Mode, reject instead of hiding */
299 if( i_action_flag == SELECT ) reject();
303 /* If EnterKey is pressed */
304 void OpenDialog::close()
306 /* If in Select Mode, accept instead of selecting a Slot */
307 if( i_action_flag == SELECT )
314 void OpenDialog::selectSlots()
316 switch ( i_action_flag )
318 case OPEN_AND_STREAM:
324 case OPEN_AND_ENQUEUE:
333 void OpenDialog::play()
338 void OpenDialog::enqueue()
344 void OpenDialog::finish( bool b_enqueue = false )
348 if( i_action_flag == SELECT )
354 /* Sort alphabetically */
357 /* Go through the item list */
358 for( int i = 0; i < itemsMRL.size(); i++ )
360 bool b_start = !i && !b_enqueue;
362 input_item_t *p_input;
363 p_input = input_item_New( p_intf, qtu( itemsMRL[i] ), NULL );
365 /* Insert options only for the first element.
366 We don't know how to edit that anyway. */
369 /* Take options from the UI, not from what we stored */
370 QStringList optionsList = ui.advancedLineInput->text().split( " :" );
373 for( int j = 0; j < optionsList.size(); j++ )
375 QString qs = colon_unescape( optionsList[j] );
378 input_item_AddOption( p_input, qtu( qs ),
379 VLC_INPUT_OPTION_TRUSTED );
381 msg_Warn( p_intf, "Input option: %s", qtu( qs ) );
387 /* Switch between enqueuing and starting the item */
388 /* FIXME: playlist_AddInput() can fail */
389 playlist_AddInput( THEPL, p_input,
390 PLAYLIST_APPEND | ( b_start ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
391 PLAYLIST_END, b_pl ? true : false, pl_Unlocked );
392 vlc_gc_decref( p_input );
394 /* Do not add the current MRL if playlist_AddInput fail */
395 RecentsMRL::getInstance( p_intf )->addRecent( itemsMRL[i] );
399 void OpenDialog::transcode()
404 void OpenDialog::stream( bool b_transcode_only )
406 QString soutMRL = getMRL( false );
407 if( soutMRL.isEmpty() ) return;
410 /* Dbg and send :D */
411 msg_Dbg( p_intf, "MRL passed to the Sout: %s", qtu( soutMRL ) );
412 THEDP->streamingDialog( this, soutMRL, b_transcode_only,
413 ui.advancedLineInput->text().split( " :" ) );
417 void OpenDialog::updateMRL( const QStringList& item, const QString& tempMRL )
419 optionsMRL = tempMRL;
424 void OpenDialog::updateMRL() {
425 QString mrl = optionsMRL;
426 if( ui.slaveCheckbox->isChecked() ) {
427 mrl += " :input-slave=" + ui.slaveText->text();
429 mrl += QString( " :%1=%2" ).arg( storedMethod ).
430 arg( ui.cacheSpinBox->value() );
431 if( ui.startTimeDoubleSpinBox->value() ) {
432 mrl += " :start-time=" + QString::number( ui.startTimeDoubleSpinBox->value() );
434 ui.advancedLineInput->setText( mrl );
435 ui.mrlLine->setText( itemsMRL.join( " " ) );
438 void OpenDialog::newCachingMethod( const QString& method )
440 if( method != storedMethod ) {
441 storedMethod = method;
442 int i_value = var_InheritInteger( p_intf, qtu( storedMethod ) );
443 ui.cacheSpinBox->setValue( i_value );
447 QStringList OpenDialog::SeparateEntries( const QString& entries )
449 bool b_quotes_mode = false;
451 QStringList entries_array;
455 while( index < entries.size() )
457 int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
458 if( delim_pos < 0 ) delim_pos = entries.size() - 1;
459 entry += entries.mid( index, delim_pos - index + 1 );
460 index = delim_pos + 1;
462 if( entry.isEmpty() ) continue;
464 if( !b_quotes_mode && entry.endsWith( "\"" ) )
466 /* Enters quotes mode */
467 entry.truncate( entry.size() - 1 );
468 b_quotes_mode = true;
470 else if( b_quotes_mode && entry.endsWith( "\"" ) )
472 /* Finished the quotes mode */
473 entry.truncate( entry.size() - 1 );
474 b_quotes_mode = false;
476 else if( !b_quotes_mode && !entry.endsWith( "\"" ) )
478 /* we found a non-quoted standalone string */
479 if( index < entries.size() ||
480 entry.endsWith( " " ) || entry.endsWith( "\t" ) ||
481 entry.endsWith( "\r" ) || entry.endsWith( "\n" ) )
482 entry.truncate( entry.size() - 1 );
483 if( !entry.isEmpty() ) entries_array.append( entry );
490 if( !entry.isEmpty() ) entries_array.append( entry );
492 return entries_array;
495 void OpenDialog::browseInputSlave()
497 OpenDialog *od = new OpenDialog( this, p_intf, true, SELECT );
499 ui.slaveText->setText( od->getMRL( false ) );