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