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 int _action_flag, bool modal )
44 instance = new OpenDialog( parent, p_intf, modal, _action_flag );
47 /* Request the instance but change small details:
49 - Modality on top of the parent dialog */
50 instance->i_action_flag = _action_flag;
51 instance->setMenuAction();
52 if( modal ) instance->setWindowModality( Qt::WindowModal );
57 OpenDialog::OpenDialog( QWidget *parent,
58 intf_thread_t *_p_intf,
60 int _action_flag ) : QVLCDialog( parent, _p_intf )
62 i_action_flag = _action_flag;
64 if( modal ) /* Select mode */
66 setWindowModality( Qt::WindowModal );
67 i_action_flag = SELECT;
70 /* Basic Creation of the Window */
72 setWindowTitle( qtr( "Open" ) );
75 /* Tab definition and creation */
76 fileOpenPanel = new FileOpenPanel( ui.Tab, p_intf );
77 discOpenPanel = new DiscOpenPanel( ui.Tab, p_intf );
78 netOpenPanel = new NetOpenPanel( ui.Tab, p_intf );
79 captureOpenPanel = new CaptureOpenPanel( ui.Tab, p_intf );
82 ui.Tab->insertTab( OPEN_FILE_TAB, fileOpenPanel, qtr( "&File" ) );
83 ui.Tab->insertTab( OPEN_DISC_TAB, discOpenPanel, qtr( "&Disc" ) );
84 ui.Tab->insertTab( OPEN_NETWORK_TAB, netOpenPanel, qtr( "&Network" ) );
85 ui.Tab->insertTab( OPEN_CAPTURE_TAB, captureOpenPanel,
86 qtr( "Capture &Device" ) );
88 /* Hide the Slave input widgets */
89 ui.slaveLabel->hide();
91 ui.slaveBrowseButton->hide();
93 /* Buttons Creation */
95 playButton = ui.playButton;
98 cancelButton = new QPushButton( qtr( "&Cancel" ) );
101 selectButton = new QPushButton( qtr( "Select" ) );
103 /* Menu for the Play button */
104 QMenu * openButtonMenu = new QMenu( "Open" );
105 openButtonMenu->addAction( qtr( "&Enqueue" ), this, SLOT( enqueue() ),
106 QKeySequence( "Alt+E" ) );
107 openButtonMenu->addAction( qtr( "&Play" ), this, SLOT( play() ),
108 QKeySequence( "Alt+P" ) );
109 openButtonMenu->addAction( qtr( "&Stream" ), this, SLOT( stream() ) ,
110 QKeySequence( "Alt+S" ) );
111 openButtonMenu->addAction( qtr( "&Convert" ), this, SLOT( transcode() ) ,
112 QKeySequence( "Alt+C" ) );
114 ui.menuButton->setMenu( openButtonMenu );
116 /* Add the three Buttons */
117 ui.buttonsBox->addButton( selectButton, QDialogButtonBox::AcceptRole );
118 ui.buttonsBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
120 /* At creation time, modify the default buttons */
123 /* Force MRL update on tab change */
124 CONNECT( ui.Tab, currentChanged( int ), this, signalCurrent() );
126 CONNECT( fileOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
127 CONNECT( netOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
128 CONNECT( discOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
129 CONNECT( captureOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
131 CONNECT( fileOpenPanel, methodChanged( QString ),
132 this, newCachingMethod( QString ) );
133 CONNECT( netOpenPanel, methodChanged( QString ),
134 this, newCachingMethod( QString ) );
135 CONNECT( discOpenPanel, methodChanged( QString ),
136 this, newCachingMethod( QString ) );
137 CONNECT( captureOpenPanel, methodChanged( QString ),
138 this, newCachingMethod( QString ) );
140 /* Advanced frame Connects */
141 CONNECT( ui.slaveText, textChanged( QString ), this, updateMRL() );
142 CONNECT( ui.cacheSpinBox, valueChanged( int ), this, updateMRL() );
143 CONNECT( ui.startTimeSpinBox, valueChanged( int ), this, updateMRL() );
144 BUTTONACT( ui.advancedCheckBox , toggleAdvancedPanel() );
147 BUTTONACT( playButton, selectSlots() );
148 BUTTONACT( selectButton, close() );
149 BUTTONACT( cancelButton, cancel() );
151 /* Hide the advancedPanel */
152 if( !config_GetInt( p_intf, "qt-adv-options" ) )
153 ui.advancedFrame->hide();
155 ui.advancedCheckBox->setChecked( true );
157 /* Initialize caching */
159 newCachingMethod( "file-caching" );
162 OpenDialog::~OpenDialog()
165 /* Finish the dialog and decide if you open another one after */
166 void OpenDialog::setMenuAction()
168 if( i_action_flag == SELECT )
171 selectButton->show();
172 selectButton->setDefault( true );
176 switch ( i_action_flag )
178 case OPEN_AND_STREAM:
179 playButton->setText( qtr( "&Stream" ) );
182 playButton->setText( qtr( "&Convert / Save" ) );
184 case OPEN_AND_ENQUEUE:
185 playButton->setText( qtr( "&Enqueue" ) );
189 playButton->setText( qtr( "&Play" ) );
192 playButton->setDefault( true );
193 selectButton->hide();
197 void OpenDialog::showTab( int i_tab=0 )
199 ui.Tab->setCurrentIndex( i_tab );
203 /* Function called on signal currentChanged triggered */
204 void OpenDialog::signalCurrent()
206 if( ui.Tab->currentWidget() != NULL )
207 ( dynamic_cast<OpenPanel *>( ui.Tab->currentWidget() ) )->updateMRL();
210 void OpenDialog::toggleAdvancedPanel()
212 if( ui.advancedFrame->isVisible() )
214 ui.advancedFrame->hide();
215 //FIXME: Clear Bug here. Qt ?
216 if( size().isValid() )
217 resize( size().width(), size().height() - ui.advancedFrame->height() );
221 ui.advancedFrame->show();
228 /* If Cancel is pressed or escaped */
229 void OpenDialog::cancel()
231 /* Clear the panels */
232 for( int i = 0; i < OPEN_TAB_MAX; i++ )
233 dynamic_cast<OpenPanel*>( ui.Tab->widget( i ) )->clear();
235 /* Clear the variables */
239 /* If in Select Mode, reject instead of hiding */
240 if( windowModality() != Qt::NonModal ) reject();
244 /* If EnterKey is pressed */
245 void OpenDialog::close()
247 if( windowModality() != Qt::NonModal )
254 void OpenDialog::selectSlots()
256 switch ( i_action_flag )
258 case OPEN_AND_STREAM:
264 case OPEN_AND_ENQUEUE:
273 void OpenDialog::play()
278 void OpenDialog::enqueue()
284 void OpenDialog::finish( bool b_enqueue = false )
287 mrl = ui.advancedLineInput->text();
289 if( windowModality() == Qt::NonModal )
291 QStringList tempMRL = SeparateEntries( mrl );
292 for( size_t i = 0; i < tempMRL.size(); i++ )
294 bool b_start = !i && !b_enqueue;
295 input_item_t *p_input;
297 p_input = input_ItemNew( p_intf, qtu( tempMRL[i] ), NULL );
300 while( i + 1 < tempMRL.size() && tempMRL[i + 1].startsWith( ":" ) )
303 input_ItemAddOption( p_input, qtu( tempMRL[i] ) );
306 /* Switch between enqueuing and starting the item */
307 /* FIXME: playlist_AddInput() can fail */
308 playlist_AddInput( THEPL, p_input,
309 PLAYLIST_APPEND | ( b_start ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
310 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
317 void OpenDialog::transcode()
322 void OpenDialog::stream( bool b_transcode_only )
324 mrl = ui.advancedLineInput->text();
326 THEDP->streamingDialog( this, mrl, b_transcode_only );
330 void OpenDialog::updateMRL( QString tempMRL )
336 void OpenDialog::updateMRL() {
338 if( ui.slaveCheckbox->isChecked() ) {
339 mrl += " :input-slave=" + ui.slaveText->text();
341 int i_cache = config_GetInt( p_intf, qta( storedMethod ) );
342 if( i_cache != ui.cacheSpinBox->value() ) {
343 mrl += QString( " :%1=%2" ).arg( storedMethod ).
344 arg( ui.cacheSpinBox->value() );
346 if( ui.startTimeSpinBox->value() ) {
347 mrl += " :start-time=" + QString( "%1" ).
348 arg( ui.startTimeSpinBox->value() );
350 ui.advancedLineInput->setText( mrl );
353 void OpenDialog::newCachingMethod( QString method )
355 if( method != storedMethod ) {
356 storedMethod = method;
357 int i_value = config_GetInt( p_intf, qta( storedMethod ) );
358 ui.cacheSpinBox->setValue( i_value );
362 QStringList OpenDialog::SeparateEntries( QString entries )
364 bool b_quotes_mode = false;
366 QStringList entries_array;
370 while( index < entries.size() )
372 int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
373 if( delim_pos < 0 ) delim_pos = entries.size() - 1;
374 entry += entries.mid( index, delim_pos - index + 1 );
375 index = delim_pos + 1;
377 if( entry.isEmpty() ) continue;
379 if( !b_quotes_mode && entry.endsWith( "\"" ) )
381 /* Enters quotes mode */
382 entry.truncate( entry.size() - 1 );
383 b_quotes_mode = true;
385 else if( b_quotes_mode && entry.endsWith( "\"" ) )
387 /* Finished the quotes mode */
388 entry.truncate( entry.size() - 1 );
389 b_quotes_mode = false;
391 else if( !b_quotes_mode && !entry.endsWith( "\"" ) )
393 /* we found a non-quoted standalone string */
394 if( index < entries.size() ||
395 entry.endsWith( " " ) || entry.endsWith( "\t" ) ||
396 entry.endsWith( "\r" ) || entry.endsWith( "\n" ) )
397 entry.truncate( entry.size() - 1 );
398 if( !entry.isEmpty() ) entries_array.append( entry );
405 if( !entry.isEmpty() ) entries_array.append( entry );
407 return entries_array;