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