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>
36 OpenDialog *OpenDialog::instance = NULL;
38 OpenDialog* OpenDialog::getInstance( QWidget *parent, intf_thread_t *p_intf,
39 bool b_rawInstance, int _action_flag, bool b_selectMode, bool _b_pl )
43 instance = new OpenDialog( parent, p_intf, b_selectMode,
44 _action_flag, _b_pl );
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->b_pl = _b_pl;
58 instance->setMenuAction();
63 OpenDialog::OpenDialog( QWidget *parent,
64 intf_thread_t *_p_intf,
67 bool _b_pl) : QVLCDialog( parent, _p_intf )
69 i_action_flag = _action_flag;
72 /* Workaround the Win32 Vout that put the video on top at regular times */
74 setWindowFlags( Qt::WindowStaysOnTopHint | Qt::Dialog );
77 if( b_selectMode ) /* Select mode */
79 i_action_flag = SELECT;
80 setWindowModality( Qt::WindowModal );
83 /* Basic Creation of the Window */
85 setWindowTitle( qtr( "Open" ) );
86 /* resize( 410, 600 ); */
87 setMinimumSize( 520, 490 );
89 /* Tab definition and creation */
90 fileOpenPanel = new FileOpenPanel( ui.Tab, p_intf );
91 discOpenPanel = new DiscOpenPanel( ui.Tab, p_intf );
92 netOpenPanel = new NetOpenPanel( ui.Tab, p_intf );
93 captureOpenPanel = new CaptureOpenPanel( ui.Tab, p_intf );
96 ui.Tab->insertTab( OPEN_FILE_TAB, fileOpenPanel, qtr( "&File" ) );
97 ui.Tab->insertTab( OPEN_DISC_TAB, discOpenPanel, qtr( "&Disc" ) );
98 ui.Tab->insertTab( OPEN_NETWORK_TAB, netOpenPanel, qtr( "&Network" ) );
99 ui.Tab->insertTab( OPEN_CAPTURE_TAB, captureOpenPanel,
100 qtr( "Capture &Device" ) );
102 /* Hide the Slave input widgets */
103 ui.slaveLabel->hide();
104 ui.slaveText->hide();
105 ui.slaveBrowseButton->hide();
107 /* Buttons Creation */
109 playButton = ui.playButton;
112 cancelButton = new QPushButton( qtr( "&Cancel" ) );
115 selectButton = new QPushButton( qtr( "&Select" ) );
117 /* Menu for the Play button */
118 QMenu * openButtonMenu = new QMenu( "Open" );
119 openButtonMenu->addAction( qtr( "&Enqueue" ), this, SLOT( enqueue() ),
120 QKeySequence( "Alt+E" ) );
121 openButtonMenu->addAction( qtr( "&Play" ), this, SLOT( play() ),
122 QKeySequence( "Alt+P" ) );
123 openButtonMenu->addAction( qtr( "&Stream" ), this, SLOT( stream() ) ,
124 QKeySequence( "Alt+S" ) );
125 openButtonMenu->addAction( qtr( "&Convert" ), this, SLOT( transcode() ) ,
126 QKeySequence( "Alt+C" ) );
128 ui.menuButton->setMenu( openButtonMenu );
130 /* Add the three Buttons */
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( int ) );
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() );
159 BUTTONACT( ui.slaveBrowseButton, browseInputSlave() );
162 BUTTONACT( playButton, selectSlots() );
163 BUTTONACT( selectButton, close() );
164 BUTTONACT( cancelButton, cancel() );
166 /* Hide the advancedPanel */
167 if( !config_GetInt( p_intf, "qt-adv-options" ) )
168 ui.advancedFrame->hide();
170 ui.advancedCheckBox->setChecked( true );
172 /* Initialize caching */
174 newCachingMethod( "file-caching" );
176 resize( getSettings()->value( "opendialog-size", QSize( 520, 490 ) ).toSize() );
179 OpenDialog::~OpenDialog()
181 getSettings()->setValue( "opendialog-size", size() );
184 /* Finish the dialog and decide if you open another one after */
185 void OpenDialog::setMenuAction()
187 if( i_action_flag == SELECT )
190 selectButton->show();
191 selectButton->setDefault( true );
195 switch ( i_action_flag )
197 case OPEN_AND_STREAM:
198 playButton->setText( qtr( "&Stream" ) );
201 playButton->setText( qtr( "&Convert / Save" ) );
203 case OPEN_AND_ENQUEUE:
204 playButton->setText( qtr( "&Enqueue" ) );
208 playButton->setText( qtr( "&Play" ) );
211 selectButton->hide();
212 playButton->setDefault( true );
216 void OpenDialog::showTab( int i_tab )
218 if( i_tab == OPEN_CAPTURE_TAB ) captureOpenPanel->initialize();
219 ui.Tab->setCurrentIndex( i_tab );
223 /* Function called on signal currentChanged triggered */
224 void OpenDialog::signalCurrent( int i_tab )
226 if( i_tab == OPEN_CAPTURE_TAB ) captureOpenPanel->initialize();
228 if( ui.Tab->currentWidget() != NULL )
229 ( dynamic_cast<OpenPanel *>( ui.Tab->currentWidget() ) )->updateMRL();
232 void OpenDialog::toggleAdvancedPanel()
234 if( ui.advancedFrame->isVisible() )
236 ui.advancedFrame->hide();
237 //setMinimumSize( 520, 460 );
238 if( size().isValid() )
239 resize( size().width(), size().height()
240 - ui.advancedFrame->height() );
244 ui.advancedFrame->show();
245 //setMinimumSize( 520, 460 + ui.advancedFrame->height() );
246 if( size().isValid() )
247 resize( size().width(), size().height()
248 + ui.advancedFrame->height() );
255 /* If Cancel is pressed or escaped */
256 void OpenDialog::cancel()
258 /* Clear the panels */
259 for( int i = 0; i < OPEN_TAB_MAX; i++ )
260 dynamic_cast<OpenPanel*>( ui.Tab->widget( i ) )->clear();
262 /* Clear the variables */
266 /* If in Select Mode, reject instead of hiding */
267 if( i_action_flag == SELECT ) reject();
271 /* If EnterKey is pressed */
272 void OpenDialog::close()
274 /* If in Select Mode, accept instead of selecting a Slot */
275 if( i_action_flag == SELECT )
282 void OpenDialog::selectSlots()
284 switch ( i_action_flag )
286 case OPEN_AND_STREAM:
292 case OPEN_AND_ENQUEUE:
301 void OpenDialog::play()
306 void OpenDialog::enqueue()
312 void OpenDialog::finish( bool b_enqueue = false )
315 mrl = ui.advancedLineInput->text();
317 if( i_action_flag != SELECT )
319 QStringList tempMRL = SeparateEntries( mrl );
320 for( size_t i = 0; i < tempMRL.size(); i++ )
322 bool b_start = !i && !b_enqueue;
323 input_item_t *p_input;
325 p_input = input_item_New( p_intf, qtu( tempMRL[i] ), NULL );
328 while( i + 1 < tempMRL.size() && tempMRL[i + 1].startsWith( ":" ) )
331 input_item_AddOption( p_input, qtu( tempMRL[i] ) );
334 /* Switch between enqueuing and starting the item */
335 /* FIXME: playlist_AddInput() can fail */
336 playlist_AddInput( THEPL, p_input,
337 PLAYLIST_APPEND | ( b_start ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
338 PLAYLIST_END, b_pl ? true : false, pl_Unlocked );
339 vlc_gc_decref( p_input );
346 void OpenDialog::transcode()
351 void OpenDialog::stream( bool b_transcode_only )
353 mrl = ui.advancedLineInput->text();
356 /* Separate the entries */
357 QStringList listMRL = SeparateEntries( mrl );
359 /* We can only take the first entry since we have no idea what
360 to do with many files ? Gather ? */
361 if( listMRL.size() > 0 )
364 QString soutMRL = listMRL[0];
366 /* Keep all the :xxx options because they are needed see v4l and dshow */
367 for( int i = 1; i < listMRL.size(); i++ )
369 if( listMRL[i].at( 0 ) == ':' )
370 soutMRL.append( " " + listMRL[i] );
375 /* Dbg and send :D */
376 msg_Dbg( p_intf, "MRL passed to the Sout: %s", qtu( soutMRL ) );
377 THEDP->streamingDialog( this, soutMRL, b_transcode_only );
382 void OpenDialog::updateMRL( QString tempMRL )
388 void OpenDialog::updateMRL() {
390 if( ui.slaveCheckbox->isChecked() ) {
391 mrl += " :input-slave=" + ui.slaveText->text();
393 int i_cache = config_GetInt( p_intf, qta( storedMethod ) );
394 if( i_cache != ui.cacheSpinBox->value() ) {
395 mrl += QString( " :%1=%2" ).arg( storedMethod ).
396 arg( ui.cacheSpinBox->value() );
398 if( ui.startTimeSpinBox->value() ) {
399 mrl += " :start-time=" + QString( "%1" ).
400 arg( ui.startTimeSpinBox->value() );
402 ui.advancedLineInput->setText( mrl );
405 void OpenDialog::newCachingMethod( QString method )
407 if( method != storedMethod ) {
408 storedMethod = method;
409 int i_value = config_GetInt( p_intf, qta( storedMethod ) );
410 ui.cacheSpinBox->setValue( i_value );
414 QStringList OpenDialog::SeparateEntries( QString entries )
416 bool b_quotes_mode = false;
418 QStringList entries_array;
422 while( index < entries.size() )
424 int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
425 if( delim_pos < 0 ) delim_pos = entries.size() - 1;
426 entry += entries.mid( index, delim_pos - index + 1 );
427 index = delim_pos + 1;
429 if( entry.isEmpty() ) continue;
431 if( !b_quotes_mode && entry.endsWith( "\"" ) )
433 /* Enters quotes mode */
434 entry.truncate( entry.size() - 1 );
435 b_quotes_mode = true;
437 else if( b_quotes_mode && entry.endsWith( "\"" ) )
439 /* Finished the quotes mode */
440 entry.truncate( entry.size() - 1 );
441 b_quotes_mode = false;
443 else if( !b_quotes_mode && !entry.endsWith( "\"" ) )
445 /* we found a non-quoted standalone string */
446 if( index < entries.size() ||
447 entry.endsWith( " " ) || entry.endsWith( "\t" ) ||
448 entry.endsWith( "\r" ) || entry.endsWith( "\n" ) )
449 entry.truncate( entry.size() - 1 );
450 if( !entry.isEmpty() ) entries_array.append( entry );
457 if( !entry.isEmpty() ) entries_array.append( entry );
459 return entries_array;
462 void OpenDialog::browseInputSlave()
464 OpenDialog *od = new OpenDialog( this, p_intf, true, SELECT );
466 ui.slaveText->setText( od->getMRL() );