]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/open.cpp
Qt: try to fix the arrow on the open dialog.
[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( this, p_intf );
92     discOpenPanel    = new DiscOpenPanel( this, p_intf );
93     netOpenPanel     = new NetOpenPanel( this, p_intf );
94     captureOpenPanel = new CaptureOpenPanel( this, 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     ui.menuButton->setIcon( QIcon( ":/down_arrow" ) );
131
132     /* Add the three Buttons */
133     ui.buttonsBox->addButton( selectButton, QDialogButtonBox::AcceptRole );
134     ui.buttonsBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
135
136     /* At creation time, modify the default buttons */
137     setMenuAction();
138
139     /* Force MRL update on tab change */
140     CONNECT( ui.Tab, currentChanged( int ), this, signalCurrent( int ) );
141
142     CONNECT( fileOpenPanel, mrlUpdated( QStringList, QString ),
143              this, updateMRL( QStringList, QString ) );
144     CONNECT( netOpenPanel, mrlUpdated( QStringList, QString ),
145              this, updateMRL( QStringList, QString ) );
146     CONNECT( discOpenPanel, mrlUpdated( QStringList, QString ),
147              this, updateMRL( QStringList, QString ) );
148     CONNECT( captureOpenPanel, mrlUpdated( QStringList, QString ),
149              this, updateMRL( QStringList, QString ) );
150
151     CONNECT( fileOpenPanel, methodChanged( QString ),
152              this, newCachingMethod( QString ) );
153     CONNECT( netOpenPanel, methodChanged( QString ),
154              this, newCachingMethod( QString ) );
155     CONNECT( discOpenPanel, methodChanged( QString ),
156              this, newCachingMethod( QString ) );
157     CONNECT( captureOpenPanel, methodChanged( QString ),
158              this, newCachingMethod( QString ) );
159
160     /* Advanced frame Connects */
161     CONNECT( ui.slaveCheckbox, toggled( bool ), this, updateMRL() );
162     CONNECT( ui.slaveText, textChanged( QString ), this, updateMRL() );
163     CONNECT( ui.cacheSpinBox, valueChanged( int ), this, updateMRL() );
164     CONNECT( ui.startTimeSpinBox, valueChanged( int ), this, updateMRL() );
165     BUTTONACT( ui.advancedCheckBox, toggleAdvancedPanel() );
166     BUTTONACT( ui.slaveBrowseButton, browseInputSlave() );
167
168     /* Buttons action */
169     BUTTONACT( playButton, selectSlots() );
170     BUTTONACT( selectButton, close() );
171     BUTTONACT( cancelButton, cancel() );
172
173     /* Hide the advancedPanel */
174     if( !config_GetInt( p_intf, "qt-adv-options" ) )
175         ui.advancedFrame->hide();
176     else
177         ui.advancedCheckBox->setChecked( true );
178
179     /* Initialize caching */
180     storedMethod = "";
181     newCachingMethod( "file-caching" );
182
183     setMinimumSize( sizeHint() );
184     resize( getSettings()->value( "opendialog-size", QSize( 500, 490 ) ).toSize() );
185 }
186
187 OpenDialog::~OpenDialog()
188 {
189     getSettings()->setValue( "opendialog-size", size() );
190 }
191
192 /* Used by VLM dialog and inputSlave selection */
193 QString OpenDialog::getMRL( bool b_all )
194 {
195     if( itemsMRL.size() == 0 ) return "";
196     return b_all ? itemsMRL[0] + ui.advancedLineInput->text()
197                  : itemsMRL[0];
198 }
199
200 /* Finish the dialog and decide if you open another one after */
201 void OpenDialog::setMenuAction()
202 {
203     if( i_action_flag == SELECT )
204     {
205         playButton->hide();
206         selectButton->show();
207         selectButton->setDefault( true );
208     }
209     else
210     {
211         switch ( i_action_flag )
212         {
213         case OPEN_AND_STREAM:
214             playButton->setText( qtr( "&Stream" ) );
215             break;
216         case OPEN_AND_SAVE:
217             playButton->setText( qtr( "&Convert / Save" ) );
218             break;
219         case OPEN_AND_ENQUEUE:
220             playButton->setText( qtr( "&Enqueue" ) );
221             break;
222         case OPEN_AND_PLAY:
223         default:
224             playButton->setText( qtr( "&Play" ) );
225         }
226         playButton->show();
227         selectButton->hide();
228         playButton->setDefault( true );
229     }
230 }
231
232 void OpenDialog::showTab( int i_tab )
233 {
234     if( i_tab == OPEN_CAPTURE_TAB ) captureOpenPanel->initialize();
235     ui.Tab->setCurrentIndex( i_tab );
236     show();
237 }
238
239 /* Function called on signal currentChanged triggered */
240 void OpenDialog::signalCurrent( int i_tab )
241 {
242     if( i_tab == OPEN_CAPTURE_TAB ) captureOpenPanel->initialize();
243
244     if( ui.Tab->currentWidget() != NULL )
245         ( dynamic_cast<OpenPanel *>( ui.Tab->currentWidget() ) )->updateMRL();
246 }
247
248 void OpenDialog::toggleAdvancedPanel()
249 {
250     if( ui.advancedFrame->isVisible() )
251     {
252         ui.advancedFrame->hide();
253         if( size().isValid() )
254             resize( size().width(), size().height()
255                     - ui.advancedFrame->height() );
256     }
257     else
258     {
259         ui.advancedFrame->show();
260         if( size().isValid() )
261             resize( size().width(), size().height()
262                     + ui.advancedFrame->height() );
263     }
264 }
265
266 /***********
267  * Actions *
268  ***********/
269 /* If Cancel is pressed or escaped */
270 void OpenDialog::cancel()
271 {
272     /* Clear the panels */
273     for( int i = 0; i < OPEN_TAB_MAX; i++ )
274         dynamic_cast<OpenPanel*>( ui.Tab->widget( i ) )->clear();
275
276     /* Clear the variables */
277     itemsMRL.clear();
278     optionsMRL.clear();
279
280     /* If in Select Mode, reject instead of hiding */
281     if( i_action_flag == SELECT ) reject();
282     else hide();
283 }
284
285 /* If EnterKey is pressed */
286 void OpenDialog::close()
287 {
288     /* If in Select Mode, accept instead of selecting a Slot */
289     if( i_action_flag == SELECT )
290         accept();
291     else
292         selectSlots();
293 }
294
295 /* Play button */
296 void OpenDialog::selectSlots()
297 {
298     switch ( i_action_flag )
299     {
300     case OPEN_AND_STREAM:
301         stream();
302         break;
303     case OPEN_AND_SAVE:
304         transcode();
305         break;
306     case OPEN_AND_ENQUEUE:
307         enqueue();
308         break;
309     case OPEN_AND_PLAY:
310     default:
311         play();
312     }
313 }
314
315 void OpenDialog::play()
316 {
317     finish( false );
318 }
319
320 void OpenDialog::enqueue()
321 {
322     finish( true );
323 }
324
325
326 void OpenDialog::finish( bool b_enqueue = false )
327 {
328     toggleVisible();
329
330     if( i_action_flag == SELECT )
331     {
332         accept();
333         return;
334     }
335
336     /* Sort alphabetically */
337     itemsMRL.sort();
338
339     /* Go through the item list */
340     for( int i = 0; i < itemsMRL.size(); i++ )
341     {
342         bool b_start = !i && !b_enqueue;
343
344         input_item_t *p_input;
345         p_input = input_item_New( p_intf, qtu( itemsMRL[i] ), NULL );
346
347         /* Insert options only for the first element.
348            We don't know how to edit that anyway. */
349         if( i == 0 )
350         {
351             /* Take options from the UI, not from what we stored */
352             QStringList optionsList = ui.advancedLineInput->text().split( ":" );
353
354             /* Insert options */
355             for( int j = 0; j < optionsList.size(); j++ )
356             {
357                 QString qs = optionsList[j].trimmed();
358                 if( !qs.isEmpty() )
359                 {
360                     input_item_AddOption( p_input, qtu( qs ), VLC_INPUT_OPTION_TRUSTED );
361                     //  msg_Err( p_intf, "Here %s", qtu( qs ));
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();
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 }
393
394 /* Update the MRL */
395 void OpenDialog::updateMRL( QStringList item, QString tempMRL )
396 {
397     optionsMRL = tempMRL;
398     itemsMRL = item;
399     updateMRL();
400 }
401
402 void OpenDialog::updateMRL() {
403     QString mrl = optionsMRL;
404     if( ui.slaveCheckbox->isChecked() ) {
405         mrl += " :input-slave=" + ui.slaveText->text();
406     }
407     int i_cache = config_GetInt( p_intf, qtu( storedMethod ) );
408     if( i_cache != ui.cacheSpinBox->value() ) {
409         mrl += QString( " :%1=%2" ).arg( storedMethod ).
410                                   arg( ui.cacheSpinBox->value() );
411     }
412     if( ui.startTimeSpinBox->value() ) {
413         mrl += " :start-time=" + QString( "%1" ).
414             arg( ui.startTimeSpinBox->value() );
415     }
416     ui.advancedLineInput->setText( mrl );
417     ui.mrlLine->setText( itemsMRL.join( " " ) );
418 }
419
420 void OpenDialog::newCachingMethod( 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( 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 }