]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/open.cpp
Qt4: add item options for all items
[vlc] / modules / gui / qt4 / dialogs / open.cpp
1 /*****************************************************************************
2  * open.cpp : Advanced open dialog
3  *****************************************************************************
4  * Copyright © 2006-2011 the VideoLAN team
5  *
6  * Authors: Jean-Baptiste Kempf <jb@videolan.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include "dialogs/open.hpp"
28 #include "dialogs_provider.hpp"
29 #include "recents.hpp"
30 #include "util/qt_dirs.hpp"
31
32 #include <QTabWidget>
33 #include <QGridLayout>
34 #include <QRegExp>
35 #include <QMenu>
36
37 #ifndef NDEBUG
38 # define DEBUG_QT 1
39 #endif
40
41 OpenDialog *OpenDialog::instance = NULL;
42
43 OpenDialog* OpenDialog::getInstance( QWidget *parent, intf_thread_t *p_intf,
44         bool b_rawInstance, int _action_flag, bool b_selectMode, bool _b_pl )
45 {
46     /* Creation */
47     if( !instance )
48         instance = new OpenDialog( parent, p_intf, b_selectMode,
49                                    _action_flag, _b_pl );
50     else if( !b_rawInstance )
51     {
52         /* Request the instance but change small details:
53            - Button menu */
54         if( b_selectMode )
55             _action_flag = SELECT; /* This should be useless, but we never know
56                                       if the call is correct */
57         instance->setWindowModality( Qt::WindowModal );
58         instance->i_action_flag = _action_flag;
59         instance->b_pl = _b_pl;
60         instance->setMenuAction();
61     }
62     return instance;
63 }
64
65 OpenDialog::OpenDialog( QWidget *parent,
66                         intf_thread_t *_p_intf,
67                         bool b_selectMode,
68                         int _action_flag,
69                         bool _b_pl)  :  QVLCDialog( parent, _p_intf )
70 {
71     i_action_flag = _action_flag;
72     b_pl =_b_pl;
73
74     if( b_selectMode ) /* Select mode */
75         i_action_flag = SELECT;
76
77     /* Basic Creation of the Window */
78     ui.setupUi( this );
79     setWindowTitle( qtr( "Open Media" ) );
80     setWindowRole( "vlc-open-media" );
81     setWindowModality( Qt::WindowModal );
82
83     /* Tab definition and creation */
84     fileOpenPanel    = new FileOpenPanel( this, p_intf );
85     discOpenPanel    = new DiscOpenPanel( this, p_intf );
86     netOpenPanel     = new NetOpenPanel( this, p_intf );
87     captureOpenPanel = new CaptureOpenPanel( this, p_intf );
88
89     /* Insert the tabs */
90     ui.Tab->insertTab( OPEN_FILE_TAB, fileOpenPanel, QIcon( ":/type/folder-grey" ),
91                        qtr( "&File" ) );
92     ui.Tab->insertTab( OPEN_DISC_TAB, discOpenPanel, QIcon( ":/type/disc" ),
93                        qtr( "&Disc" ) );
94     ui.Tab->insertTab( OPEN_NETWORK_TAB, netOpenPanel, QIcon( ":/type/network" ),
95                        qtr( "&Network" ) );
96     ui.Tab->insertTab( OPEN_CAPTURE_TAB, captureOpenPanel,
97                        QIcon( ":/type/capture-card" ), qtr( "Capture &Device" ) );
98
99     /* Hide the Slave input widgets */
100     ui.slaveLabel->hide();
101     ui.slaveText->hide();
102     ui.slaveBrowseButton->hide();
103
104     /* Buttons Creation */
105     /* Play Button */
106     playButton = ui.playButton;
107
108     /* Cancel Button */
109     cancelButton = new QPushButton( qtr( "&Cancel" ) );
110
111     /* Select Button */
112     selectButton = new QPushButton( qtr( "&Select" ) );
113
114     /* Menu for the Play button */
115     QMenu * openButtonMenu = new QMenu( "Open" );
116     openButtonMenu->addAction( qtr( "&Enqueue" ), this, SLOT( enqueue() ),
117                                     QKeySequence( "Alt+E" ) );
118     openButtonMenu->addAction( qtr( "&Play" ), this, SLOT( play() ),
119                                     QKeySequence( "Alt+P" ) );
120     openButtonMenu->addAction( qtr( "&Stream" ), this, SLOT( stream() ) ,
121                                     QKeySequence( "Alt+S" ) );
122     openButtonMenu->addAction( qtr( "&Convert" ), this, SLOT( transcode() ) ,
123                                     QKeySequence( "Alt+C" ) );
124
125     playButton->setMenu( openButtonMenu );
126
127     /* Add the three Buttons */
128     ui.buttonsBox->addButton( selectButton, QDialogButtonBox::AcceptRole );
129     ui.buttonsBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
130
131     /* At creation time, modify the default buttons */
132     setMenuAction();
133
134     /* Force MRL update on tab change */
135     CONNECT( ui.Tab, currentChanged( int ), this, signalCurrent( int ) );
136
137     CONNECT( fileOpenPanel, mrlUpdated( const QStringList&, const QString& ),
138              this, updateMRL( const QStringList&, const QString& ) );
139     CONNECT( netOpenPanel, mrlUpdated( const QStringList&, const QString& ),
140              this, updateMRL( const QStringList&, const QString& ) );
141     CONNECT( discOpenPanel, mrlUpdated( const QStringList&, const QString& ),
142              this, updateMRL( const QStringList&, const QString& ) );
143     CONNECT( captureOpenPanel, mrlUpdated( const QStringList&, const QString& ),
144              this, updateMRL( const QStringList&, const QString& ) );
145
146     CONNECT( fileOpenPanel, methodChanged( const QString& ),
147              this, newCachingMethod( const QString& ) );
148     CONNECT( netOpenPanel, methodChanged( const QString& ),
149              this, newCachingMethod( const QString& ) );
150     CONNECT( discOpenPanel, methodChanged( const QString& ),
151              this, newCachingMethod( const QString& ) );
152     CONNECT( captureOpenPanel, methodChanged( const QString& ),
153              this, newCachingMethod( const QString& ) );
154
155     /* Advanced frame Connects */
156     CONNECT( ui.slaveCheckbox, toggled( bool ), this, updateMRL() );
157     CONNECT( ui.slaveText, textChanged( const QString& ), this, updateMRL() );
158     CONNECT( ui.cacheSpinBox, valueChanged( int ), this, updateMRL() );
159     CONNECT( ui.startTimeTimeEdit, timeChanged ( const QTime& ), this, updateMRL() );
160     BUTTONACT( ui.advancedCheckBox, toggleAdvancedPanel() );
161     BUTTONACT( ui.slaveBrowseButton, browseInputSlave() );
162
163     /* Buttons action */
164     BUTTONACT( playButton, selectSlots() );
165     BUTTONACT( selectButton, close() );
166     BUTTONACT( cancelButton, cancel() );
167
168     /* Hide the advancedPanel */
169     if( !getSettings()->value( "OpenDialog/advanced", false ).toBool())
170     {
171         ui.advancedFrame->hide();
172         ui.advancedFrame->setEnabled( false );
173     }
174     else
175         ui.advancedCheckBox->setChecked( true );
176
177     /* Initialize caching */
178     storedMethod = "";
179     newCachingMethod( "file-caching" );
180
181     /* enforce section due to .ui bug */
182     ui.startTimeTimeEdit->setCurrentSection( QDateTimeEdit::SecondSection );
183
184     setMinimumSize( sizeHint() );
185     setMaximumWidth( 900 );
186     resize( getSettings()->value( "OpenDialog/size", QSize( 500, 400 ) ).toSize() );
187 }
188
189 /* Finish the dialog and decide if you open another one after */
190 void OpenDialog::setMenuAction()
191 {
192     if( i_action_flag == SELECT )
193     {
194         playButton->hide();
195         selectButton->show();
196         selectButton->setDefault( true );
197     }
198     else
199     {
200         switch ( i_action_flag )
201         {
202         case OPEN_AND_STREAM:
203             playButton->setText( qtr( "&Stream" ) );
204             break;
205         case OPEN_AND_SAVE:
206             playButton->setText( qtr( "&Convert / Save" ) );
207             break;
208         case OPEN_AND_ENQUEUE:
209             playButton->setText( qtr( "&Enqueue" ) );
210             break;
211         case OPEN_AND_PLAY:
212         default:
213             playButton->setText( qtr( "&Play" ) );
214         }
215         playButton->show();
216         selectButton->hide();
217     }
218 }
219
220 OpenDialog::~OpenDialog()
221 {
222     getSettings()->setValue( "OpenDialog/size", size() -
223                  ( ui.advancedFrame->isEnabled() ?
224                    QSize(0, ui.advancedFrame->height()) : QSize(0, 0) ) );
225     getSettings()->setValue( "OpenDialog/advanced", ui.advancedFrame->isVisible() );
226 }
227
228 /* Used by VLM dialog and inputSlave selection */
229 QString OpenDialog::getMRL( bool b_all )
230 {
231     if( itemsMRL.count() == 0 ) return "";
232     return b_all ? itemsMRL[0] + ui.advancedLineInput->text()
233                  : itemsMRL[0];
234 }
235
236 QString OpenDialog::getOptions()
237 {
238     return ui.advancedLineInput->text();
239 }
240
241 void OpenDialog::showTab( int i_tab )
242 {
243     if( i_tab == OPEN_CAPTURE_TAB ) captureOpenPanel->initialize();
244     ui.Tab->setCurrentIndex( i_tab );
245     show();
246     if( ui.Tab->currentWidget() != NULL )
247     {
248         OpenPanel *panel = qobject_cast<OpenPanel *>( ui.Tab->currentWidget() );
249         assert( panel );
250         panel->onFocus();
251     }
252 }
253
254 void OpenDialog::toggleAdvancedPanel()
255 {
256     if( ui.advancedFrame->isVisible() )
257     {
258         ui.advancedFrame->hide();
259         ui.advancedFrame->setEnabled( false );
260         if( size().isValid() )
261             resize( size().width(), size().height()
262                     - ui.advancedFrame->height() );
263     }
264     else
265     {
266         ui.advancedFrame->show();
267         ui.advancedFrame->setEnabled( true );
268         if( size().isValid() )
269             resize( size().width(), size().height()
270                     + ui.advancedFrame->height() );
271     }
272 }
273
274 void OpenDialog::browseInputSlave()
275 {
276     OpenDialog *od = new OpenDialog( this, p_intf, true, SELECT );
277     od->exec();
278     ui.slaveText->setText( od->getMRL( false ) );
279     delete od;
280 }
281
282 /* Function called on signal currentChanged triggered */
283 void OpenDialog::signalCurrent( int i_tab )
284 {
285     if( i_tab == OPEN_CAPTURE_TAB ) captureOpenPanel->initialize();
286     if( ui.Tab->currentWidget() != NULL )
287     {
288         OpenPanel *panel = qobject_cast<OpenPanel *>( ui.Tab->currentWidget() );
289         assert( panel );
290         panel->onFocus();
291         panel->updateMRL();
292     }
293 }
294
295 /***********
296  * Actions *
297  ***********/
298 /* If Cancel is pressed or escaped */
299 void OpenDialog::cancel()
300 {
301     /* Clear the panels */
302     for( int i = 0; i < OPEN_TAB_MAX; i++ )
303         qobject_cast<OpenPanel*>( ui.Tab->widget( i ) )->clear();
304
305     /* Clear the variables */
306     itemsMRL.clear();
307     optionsMRL.clear();
308
309     /* If in Select Mode, reject instead of hiding */
310     if( i_action_flag == SELECT ) reject();
311     else hide();
312 }
313
314 /* If EnterKey is pressed */
315 void OpenDialog::close()
316 {
317     /* If in Select Mode, accept instead of selecting a Slot */
318     if( i_action_flag == SELECT )
319         accept();
320     else
321         selectSlots();
322 }
323
324 /* Play button */
325 void OpenDialog::selectSlots()
326 {
327     switch ( i_action_flag )
328     {
329     case OPEN_AND_STREAM:
330         stream();
331         break;
332     case OPEN_AND_SAVE:
333         transcode();
334         break;
335     case OPEN_AND_ENQUEUE:
336         enqueue();
337         break;
338     case OPEN_AND_PLAY:
339     default:
340         play();
341     }
342 }
343
344 /* Play Action, called from selectSlots or play Menu */
345 void OpenDialog::play()
346 {
347     enqueue( false );
348 }
349
350 /* Enqueue Action, called from selectSlots or enqueue Menu */
351 void OpenDialog::enqueue( bool b_enqueue )
352 {
353     toggleVisible();
354
355     if( i_action_flag == SELECT )
356     {
357         accept();
358         return;
359     }
360
361     for( int i = 0; i < OPEN_TAB_MAX; i++ )
362         qobject_cast<OpenPanel*>( ui.Tab->widget( i ) )->onAccept();
363
364     /* Sort alphabetically */
365     itemsMRL.sort();
366
367     /* Go through the item list */
368     for( int i = 0; i < itemsMRL.count(); i++ )
369     {
370         bool b_start = !i && !b_enqueue;
371
372         input_item_t *p_input;
373         p_input = input_item_New( qtu( itemsMRL[i] ), NULL );
374
375         /* Take options from the UI, not from what we stored */
376         QStringList optionsList = ui.advancedLineInput->text().split( " :" );
377
378         /* Insert options */
379         for( int j = 0; j < optionsList.count(); j++ )
380         {
381             QString qs = colon_unescape( optionsList[j] );
382             if( !qs.isEmpty() )
383             {
384                 input_item_AddOption( p_input, qtu( qs ),
385                                       VLC_INPUT_OPTION_TRUSTED );
386 #ifdef DEBUG_QT
387                 msg_Warn( p_intf, "Input option: %s", qtu( qs ) );
388 #endif
389             }
390         }
391
392         /* Switch between enqueuing and starting the item */
393         /* FIXME: playlist_AddInput() can fail */
394         playlist_AddInput( THEPL, p_input,
395                 PLAYLIST_APPEND | ( b_start ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
396                 PLAYLIST_END, b_pl ? true : false, pl_Unlocked );
397         vlc_gc_decref( p_input );
398
399         /* Do not add the current MRL if playlist_AddInput fail */
400         RecentsMRL::getInstance( p_intf )->addRecent( itemsMRL[i] );
401     }
402 }
403
404 void OpenDialog::transcode()
405 {
406     stream( true );
407 }
408
409 void OpenDialog::stream( bool b_transcode_only )
410 {
411     QString soutMRL = getMRL( false );
412     if( soutMRL.isEmpty() ) return;
413     toggleVisible();
414
415     /* Dbg and send :D */
416     msg_Dbg( p_intf, "MRL passed to the Sout: %s", qtu( soutMRL ) );
417     THEDP->streamingDialog( this, soutMRL, b_transcode_only,
418                             ui.advancedLineInput->text().split( " :" ) );
419 }
420
421 /* Update the MRL items from the panels */
422 void OpenDialog::updateMRL( const QStringList& item, const QString& tempMRL )
423 {
424     optionsMRL = tempMRL;
425     itemsMRL = item;
426     updateMRL();
427 }
428
429 /* Update the complete MRL */
430 void OpenDialog::updateMRL() {
431     QString mrl = optionsMRL;
432     if( ui.slaveCheckbox->isChecked() ) {
433         mrl += " :input-slave=" + ui.slaveText->text();
434     }
435     mrl += QString( " :%1=%2" ).arg( storedMethod ).
436                                 arg( ui.cacheSpinBox->value() );
437     if( ui.startTimeTimeEdit->time() != ui.startTimeTimeEdit->minimumTime() ) {
438         mrl += QString( " :start-time=%1.%2" )
439                 .arg( QString::number(
440                     ui.startTimeTimeEdit->minimumTime().secsTo(
441                         ui.startTimeTimeEdit->time()
442                 ) ) )
443                .arg( ui.startTimeTimeEdit->time().msec(), 3, 10, QChar('0') );
444     }
445     ui.advancedLineInput->setText( mrl );
446     ui.mrlLine->setText( itemsMRL.join( " " ) );
447 }
448
449 /* Change the caching combobox */
450 void OpenDialog::newCachingMethod( const QString& method )
451 {
452     if( method != storedMethod ) {
453         storedMethod = method;
454         int i_value = var_InheritInteger( p_intf, qtu( storedMethod ) );
455         ui.cacheSpinBox->setValue( i_value );
456     }
457 }
458
459 /* Split the entries
460  * FIXME! */
461 QStringList OpenDialog::SeparateEntries( const QString& entries )
462 {
463     bool b_quotes_mode = false;
464
465     QStringList entries_array;
466     QString entry;
467
468     int index = 0;
469     while( index < entries.count() )
470     {
471         int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
472         if( delim_pos < 0 ) delim_pos = entries.count() - 1;
473         entry += entries.mid( index, delim_pos - index + 1 );
474         index = delim_pos + 1;
475
476         if( entry.isEmpty() ) continue;
477
478         if( !b_quotes_mode && entry.endsWith( "\"" ) )
479         {
480             /* Enters quotes mode */
481             entry.truncate( entry.count() - 1 );
482             b_quotes_mode = true;
483         }
484         else if( b_quotes_mode && entry.endsWith( "\"" ) )
485         {
486             /* Finished the quotes mode */
487             entry.truncate( entry.count() - 1 );
488             b_quotes_mode = false;
489         }
490         else if( !b_quotes_mode && !entry.endsWith( "\"" ) )
491         {
492             /* we found a non-quoted standalone string */
493             if( index < entries.count() ||
494                 entry.endsWith( " " ) || entry.endsWith( "\t" ) ||
495                 entry.endsWith( "\r" ) || entry.endsWith( "\n" ) )
496                 entry.truncate( entry.count() - 1 );
497             if( !entry.isEmpty() ) entries_array.append( entry );
498             entry.clear();
499         }
500         else
501         {;}
502     }
503
504     if( !entry.isEmpty() ) entries_array.append( entry );
505
506     return entries_array;
507 }
508