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 if( itemsMRL.size() == 0 ) return "";
194 return b_all ? itemsMRL[0] + ui.advancedLineInput->text()
198 /* Finish the dialog and decide if you open another one after */
199 void OpenDialog::setMenuAction()
201 if( i_action_flag == SELECT )
204 selectButton->show();
205 selectButton->setDefault( true );
209 switch ( i_action_flag )
211 case OPEN_AND_STREAM:
212 playButton->setText( qtr( "&Stream" ) );
215 playButton->setText( qtr( "&Convert / Save" ) );
217 case OPEN_AND_ENQUEUE:
218 playButton->setText( qtr( "&Enqueue" ) );
222 playButton->setText( qtr( "&Play" ) );
225 selectButton->hide();
226 playButton->setDefault( true );
230 void OpenDialog::showTab( int i_tab )
232 if( i_tab == OPEN_CAPTURE_TAB ) captureOpenPanel->initialize();
233 ui.Tab->setCurrentIndex( i_tab );
237 /* Function called on signal currentChanged triggered */
238 void OpenDialog::signalCurrent( int i_tab )
240 if( i_tab == OPEN_CAPTURE_TAB ) captureOpenPanel->initialize();
242 if( ui.Tab->currentWidget() != NULL )
243 ( dynamic_cast<OpenPanel *>( ui.Tab->currentWidget() ) )->updateMRL();
246 void OpenDialog::toggleAdvancedPanel()
248 if( ui.advancedFrame->isVisible() )
250 ui.advancedFrame->hide();
251 if( size().isValid() )
252 resize( size().width(), size().height()
253 - ui.advancedFrame->height() );
257 ui.advancedFrame->show();
258 if( size().isValid() )
259 resize( size().width(), size().height()
260 + ui.advancedFrame->height() );
267 /* If Cancel is pressed or escaped */
268 void OpenDialog::cancel()
270 /* Clear the panels */
271 for( int i = 0; i < OPEN_TAB_MAX; i++ )
272 dynamic_cast<OpenPanel*>( ui.Tab->widget( i ) )->clear();
274 /* Clear the variables */
278 /* If in Select Mode, reject instead of hiding */
279 if( i_action_flag == SELECT ) reject();
283 /* If EnterKey is pressed */
284 void OpenDialog::close()
286 /* If in Select Mode, accept instead of selecting a Slot */
287 if( i_action_flag == SELECT )
294 void OpenDialog::selectSlots()
296 switch ( i_action_flag )
298 case OPEN_AND_STREAM:
304 case OPEN_AND_ENQUEUE:
313 void OpenDialog::play()
318 void OpenDialog::enqueue()
324 void OpenDialog::finish( bool b_enqueue = false )
328 if( i_action_flag == SELECT )
334 /* Sort alphabetically */
337 /* Go through the item list */
338 for( int i = 0; i < itemsMRL.size(); i++ )
340 bool b_start = !i && !b_enqueue;
342 input_item_t *p_input;
343 p_input = input_item_New( p_intf, qtu( itemsMRL[i] ), NULL );
345 /* Insert options only for the first element.
346 We don't know how to edit that anyway. */
349 /* Take options from the UI, not from what we stored */
350 QStringList optionsList = ui.advancedLineInput->text().split( ":" );
353 for( int j = 0; j < optionsList.size(); j++ )
355 QString qs = optionsList[j].trimmed();
358 input_item_AddOption( p_input, qtu( qs ), VLC_INPUT_OPTION_TRUSTED );
359 // msg_Err( p_intf, "Here %s", qtu( qs ));
364 /* Switch between enqueuing and starting the item */
365 /* FIXME: playlist_AddInput() can fail */
366 playlist_AddInput( THEPL, p_input,
367 PLAYLIST_APPEND | ( b_start ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
368 PLAYLIST_END, b_pl ? true : false, pl_Unlocked );
369 vlc_gc_decref( p_input );
371 /* Do not add the current MRL if playlist_AddInput fail */
372 RecentsMRL::getInstance( p_intf )->addRecent( itemsMRL[i] );
376 void OpenDialog::transcode()
381 void OpenDialog::stream( bool b_transcode_only )
383 QString soutMRL = getMRL();
384 if( soutMRL.isEmpty() ) return;
387 /* Dbg and send :D */
388 msg_Dbg( p_intf, "MRL passed to the Sout: %s", qtu( soutMRL ) );
389 THEDP->streamingDialog( this, soutMRL, b_transcode_only );
393 void OpenDialog::updateMRL( QStringList item, QString tempMRL )
395 optionsMRL = tempMRL;
400 void OpenDialog::updateMRL() {
401 QString mrl = optionsMRL;
402 if( ui.slaveCheckbox->isChecked() ) {
403 mrl += " :input-slave=" + ui.slaveText->text();
405 int i_cache = config_GetInt( p_intf, qtu( storedMethod ) );
406 if( i_cache != ui.cacheSpinBox->value() ) {
407 mrl += QString( " :%1=%2" ).arg( storedMethod ).
408 arg( ui.cacheSpinBox->value() );
410 if( ui.startTimeSpinBox->value() ) {
411 mrl += " :start-time=" + QString( "%1" ).
412 arg( ui.startTimeSpinBox->value() );
414 ui.advancedLineInput->setText( mrl );
415 ui.mrlLine->setText( itemsMRL.join( " " ) );
418 void OpenDialog::newCachingMethod( QString method )
420 if( method != storedMethod ) {
421 storedMethod = method;
422 int i_value = config_GetInt( p_intf, qtu( storedMethod ) );
423 ui.cacheSpinBox->setValue( i_value );
427 QStringList OpenDialog::SeparateEntries( QString entries )
429 bool b_quotes_mode = false;
431 QStringList entries_array;
435 while( index < entries.size() )
437 int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
438 if( delim_pos < 0 ) delim_pos = entries.size() - 1;
439 entry += entries.mid( index, delim_pos - index + 1 );
440 index = delim_pos + 1;
442 if( entry.isEmpty() ) continue;
444 if( !b_quotes_mode && entry.endsWith( "\"" ) )
446 /* Enters quotes mode */
447 entry.truncate( entry.size() - 1 );
448 b_quotes_mode = true;
450 else if( b_quotes_mode && entry.endsWith( "\"" ) )
452 /* Finished the quotes mode */
453 entry.truncate( entry.size() - 1 );
454 b_quotes_mode = false;
456 else if( !b_quotes_mode && !entry.endsWith( "\"" ) )
458 /* we found a non-quoted standalone string */
459 if( index < entries.size() ||
460 entry.endsWith( " " ) || entry.endsWith( "\t" ) ||
461 entry.endsWith( "\r" ) || entry.endsWith( "\n" ) )
462 entry.truncate( entry.size() - 1 );
463 if( !entry.isEmpty() ) entries_array.append( entry );
470 if( !entry.isEmpty() ) entries_array.append( entry );
472 return entries_array;
475 void OpenDialog::browseInputSlave()
477 OpenDialog *od = new OpenDialog( this, p_intf, true, SELECT );
479 ui.slaveText->setText( od->getMRL( false ) );