]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/open.cpp
d755fddc12502268dff82fcc7f4d8cd2ff452157
[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.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( !var_InheritBool( p_intf, "qt-adv-options" ) )
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     setMinimumSize( sizeHint() );
183     setMaximumWidth( 900 );
184     resize( getSettings()->value( "opendialog-size", QSize( 500, 490 ) ).toSize() );
185 }
186
187 OpenDialog::~OpenDialog()
188 {
189     getSettings()->setValue( "opendialog-size", size() -
190                  ( ui.advancedFrame->isEnabled() ?
191                    QSize(0, ui.advancedFrame->height()) : QSize(0, 0) ) );
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     }
231 }
232
233 void OpenDialog::showTab( int i_tab )
234 {
235     if( i_tab == OPEN_CAPTURE_TAB ) captureOpenPanel->initialize();
236     ui.Tab->setCurrentIndex( i_tab );
237     show();
238 }
239
240 /* Function called on signal currentChanged triggered */
241 void OpenDialog::signalCurrent( int i_tab )
242 {
243     if( i_tab == OPEN_CAPTURE_TAB ) captureOpenPanel->initialize();
244
245     if( ui.Tab->currentWidget() != NULL )
246         ( dynamic_cast<OpenPanel *>( ui.Tab->currentWidget() ) )->updateMRL();
247 }
248
249 void OpenDialog::toggleAdvancedPanel()
250 {
251     if( ui.advancedFrame->isVisible() )
252     {
253         ui.advancedFrame->hide();
254         ui.advancedFrame->setEnabled( false );
255         if( size().isValid() )
256             resize( size().width(), size().height()
257                     - ui.advancedFrame->height() );
258     }
259     else
260     {
261         ui.advancedFrame->show();
262         ui.advancedFrame->setEnabled( true );
263         if( size().isValid() )
264             resize( size().width(), size().height()
265                     + ui.advancedFrame->height() );
266     }
267 }
268
269 /***********
270  * Actions *
271  ***********/
272 /* If Cancel is pressed or escaped */
273 void OpenDialog::cancel()
274 {
275     /* Clear the panels */
276     for( int i = 0; i < OPEN_TAB_MAX; i++ )
277         dynamic_cast<OpenPanel*>( ui.Tab->widget( i ) )->clear();
278
279     /* Clear the variables */
280     itemsMRL.clear();
281     optionsMRL.clear();
282
283     /* If in Select Mode, reject instead of hiding */
284     if( i_action_flag == SELECT ) reject();
285     else hide();
286 }
287
288 /* If EnterKey is pressed */
289 void OpenDialog::close()
290 {
291     /* If in Select Mode, accept instead of selecting a Slot */
292     if( i_action_flag == SELECT )
293         accept();
294     else
295         selectSlots();
296 }
297
298 /* Play button */
299 void OpenDialog::selectSlots()
300 {
301     switch ( i_action_flag )
302     {
303     case OPEN_AND_STREAM:
304         stream();
305         break;
306     case OPEN_AND_SAVE:
307         transcode();
308         break;
309     case OPEN_AND_ENQUEUE:
310         enqueue();
311         break;
312     case OPEN_AND_PLAY:
313     default:
314         play();
315     }
316 }
317
318 void OpenDialog::play()
319 {
320     finish( false );
321 }
322
323 void OpenDialog::enqueue()
324 {
325     finish( true );
326 }
327
328
329 void OpenDialog::finish( bool b_enqueue = false )
330 {
331     toggleVisible();
332
333     if( i_action_flag == SELECT )
334     {
335         accept();
336         return;
337     }
338
339     /* Sort alphabetically */
340     itemsMRL.sort();
341
342     /* Go through the item list */
343     for( int i = 0; i < itemsMRL.size(); i++ )
344     {
345         bool b_start = !i && !b_enqueue;
346
347         input_item_t *p_input;
348         char* psz_uri = make_URI( qtu( itemsMRL[i] ), "file" );
349         p_input = input_item_New( p_intf, psz_uri, NULL );
350         free( psz_uri );
351
352         /* Insert options only for the first element.
353            We don't know how to edit that anyway. */
354         if( i == 0 )
355         {
356             /* Take options from the UI, not from what we stored */
357             QStringList optionsList = ui.advancedLineInput->text().split( " :" );
358
359             /* Insert options */
360             for( int j = 0; j < optionsList.size(); j++ )
361             {
362                 QString qs = colon_unescape( optionsList[j] );
363                 if( !qs.isEmpty() )
364                 {
365                     input_item_AddOption( p_input, qtu( qs ),
366                                           VLC_INPUT_OPTION_TRUSTED );
367 #ifdef DEBUG_QT
368                     msg_Warn( p_intf, "Input option: %s", qtu( qs ) );
369 #endif
370                 }
371             }
372         }
373
374         /* Switch between enqueuing and starting the item */
375         /* FIXME: playlist_AddInput() can fail */
376         playlist_AddInput( THEPL, p_input,
377                 PLAYLIST_APPEND | ( b_start ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
378                 PLAYLIST_END, b_pl ? true : false, pl_Unlocked );
379         vlc_gc_decref( p_input );
380
381         /* Do not add the current MRL if playlist_AddInput fail */
382         RecentsMRL::getInstance( p_intf )->addRecent( itemsMRL[i] );
383     }
384 }
385
386 void OpenDialog::transcode()
387 {
388     stream( true );
389 }
390
391 void OpenDialog::stream( bool b_transcode_only )
392 {
393     QString soutMRL = getMRL( false );
394     if( soutMRL.isEmpty() ) return;
395     toggleVisible();
396
397     /* Dbg and send :D */
398     msg_Dbg( p_intf, "MRL passed to the Sout: %s", qtu( soutMRL ) );
399     THEDP->streamingDialog( this, soutMRL, b_transcode_only,
400                             ui.advancedLineInput->text().split( " :" ) );
401 }
402
403 /* Update the MRL */
404 void OpenDialog::updateMRL( const QStringList& item, const QString& tempMRL )
405 {
406     optionsMRL = tempMRL;
407     itemsMRL = item;
408     updateMRL();
409 }
410
411 void OpenDialog::updateMRL() {
412     QString mrl = optionsMRL;
413     if( ui.slaveCheckbox->isChecked() ) {
414         mrl += " :input-slave=" + ui.slaveText->text();
415     }
416     mrl += QString( " :%1=%2" ).arg( storedMethod ).
417                                 arg( ui.cacheSpinBox->value() );
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 = var_InheritInteger( 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 }