]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/open.cpp
Don't include config.h from the headers - refs #297.
[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         int _action_flag, bool modal )
41 {
42     /* Creation */
43     if( !instance )
44         instance = new OpenDialog( parent, p_intf, modal, _action_flag );
45     else
46     {
47         /* Request the instance but change small details:
48            - Button menu
49            - Modality on top of the parent dialog */
50         instance->i_action_flag = _action_flag;
51         instance->setMenuAction();
52         if( modal ) instance->setWindowModality( Qt::WindowModal );
53     }
54     return instance;
55 }
56
57 OpenDialog::OpenDialog( QWidget *parent,
58                         intf_thread_t *_p_intf,
59                         bool modal,
60                         int _action_flag )  :  QVLCDialog( parent, _p_intf )
61 {
62     i_action_flag = _action_flag;
63
64     if( modal ) /* Select mode */
65     {
66         setWindowModality( Qt::WindowModal );
67         i_action_flag = SELECT;
68     }
69
70     /* Basic Creation of the Window */
71     ui.setupUi( this );
72     setWindowTitle( qtr( "Open" ) );
73     resize( 410, 300 );
74
75     /* Tab definition and creation */
76     fileOpenPanel    = new FileOpenPanel( ui.Tab, p_intf );
77     discOpenPanel    = new DiscOpenPanel( ui.Tab, p_intf );
78     netOpenPanel     = new NetOpenPanel( ui.Tab, p_intf );
79     captureOpenPanel = new CaptureOpenPanel( ui.Tab, p_intf );
80
81     /* Insert the tabs */
82     ui.Tab->insertTab( OPEN_FILE_TAB, fileOpenPanel, qtr( "&File" ) );
83     ui.Tab->insertTab( OPEN_DISC_TAB, discOpenPanel, qtr( "&Disc" ) );
84     ui.Tab->insertTab( OPEN_NETWORK_TAB, netOpenPanel, qtr( "&Network" ) );
85     ui.Tab->insertTab( OPEN_CAPTURE_TAB, captureOpenPanel,
86                        qtr( "Capture &Device" ) );
87
88     /* Hide the Slave input widgets */
89     ui.slaveLabel->hide();
90     ui.slaveText->hide();
91     ui.slaveBrowseButton->hide();
92
93     /* Buttons Creation */
94     QSizePolicy buttonSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
95     buttonSizePolicy.setHorizontalStretch( 0 );
96     buttonSizePolicy.setVerticalStretch( 0 );
97
98     /* Play Button */
99     playButton = new QToolButton;
100     playButton->setText( qtr( "&Play" ) );
101     playButton->setSizePolicy( buttonSizePolicy );
102     playButton->setMinimumSize( QSize( 90, 0 ) );
103     playButton->setPopupMode( QToolButton::MenuButtonPopup );
104     playButton->setToolButtonStyle( Qt::ToolButtonTextOnly );
105
106     /* Cancel Button */
107     cancelButton = new QToolButton;
108     cancelButton->setText( qtr( "&Cancel" ) );
109     cancelButton->setSizePolicy( buttonSizePolicy );
110
111     /* Select Button */
112     selectButton = new QToolButton;
113     selectButton->setText( qtr( "Select" ) );
114     selectButton->setSizePolicy( buttonSizePolicy );
115
116     /* Menu for the Play button */
117     QMenu * openButtonMenu = new QMenu( "Open" );
118     openButtonMenu->addAction( qtr( "&Enqueue" ), this, SLOT( enqueue() ),
119                                     QKeySequence( "Alt+E" ) );
120     openButtonMenu->addAction( qtr( "&Play" ), this, SLOT( play() ),
121                                     QKeySequence( "Alt+P" ) );
122     openButtonMenu->addAction( qtr( "&Stream" ), this, SLOT( stream() ) ,
123                                     QKeySequence( "Alt+S" ) );
124     openButtonMenu->addAction( qtr( "&Convert" ), this, SLOT( transcode() ) ,
125                                     QKeySequence( "Alt+C" ) );
126
127     playButton->setMenu( openButtonMenu );
128
129     /* Add the three Buttons */
130     ui.buttonsBox->addButton( playButton, QDialogButtonBox::ActionRole );
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() );
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
160     /* Buttons action */
161     BUTTONACT( playButton, selectSlots() );
162     BUTTONACT( selectButton, close() );
163     BUTTONACT( cancelButton, cancel() );
164
165     /* Hide the advancedPanel */
166     if( !config_GetInt( p_intf, "qt-adv-options" ) )
167         ui.advancedFrame->hide();
168     else
169         ui.advancedCheckBox->setChecked( true );
170
171     /* Initialize caching */
172     storedMethod = "";
173     newCachingMethod( "file-caching" );
174 }
175
176 OpenDialog::~OpenDialog()
177 {}
178
179 /* Finish the dialog and decide if you open another one after */
180 void OpenDialog::setMenuAction()
181 {
182     if( i_action_flag == SELECT )
183     {
184         playButton->hide();
185         selectButton->show();
186     }
187     else
188     {
189         switch ( i_action_flag )
190         {
191         case OPEN_AND_STREAM:
192             playButton->setText( qtr( "&Stream" ) );
193             break;
194         case OPEN_AND_SAVE:
195             playButton->setText( qtr( "&Convert / Save" ) );
196             break;
197         case OPEN_AND_ENQUEUE:
198             playButton->setText( qtr( "&Enqueue" ) );
199             break;
200         case OPEN_AND_PLAY:
201         default:
202             playButton->setText( qtr( "&Play" ) );
203         }
204         playButton->show();
205         selectButton->hide();
206     }
207 }
208
209 void OpenDialog::showTab( int i_tab=0 )
210 {
211     ui.Tab->setCurrentIndex( i_tab );
212     show();
213 }
214
215 /* Function called on signal currentChanged triggered */
216 void OpenDialog::signalCurrent()
217 {
218     if( ui.Tab->currentWidget() != NULL )
219         ( dynamic_cast<OpenPanel *>( ui.Tab->currentWidget() ) )->updateMRL();
220 }
221
222 void OpenDialog::toggleAdvancedPanel()
223 {
224     if( ui.advancedFrame->isVisible() )
225     {
226         ui.advancedFrame->hide();
227         //FIXME: Clear Bug here. Qt ?
228         resize( size().width(), size().height() - ui.advancedFrame->height() );
229     }
230     else
231     {
232         ui.advancedFrame->show();
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( windowModality() != Qt::NonModal ) reject();
252     else hide();
253 }
254
255 /* If EnterKey is pressed */
256 void OpenDialog::close()
257 {
258     if( windowModality() != Qt::NonModal )
259         accept();
260     else
261         selectSlots();
262 }
263
264 /* Play button */
265 void OpenDialog::selectSlots()
266 {
267     switch ( i_action_flag )
268     {
269     case OPEN_AND_STREAM:
270         stream();
271         break;
272     case OPEN_AND_SAVE:
273         transcode();
274         break;
275     case OPEN_AND_ENQUEUE:
276         enqueue();
277         break;
278     case OPEN_AND_PLAY:
279     default:
280         play();
281     }
282 }
283
284 void OpenDialog::play()
285 {
286     finish( false );
287 }
288
289 void OpenDialog::enqueue()
290 {
291     finish( true );
292 }
293
294
295 void OpenDialog::finish( bool b_enqueue = false )
296 {
297     toggleVisible();
298     mrl = ui.advancedLineInput->text();
299
300     if( windowModality() == Qt::NonModal )
301     {
302         QStringList tempMRL = SeparateEntries( mrl );
303         for( size_t i = 0; i < tempMRL.size(); i++ )
304         {
305             bool b_start = !i && !b_enqueue;
306             input_item_t *p_input;
307
308             p_input = input_ItemNew( p_intf, qtu( tempMRL[i] ), NULL );
309
310             /* Insert options */
311             while( i + 1 < tempMRL.size() && tempMRL[i + 1].startsWith( ":" ) )
312             {
313                 i++;
314                 input_ItemAddOption( p_input, qtu( tempMRL[i] ) );
315             }
316
317             /* Switch between enqueuing and starting the item */
318             if( b_start )
319             {
320                 playlist_AddInput( THEPL, p_input,
321                                    PLAYLIST_APPEND | PLAYLIST_GO,
322                                    PLAYLIST_END, VLC_TRUE, VLC_FALSE );
323             }
324             else
325             {
326                 playlist_AddInput( THEPL, p_input,
327                                    PLAYLIST_APPEND | PLAYLIST_PREPARSE,
328                                    PLAYLIST_END, VLC_TRUE, VLC_FALSE );
329             }
330         }
331     }
332     else
333         accept();
334 }
335
336 void OpenDialog::transcode()
337 {
338     stream( true );
339 }
340
341 void OpenDialog::stream( bool b_transcode_only )
342 {
343     mrl = ui.advancedLineInput->text();
344     toggleVisible();
345     THEDP->streamingDialog( this, mrl, b_transcode_only );
346 }
347
348 /* Update the MRL */
349 void OpenDialog::updateMRL( QString tempMRL )
350 {
351     mainMRL = tempMRL;
352     updateMRL();
353 }
354
355 void OpenDialog::updateMRL() {
356     mrl = mainMRL;
357     if( ui.slaveCheckbox->isChecked() ) {
358         mrl += " :input-slave=" + ui.slaveText->text();
359     }
360     int i_cache = config_GetInt( p_intf, qta( storedMethod ) );
361     if( i_cache != ui.cacheSpinBox->value() ) {
362         mrl += QString( " :%1=%2" ).arg( storedMethod ).
363                                   arg( ui.cacheSpinBox->value() );
364     }
365     if( ui.startTimeSpinBox->value() ) {
366         mrl += " :start-time=" + QString( "%1" ).
367             arg( ui.startTimeSpinBox->value() );
368     }
369     ui.advancedLineInput->setText( mrl );
370 }
371
372 void OpenDialog::newCachingMethod( QString method )
373 {
374     if( method != storedMethod ) {
375         storedMethod = method;
376         int i_value = config_GetInt( p_intf, qta( storedMethod ) );
377         ui.cacheSpinBox->setValue( i_value );
378     }
379 }
380
381 QStringList OpenDialog::SeparateEntries( QString entries )
382 {
383     bool b_quotes_mode = false;
384
385     QStringList entries_array;
386     QString entry;
387
388     int index = 0;
389     while( index < entries.size() )
390     {
391         int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
392         if( delim_pos < 0 ) delim_pos = entries.size() - 1;
393         entry += entries.mid( index, delim_pos - index + 1 );
394         index = delim_pos + 1;
395
396         if( entry.isEmpty() ) continue;
397
398         if( !b_quotes_mode && entry.endsWith( "\"" ) )
399         {
400             /* Enters quotes mode */
401             entry.truncate( entry.size() - 1 );
402             b_quotes_mode = true;
403         }
404         else if( b_quotes_mode && entry.endsWith( "\"" ) )
405         {
406             /* Finished the quotes mode */
407             entry.truncate( entry.size() - 1 );
408             b_quotes_mode = false;
409         }
410         else if( !b_quotes_mode && !entry.endsWith( "\"" ) )
411         {
412             /* we found a non-quoted standalone string */
413             if( index < entries.size() ||
414                 entry.endsWith( " " ) || entry.endsWith( "\t" ) ||
415                 entry.endsWith( "\r" ) || entry.endsWith( "\n" ) )
416                 entry.truncate( entry.size() - 1 );
417             if( !entry.isEmpty() ) entries_array.append( entry );
418             entry.clear();
419         }
420         else
421         {;}
422     }
423
424     if( !entry.isEmpty() ) entries_array.append( entry );
425
426     return entries_array;
427 }