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>
39 OpenDialog *OpenDialog::instance = NULL;
41 OpenDialog* OpenDialog::getInstance( QWidget *parent, intf_thread_t *p_intf,
42 bool b_rawInstance, int _action_flag, bool b_selectMode, bool _b_pl )
46 instance = new OpenDialog( parent, p_intf, b_selectMode,
47 _action_flag, _b_pl );
48 else if( !b_rawInstance )
50 /* Request the instance but change small details:
52 - Modality on top of the parent dialog */
55 instance->setWindowModality( Qt::WindowModal );
56 _action_flag = SELECT; /* This should be useless, but we never know
57 if the call is correct */
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 /* Workaround the Win32 Vout that put the video on top at regular times */
77 setWindowFlags( Qt::WindowStaysOnTopHint | Qt::Dialog );
80 if( b_selectMode ) /* Select mode */
82 i_action_flag = SELECT;
83 setWindowModality( Qt::WindowModal );
86 /* Basic Creation of the Window */
88 setWindowTitle( qtr( "Open a Media" ) );
90 /* Tab definition and creation */
91 fileOpenPanel = new FileOpenPanel( ui.Tab, p_intf );
92 discOpenPanel = new DiscOpenPanel( ui.Tab, p_intf );
93 netOpenPanel = new NetOpenPanel( ui.Tab, p_intf );
94 captureOpenPanel = new CaptureOpenPanel( ui.Tab, p_intf );
97 ui.Tab->insertTab( OPEN_FILE_TAB, fileOpenPanel, qtr( "&File" ) );
98 ui.Tab->insertTab( OPEN_DISC_TAB, discOpenPanel, qtr( "&Disc" ) );
99 ui.Tab->insertTab( OPEN_NETWORK_TAB, netOpenPanel, qtr( "&Network" ) );
100 ui.Tab->insertTab( OPEN_CAPTURE_TAB, captureOpenPanel,
101 qtr( "Capture &Device" ) );
103 /* Hide the Slave input widgets */
104 ui.slaveLabel->hide();
105 ui.slaveText->hide();
106 ui.slaveBrowseButton->hide();
108 /* Buttons Creation */
110 playButton = ui.playButton;
113 cancelButton = new QPushButton( qtr( "&Cancel" ) );
116 selectButton = new QPushButton( qtr( "&Select" ) );
118 /* Menu for the Play button */
119 QMenu * openButtonMenu = new QMenu( "Open" );
120 openButtonMenu->addAction( qtr( "&Enqueue" ), this, SLOT( enqueue() ),
121 QKeySequence( "Alt+E" ) );
122 openButtonMenu->addAction( qtr( "&Play" ), this, SLOT( play() ),
123 QKeySequence( "Alt+P" ) );
124 openButtonMenu->addAction( qtr( "&Stream" ), this, SLOT( stream() ) ,
125 QKeySequence( "Alt+S" ) );
126 openButtonMenu->addAction( qtr( "&Convert" ), this, SLOT( transcode() ) ,
127 QKeySequence( "Alt+C" ) );
129 ui.menuButton->setMenu( openButtonMenu );
131 /* Add the three Buttons */
132 ui.buttonsBox->addButton( selectButton, QDialogButtonBox::AcceptRole );
133 ui.buttonsBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
135 /* At creation time, modify the default buttons */
138 /* Force MRL update on tab change */
139 CONNECT( ui.Tab, currentChanged( int ), this, signalCurrent( int ) );
141 CONNECT( fileOpenPanel, mrlUpdated( QStringList, QString ),
142 this, updateMRL( QStringList, QString ) );
143 CONNECT( netOpenPanel, mrlUpdated( QStringList, QString ),
144 this, updateMRL( QStringList, QString ) );
145 CONNECT( discOpenPanel, mrlUpdated( QStringList, QString ),
146 this, updateMRL( QStringList, QString ) );
147 CONNECT( captureOpenPanel, mrlUpdated( QStringList, QString ),
148 this, updateMRL( QStringList, QString ) );
150 CONNECT( fileOpenPanel, methodChanged( QString ),
151 this, newCachingMethod( QString ) );
152 CONNECT( netOpenPanel, methodChanged( QString ),
153 this, newCachingMethod( QString ) );
154 CONNECT( discOpenPanel, methodChanged( QString ),
155 this, newCachingMethod( QString ) );
156 CONNECT( captureOpenPanel, methodChanged( QString ),
157 this, newCachingMethod( QString ) );
159 /* Advanced frame Connects */
160 CONNECT( ui.slaveCheckbox, toggled( bool ), this, updateMRL() );
161 CONNECT( ui.slaveText, textChanged( QString ), this, updateMRL() );
162 CONNECT( ui.cacheSpinBox, valueChanged( int ), this, updateMRL() );
163 CONNECT( ui.startTimeSpinBox, valueChanged( int ), this, updateMRL() );
164 BUTTONACT( ui.advancedCheckBox, toggleAdvancedPanel() );
165 BUTTONACT( ui.slaveBrowseButton, browseInputSlave() );
168 BUTTONACT( playButton, selectSlots() );
169 BUTTONACT( selectButton, close() );
170 BUTTONACT( cancelButton, cancel() );
172 /* Hide the advancedPanel */
173 if( !config_GetInt( p_intf, "qt-adv-options" ) )
174 ui.advancedFrame->hide();
176 ui.advancedCheckBox->setChecked( true );
178 /* Initialize caching */
180 newCachingMethod( "file-caching" );
182 resize( getSettings()->value( "opendialog-size", QSize( 400, 490 ) ).toSize() );
185 OpenDialog::~OpenDialog()
187 getSettings()->setValue( "opendialog-size", size() );
190 /* Used by VLM dialog and inputSlave selection */
191 QString OpenDialog::getMRL( bool b_all )
193 return b_all ? itemsMRL[0] + ui.advancedLineInput->text()
197 /* Finish the dialog and decide if you open another one after */
198 void OpenDialog::setMenuAction()
200 if( i_action_flag == SELECT )
203 selectButton->show();
204 selectButton->setDefault( true );
208 switch ( i_action_flag )
210 case OPEN_AND_STREAM:
211 playButton->setText( qtr( "&Stream" ) );
214 playButton->setText( qtr( "&Convert / Save" ) );
216 case OPEN_AND_ENQUEUE:
217 playButton->setText( qtr( "&Enqueue" ) );
221 playButton->setText( qtr( "&Play" ) );
224 selectButton->hide();
225 playButton->setDefault( true );
229 void OpenDialog::showTab( int i_tab )
231 if( i_tab == OPEN_CAPTURE_TAB ) captureOpenPanel->initialize();
232 ui.Tab->setCurrentIndex( i_tab );
236 /* Function called on signal currentChanged triggered */
237 void OpenDialog::signalCurrent( int i_tab )
239 if( i_tab == OPEN_CAPTURE_TAB ) captureOpenPanel->initialize();
241 if( ui.Tab->currentWidget() != NULL )
242 ( dynamic_cast<OpenPanel *>( ui.Tab->currentWidget() ) )->updateMRL();
245 void OpenDialog::toggleAdvancedPanel()
247 if( ui.advancedFrame->isVisible() )
249 ui.advancedFrame->hide();
250 if( size().isValid() )
251 resize( size().width(), size().height()
252 - ui.advancedFrame->height() );
256 ui.advancedFrame->show();
257 if( size().isValid() )
258 resize( size().width(), size().height()
259 + ui.advancedFrame->height() );
266 /* If Cancel is pressed or escaped */
267 void OpenDialog::cancel()
269 /* Clear the panels */
270 for( int i = 0; i < OPEN_TAB_MAX; i++ )
271 dynamic_cast<OpenPanel*>( ui.Tab->widget( i ) )->clear();
273 /* Clear the variables */
277 /* If in Select Mode, reject instead of hiding */
278 if( i_action_flag == SELECT ) reject();
282 /* If EnterKey is pressed */
283 void OpenDialog::close()
285 /* If in Select Mode, accept instead of selecting a Slot */
286 if( i_action_flag == SELECT )
293 void OpenDialog::selectSlots()
295 switch ( i_action_flag )
297 case OPEN_AND_STREAM:
303 case OPEN_AND_ENQUEUE:
312 void OpenDialog::play()
317 void OpenDialog::enqueue()
323 void OpenDialog::finish( bool b_enqueue = false )
327 if( i_action_flag == SELECT )
333 /* Go through the item list */
334 for( int i = 0; i < itemsMRL.size(); i++ )
336 bool b_start = !i && !b_enqueue;
338 input_item_t *p_input;
339 p_input = input_item_New( p_intf, qtu( itemsMRL[i] ), NULL );
341 /* Insert options only for the first element.
342 We don't know how to edit that anyway. */
345 /* Take options from the UI, not from what we stored */
346 QStringList optionsList = ui.advancedLineInput->text().split( ":" );
349 for( int j = 0; j < optionsList.size(); j++ )
351 QString qs = optionsList[j].trimmed();
354 input_item_AddOption( p_input, qtu( qs ), VLC_INPUT_OPTION_TRUSTED );
355 // msg_Err( p_intf, "Here %s", qtu( qs ));
360 /* Switch between enqueuing and starting the item */
361 /* FIXME: playlist_AddInput() can fail */
362 playlist_AddInput( THEPL, p_input,
363 PLAYLIST_APPEND | ( b_start ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
364 PLAYLIST_END, b_pl ? true : false, pl_Unlocked );
365 vlc_gc_decref( p_input );
367 /* Do not add the current MRL if playlist_AddInput fail */
368 RecentsMRL::getInstance( p_intf )->addRecent( itemsMRL[i] );
372 void OpenDialog::transcode()
377 void OpenDialog::stream( bool b_transcode_only )
379 QString soutMRL = getMRL();
382 /* Dbg and send :D */
383 msg_Dbg( p_intf, "MRL passed to the Sout: %s", qtu( soutMRL ) );
384 THEDP->streamingDialog( this, soutMRL, b_transcode_only );
388 void OpenDialog::updateMRL( QStringList item, QString tempMRL )
390 optionsMRL = tempMRL;
395 void OpenDialog::updateMRL() {
396 QString mrl = optionsMRL;
397 if( ui.slaveCheckbox->isChecked() ) {
398 mrl += " :input-slave=" + ui.slaveText->text();
400 int i_cache = config_GetInt( p_intf, qtu( storedMethod ) );
401 if( i_cache != ui.cacheSpinBox->value() ) {
402 mrl += QString( " :%1=%2" ).arg( storedMethod ).
403 arg( ui.cacheSpinBox->value() );
405 if( ui.startTimeSpinBox->value() ) {
406 mrl += " :start-time=" + QString( "%1" ).
407 arg( ui.startTimeSpinBox->value() );
409 ui.advancedLineInput->setText( mrl );
410 ui.mrlLine->setText( itemsMRL.join( " " ) );
413 void OpenDialog::newCachingMethod( QString method )
415 if( method != storedMethod ) {
416 storedMethod = method;
417 int i_value = config_GetInt( p_intf, qtu( storedMethod ) );
418 ui.cacheSpinBox->setValue( i_value );
422 QStringList OpenDialog::SeparateEntries( QString entries )
424 bool b_quotes_mode = false;
426 QStringList entries_array;
430 while( index < entries.size() )
432 int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
433 if( delim_pos < 0 ) delim_pos = entries.size() - 1;
434 entry += entries.mid( index, delim_pos - index + 1 );
435 index = delim_pos + 1;
437 if( entry.isEmpty() ) continue;
439 if( !b_quotes_mode && entry.endsWith( "\"" ) )
441 /* Enters quotes mode */
442 entry.truncate( entry.size() - 1 );
443 b_quotes_mode = true;
445 else if( b_quotes_mode && entry.endsWith( "\"" ) )
447 /* Finished the quotes mode */
448 entry.truncate( entry.size() - 1 );
449 b_quotes_mode = false;
451 else if( !b_quotes_mode && !entry.endsWith( "\"" ) )
453 /* we found a non-quoted standalone string */
454 if( index < entries.size() ||
455 entry.endsWith( " " ) || entry.endsWith( "\t" ) ||
456 entry.endsWith( "\r" ) || entry.endsWith( "\n" ) )
457 entry.truncate( entry.size() - 1 );
458 if( !entry.isEmpty() ) entries_array.append( entry );
465 if( !entry.isEmpty() ) entries_array.append( entry );
467 return entries_array;
470 void OpenDialog::browseInputSlave()
472 OpenDialog *od = new OpenDialog( this, p_intf, true, SELECT );
474 ui.slaveText->setText( od->getMRL( false ) );