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