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" ) );
73 /* resize( 410, 600 ); */
74 setMinimumSize( 520, 460 );
76 /* Tab definition and creation */
77 fileOpenPanel = new FileOpenPanel( ui.Tab, p_intf );
78 discOpenPanel = new DiscOpenPanel( ui.Tab, p_intf );
79 netOpenPanel = new NetOpenPanel( ui.Tab, p_intf );
80 captureOpenPanel = new CaptureOpenPanel( ui.Tab, p_intf );
83 ui.Tab->insertTab( OPEN_FILE_TAB, fileOpenPanel, qtr( "&File" ) );
84 ui.Tab->insertTab( OPEN_DISC_TAB, discOpenPanel, qtr( "&Disc" ) );
85 ui.Tab->insertTab( OPEN_NETWORK_TAB, netOpenPanel, qtr( "&Network" ) );
86 ui.Tab->insertTab( OPEN_CAPTURE_TAB, captureOpenPanel,
87 qtr( "Capture &Device" ) );
89 /* Hide the Slave input widgets */
90 ui.slaveLabel->hide();
92 ui.slaveBrowseButton->hide();
94 /* Buttons Creation */
96 playButton = ui.playButton;
99 cancelButton = new QPushButton( qtr( "&Cancel" ) );
102 selectButton = new QPushButton( qtr( "&Select" ) );
104 /* Menu for the Play button */
105 QMenu * openButtonMenu = new QMenu( "Open" );
106 openButtonMenu->addAction( qtr( "&Enqueue" ), this, SLOT( enqueue() ),
107 QKeySequence( "Alt+E" ) );
108 openButtonMenu->addAction( qtr( "&Play" ), this, SLOT( play() ),
109 QKeySequence( "Alt+P" ) );
110 openButtonMenu->addAction( qtr( "&Stream" ), this, SLOT( stream() ) ,
111 QKeySequence( "Alt+S" ) );
112 openButtonMenu->addAction( qtr( "&Convert" ), this, SLOT( transcode() ) ,
113 QKeySequence( "Alt+C" ) );
115 ui.menuButton->setMenu( openButtonMenu );
117 /* Add the three Buttons */
118 ui.buttonsBox->addButton( selectButton, QDialogButtonBox::AcceptRole );
119 ui.buttonsBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
121 /* At creation time, modify the default buttons */
124 /* Force MRL update on tab change */
125 CONNECT( ui.Tab, currentChanged( int ), this, signalCurrent() );
127 CONNECT( fileOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
128 CONNECT( netOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
129 CONNECT( discOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
130 CONNECT( captureOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
132 CONNECT( fileOpenPanel, methodChanged( QString ),
133 this, newCachingMethod( QString ) );
134 CONNECT( netOpenPanel, methodChanged( QString ),
135 this, newCachingMethod( QString ) );
136 CONNECT( discOpenPanel, methodChanged( QString ),
137 this, newCachingMethod( QString ) );
138 CONNECT( captureOpenPanel, methodChanged( QString ),
139 this, newCachingMethod( QString ) );
141 /* Advanced frame Connects */
142 CONNECT( ui.slaveText, textChanged( QString ), this, updateMRL() );
143 CONNECT( ui.cacheSpinBox, valueChanged( int ), this, updateMRL() );
144 CONNECT( ui.startTimeSpinBox, valueChanged( int ), this, updateMRL() );
145 BUTTONACT( ui.advancedCheckBox , toggleAdvancedPanel() );
148 BUTTONACT( playButton, selectSlots() );
149 BUTTONACT( selectButton, close() );
150 BUTTONACT( cancelButton, cancel() );
152 /* Hide the advancedPanel */
153 if( !config_GetInt( p_intf, "qt-adv-options" ) )
154 ui.advancedFrame->hide();
156 ui.advancedCheckBox->setChecked( true );
158 /* Initialize caching */
160 newCachingMethod( "file-caching" );
163 OpenDialog::~OpenDialog()
166 /* Finish the dialog and decide if you open another one after */
167 void OpenDialog::setMenuAction()
169 if( i_action_flag == SELECT )
172 selectButton->show();
173 selectButton->setDefault( true );
177 switch ( i_action_flag )
179 case OPEN_AND_STREAM:
180 playButton->setText( qtr( "&Stream" ) );
183 playButton->setText( qtr( "&Convert / Save" ) );
185 case OPEN_AND_ENQUEUE:
186 playButton->setText( qtr( "&Enqueue" ) );
190 playButton->setText( qtr( "&Play" ) );
193 playButton->setDefault( true );
194 selectButton->hide();
198 void OpenDialog::showTab( int i_tab=0 )
200 ui.Tab->setCurrentIndex( i_tab );
204 /* Function called on signal currentChanged triggered */
205 void OpenDialog::signalCurrent()
207 if( ui.Tab->currentWidget() != NULL )
208 ( dynamic_cast<OpenPanel *>( ui.Tab->currentWidget() ) )->updateMRL();
211 void OpenDialog::toggleAdvancedPanel()
213 if( ui.advancedFrame->isVisible() )
215 ui.advancedFrame->hide();
216 //setMinimumSize( 520, 460 );
217 if( size().isValid() )
218 resize( size().width(), size().height()
219 - ui.advancedFrame->height() );
223 ui.advancedFrame->show();
224 //setMinimumSize( 520, 460 + ui.advancedFrame->height() );
225 if( size().isValid() )
226 resize( size().width(), size().height()
227 + ui.advancedFrame->height() );
234 /* If Cancel is pressed or escaped */
235 void OpenDialog::cancel()
237 /* Clear the panels */
238 for( int i = 0; i < OPEN_TAB_MAX; i++ )
239 dynamic_cast<OpenPanel*>( ui.Tab->widget( i ) )->clear();
241 /* Clear the variables */
245 /* If in Select Mode, reject instead of hiding */
246 if( windowModality() != Qt::NonModal ) reject();
250 /* If EnterKey is pressed */
251 void OpenDialog::close()
253 if( windowModality() != Qt::NonModal )
260 void OpenDialog::selectSlots()
262 switch ( i_action_flag )
264 case OPEN_AND_STREAM:
270 case OPEN_AND_ENQUEUE:
279 void OpenDialog::play()
284 void OpenDialog::enqueue()
290 void OpenDialog::finish( bool b_enqueue = false )
293 mrl = ui.advancedLineInput->text();
295 if( windowModality() == Qt::NonModal )
297 QStringList tempMRL = SeparateEntries( mrl );
298 for( size_t i = 0; i < tempMRL.size(); i++ )
300 bool b_start = !i && !b_enqueue;
301 input_item_t *p_input;
303 p_input = input_ItemNew( p_intf, qtu( tempMRL[i] ), NULL );
306 while( i + 1 < tempMRL.size() && tempMRL[i + 1].startsWith( ":" ) )
309 input_ItemAddOption( p_input, qtu( tempMRL[i] ) );
312 /* Switch between enqueuing and starting the item */
313 /* FIXME: playlist_AddInput() can fail */
314 playlist_AddInput( THEPL, p_input,
315 PLAYLIST_APPEND | ( b_start ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
316 PLAYLIST_END, true, pl_Unlocked );
317 vlc_gc_decref( p_input );
324 void OpenDialog::transcode()
329 void OpenDialog::stream( bool b_transcode_only )
331 mrl = ui.advancedLineInput->text();
333 THEDP->streamingDialog( this, mrl, b_transcode_only );
337 void OpenDialog::updateMRL( QString tempMRL )
343 void OpenDialog::updateMRL() {
345 if( ui.slaveCheckbox->isChecked() ) {
346 mrl += " :input-slave=" + ui.slaveText->text();
348 int i_cache = config_GetInt( p_intf, qta( storedMethod ) );
349 if( i_cache != ui.cacheSpinBox->value() ) {
350 mrl += QString( " :%1=%2" ).arg( storedMethod ).
351 arg( ui.cacheSpinBox->value() );
353 if( ui.startTimeSpinBox->value() ) {
354 mrl += " :start-time=" + QString( "%1" ).
355 arg( ui.startTimeSpinBox->value() );
357 ui.advancedLineInput->setText( mrl );
360 void OpenDialog::newCachingMethod( QString method )
362 if( method != storedMethod ) {
363 storedMethod = method;
364 int i_value = config_GetInt( p_intf, qta( storedMethod ) );
365 ui.cacheSpinBox->setValue( i_value );
369 QStringList OpenDialog::SeparateEntries( QString entries )
371 bool b_quotes_mode = false;
373 QStringList entries_array;
377 while( index < entries.size() )
379 int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
380 if( delim_pos < 0 ) delim_pos = entries.size() - 1;
381 entry += entries.mid( index, delim_pos - index + 1 );
382 index = delim_pos + 1;
384 if( entry.isEmpty() ) continue;
386 if( !b_quotes_mode && entry.endsWith( "\"" ) )
388 /* Enters quotes mode */
389 entry.truncate( entry.size() - 1 );
390 b_quotes_mode = true;
392 else if( b_quotes_mode && entry.endsWith( "\"" ) )
394 /* Finished the quotes mode */
395 entry.truncate( entry.size() - 1 );
396 b_quotes_mode = false;
398 else if( !b_quotes_mode && !entry.endsWith( "\"" ) )
400 /* we found a non-quoted standalone string */
401 if( index < entries.size() ||
402 entry.endsWith( " " ) || entry.endsWith( "\t" ) ||
403 entry.endsWith( "\r" ) || entry.endsWith( "\n" ) )
404 entry.truncate( entry.size() - 1 );
405 if( !entry.isEmpty() ) entries_array.append( entry );
412 if( !entry.isEmpty() ) entries_array.append( entry );
414 return entries_array;