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