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