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