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