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 */
94 QSizePolicy buttonSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
95 buttonSizePolicy.setHorizontalStretch( 0 );
96 buttonSizePolicy.setVerticalStretch( 0 );
99 playButton = new QToolButton;
100 playButton->setText( qtr( "&Play" ) );
101 playButton->setSizePolicy( buttonSizePolicy );
102 playButton->setMinimumSize( QSize( 90, 0 ) );
103 playButton->setPopupMode( QToolButton::MenuButtonPopup );
104 playButton->setToolButtonStyle( Qt::ToolButtonTextOnly );
107 cancelButton = new QToolButton;
108 cancelButton->setText( qtr( "&Cancel" ) );
109 cancelButton->setSizePolicy( buttonSizePolicy );
112 selectButton = new QToolButton;
113 selectButton->setText( qtr( "Select" ) );
114 selectButton->setSizePolicy( buttonSizePolicy );
116 /* Menu for the Play button */
117 QMenu * openButtonMenu = new QMenu( "Open" );
118 openButtonMenu->addAction( qtr( "&Enqueue" ), this, SLOT( enqueue() ),
119 QKeySequence( "Alt+E" ) );
120 openButtonMenu->addAction( qtr( "&Play" ), this, SLOT( play() ),
121 QKeySequence( "Alt+P" ) );
122 openButtonMenu->addAction( qtr( "&Stream" ), this, SLOT( stream() ) ,
123 QKeySequence( "Alt+S" ) );
124 openButtonMenu->addAction( qtr( "&Convert" ), this, SLOT( transcode() ) ,
125 QKeySequence( "Alt+C" ) );
127 playButton->setMenu( openButtonMenu );
129 /* Add the three Buttons */
130 ui.buttonsBox->addButton( playButton, QDialogButtonBox::ActionRole );
131 ui.buttonsBox->addButton( selectButton, QDialogButtonBox::AcceptRole );
132 ui.buttonsBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
134 /* At creation time, modify the default buttons */
137 /* Force MRL update on tab change */
138 CONNECT( ui.Tab, currentChanged( int ), this, signalCurrent() );
140 CONNECT( fileOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
141 CONNECT( netOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
142 CONNECT( discOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
143 CONNECT( captureOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
145 CONNECT( fileOpenPanel, methodChanged( QString ),
146 this, newCachingMethod( QString ) );
147 CONNECT( netOpenPanel, methodChanged( QString ),
148 this, newCachingMethod( QString ) );
149 CONNECT( discOpenPanel, methodChanged( QString ),
150 this, newCachingMethod( QString ) );
151 CONNECT( captureOpenPanel, methodChanged( QString ),
152 this, newCachingMethod( QString ) );
154 /* Advanced frame Connects */
155 CONNECT( ui.slaveText, textChanged( QString ), this, updateMRL() );
156 CONNECT( ui.cacheSpinBox, valueChanged( int ), this, updateMRL() );
157 CONNECT( ui.startTimeSpinBox, valueChanged( int ), this, updateMRL() );
158 BUTTONACT( ui.advancedCheckBox , toggleAdvancedPanel() );
161 BUTTONACT( playButton, selectSlots() );
162 BUTTONACT( selectButton, close() );
163 BUTTONACT( cancelButton, cancel() );
165 /* Hide the advancedPanel */
166 if( !config_GetInt( p_intf, "qt-adv-options" ) )
167 ui.advancedFrame->hide();
169 ui.advancedCheckBox->setChecked( true );
171 /* Initialize caching */
173 newCachingMethod( "file-caching" );
176 OpenDialog::~OpenDialog()
179 /* Finish the dialog and decide if you open another one after */
180 void OpenDialog::setMenuAction()
182 if( i_action_flag == SELECT )
185 selectButton->show();
189 switch ( i_action_flag )
191 case OPEN_AND_STREAM:
192 playButton->setText( qtr( "&Stream" ) );
195 playButton->setText( qtr( "&Convert / Save" ) );
197 case OPEN_AND_ENQUEUE:
198 playButton->setText( qtr( "&Enqueue" ) );
202 playButton->setText( qtr( "&Play" ) );
205 selectButton->hide();
209 void OpenDialog::showTab( int i_tab=0 )
211 ui.Tab->setCurrentIndex( i_tab );
215 /* Function called on signal currentChanged triggered */
216 void OpenDialog::signalCurrent()
218 if( ui.Tab->currentWidget() != NULL )
219 ( dynamic_cast<OpenPanel *>( ui.Tab->currentWidget() ) )->updateMRL();
222 void OpenDialog::toggleAdvancedPanel()
224 if( ui.advancedFrame->isVisible() )
226 ui.advancedFrame->hide();
227 //FIXME: Clear Bug here. Qt ?
228 if( size().isValid() )
229 resize( size().width(), size().height() - ui.advancedFrame->height() );
233 ui.advancedFrame->show();
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( windowModality() != Qt::NonModal ) reject();
256 /* If EnterKey is pressed */
257 void OpenDialog::close()
259 if( windowModality() != Qt::NonModal )
266 void OpenDialog::selectSlots()
268 switch ( i_action_flag )
270 case OPEN_AND_STREAM:
276 case OPEN_AND_ENQUEUE:
285 void OpenDialog::play()
290 void OpenDialog::enqueue()
296 void OpenDialog::finish( bool b_enqueue = false )
299 mrl = ui.advancedLineInput->text();
301 if( windowModality() == Qt::NonModal )
303 QStringList tempMRL = SeparateEntries( mrl );
304 for( size_t i = 0; i < tempMRL.size(); i++ )
306 bool b_start = !i && !b_enqueue;
307 input_item_t *p_input;
309 p_input = input_ItemNew( p_intf, qtu( tempMRL[i] ), NULL );
312 while( i + 1 < tempMRL.size() && tempMRL[i + 1].startsWith( ":" ) )
315 input_ItemAddOption( p_input, qtu( tempMRL[i] ) );
318 /* Switch between enqueuing and starting the item */
321 playlist_AddInput( THEPL, p_input,
322 PLAYLIST_APPEND | PLAYLIST_GO,
323 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
327 playlist_AddInput( THEPL, p_input,
328 PLAYLIST_APPEND | PLAYLIST_PREPARSE,
329 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
337 void OpenDialog::transcode()
342 void OpenDialog::stream( bool b_transcode_only )
344 mrl = ui.advancedLineInput->text();
346 THEDP->streamingDialog( this, mrl, b_transcode_only );
350 void OpenDialog::updateMRL( QString tempMRL )
356 void OpenDialog::updateMRL() {
358 if( ui.slaveCheckbox->isChecked() ) {
359 mrl += " :input-slave=" + ui.slaveText->text();
361 int i_cache = config_GetInt( p_intf, qta( storedMethod ) );
362 if( i_cache != ui.cacheSpinBox->value() ) {
363 mrl += QString( " :%1=%2" ).arg( storedMethod ).
364 arg( ui.cacheSpinBox->value() );
366 if( ui.startTimeSpinBox->value() ) {
367 mrl += " :start-time=" + QString( "%1" ).
368 arg( ui.startTimeSpinBox->value() );
370 ui.advancedLineInput->setText( mrl );
373 void OpenDialog::newCachingMethod( QString method )
375 if( method != storedMethod ) {
376 storedMethod = method;
377 int i_value = config_GetInt( p_intf, qta( storedMethod ) );
378 ui.cacheSpinBox->setValue( i_value );
382 QStringList OpenDialog::SeparateEntries( QString entries )
384 bool b_quotes_mode = false;
386 QStringList entries_array;
390 while( index < entries.size() )
392 int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
393 if( delim_pos < 0 ) delim_pos = entries.size() - 1;
394 entry += entries.mid( index, delim_pos - index + 1 );
395 index = delim_pos + 1;
397 if( entry.isEmpty() ) continue;
399 if( !b_quotes_mode && entry.endsWith( "\"" ) )
401 /* Enters quotes mode */
402 entry.truncate( entry.size() - 1 );
403 b_quotes_mode = true;
405 else if( b_quotes_mode && entry.endsWith( "\"" ) )
407 /* Finished the quotes mode */
408 entry.truncate( entry.size() - 1 );
409 b_quotes_mode = false;
411 else if( !b_quotes_mode && !entry.endsWith( "\"" ) )
413 /* we found a non-quoted standalone string */
414 if( index < entries.size() ||
415 entry.endsWith( " " ) || entry.endsWith( "\t" ) ||
416 entry.endsWith( "\r" ) || entry.endsWith( "\n" ) )
417 entry.truncate( entry.size() - 1 );
418 if( !entry.isEmpty() ) entries_array.append( entry );
425 if( !entry.isEmpty() ) entries_array.append( entry );
427 return entries_array;