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 setWindowModality( Qt::WindowModal );
71 if( b_selectMode ) /* Select mode */
73 i_action_flag = SELECT;
76 /* Basic Creation of the Window */
78 setWindowTitle( qtr( "Open" ) );
79 /* resize( 410, 600 ); */
80 setMinimumSize( 520, 490 );
82 /* Tab definition and creation */
83 fileOpenPanel = new FileOpenPanel( ui.Tab, p_intf );
84 discOpenPanel = new DiscOpenPanel( ui.Tab, p_intf );
85 netOpenPanel = new NetOpenPanel( ui.Tab, p_intf );
86 captureOpenPanel = new CaptureOpenPanel( ui.Tab, p_intf );
89 ui.Tab->insertTab( OPEN_FILE_TAB, fileOpenPanel, qtr( "&File" ) );
90 ui.Tab->insertTab( OPEN_DISC_TAB, discOpenPanel, qtr( "&Disc" ) );
91 ui.Tab->insertTab( OPEN_NETWORK_TAB, netOpenPanel, qtr( "&Network" ) );
92 ui.Tab->insertTab( OPEN_CAPTURE_TAB, captureOpenPanel,
93 qtr( "Capture &Device" ) );
95 /* Hide the Slave input widgets */
96 ui.slaveLabel->hide();
98 ui.slaveBrowseButton->hide();
100 /* Buttons Creation */
102 playButton = ui.playButton;
105 cancelButton = new QPushButton( qtr( "&Cancel" ) );
108 selectButton = new QPushButton( qtr( "&Select" ) );
110 /* Menu for the Play button */
111 QMenu * openButtonMenu = new QMenu( "Open" );
112 openButtonMenu->addAction( qtr( "&Enqueue" ), this, SLOT( enqueue() ),
113 QKeySequence( "Alt+E" ) );
114 openButtonMenu->addAction( qtr( "&Play" ), this, SLOT( play() ),
115 QKeySequence( "Alt+P" ) );
116 openButtonMenu->addAction( qtr( "&Stream" ), this, SLOT( stream() ) ,
117 QKeySequence( "Alt+S" ) );
118 openButtonMenu->addAction( qtr( "&Convert" ), this, SLOT( transcode() ) ,
119 QKeySequence( "Alt+C" ) );
121 ui.menuButton->setMenu( openButtonMenu );
123 /* Add the three Buttons */
124 ui.buttonsBox->addButton( selectButton, QDialogButtonBox::AcceptRole );
125 ui.buttonsBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
127 /* At creation time, modify the default buttons */
130 /* Force MRL update on tab change */
131 CONNECT( ui.Tab, currentChanged( int ), this, signalCurrent() );
133 CONNECT( fileOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
134 CONNECT( netOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
135 CONNECT( discOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
136 CONNECT( captureOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
138 CONNECT( fileOpenPanel, methodChanged( QString ),
139 this, newCachingMethod( QString ) );
140 CONNECT( netOpenPanel, methodChanged( QString ),
141 this, newCachingMethod( QString ) );
142 CONNECT( discOpenPanel, methodChanged( QString ),
143 this, newCachingMethod( QString ) );
144 CONNECT( captureOpenPanel, methodChanged( QString ),
145 this, newCachingMethod( QString ) );
147 /* Advanced frame Connects */
148 CONNECT( ui.slaveText, textChanged( QString ), this, updateMRL() );
149 CONNECT( ui.cacheSpinBox, valueChanged( int ), this, updateMRL() );
150 CONNECT( ui.startTimeSpinBox, valueChanged( int ), this, updateMRL() );
151 BUTTONACT( ui.advancedCheckBox, toggleAdvancedPanel() );
152 BUTTONACT( ui.slaveBrowseButton, browseInputSlave() );
155 BUTTONACT( playButton, selectSlots() );
156 BUTTONACT( selectButton, close() );
157 BUTTONACT( cancelButton, cancel() );
159 /* Hide the advancedPanel */
160 if( !config_GetInt( p_intf, "qt-adv-options" ) )
161 ui.advancedFrame->hide();
163 ui.advancedCheckBox->setChecked( true );
165 /* Initialize caching */
167 newCachingMethod( "file-caching" );
170 OpenDialog::~OpenDialog()
173 /* Finish the dialog and decide if you open another one after */
174 void OpenDialog::setMenuAction()
176 if( i_action_flag == SELECT )
179 selectButton->show();
180 selectButton->setDefault( true );
184 switch ( i_action_flag )
186 case OPEN_AND_STREAM:
187 playButton->setText( qtr( "&Stream" ) );
190 playButton->setText( qtr( "&Convert / Save" ) );
192 case OPEN_AND_ENQUEUE:
193 playButton->setText( qtr( "&Enqueue" ) );
197 playButton->setText( qtr( "&Play" ) );
200 selectButton->hide();
201 playButton->setDefault( true );
205 void OpenDialog::showTab( int i_tab )
207 ui.Tab->setCurrentIndex( i_tab );
211 /* Function called on signal currentChanged triggered */
212 void OpenDialog::signalCurrent()
214 if( ui.Tab->currentWidget() != NULL )
215 ( dynamic_cast<OpenPanel *>( ui.Tab->currentWidget() ) )->updateMRL();
218 void OpenDialog::toggleAdvancedPanel()
220 if( ui.advancedFrame->isVisible() )
222 ui.advancedFrame->hide();
223 //setMinimumSize( 520, 460 );
224 if( size().isValid() )
225 resize( size().width(), size().height()
226 - ui.advancedFrame->height() );
230 ui.advancedFrame->show();
231 //setMinimumSize( 520, 460 + ui.advancedFrame->height() );
232 if( size().isValid() )
233 resize( size().width(), size().height()
234 + ui.advancedFrame->height() );
241 /* If Cancel is pressed or escaped */
242 void OpenDialog::cancel()
244 /* Clear the panels */
245 for( int i = 0; i < OPEN_TAB_MAX; i++ )
246 dynamic_cast<OpenPanel*>( ui.Tab->widget( i ) )->clear();
248 /* Clear the variables */
252 /* If in Select Mode, reject instead of hiding */
253 if( i_action_flag == SELECT ) reject();
257 /* If EnterKey is pressed */
258 void OpenDialog::close()
260 /* If in Select Mode, accept instead of selecting a Slot */
261 if( i_action_flag == SELECT )
268 void OpenDialog::selectSlots()
270 switch ( i_action_flag )
272 case OPEN_AND_STREAM:
278 case OPEN_AND_ENQUEUE:
287 void OpenDialog::play()
292 void OpenDialog::enqueue()
298 void OpenDialog::finish( bool b_enqueue = false )
301 mrl = ui.advancedLineInput->text();
303 if( i_action_flag != SELECT )
305 QStringList tempMRL = SeparateEntries( mrl );
306 for( size_t i = 0; i < tempMRL.size(); i++ )
308 bool b_start = !i && !b_enqueue;
309 input_item_t *p_input;
311 p_input = input_ItemNew( p_intf, qtu( tempMRL[i] ), NULL );
314 while( i + 1 < tempMRL.size() && tempMRL[i + 1].startsWith( ":" ) )
317 input_ItemAddOption( p_input, qtu( tempMRL[i] ) );
320 /* Switch between enqueuing and starting the item */
321 /* FIXME: playlist_AddInput() can fail */
322 playlist_AddInput( THEPL, p_input,
323 PLAYLIST_APPEND | ( b_start ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
324 PLAYLIST_END, true, pl_Unlocked );
325 vlc_gc_decref( p_input );
332 void OpenDialog::transcode()
337 void OpenDialog::stream( bool b_transcode_only )
339 mrl = ui.advancedLineInput->text();
341 QStringList listMRL = SeparateEntries( mrl );
342 if( listMRL.size() > 0 )
343 THEDP->streamingDialog( this, SeparateEntries( mrl )[0], b_transcode_only );
347 void OpenDialog::updateMRL( QString tempMRL )
353 void OpenDialog::updateMRL() {
355 if( ui.slaveCheckbox->isChecked() ) {
356 mrl += " :input-slave=" + ui.slaveText->text();
358 int i_cache = config_GetInt( p_intf, qta( storedMethod ) );
359 if( i_cache != ui.cacheSpinBox->value() ) {
360 mrl += QString( " :%1=%2" ).arg( storedMethod ).
361 arg( ui.cacheSpinBox->value() );
363 if( ui.startTimeSpinBox->value() ) {
364 mrl += " :start-time=" + QString( "%1" ).
365 arg( ui.startTimeSpinBox->value() );
367 ui.advancedLineInput->setText( mrl );
370 void OpenDialog::newCachingMethod( QString method )
372 if( method != storedMethod ) {
373 storedMethod = method;
374 int i_value = config_GetInt( p_intf, qta( storedMethod ) );
375 ui.cacheSpinBox->setValue( i_value );
379 QStringList OpenDialog::SeparateEntries( QString entries )
381 bool b_quotes_mode = false;
383 QStringList entries_array;
387 while( index < entries.size() )
389 int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
390 if( delim_pos < 0 ) delim_pos = entries.size() - 1;
391 entry += entries.mid( index, delim_pos - index + 1 );
392 index = delim_pos + 1;
394 if( entry.isEmpty() ) continue;
396 if( !b_quotes_mode && entry.endsWith( "\"" ) )
398 /* Enters quotes mode */
399 entry.truncate( entry.size() - 1 );
400 b_quotes_mode = true;
402 else if( b_quotes_mode && entry.endsWith( "\"" ) )
404 /* Finished the quotes mode */
405 entry.truncate( entry.size() - 1 );
406 b_quotes_mode = false;
408 else if( !b_quotes_mode && !entry.endsWith( "\"" ) )
410 /* we found a non-quoted standalone string */
411 if( index < entries.size() ||
412 entry.endsWith( " " ) || entry.endsWith( "\t" ) ||
413 entry.endsWith( "\r" ) || entry.endsWith( "\n" ) )
414 entry.truncate( entry.size() - 1 );
415 if( !entry.isEmpty() ) entries_array.append( entry );
422 if( !entry.isEmpty() ) entries_array.append( entry );
424 return entries_array;
427 void OpenDialog::browseInputSlave()
429 OpenDialog *od = new OpenDialog( this, p_intf, true, SELECT );
431 ui.slaveText->setText( od->getMRL() );