1 /*****************************************************************************
2 * open.cpp : Advanced open dialog
3 *****************************************************************************
4 * Copyright © 2006-2007 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 *****************************************************************************/
27 #include "input_manager.hpp"
29 #include "dialogs/open.hpp"
32 #include <QGridLayout>
33 #include <QFileDialog>
37 OpenDialog *OpenDialog::instance = NULL;
39 OpenDialog* OpenDialog::getInstance( QWidget *parent, intf_thread_t *p_intf,
40 bool b_rawInstance, int _action_flag, bool b_selectMode )
44 instance = new OpenDialog( parent, p_intf, b_selectMode, _action_flag );
45 else if( !b_rawInstance )
47 /* Request the instance but change small details:
49 - Modality on top of the parent dialog */
52 instance->setWindowModality( Qt::WindowModal );
53 _action_flag = SELECT; /* This should be useless, but we never know
54 if the call is correct */
56 instance->i_action_flag = _action_flag;
57 instance->setMenuAction();
62 OpenDialog::OpenDialog( QWidget *parent,
63 intf_thread_t *_p_intf,
65 int _action_flag ) : QVLCDialog( parent, _p_intf )
67 i_action_flag = _action_flag;
69 /* Workaround the Win32 Vout that put the video on top at regular times */
71 setWindowFlags( Qt::WindowStaysOnTopHint | Qt::Dialog );
74 if( b_selectMode ) /* Select mode */
76 i_action_flag = SELECT;
77 setWindowModality( Qt::WindowModal );
80 /* Basic Creation of the Window */
82 setWindowTitle( qtr( "Open" ) );
83 /* resize( 410, 600 ); */
84 setMinimumSize( 520, 490 );
86 /* Tab definition and creation */
87 fileOpenPanel = new FileOpenPanel( ui.Tab, p_intf );
88 discOpenPanel = new DiscOpenPanel( ui.Tab, p_intf );
89 netOpenPanel = new NetOpenPanel( ui.Tab, p_intf );
90 captureOpenPanel = new CaptureOpenPanel( ui.Tab, p_intf );
93 ui.Tab->insertTab( OPEN_FILE_TAB, fileOpenPanel, qtr( "&File" ) );
94 ui.Tab->insertTab( OPEN_DISC_TAB, discOpenPanel, qtr( "&Disc" ) );
95 ui.Tab->insertTab( OPEN_NETWORK_TAB, netOpenPanel, qtr( "&Network" ) );
96 ui.Tab->insertTab( OPEN_CAPTURE_TAB, captureOpenPanel,
97 qtr( "Capture &Device" ) );
99 /* Hide the Slave input widgets */
100 ui.slaveLabel->hide();
101 ui.slaveText->hide();
102 ui.slaveBrowseButton->hide();
104 /* Buttons Creation */
106 playButton = ui.playButton;
109 cancelButton = new QPushButton( qtr( "&Cancel" ) );
112 selectButton = new QPushButton( qtr( "&Select" ) );
114 /* Menu for the Play button */
115 QMenu * openButtonMenu = new QMenu( "Open" );
116 openButtonMenu->addAction( qtr( "&Enqueue" ), this, SLOT( enqueue() ),
117 QKeySequence( "Alt+E" ) );
118 openButtonMenu->addAction( qtr( "&Play" ), this, SLOT( play() ),
119 QKeySequence( "Alt+P" ) );
120 openButtonMenu->addAction( qtr( "&Stream" ), this, SLOT( stream() ) ,
121 QKeySequence( "Alt+S" ) );
122 openButtonMenu->addAction( qtr( "&Convert" ), this, SLOT( transcode() ) ,
123 QKeySequence( "Alt+C" ) );
125 ui.menuButton->setMenu( openButtonMenu );
127 /* Add the three Buttons */
128 ui.buttonsBox->addButton( selectButton, QDialogButtonBox::AcceptRole );
129 ui.buttonsBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
131 /* At creation time, modify the default buttons */
134 /* Force MRL update on tab change */
135 CONNECT( ui.Tab, currentChanged( int ), this, signalCurrent( int ) );
137 CONNECT( fileOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
138 CONNECT( netOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
139 CONNECT( discOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
140 CONNECT( captureOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
142 CONNECT( fileOpenPanel, methodChanged( QString ),
143 this, newCachingMethod( QString ) );
144 CONNECT( netOpenPanel, methodChanged( QString ),
145 this, newCachingMethod( QString ) );
146 CONNECT( discOpenPanel, methodChanged( QString ),
147 this, newCachingMethod( QString ) );
148 CONNECT( captureOpenPanel, methodChanged( QString ),
149 this, newCachingMethod( QString ) );
151 /* Advanced frame Connects */
152 CONNECT( ui.slaveText, textChanged( QString ), this, updateMRL() );
153 CONNECT( ui.cacheSpinBox, valueChanged( int ), this, updateMRL() );
154 CONNECT( ui.startTimeSpinBox, valueChanged( int ), this, updateMRL() );
155 BUTTONACT( ui.advancedCheckBox, toggleAdvancedPanel() );
156 BUTTONACT( ui.slaveBrowseButton, browseInputSlave() );
159 BUTTONACT( playButton, selectSlots() );
160 BUTTONACT( selectButton, close() );
161 BUTTONACT( cancelButton, cancel() );
163 /* Hide the advancedPanel */
164 if( !config_GetInt( p_intf, "qt-adv-options" ) )
165 ui.advancedFrame->hide();
167 ui.advancedCheckBox->setChecked( true );
169 /* Initialize caching */
171 newCachingMethod( "file-caching" );
173 resize( getSettings()->value( "opendialog-size", QSize( 520, 490 ) ).toSize() );
176 OpenDialog::~OpenDialog()
178 getSettings()->setValue( "opendialog-size", size() );
181 /* Finish the dialog and decide if you open another one after */
182 void OpenDialog::setMenuAction()
184 if( i_action_flag == SELECT )
187 selectButton->show();
188 selectButton->setDefault( true );
192 switch ( i_action_flag )
194 case OPEN_AND_STREAM:
195 playButton->setText( qtr( "&Stream" ) );
198 playButton->setText( qtr( "&Convert / Save" ) );
200 case OPEN_AND_ENQUEUE:
201 playButton->setText( qtr( "&Enqueue" ) );
205 playButton->setText( qtr( "&Play" ) );
208 selectButton->hide();
209 playButton->setDefault( true );
213 void OpenDialog::showTab( int i_tab )
215 if( i_tab == OPEN_CAPTURE_TAB ) captureOpenPanel->initialize();
216 ui.Tab->setCurrentIndex( i_tab );
220 /* Function called on signal currentChanged triggered */
221 void OpenDialog::signalCurrent( int i_tab )
223 if( i_tab == OPEN_CAPTURE_TAB ) captureOpenPanel->initialize();
225 if( ui.Tab->currentWidget() != NULL )
226 ( dynamic_cast<OpenPanel *>( ui.Tab->currentWidget() ) )->updateMRL();
229 void OpenDialog::toggleAdvancedPanel()
231 if( ui.advancedFrame->isVisible() )
233 ui.advancedFrame->hide();
234 //setMinimumSize( 520, 460 );
235 if( size().isValid() )
236 resize( size().width(), size().height()
237 - ui.advancedFrame->height() );
241 ui.advancedFrame->show();
242 //setMinimumSize( 520, 460 + ui.advancedFrame->height() );
243 if( size().isValid() )
244 resize( size().width(), size().height()
245 + ui.advancedFrame->height() );
252 /* If Cancel is pressed or escaped */
253 void OpenDialog::cancel()
255 /* Clear the panels */
256 for( int i = 0; i < OPEN_TAB_MAX; i++ )
257 dynamic_cast<OpenPanel*>( ui.Tab->widget( i ) )->clear();
259 /* Clear the variables */
263 /* If in Select Mode, reject instead of hiding */
264 if( i_action_flag == SELECT ) reject();
268 /* If EnterKey is pressed */
269 void OpenDialog::close()
271 /* If in Select Mode, accept instead of selecting a Slot */
272 if( i_action_flag == SELECT )
279 void OpenDialog::selectSlots()
281 switch ( i_action_flag )
283 case OPEN_AND_STREAM:
289 case OPEN_AND_ENQUEUE:
298 void OpenDialog::play()
303 void OpenDialog::enqueue()
309 void OpenDialog::finish( bool b_enqueue = false )
312 mrl = ui.advancedLineInput->text();
314 if( i_action_flag != SELECT )
316 QStringList tempMRL = SeparateEntries( mrl );
317 for( size_t i = 0; i < tempMRL.size(); i++ )
319 bool b_start = !i && !b_enqueue;
320 input_item_t *p_input;
322 p_input = input_item_New( p_intf, qtu( tempMRL[i] ), NULL );
325 while( i + 1 < tempMRL.size() && tempMRL[i + 1].startsWith( ":" ) )
328 input_item_AddOption( p_input, qtu( tempMRL[i] ) );
331 /* Switch between enqueuing and starting the item */
332 /* FIXME: playlist_AddInput() can fail */
333 playlist_AddInput( THEPL, p_input,
334 PLAYLIST_APPEND | ( b_start ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
335 PLAYLIST_END, true, pl_Unlocked );
336 vlc_gc_decref( p_input );
343 void OpenDialog::transcode()
348 void OpenDialog::stream( bool b_transcode_only )
350 mrl = ui.advancedLineInput->text();
352 QStringList listMRL = SeparateEntries( mrl );
353 if( listMRL.size() > 0 )
354 THEDP->streamingDialog( this, listMRL[0], b_transcode_only );
358 void OpenDialog::updateMRL( QString tempMRL )
364 void OpenDialog::updateMRL() {
366 if( ui.slaveCheckbox->isChecked() ) {
367 mrl += " :input-slave=" + ui.slaveText->text();
369 int i_cache = config_GetInt( p_intf, qta( storedMethod ) );
370 if( i_cache != ui.cacheSpinBox->value() ) {
371 mrl += QString( " :%1=%2" ).arg( storedMethod ).
372 arg( ui.cacheSpinBox->value() );
374 if( ui.startTimeSpinBox->value() ) {
375 mrl += " :start-time=" + QString( "%1" ).
376 arg( ui.startTimeSpinBox->value() );
378 ui.advancedLineInput->setText( mrl );
381 void OpenDialog::newCachingMethod( QString method )
383 if( method != storedMethod ) {
384 storedMethod = method;
385 int i_value = config_GetInt( p_intf, qta( storedMethod ) );
386 ui.cacheSpinBox->setValue( i_value );
390 QStringList OpenDialog::SeparateEntries( QString entries )
392 bool b_quotes_mode = false;
394 QStringList entries_array;
398 while( index < entries.size() )
400 int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
401 if( delim_pos < 0 ) delim_pos = entries.size() - 1;
402 entry += entries.mid( index, delim_pos - index + 1 );
403 index = delim_pos + 1;
405 if( entry.isEmpty() ) continue;
407 if( !b_quotes_mode && entry.endsWith( "\"" ) )
409 /* Enters quotes mode */
410 entry.truncate( entry.size() - 1 );
411 b_quotes_mode = true;
413 else if( b_quotes_mode && entry.endsWith( "\"" ) )
415 /* Finished the quotes mode */
416 entry.truncate( entry.size() - 1 );
417 b_quotes_mode = false;
419 else if( !b_quotes_mode && !entry.endsWith( "\"" ) )
421 /* we found a non-quoted standalone string */
422 if( index < entries.size() ||
423 entry.endsWith( " " ) || entry.endsWith( "\t" ) ||
424 entry.endsWith( "\r" ) || entry.endsWith( "\n" ) )
425 entry.truncate( entry.size() - 1 );
426 if( !entry.isEmpty() ) entries_array.append( entry );
433 if( !entry.isEmpty() ) entries_array.append( entry );
435 return entries_array;
438 void OpenDialog::browseInputSlave()
440 OpenDialog *od = new OpenDialog( this, p_intf, true, SELECT );
442 ui.slaveText->setText( od->getMRL() );