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"
35 #include <QGridLayout>
41 OpenDialog *OpenDialog::instance = NULL;
43 OpenDialog* OpenDialog::getInstance( QWidget *parent, intf_thread_t *p_intf,
44 bool b_rawInstance, int _action_flag, bool b_selectMode, bool _b_pl )
48 instance = new OpenDialog( parent, p_intf, b_selectMode,
49 _action_flag, _b_pl );
50 else if( !b_rawInstance )
52 /* Request the instance but change small details:
54 - Modality on top of the parent dialog */
57 instance->setWindowModality( Qt::WindowModal );
58 _action_flag = SELECT; /* This should be useless, but we never know
59 if the call is correct */
61 instance->i_action_flag = _action_flag;
62 instance->b_pl = _b_pl;
63 instance->setMenuAction();
68 OpenDialog::OpenDialog( QWidget *parent,
69 intf_thread_t *_p_intf,
72 bool _b_pl) : QVLCDialog( parent, _p_intf )
74 i_action_flag = _action_flag;
77 /* Workaround the Win32 Vout that put the video on top at regular times */
79 setWindowFlags( Qt::WindowStaysOnTopHint | Qt::Dialog );
82 if( b_selectMode ) /* Select mode */
84 i_action_flag = SELECT;
85 setWindowModality( Qt::WindowModal );
88 /* Basic Creation of the Window */
90 setWindowTitle( qtr( "Open a Media" ) );
92 /* Tab definition and creation */
93 fileOpenPanel = new FileOpenPanel( this, p_intf );
94 discOpenPanel = new DiscOpenPanel( this, p_intf );
95 netOpenPanel = new NetOpenPanel( this, p_intf );
96 captureOpenPanel = new CaptureOpenPanel( this, p_intf );
99 ui.Tab->insertTab( OPEN_FILE_TAB, fileOpenPanel, qtr( "&File" ) );
100 ui.Tab->insertTab( OPEN_DISC_TAB, discOpenPanel, qtr( "&Disc" ) );
101 ui.Tab->insertTab( OPEN_NETWORK_TAB, netOpenPanel, qtr( "&Network" ) );
102 ui.Tab->insertTab( OPEN_CAPTURE_TAB, captureOpenPanel,
103 qtr( "Capture &Device" ) );
105 /* Hide the Slave input widgets */
106 ui.slaveLabel->hide();
107 ui.slaveText->hide();
108 ui.slaveBrowseButton->hide();
110 /* Buttons Creation */
112 playButton = ui.playButton;
115 cancelButton = new QPushButton( qtr( "&Cancel" ) );
118 selectButton = new QPushButton( qtr( "&Select" ) );
120 /* Menu for the Play button */
121 QMenu * openButtonMenu = new QMenu( "Open" );
122 openButtonMenu->addAction( qtr( "&Enqueue" ), this, SLOT( enqueue() ),
123 QKeySequence( "Alt+E" ) );
124 openButtonMenu->addAction( qtr( "&Play" ), this, SLOT( play() ),
125 QKeySequence( "Alt+P" ) );
126 openButtonMenu->addAction( qtr( "&Stream" ), this, SLOT( stream() ) ,
127 QKeySequence( "Alt+S" ) );
128 openButtonMenu->addAction( qtr( "&Convert" ), this, SLOT( transcode() ) ,
129 QKeySequence( "Alt+C" ) );
131 ui.menuButton->setMenu( openButtonMenu );
132 ui.menuButton->setIcon( QIcon( ":/down_arrow" ) );
134 /* Add the three Buttons */
135 ui.buttonsBox->addButton( selectButton, QDialogButtonBox::AcceptRole );
136 ui.buttonsBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
138 /* At creation time, modify the default buttons */
141 /* Force MRL update on tab change */
142 CONNECT( ui.Tab, currentChanged( int ), this, signalCurrent( int ) );
144 CONNECT( fileOpenPanel, mrlUpdated( QStringList, QString ),
145 this, updateMRL( QStringList, QString ) );
146 CONNECT( netOpenPanel, mrlUpdated( QStringList, QString ),
147 this, updateMRL( QStringList, QString ) );
148 CONNECT( discOpenPanel, mrlUpdated( QStringList, QString ),
149 this, updateMRL( QStringList, QString ) );
150 CONNECT( captureOpenPanel, mrlUpdated( QStringList, QString ),
151 this, updateMRL( QStringList, QString ) );
153 CONNECT( fileOpenPanel, methodChanged( QString ),
154 this, newCachingMethod( QString ) );
155 CONNECT( netOpenPanel, methodChanged( QString ),
156 this, newCachingMethod( QString ) );
157 CONNECT( discOpenPanel, methodChanged( QString ),
158 this, newCachingMethod( QString ) );
159 CONNECT( captureOpenPanel, methodChanged( QString ),
160 this, newCachingMethod( QString ) );
162 /* Advanced frame Connects */
163 CONNECT( ui.slaveCheckbox, toggled( bool ), this, updateMRL() );
164 CONNECT( ui.slaveText, textChanged( QString ), this, updateMRL() );
165 CONNECT( ui.cacheSpinBox, valueChanged( int ), this, updateMRL() );
166 CONNECT( ui.startTimeSpinBox, valueChanged( int ), this, updateMRL() );
167 BUTTONACT( ui.advancedCheckBox, toggleAdvancedPanel() );
168 BUTTONACT( ui.slaveBrowseButton, browseInputSlave() );
171 BUTTONACT( playButton, selectSlots() );
172 BUTTONACT( selectButton, close() );
173 BUTTONACT( cancelButton, cancel() );
175 /* Hide the advancedPanel */
176 if( !config_GetInt( p_intf, "qt-adv-options" ) )
177 ui.advancedFrame->hide();
179 ui.advancedCheckBox->setChecked( true );
181 /* Initialize caching */
183 newCachingMethod( "file-caching" );
185 setMinimumSize( sizeHint() );
186 setMaximumWidth( 900 );
187 resize( getSettings()->value( "opendialog-size", QSize( 500, 490 ) ).toSize() );
190 OpenDialog::~OpenDialog()
192 getSettings()->setValue( "opendialog-size", size() );
195 /* Used by VLM dialog and inputSlave selection */
196 QString OpenDialog::getMRL( bool b_all )
198 if( itemsMRL.size() == 0 ) return "";
199 return b_all ? itemsMRL[0] + ui.advancedLineInput->text()
203 /* Finish the dialog and decide if you open another one after */
204 void OpenDialog::setMenuAction()
206 if( i_action_flag == SELECT )
209 selectButton->show();
210 selectButton->setDefault( true );
214 switch ( i_action_flag )
216 case OPEN_AND_STREAM:
217 playButton->setText( qtr( "&Stream" ) );
220 playButton->setText( qtr( "&Convert / Save" ) );
222 case OPEN_AND_ENQUEUE:
223 playButton->setText( qtr( "&Enqueue" ) );
227 playButton->setText( qtr( "&Play" ) );
230 selectButton->hide();
231 playButton->setDefault( true );
235 void OpenDialog::showTab( int i_tab )
237 if( i_tab == OPEN_CAPTURE_TAB ) captureOpenPanel->initialize();
238 ui.Tab->setCurrentIndex( i_tab );
242 /* Function called on signal currentChanged triggered */
243 void OpenDialog::signalCurrent( int i_tab )
245 if( i_tab == OPEN_CAPTURE_TAB ) captureOpenPanel->initialize();
247 if( ui.Tab->currentWidget() != NULL )
248 ( dynamic_cast<OpenPanel *>( ui.Tab->currentWidget() ) )->updateMRL();
251 void OpenDialog::toggleAdvancedPanel()
253 if( ui.advancedFrame->isVisible() )
255 ui.advancedFrame->hide();
256 if( size().isValid() )
257 resize( size().width(), size().height()
258 - ui.advancedFrame->height() );
262 ui.advancedFrame->show();
263 if( size().isValid() )
264 resize( size().width(), size().height()
265 + ui.advancedFrame->height() );
272 /* If Cancel is pressed or escaped */
273 void OpenDialog::cancel()
275 /* Clear the panels */
276 for( int i = 0; i < OPEN_TAB_MAX; i++ )
277 dynamic_cast<OpenPanel*>( ui.Tab->widget( i ) )->clear();
279 /* Clear the variables */
283 /* If in Select Mode, reject instead of hiding */
284 if( i_action_flag == SELECT ) reject();
288 /* If EnterKey is pressed */
289 void OpenDialog::close()
291 /* If in Select Mode, accept instead of selecting a Slot */
292 if( i_action_flag == SELECT )
299 void OpenDialog::selectSlots()
301 switch ( i_action_flag )
303 case OPEN_AND_STREAM:
309 case OPEN_AND_ENQUEUE:
318 void OpenDialog::play()
323 void OpenDialog::enqueue()
329 void OpenDialog::finish( bool b_enqueue = false )
333 if( i_action_flag == SELECT )
339 /* Sort alphabetically */
342 /* Go through the item list */
343 for( int i = 0; i < itemsMRL.size(); i++ )
345 bool b_start = !i && !b_enqueue;
347 input_item_t *p_input;
348 p_input = input_item_New( p_intf, qtu( itemsMRL[i] ), NULL );
350 /* Insert options only for the first element.
351 We don't know how to edit that anyway. */
354 /* Take options from the UI, not from what we stored */
355 QStringList optionsList = ui.advancedLineInput->text().split( ":" );
358 for( int j = 0; j < optionsList.size(); j++ )
360 QString qs = optionsList[j].trimmed();
363 input_item_AddOption( p_input, qtu( qs ),
364 VLC_INPUT_OPTION_TRUSTED );
366 msg_Warn( p_intf, "Input option: %s", qtu( qs ) );
372 /* Switch between enqueuing and starting the item */
373 /* FIXME: playlist_AddInput() can fail */
374 playlist_AddInput( THEPL, p_input,
375 PLAYLIST_APPEND | ( b_start ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
376 PLAYLIST_END, b_pl ? true : false, pl_Unlocked );
377 vlc_gc_decref( p_input );
379 /* Do not add the current MRL if playlist_AddInput fail */
380 RecentsMRL::getInstance( p_intf )->addRecent( itemsMRL[i] );
384 void OpenDialog::transcode()
389 void OpenDialog::stream( bool b_transcode_only )
391 QString soutMRL = getMRL( false );
392 if( soutMRL.isEmpty() ) return;
395 /* Dbg and send :D */
396 msg_Dbg( p_intf, "MRL passed to the Sout: %s", qtu( soutMRL ) );
397 THEDP->streamingDialog( this, soutMRL, b_transcode_only,
398 ui.advancedLineInput->text().split( ":" ) );
402 void OpenDialog::updateMRL( QStringList item, QString tempMRL )
404 optionsMRL = tempMRL;
409 void OpenDialog::updateMRL() {
410 QString mrl = optionsMRL;
411 if( ui.slaveCheckbox->isChecked() ) {
412 mrl += " :input-slave=" + ui.slaveText->text();
414 int i_cache = config_GetInt( p_intf, qtu( storedMethod ) );
415 if( i_cache != ui.cacheSpinBox->value() ) {
416 mrl += QString( " :%1=%2" ).arg( storedMethod ).
417 arg( ui.cacheSpinBox->value() );
419 if( ui.startTimeSpinBox->value() ) {
420 mrl += " :start-time=" + QString( "%1" ).
421 arg( ui.startTimeSpinBox->value() );
423 ui.advancedLineInput->setText( mrl );
424 ui.mrlLine->setText( itemsMRL.join( " " ) );
427 void OpenDialog::newCachingMethod( QString method )
429 if( method != storedMethod ) {
430 storedMethod = method;
431 int i_value = config_GetInt( p_intf, qtu( storedMethod ) );
432 ui.cacheSpinBox->setValue( i_value );
436 QStringList OpenDialog::SeparateEntries( QString entries )
438 bool b_quotes_mode = false;
440 QStringList entries_array;
444 while( index < entries.size() )
446 int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
447 if( delim_pos < 0 ) delim_pos = entries.size() - 1;
448 entry += entries.mid( index, delim_pos - index + 1 );
449 index = delim_pos + 1;
451 if( entry.isEmpty() ) continue;
453 if( !b_quotes_mode && entry.endsWith( "\"" ) )
455 /* Enters quotes mode */
456 entry.truncate( entry.size() - 1 );
457 b_quotes_mode = true;
459 else if( b_quotes_mode && entry.endsWith( "\"" ) )
461 /* Finished the quotes mode */
462 entry.truncate( entry.size() - 1 );
463 b_quotes_mode = false;
465 else if( !b_quotes_mode && !entry.endsWith( "\"" ) )
467 /* we found a non-quoted standalone string */
468 if( index < entries.size() ||
469 entry.endsWith( " " ) || entry.endsWith( "\t" ) ||
470 entry.endsWith( "\r" ) || entry.endsWith( "\n" ) )
471 entry.truncate( entry.size() - 1 );
472 if( !entry.isEmpty() ) entries_array.append( entry );
479 if( !entry.isEmpty() ) entries_array.append( entry );
481 return entries_array;
484 void OpenDialog::browseInputSlave()
486 OpenDialog *od = new OpenDialog( this, p_intf, true, SELECT );
488 ui.slaveText->setText( od->getMRL( false ) );