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