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