]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/open.cpp
b2e3d7e760b1dd259121442219315c31728b652d
[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     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     ui.menuButton->setMenu( openButtonMenu );
126     ui.menuButton->setIcon( QIcon( ":/down_arrow" ) );
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.startTimeDoubleSpinBox, valueChanged( double ), 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( !config_GetInt( p_intf, "qt-adv-options" ) )
171         ui.advancedFrame->hide();
172     else
173         ui.advancedCheckBox->setChecked( true );
174
175     /* Initialize caching */
176     storedMethod = "";
177     newCachingMethod( "file-caching" );
178
179     setMinimumSize( sizeHint() );
180     setMaximumWidth( 900 );
181     resize( getSettings()->value( "opendialog-size", QSize( 500, 490 ) ).toSize() );
182 }
183
184 OpenDialog::~OpenDialog()
185 {
186     getSettings()->setValue( "opendialog-size", size() );
187 }
188
189 /* Used by VLM dialog and inputSlave selection */
190 QString OpenDialog::getMRL( bool b_all )
191 {
192     if( itemsMRL.size() == 0 ) return "";
193     return b_all ? itemsMRL[0] + ui.advancedLineInput->text()
194                  : itemsMRL[0];
195 }
196
197 /* Finish the dialog and decide if you open another one after */
198 void OpenDialog::setMenuAction()
199 {
200     if( i_action_flag == SELECT )
201     {
202         playButton->hide();
203         selectButton->show();
204         selectButton->setDefault( true );
205     }
206     else
207     {
208         switch ( i_action_flag )
209         {
210         case OPEN_AND_STREAM:
211             playButton->setText( qtr( "&Stream" ) );
212             break;
213         case OPEN_AND_SAVE:
214             playButton->setText( qtr( "&Convert / Save" ) );
215             break;
216         case OPEN_AND_ENQUEUE:
217             playButton->setText( qtr( "&Enqueue" ) );
218             break;
219         case OPEN_AND_PLAY:
220         default:
221             playButton->setText( qtr( "&Play" ) );
222         }
223         playButton->show();
224         selectButton->hide();
225         playButton->setDefault( true );
226     }
227 }
228
229 void OpenDialog::showTab( int i_tab )
230 {
231     if( i_tab == OPEN_CAPTURE_TAB ) captureOpenPanel->initialize();
232     ui.Tab->setCurrentIndex( i_tab );
233     show();
234 }
235
236 /* Function called on signal currentChanged triggered */
237 void OpenDialog::signalCurrent( int i_tab )
238 {
239     if( i_tab == OPEN_CAPTURE_TAB ) captureOpenPanel->initialize();
240
241     if( ui.Tab->currentWidget() != NULL )
242         ( dynamic_cast<OpenPanel *>( ui.Tab->currentWidget() ) )->updateMRL();
243 }
244
245 void OpenDialog::toggleAdvancedPanel()
246 {
247     if( ui.advancedFrame->isVisible() )
248     {
249         ui.advancedFrame->hide();
250         if( size().isValid() )
251             resize( size().width(), size().height()
252                     - ui.advancedFrame->height() );
253     }
254     else
255     {
256         ui.advancedFrame->show();
257         if( size().isValid() )
258             resize( size().width(), size().height()
259                     + ui.advancedFrame->height() );
260     }
261 }
262
263 /***********
264  * Actions *
265  ***********/
266 /* If Cancel is pressed or escaped */
267 void OpenDialog::cancel()
268 {
269     /* Clear the panels */
270     for( int i = 0; i < OPEN_TAB_MAX; i++ )
271         dynamic_cast<OpenPanel*>( ui.Tab->widget( i ) )->clear();
272
273     /* Clear the variables */
274     itemsMRL.clear();
275     optionsMRL.clear();
276
277     /* If in Select Mode, reject instead of hiding */
278     if( i_action_flag == SELECT ) reject();
279     else hide();
280 }
281
282 /* If EnterKey is pressed */
283 void OpenDialog::close()
284 {
285     /* If in Select Mode, accept instead of selecting a Slot */
286     if( i_action_flag == SELECT )
287         accept();
288     else
289         selectSlots();
290 }
291
292 /* Play button */
293 void OpenDialog::selectSlots()
294 {
295     switch ( i_action_flag )
296     {
297     case OPEN_AND_STREAM:
298         stream();
299         break;
300     case OPEN_AND_SAVE:
301         transcode();
302         break;
303     case OPEN_AND_ENQUEUE:
304         enqueue();
305         break;
306     case OPEN_AND_PLAY:
307     default:
308         play();
309     }
310 }
311
312 void OpenDialog::play()
313 {
314     finish( false );
315 }
316
317 void OpenDialog::enqueue()
318 {
319     finish( true );
320 }
321
322
323 void OpenDialog::finish( bool b_enqueue = false )
324 {
325     toggleVisible();
326
327     if( i_action_flag == SELECT )
328     {
329         accept();
330         return;
331     }
332
333     /* Sort alphabetically */
334     itemsMRL.sort();
335
336     /* Go through the item list */
337     for( int i = 0; i < itemsMRL.size(); i++ )
338     {
339         bool b_start = !i && !b_enqueue;
340
341         input_item_t *p_input;
342         p_input = input_item_New( p_intf, qtu( itemsMRL[i] ), NULL );
343
344         /* Insert options only for the first element.
345            We don't know how to edit that anyway. */
346         if( i == 0 )
347         {
348             /* Take options from the UI, not from what we stored */
349             QStringList optionsList = ui.advancedLineInput->text().split( " :" );
350
351             /* Insert options */
352             for( int j = 0; j < optionsList.size(); j++ )
353             {
354                 QString qs = colon_unescape( optionsList[j] );
355                 if( !qs.isEmpty() )
356                 {
357                     input_item_AddOption( p_input, qtu( qs ),
358                                           VLC_INPUT_OPTION_TRUSTED );
359 #ifdef DEBUG_QT
360                     msg_Warn( p_intf, "Input option: %s", qtu( qs ) );
361 #endif
362                 }
363             }
364         }
365
366         /* Switch between enqueuing and starting the item */
367         /* FIXME: playlist_AddInput() can fail */
368         playlist_AddInput( THEPL, p_input,
369                 PLAYLIST_APPEND | ( b_start ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
370                 PLAYLIST_END, b_pl ? true : false, pl_Unlocked );
371         vlc_gc_decref( p_input );
372
373         /* Do not add the current MRL if playlist_AddInput fail */
374         RecentsMRL::getInstance( p_intf )->addRecent( itemsMRL[i] );
375     }
376 }
377
378 void OpenDialog::transcode()
379 {
380     stream( true );
381 }
382
383 void OpenDialog::stream( bool b_transcode_only )
384 {
385     QString soutMRL = getMRL( false );
386     if( soutMRL.isEmpty() ) return;
387     toggleVisible();
388
389     /* Dbg and send :D */
390     msg_Dbg( p_intf, "MRL passed to the Sout: %s", qtu( soutMRL ) );
391     THEDP->streamingDialog( this, soutMRL, b_transcode_only,
392                             ui.advancedLineInput->text().split( " :" ) );
393 }
394
395 /* Update the MRL */
396 void OpenDialog::updateMRL( const QStringList& item, const QString& tempMRL )
397 {
398     optionsMRL = tempMRL;
399     itemsMRL = item;
400     updateMRL();
401 }
402
403 void OpenDialog::updateMRL() {
404     QString mrl = optionsMRL;
405     if( ui.slaveCheckbox->isChecked() ) {
406         mrl += " :input-slave=" + ui.slaveText->text();
407     }
408     int i_cache = config_GetInt( p_intf, qtu( storedMethod ) );
409     if( i_cache != ui.cacheSpinBox->value() ) {
410         mrl += QString( " :%1=%2" ).arg( storedMethod ).
411                                   arg( ui.cacheSpinBox->value() );
412     }
413     if( ui.startTimeDoubleSpinBox->value() ) {
414         mrl += " :start-time=" + QString::number( ui.startTimeDoubleSpinBox->value() );
415     }
416     ui.advancedLineInput->setText( mrl );
417     ui.mrlLine->setText( itemsMRL.join( " " ) );
418 }
419
420 void OpenDialog::newCachingMethod( const QString& method )
421 {
422     if( method != storedMethod ) {
423         storedMethod = method;
424         int i_value = config_GetInt( p_intf, qtu( storedMethod ) );
425         ui.cacheSpinBox->setValue( i_value );
426     }
427 }
428
429 QStringList OpenDialog::SeparateEntries( const QString& entries )
430 {
431     bool b_quotes_mode = false;
432
433     QStringList entries_array;
434     QString entry;
435
436     int index = 0;
437     while( index < entries.size() )
438     {
439         int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
440         if( delim_pos < 0 ) delim_pos = entries.size() - 1;
441         entry += entries.mid( index, delim_pos - index + 1 );
442         index = delim_pos + 1;
443
444         if( entry.isEmpty() ) continue;
445
446         if( !b_quotes_mode && entry.endsWith( "\"" ) )
447         {
448             /* Enters quotes mode */
449             entry.truncate( entry.size() - 1 );
450             b_quotes_mode = true;
451         }
452         else if( b_quotes_mode && entry.endsWith( "\"" ) )
453         {
454             /* Finished the quotes mode */
455             entry.truncate( entry.size() - 1 );
456             b_quotes_mode = false;
457         }
458         else if( !b_quotes_mode && !entry.endsWith( "\"" ) )
459         {
460             /* we found a non-quoted standalone string */
461             if( index < entries.size() ||
462                 entry.endsWith( " " ) || entry.endsWith( "\t" ) ||
463                 entry.endsWith( "\r" ) || entry.endsWith( "\n" ) )
464                 entry.truncate( entry.size() - 1 );
465             if( !entry.isEmpty() ) entries_array.append( entry );
466             entry.clear();
467         }
468         else
469         {;}
470     }
471
472     if( !entry.isEmpty() ) entries_array.append( entry );
473
474     return entries_array;
475 }
476
477 void OpenDialog::browseInputSlave()
478 {
479     OpenDialog *od = new OpenDialog( this, p_intf, true, SELECT );
480     od->exec();
481     ui.slaveText->setText( od->getMRL( false ) );
482     delete od;
483 }