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 ui.menuButton->setMenu( openButtonMenu );
127 ui.menuButton->setIcon( QIcon( ":/down_arrow" ) );
129 /* Add the three Buttons */
130 ui.buttonsBox->addButton( selectButton, QDialogButtonBox::AcceptRole );
131 ui.buttonsBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
133 /* At creation time, modify the default buttons */
136 /* Force MRL update on tab change */
137 CONNECT( ui.Tab, currentChanged( int ), this, signalCurrent( int ) );
139 CONNECT( fileOpenPanel, mrlUpdated( const QStringList&, const QString& ),
140 this, updateMRL( const QStringList&, const QString& ) );
141 CONNECT( netOpenPanel, mrlUpdated( const QStringList&, const QString& ),
142 this, updateMRL( const QStringList&, const QString& ) );
143 CONNECT( discOpenPanel, mrlUpdated( const QStringList&, const QString& ),
144 this, updateMRL( const QStringList&, const QString& ) );
145 CONNECT( captureOpenPanel, mrlUpdated( const QStringList&, const QString& ),
146 this, updateMRL( const QStringList&, const QString& ) );
148 CONNECT( fileOpenPanel, methodChanged( const QString& ),
149 this, newCachingMethod( const QString& ) );
150 CONNECT( netOpenPanel, methodChanged( const QString& ),
151 this, newCachingMethod( const QString& ) );
152 CONNECT( discOpenPanel, methodChanged( const QString& ),
153 this, newCachingMethod( const QString& ) );
154 CONNECT( captureOpenPanel, methodChanged( const QString& ),
155 this, newCachingMethod( const QString& ) );
157 /* Advanced frame Connects */
158 CONNECT( ui.slaveCheckbox, toggled( bool ), this, updateMRL() );
159 CONNECT( ui.slaveText, textChanged( const QString& ), this, updateMRL() );
160 CONNECT( ui.cacheSpinBox, valueChanged( int ), this, updateMRL() );
161 CONNECT( ui.startTimeDoubleSpinBox, valueChanged( double ), this, updateMRL() );
162 BUTTONACT( ui.advancedCheckBox, toggleAdvancedPanel() );
163 BUTTONACT( ui.slaveBrowseButton, browseInputSlave() );
166 BUTTONACT( playButton, selectSlots() );
167 BUTTONACT( selectButton, close() );
168 BUTTONACT( cancelButton, cancel() );
170 /* Hide the advancedPanel */
171 if( !config_GetInt( p_intf, "qt-adv-options" ) )
172 ui.advancedFrame->hide();
174 ui.advancedCheckBox->setChecked( true );
176 /* Initialize caching */
178 newCachingMethod( "file-caching" );
180 setMinimumSize( sizeHint() );
181 setMaximumWidth( 900 );
182 resize( getSettings()->value( "opendialog-size", QSize( 500, 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 char* psz_uri = make_URI( qtu( itemsMRL[i] ) );
344 p_input = input_item_New( p_intf, psz_uri, NULL );
347 /* Insert options only for the first element.
348 We don't know how to edit that anyway. */
351 /* Take options from the UI, not from what we stored */
352 QStringList optionsList = ui.advancedLineInput->text().split( " :" );
355 for( int j = 0; j < optionsList.size(); j++ )
357 QString qs = colon_unescape( optionsList[j] );
360 input_item_AddOption( p_input, qtu( qs ),
361 VLC_INPUT_OPTION_TRUSTED );
363 msg_Warn( p_intf, "Input option: %s", qtu( qs ) );
369 /* Switch between enqueuing and starting the item */
370 /* FIXME: playlist_AddInput() can fail */
371 playlist_AddInput( THEPL, p_input,
372 PLAYLIST_APPEND | ( b_start ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
373 PLAYLIST_END, b_pl ? true : false, pl_Unlocked );
374 vlc_gc_decref( p_input );
376 /* Do not add the current MRL if playlist_AddInput fail */
377 RecentsMRL::getInstance( p_intf )->addRecent( itemsMRL[i] );
381 void OpenDialog::transcode()
386 void OpenDialog::stream( bool b_transcode_only )
388 QString soutMRL = getMRL( false );
389 if( soutMRL.isEmpty() ) return;
392 /* Dbg and send :D */
393 msg_Dbg( p_intf, "MRL passed to the Sout: %s", qtu( soutMRL ) );
394 THEDP->streamingDialog( this, soutMRL, b_transcode_only,
395 ui.advancedLineInput->text().split( " :" ) );
399 void OpenDialog::updateMRL( const QStringList& item, const QString& tempMRL )
401 optionsMRL = tempMRL;
406 void OpenDialog::updateMRL() {
407 QString mrl = optionsMRL;
408 if( ui.slaveCheckbox->isChecked() ) {
409 mrl += " :input-slave=" + ui.slaveText->text();
411 int i_cache = config_GetInt( p_intf, qtu( storedMethod ) );
412 if( i_cache != ui.cacheSpinBox->value() ) {
413 mrl += QString( " :%1=%2" ).arg( storedMethod ).
414 arg( ui.cacheSpinBox->value() );
416 if( ui.startTimeDoubleSpinBox->value() ) {
417 mrl += " :start-time=" + QString::number( ui.startTimeDoubleSpinBox->value() );
419 ui.advancedLineInput->setText( mrl );
420 ui.mrlLine->setText( itemsMRL.join( " " ) );
423 void OpenDialog::newCachingMethod( const QString& method )
425 if( method != storedMethod ) {
426 storedMethod = method;
427 int i_value = config_GetInt( p_intf, qtu( storedMethod ) );
428 ui.cacheSpinBox->setValue( i_value );
432 QStringList OpenDialog::SeparateEntries( const QString& entries )
434 bool b_quotes_mode = false;
436 QStringList entries_array;
440 while( index < entries.size() )
442 int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
443 if( delim_pos < 0 ) delim_pos = entries.size() - 1;
444 entry += entries.mid( index, delim_pos - index + 1 );
445 index = delim_pos + 1;
447 if( entry.isEmpty() ) continue;
449 if( !b_quotes_mode && entry.endsWith( "\"" ) )
451 /* Enters quotes mode */
452 entry.truncate( entry.size() - 1 );
453 b_quotes_mode = true;
455 else if( b_quotes_mode && entry.endsWith( "\"" ) )
457 /* Finished the quotes mode */
458 entry.truncate( entry.size() - 1 );
459 b_quotes_mode = false;
461 else if( !b_quotes_mode && !entry.endsWith( "\"" ) )
463 /* we found a non-quoted standalone string */
464 if( index < entries.size() ||
465 entry.endsWith( " " ) || entry.endsWith( "\t" ) ||
466 entry.endsWith( "\r" ) || entry.endsWith( "\n" ) )
467 entry.truncate( entry.size() - 1 );
468 if( !entry.isEmpty() ) entries_array.append( entry );
475 if( !entry.isEmpty() ) entries_array.append( entry );
477 return entries_array;
480 void OpenDialog::browseInputSlave()
482 OpenDialog *od = new OpenDialog( this, p_intf, true, SELECT );
484 ui.slaveText->setText( od->getMRL( false ) );