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