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 if( b_selectMode ) /* Select mode */
71 setWindowModality( Qt::WindowModal );
72 i_action_flag = SELECT;
75 /* Basic Creation of the Window */
77 setWindowTitle( qtr( "Open" ) );
78 /* resize( 410, 600 ); */
79 setMinimumSize( 520, 460 );
81 /* Tab definition and creation */
82 fileOpenPanel = new FileOpenPanel( ui.Tab, p_intf );
83 discOpenPanel = new DiscOpenPanel( ui.Tab, p_intf );
84 netOpenPanel = new NetOpenPanel( ui.Tab, p_intf );
85 captureOpenPanel = new CaptureOpenPanel( ui.Tab, p_intf );
88 ui.Tab->insertTab( OPEN_FILE_TAB, fileOpenPanel, qtr( "&File" ) );
89 ui.Tab->insertTab( OPEN_DISC_TAB, discOpenPanel, qtr( "&Disc" ) );
90 ui.Tab->insertTab( OPEN_NETWORK_TAB, netOpenPanel, qtr( "&Network" ) );
91 ui.Tab->insertTab( OPEN_CAPTURE_TAB, captureOpenPanel,
92 qtr( "Capture &Device" ) );
94 /* Hide the Slave input widgets */
95 ui.slaveLabel->hide();
97 ui.slaveBrowseButton->hide();
99 /* Buttons Creation */
101 playButton = ui.playButton;
104 cancelButton = new QPushButton( qtr( "&Cancel" ) );
107 selectButton = new QPushButton( qtr( "&Select" ) );
109 /* Menu for the Play button */
110 QMenu * openButtonMenu = new QMenu( "Open" );
111 openButtonMenu->addAction( qtr( "&Enqueue" ), this, SLOT( enqueue() ),
112 QKeySequence( "Alt+E" ) );
113 openButtonMenu->addAction( qtr( "&Play" ), this, SLOT( play() ),
114 QKeySequence( "Alt+P" ) );
115 openButtonMenu->addAction( qtr( "&Stream" ), this, SLOT( stream() ) ,
116 QKeySequence( "Alt+S" ) );
117 openButtonMenu->addAction( qtr( "&Convert" ), this, SLOT( transcode() ) ,
118 QKeySequence( "Alt+C" ) );
120 ui.menuButton->setMenu( openButtonMenu );
122 /* Add the three Buttons */
123 ui.buttonsBox->addButton( selectButton, QDialogButtonBox::AcceptRole );
124 ui.buttonsBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
126 /* At creation time, modify the default buttons */
129 /* Force MRL update on tab change */
130 CONNECT( ui.Tab, currentChanged( int ), this, signalCurrent() );
132 CONNECT( fileOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
133 CONNECT( netOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
134 CONNECT( discOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
135 CONNECT( captureOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
137 CONNECT( fileOpenPanel, methodChanged( QString ),
138 this, newCachingMethod( QString ) );
139 CONNECT( netOpenPanel, methodChanged( QString ),
140 this, newCachingMethod( QString ) );
141 CONNECT( discOpenPanel, methodChanged( QString ),
142 this, newCachingMethod( QString ) );
143 CONNECT( captureOpenPanel, methodChanged( QString ),
144 this, newCachingMethod( QString ) );
146 /* Advanced frame Connects */
147 CONNECT( ui.slaveText, textChanged( QString ), this, updateMRL() );
148 CONNECT( ui.cacheSpinBox, valueChanged( int ), this, updateMRL() );
149 CONNECT( ui.startTimeSpinBox, valueChanged( int ), this, updateMRL() );
150 BUTTONACT( ui.advancedCheckBox, toggleAdvancedPanel() );
151 BUTTONACT( ui.slaveBrowseButton, browseInputSlave() );
154 BUTTONACT( playButton, selectSlots() );
155 BUTTONACT( selectButton, close() );
156 BUTTONACT( cancelButton, cancel() );
158 /* Hide the advancedPanel */
159 if( !config_GetInt( p_intf, "qt-adv-options" ) )
160 ui.advancedFrame->hide();
162 ui.advancedCheckBox->setChecked( true );
164 /* Initialize caching */
166 newCachingMethod( "file-caching" );
169 OpenDialog::~OpenDialog()
172 /* Finish the dialog and decide if you open another one after */
173 void OpenDialog::setMenuAction()
175 if( i_action_flag == SELECT )
178 selectButton->show();
179 selectButton->setDefault( true );
183 switch ( i_action_flag )
185 case OPEN_AND_STREAM:
186 playButton->setText( qtr( "&Stream" ) );
189 playButton->setText( qtr( "&Convert / Save" ) );
191 case OPEN_AND_ENQUEUE:
192 playButton->setText( qtr( "&Enqueue" ) );
196 playButton->setText( qtr( "&Play" ) );
199 selectButton->hide();
200 playButton->setDefault( true );
204 void OpenDialog::showTab( int i_tab )
206 ui.Tab->setCurrentIndex( i_tab );
210 /* Function called on signal currentChanged triggered */
211 void OpenDialog::signalCurrent()
213 if( ui.Tab->currentWidget() != NULL )
214 ( dynamic_cast<OpenPanel *>( ui.Tab->currentWidget() ) )->updateMRL();
217 void OpenDialog::toggleAdvancedPanel()
219 if( ui.advancedFrame->isVisible() )
221 ui.advancedFrame->hide();
222 //setMinimumSize( 520, 460 );
223 if( size().isValid() )
224 resize( size().width(), size().height()
225 - ui.advancedFrame->height() );
229 ui.advancedFrame->show();
230 //setMinimumSize( 520, 460 + ui.advancedFrame->height() );
231 if( size().isValid() )
232 resize( size().width(), size().height()
233 + ui.advancedFrame->height() );
240 /* If Cancel is pressed or escaped */
241 void OpenDialog::cancel()
243 /* Clear the panels */
244 for( int i = 0; i < OPEN_TAB_MAX; i++ )
245 dynamic_cast<OpenPanel*>( ui.Tab->widget( i ) )->clear();
247 /* Clear the variables */
251 /* If in Select Mode, reject instead of hiding */
252 if( i_action_flag == SELECT ) reject();
256 /* If EnterKey is pressed */
257 void OpenDialog::close()
259 /* If in Select Mode, accept instead of selecting a Slot */
260 if( i_action_flag == SELECT )
267 void OpenDialog::selectSlots()
269 switch ( i_action_flag )
271 case OPEN_AND_STREAM:
277 case OPEN_AND_ENQUEUE:
286 void OpenDialog::play()
291 void OpenDialog::enqueue()
297 void OpenDialog::finish( bool b_enqueue = false )
300 mrl = ui.advancedLineInput->text();
302 if( i_action_flag != SELECT )
304 QStringList tempMRL = SeparateEntries( mrl );
305 for( size_t i = 0; i < tempMRL.size(); i++ )
307 bool b_start = !i && !b_enqueue;
308 input_item_t *p_input;
310 p_input = input_ItemNew( p_intf, qtu( tempMRL[i] ), NULL );
313 while( i + 1 < tempMRL.size() && tempMRL[i + 1].startsWith( ":" ) )
316 input_ItemAddOption( p_input, qtu( tempMRL[i] ) );
319 /* Switch between enqueuing and starting the item */
320 /* FIXME: playlist_AddInput() can fail */
321 playlist_AddInput( THEPL, p_input,
322 PLAYLIST_APPEND | ( b_start ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
323 PLAYLIST_END, true, pl_Unlocked );
324 vlc_gc_decref( p_input );
331 void OpenDialog::transcode()
336 void OpenDialog::stream( bool b_transcode_only )
338 mrl = ui.advancedLineInput->text();
340 QStringList listMRL = SeparateEntries( mrl );
341 if( listMRL.size() > 0 )
342 THEDP->streamingDialog( this, SeparateEntries( mrl )[0], b_transcode_only );
346 void OpenDialog::updateMRL( QString tempMRL )
352 void OpenDialog::updateMRL() {
354 if( ui.slaveCheckbox->isChecked() ) {
355 mrl += " :input-slave=" + ui.slaveText->text();
357 int i_cache = config_GetInt( p_intf, qta( storedMethod ) );
358 if( i_cache != ui.cacheSpinBox->value() ) {
359 mrl += QString( " :%1=%2" ).arg( storedMethod ).
360 arg( ui.cacheSpinBox->value() );
362 if( ui.startTimeSpinBox->value() ) {
363 mrl += " :start-time=" + QString( "%1" ).
364 arg( ui.startTimeSpinBox->value() );
366 ui.advancedLineInput->setText( mrl );
369 void OpenDialog::newCachingMethod( QString method )
371 if( method != storedMethod ) {
372 storedMethod = method;
373 int i_value = config_GetInt( p_intf, qta( storedMethod ) );
374 ui.cacheSpinBox->setValue( i_value );
378 QStringList OpenDialog::SeparateEntries( QString entries )
380 bool b_quotes_mode = false;
382 QStringList entries_array;
386 while( index < entries.size() )
388 int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
389 if( delim_pos < 0 ) delim_pos = entries.size() - 1;
390 entry += entries.mid( index, delim_pos - index + 1 );
391 index = delim_pos + 1;
393 if( entry.isEmpty() ) continue;
395 if( !b_quotes_mode && entry.endsWith( "\"" ) )
397 /* Enters quotes mode */
398 entry.truncate( entry.size() - 1 );
399 b_quotes_mode = true;
401 else if( b_quotes_mode && entry.endsWith( "\"" ) )
403 /* Finished the quotes mode */
404 entry.truncate( entry.size() - 1 );
405 b_quotes_mode = false;
407 else if( !b_quotes_mode && !entry.endsWith( "\"" ) )
409 /* we found a non-quoted standalone string */
410 if( index < entries.size() ||
411 entry.endsWith( " " ) || entry.endsWith( "\t" ) ||
412 entry.endsWith( "\r" ) || entry.endsWith( "\n" ) )
413 entry.truncate( entry.size() - 1 );
414 if( !entry.isEmpty() ) entries_array.append( entry );
421 if( !entry.isEmpty() ) entries_array.append( entry );
423 return entries_array;
426 void OpenDialog::browseInputSlave()
428 OpenDialog *od = new OpenDialog( this, p_intf, true, SELECT );
430 ui.slaveText->setText( od->getMRL() );