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