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