]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/open.cpp
Added window roles for X11
[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     ui.menuButton->setMenu( openButtonMenu );
127     ui.menuButton->setIcon( QIcon( ":/down_arrow" ) );
128
129     /* Add the three Buttons */
130     ui.buttonsBox->addButton( selectButton, QDialogButtonBox::AcceptRole );
131     ui.buttonsBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
132
133     /* At creation time, modify the default buttons */
134     setMenuAction();
135
136     /* Force MRL update on tab change */
137     CONNECT( ui.Tab, currentChanged( int ), this, signalCurrent( int ) );
138
139     CONNECT( fileOpenPanel, mrlUpdated( const QStringList&, const QString& ),
140              this, updateMRL( const QStringList&, const QString& ) );
141     CONNECT( netOpenPanel, mrlUpdated( const QStringList&, const QString& ),
142              this, updateMRL( const QStringList&, const QString& ) );
143     CONNECT( discOpenPanel, mrlUpdated( const QStringList&, const QString& ),
144              this, updateMRL( const QStringList&, const QString& ) );
145     CONNECT( captureOpenPanel, mrlUpdated( const QStringList&, const QString& ),
146              this, updateMRL( const QStringList&, const QString& ) );
147
148     CONNECT( fileOpenPanel, methodChanged( const QString& ),
149              this, newCachingMethod( const QString& ) );
150     CONNECT( netOpenPanel, methodChanged( const QString& ),
151              this, newCachingMethod( const QString& ) );
152     CONNECT( discOpenPanel, methodChanged( const QString& ),
153              this, newCachingMethod( const QString& ) );
154     CONNECT( captureOpenPanel, methodChanged( const QString& ),
155              this, newCachingMethod( const QString& ) );
156
157     /* Advanced frame Connects */
158     CONNECT( ui.slaveCheckbox, toggled( bool ), this, updateMRL() );
159     CONNECT( ui.slaveText, textChanged( const QString& ), this, updateMRL() );
160     CONNECT( ui.cacheSpinBox, valueChanged( int ), this, updateMRL() );
161     CONNECT( ui.startTimeDoubleSpinBox, valueChanged( double ), this, updateMRL() );
162     BUTTONACT( ui.advancedCheckBox, toggleAdvancedPanel() );
163     BUTTONACT( ui.slaveBrowseButton, browseInputSlave() );
164
165     /* Buttons action */
166     BUTTONACT( playButton, selectSlots() );
167     BUTTONACT( selectButton, close() );
168     BUTTONACT( cancelButton, cancel() );
169
170     /* Hide the advancedPanel */
171     if( !config_GetInt( p_intf, "qt-adv-options" ) )
172         ui.advancedFrame->hide();
173     else
174         ui.advancedCheckBox->setChecked( true );
175
176     /* Initialize caching */
177     storedMethod = "";
178     newCachingMethod( "file-caching" );
179
180     setMinimumSize( sizeHint() );
181     setMaximumWidth( 900 );
182     resize( getSettings()->value( "opendialog-size", QSize( 500, 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         char* psz_uri = make_URI( qtu( itemsMRL[i] ) );
344         p_input = input_item_New( p_intf, psz_uri, NULL );
345         free( psz_uri );
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 = colon_unescape( optionsList[j] );
358                 if( !qs.isEmpty() )
359                 {
360                     input_item_AddOption( p_input, qtu( qs ),
361                                           VLC_INPUT_OPTION_TRUSTED );
362 #ifdef DEBUG_QT
363                     msg_Warn( p_intf, "Input option: %s", qtu( qs ) );
364 #endif
365                 }
366             }
367         }
368
369         /* Switch between enqueuing and starting the item */
370         /* FIXME: playlist_AddInput() can fail */
371         playlist_AddInput( THEPL, p_input,
372                 PLAYLIST_APPEND | ( b_start ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
373                 PLAYLIST_END, b_pl ? true : false, pl_Unlocked );
374         vlc_gc_decref( p_input );
375
376         /* Do not add the current MRL if playlist_AddInput fail */
377         RecentsMRL::getInstance( p_intf )->addRecent( itemsMRL[i] );
378     }
379 }
380
381 void OpenDialog::transcode()
382 {
383     stream( true );
384 }
385
386 void OpenDialog::stream( bool b_transcode_only )
387 {
388     QString soutMRL = getMRL( false );
389     if( soutMRL.isEmpty() ) return;
390     toggleVisible();
391
392     /* Dbg and send :D */
393     msg_Dbg( p_intf, "MRL passed to the Sout: %s", qtu( soutMRL ) );
394     THEDP->streamingDialog( this, soutMRL, b_transcode_only,
395                             ui.advancedLineInput->text().split( " :" ) );
396 }
397
398 /* Update the MRL */
399 void OpenDialog::updateMRL( const QStringList& item, const QString& tempMRL )
400 {
401     optionsMRL = tempMRL;
402     itemsMRL = item;
403     updateMRL();
404 }
405
406 void OpenDialog::updateMRL() {
407     QString mrl = optionsMRL;
408     if( ui.slaveCheckbox->isChecked() ) {
409         mrl += " :input-slave=" + ui.slaveText->text();
410     }
411     int i_cache = config_GetInt( p_intf, qtu( storedMethod ) );
412     if( i_cache != ui.cacheSpinBox->value() ) {
413         mrl += QString( " :%1=%2" ).arg( storedMethod ).
414                                   arg( ui.cacheSpinBox->value() );
415     }
416     if( ui.startTimeDoubleSpinBox->value() ) {
417         mrl += " :start-time=" + QString::number( ui.startTimeDoubleSpinBox->value() );
418     }
419     ui.advancedLineInput->setText( mrl );
420     ui.mrlLine->setText( itemsMRL.join( " " ) );
421 }
422
423 void OpenDialog::newCachingMethod( const QString& method )
424 {
425     if( method != storedMethod ) {
426         storedMethod = method;
427         int i_value = config_GetInt( p_intf, qtu( storedMethod ) );
428         ui.cacheSpinBox->setValue( i_value );
429     }
430 }
431
432 QStringList OpenDialog::SeparateEntries( const QString& entries )
433 {
434     bool b_quotes_mode = false;
435
436     QStringList entries_array;
437     QString entry;
438
439     int index = 0;
440     while( index < entries.size() )
441     {
442         int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
443         if( delim_pos < 0 ) delim_pos = entries.size() - 1;
444         entry += entries.mid( index, delim_pos - index + 1 );
445         index = delim_pos + 1;
446
447         if( entry.isEmpty() ) continue;
448
449         if( !b_quotes_mode && entry.endsWith( "\"" ) )
450         {
451             /* Enters quotes mode */
452             entry.truncate( entry.size() - 1 );
453             b_quotes_mode = true;
454         }
455         else if( b_quotes_mode && entry.endsWith( "\"" ) )
456         {
457             /* Finished the quotes mode */
458             entry.truncate( entry.size() - 1 );
459             b_quotes_mode = false;
460         }
461         else if( !b_quotes_mode && !entry.endsWith( "\"" ) )
462         {
463             /* we found a non-quoted standalone string */
464             if( index < entries.size() ||
465                 entry.endsWith( " " ) || entry.endsWith( "\t" ) ||
466                 entry.endsWith( "\r" ) || entry.endsWith( "\n" ) )
467                 entry.truncate( entry.size() - 1 );
468             if( !entry.isEmpty() ) entries_array.append( entry );
469             entry.clear();
470         }
471         else
472         {;}
473     }
474
475     if( !entry.isEmpty() ) entries_array.append( entry );
476
477     return entries_array;
478 }
479
480 void OpenDialog::browseInputSlave()
481 {
482     OpenDialog *od = new OpenDialog( this, p_intf, true, SELECT );
483     od->exec();
484     ui.slaveText->setText( od->getMRL( false ) );
485     delete od;
486 }