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 *****************************************************************************/
24 #include "input_manager.hpp"
26 #include "dialogs/open.hpp"
29 #include <QGridLayout>
30 #include <QFileDialog>
34 OpenDialog *OpenDialog::instance = NULL;
36 OpenDialog* OpenDialog::getInstance( QWidget *parent, intf_thread_t *p_intf,
37 int _action_flag, bool modal )
41 instance = new OpenDialog( parent, p_intf, modal, _action_flag );
44 /* Request the instance but change small details:
46 - Modality on top of the parent dialog */
47 instance->i_action_flag = _action_flag;
48 instance->setMenuAction();
49 if( modal ) instance->setWindowModality( Qt::WindowModal );
54 OpenDialog::OpenDialog( QWidget *parent,
55 intf_thread_t *_p_intf,
57 int _action_flag ) : QVLCDialog( parent, _p_intf )
59 i_action_flag = _action_flag;
61 if( modal ) /* Select mode */
63 setWindowModality( Qt::WindowModal );
64 i_action_flag = SELECT;
67 /* Basic Creation of the Window */
69 setWindowTitle( qtr( "Open" ) );
72 /* Tab definition and creation */
73 fileOpenPanel = new FileOpenPanel( ui.Tab, p_intf );
74 discOpenPanel = new DiscOpenPanel( ui.Tab, p_intf );
75 netOpenPanel = new NetOpenPanel( ui.Tab, p_intf );
76 captureOpenPanel = new CaptureOpenPanel( ui.Tab, p_intf );
79 ui.Tab->insertTab( OPEN_FILE_TAB, fileOpenPanel, qtr( "&File" ) );
80 ui.Tab->insertTab( OPEN_DISC_TAB, discOpenPanel, qtr( "&Disc" ) );
81 ui.Tab->insertTab( OPEN_NETWORK_TAB, netOpenPanel, qtr( "&Network" ) );
82 ui.Tab->insertTab( OPEN_CAPTURE_TAB, captureOpenPanel,
83 qtr( "Capture &Device" ) );
85 /* Hide the Slave input widgets */
86 ui.slaveLabel->hide();
88 ui.slaveBrowseButton->hide();
90 /* Buttons Creation */
91 QSizePolicy buttonSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
92 buttonSizePolicy.setHorizontalStretch( 0 );
93 buttonSizePolicy.setVerticalStretch( 0 );
96 playButton = new QToolButton( this );
97 playButton->setText( qtr( "&Play" ) );
98 playButton->setSizePolicy( buttonSizePolicy );
99 playButton->setMinimumSize( QSize( 90, 0 ) );
100 playButton->setPopupMode( QToolButton::MenuButtonPopup );
101 playButton->setToolButtonStyle( Qt::ToolButtonTextOnly );
104 cancelButton = new QPushButton();
105 cancelButton->setText( qtr( "&Cancel" ) );
106 cancelButton->setSizePolicy( buttonSizePolicy );
109 selectButton = new QPushButton;
110 selectButton->setText( qtr( "Select" ) );
111 selectButton->setSizePolicy( buttonSizePolicy );
113 /* Menu for the Play button */
114 QMenu * openButtonMenu = new QMenu( "Open" );
115 openButtonMenu->addAction( qtr( "&Enqueue" ), this, SLOT( enqueue() ),
116 QKeySequence( "Alt+E" ) );
117 openButtonMenu->addAction( qtr( "&Play" ), this, SLOT( play() ),
118 QKeySequence( "Alt+P" ) );
119 openButtonMenu->addAction( qtr( "&Stream" ), this, SLOT( stream() ) ,
120 QKeySequence( "Alt+S" ) );
121 openButtonMenu->addAction( qtr( "&Convert" ), this, SLOT( transcode() ) ,
122 QKeySequence( "Alt+C" ) );
124 playButton->setMenu( openButtonMenu );
126 /* Add the three Buttons */
127 ui.buttonsBox->addButton( playButton, QDialogButtonBox::ActionRole );
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() );
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() );
158 BUTTONACT( playButton, selectSlots() );
159 BUTTONACT( selectButton, close() );
160 BUTTONACT( cancelButton, cancel() );
162 /* Hide the advancedPanel */
163 if( !config_GetInt( p_intf, "qt-adv-options" ) )
164 ui.advancedFrame->hide();
166 ui.advancedCheckBox->setChecked( true );
168 /* Initialize caching */
170 newCachingMethod( "file-caching" );
173 OpenDialog::~OpenDialog()
176 /* Finish the dialog and decide if you open another one after */
177 void OpenDialog::setMenuAction()
179 if( i_action_flag == SELECT )
182 selectButton->show();
186 switch ( i_action_flag )
188 case OPEN_AND_STREAM:
189 playButton->setText( qtr( "&Stream" ) );
192 playButton->setText( qtr( "&Convert / Save" ) );
194 case OPEN_AND_ENQUEUE:
195 playButton->setText( qtr( "&Enqueue" ) );
199 playButton->setText( qtr( "&Play" ) );
202 selectButton->hide();
206 void OpenDialog::showTab( int i_tab=0 )
208 ui.Tab->setCurrentIndex( i_tab );
212 /* Function called on signal currentChanged triggered */
213 void OpenDialog::signalCurrent()
215 if( ui.Tab->currentWidget() != NULL )
216 ( dynamic_cast<OpenPanel *>( ui.Tab->currentWidget() ) )->updateMRL();
219 void OpenDialog::toggleAdvancedPanel()
221 if( ui.advancedFrame->isVisible() )
223 ui.advancedFrame->hide();
224 //FIXME: Clear Bug here. Qt ?
225 resize( size().width(), size().height() - ui.advancedFrame->height() );
229 ui.advancedFrame->show();
236 /* If Cancel is pressed or escaped */
237 void OpenDialog::cancel()
239 /* Clear the panels */
240 for( int i = 0; i < OPEN_TAB_MAX; i++ )
241 dynamic_cast<OpenPanel*>( ui.Tab->widget( i ) )->clear();
243 /* Clear the variables */
247 /* If in Select Mode, reject instead of hiding */
248 if( windowModality() != Qt::NonModal ) reject();
252 /* If EnterKey is pressed */
253 void OpenDialog::close()
255 if( windowModality() != Qt::NonModal )
262 void OpenDialog::selectSlots()
264 switch ( i_action_flag )
266 case OPEN_AND_STREAM:
272 case OPEN_AND_ENQUEUE:
281 void OpenDialog::play()
286 void OpenDialog::enqueue()
292 void OpenDialog::finish( bool b_enqueue = false )
295 mrl = ui.advancedLineInput->text();
297 if( windowModality() == Qt::NonModal )
299 QStringList tempMRL = SeparateEntries( mrl );
300 for( size_t i = 0; i < tempMRL.size(); i++ )
302 bool b_start = !i && !b_enqueue;
303 input_item_t *p_input;
305 p_input = input_ItemNew( p_intf, qtu( tempMRL[i] ), NULL );
308 while( i + 1 < tempMRL.size() && tempMRL[i + 1].startsWith( ":" ) )
311 input_ItemAddOption( p_input, qtu( tempMRL[i] ) );
314 /* Switch between enqueuing and starting the item */
317 playlist_AddInput( THEPL, p_input,
318 PLAYLIST_APPEND | PLAYLIST_GO,
319 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
323 playlist_AddInput( THEPL, p_input,
324 PLAYLIST_APPEND | PLAYLIST_PREPARSE,
325 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
333 void OpenDialog::transcode()
338 void OpenDialog::stream( bool b_transcode_only )
340 mrl = ui.advancedLineInput->text();
342 THEDP->streamingDialog( this, mrl, 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;