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