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