]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/open.cpp
Qt4 - Open: clean again, and fix the clear() bug.
[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 #include "components/open.hpp"
28
29 #include <QTabWidget>
30 #include <QGridLayout>
31 #include <QFileDialog>
32 #include <QRegExp>
33 #include <QMenu>
34
35 OpenDialog *OpenDialog::instance = NULL;
36
37 OpenDialog::OpenDialog( QWidget *parent, intf_thread_t *_p_intf, bool modal,
38                         int _action_flag )  :  QVLCDialog( parent, _p_intf )
39 {
40     setModal( modal );
41     i_action_flag = _action_flag;
42
43     /* Basic Creation of the Window */
44     ui.setupUi( this );
45     setWindowTitle( qtr("Open" ) );
46     resize( 410, 300);
47
48     /* Tab definition and creation */
49     fileOpenPanel = new FileOpenPanel( ui.Tab, p_intf );
50     discOpenPanel = new DiscOpenPanel( ui.Tab, p_intf );
51     netOpenPanel = new NetOpenPanel( ui.Tab, p_intf );
52     captureOpenPanel = new CaptureOpenPanel( ui.Tab, p_intf );
53
54     /* Insert the tabs */
55     ui.Tab->insertTab( OPEN_FILE_TAB, fileOpenPanel, qtr( "&File" ) );
56     ui.Tab->insertTab( OPEN_DISC_TAB, discOpenPanel, qtr( "&Disc" ) );
57     ui.Tab->insertTab( OPEN_NETWORK_TAB, netOpenPanel, qtr( "&Network" ) );
58     ui.Tab->insertTab( OPEN_CAPTURE_TAB, captureOpenPanel,
59                                 qtr( "Capture &Device" ) );
60
61     /* Hide the Slave input widgets */
62     ui.slaveLabel->hide();
63     ui.slaveText->hide();
64     ui.slaveBrowseButton->hide();
65
66     /* Hide the advancedPanel */
67     if(! config_GetInt( p_intf, "qt-adv-options") )
68         ui.advancedFrame->hide();
69     else
70         ui.advancedCheckBox->setCheckState( Qt::Checked );
71
72     /* Buttons Creation */
73     QSizePolicy buttonSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
74     buttonSizePolicy.setHorizontalStretch(0);
75     buttonSizePolicy.setVerticalStretch(0);
76
77     /* Play Button */
78     playButton = new QToolButton( this );
79     playButton->setText( qtr( "&Play" ) );
80     playButton->setSizePolicy( buttonSizePolicy );
81     playButton->setMinimumSize( QSize(90, 0) );
82     playButton->setPopupMode( QToolButton::MenuButtonPopup );
83     playButton->setToolButtonStyle( Qt::ToolButtonTextOnly );
84
85     /* Cancel Button */
86     cancelButton = new QPushButton();
87     cancelButton->setText( qtr( "&Cancel" ) );
88     cancelButton->setSizePolicy( buttonSizePolicy );
89
90     /* Menu for the Play button */
91     QMenu * openButtonMenu = new QMenu( "Open" );
92     openButtonMenu->addAction( qtr("&Enqueue"), this, SLOT( enqueue() ),
93                                     QKeySequence( "Alt+E") );
94     openButtonMenu->addAction( qtr("&Play"), this, SLOT( play() ),
95                                     QKeySequence( "Alt+P" ) );
96     openButtonMenu->addAction( qtr("&Stream"), this, SLOT( stream() ) ,
97                                     QKeySequence( "Alt+S" ) );
98     openButtonMenu->addAction( qtr("&Convert"), this, SLOT( transcode() ) ,
99                                     QKeySequence( "Alt+C" ) );
100
101     playButton->setMenu( openButtonMenu );
102
103     ui.buttonsBox->addButton( playButton, QDialogButtonBox::AcceptRole );
104     ui.buttonsBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
105
106     /* Force MRL update on tab change */
107     CONNECT( ui.Tab, currentChanged(int), this, signalCurrent());
108
109     CONNECT( fileOpenPanel, mrlUpdated( QString ), this, updateMRL(QString) );
110     CONNECT( netOpenPanel, mrlUpdated( QString ), this, updateMRL(QString) );
111     CONNECT( discOpenPanel, mrlUpdated( QString ), this, updateMRL(QString) );
112     CONNECT( captureOpenPanel, mrlUpdated( QString ), this, updateMRL(QString) );
113
114     CONNECT( fileOpenPanel, methodChanged( QString ),
115                                                  this, newCachingMethod(QString) );
116     CONNECT( netOpenPanel, methodChanged( QString ),
117                                                  this, newCachingMethod(QString) );
118     CONNECT( discOpenPanel, methodChanged( QString ),
119                                                  this, newCachingMethod(QString) );
120     CONNECT( captureOpenPanel, methodChanged( QString ),
121                                                  this, newCachingMethod(QString) );
122
123     /* Advanced frame Connects */
124     CONNECT( ui.slaveText, textChanged(QString), this, updateMRL());
125     CONNECT( ui.cacheSpinBox, valueChanged(int), this, updateMRL());
126     CONNECT( ui.startTimeSpinBox, valueChanged(int), this, updateMRL());
127     BUTTONACT( ui.advancedCheckBox , toggleAdvancedPanel() );
128
129     /* Buttons action */
130     BUTTONACT( playButton, play());
131     BUTTONACT( cancelButton, cancel());
132
133     /* At creation time, modify the default buttons */
134     if ( i_action_flag ) setMenuAction();
135
136     /* Initialize caching */
137     storedMethod = "";
138     newCachingMethod("file-caching");
139
140     mainHeight = advHeight = 0;
141 }
142
143 OpenDialog::~OpenDialog()
144 {
145 }
146
147 /* Finish the dialog and decide if you open another one after */
148 void OpenDialog::setMenuAction()
149 {
150     switch ( i_action_flag )
151     {
152         case OPEN_AND_STREAM:
153             playButton->setText( qtr("&Stream") );
154             BUTTONACT( playButton, stream() );
155             break;
156         case OPEN_AND_SAVE:
157             playButton->setText( qtr("&Convert / Save") );
158             BUTTONACT( playButton, transcode() );
159             break;
160         case OPEN_AND_ENQUEUE:
161             playButton->setText( qtr("&Enqueue") );
162             BUTTONACT( playButton, enqueue() );
163             break;
164         case OPEN_AND_PLAY:
165         default:
166             playButton->setText( qtr("&Play") );
167             BUTTONACT( playButton, play() );
168    }
169 }
170
171 void OpenDialog::showTab( int i_tab=0 )
172 {
173     this->show();
174     ui.Tab->setCurrentIndex( i_tab );
175 }
176
177 void OpenDialog::signalCurrent() {
178     if (ui.Tab->currentWidget() != NULL)
179         (dynamic_cast<OpenPanel *>( ui.Tab->currentWidget() ))->updateMRL();
180 }
181
182 void OpenDialog::toggleAdvancedPanel()
183 {
184     //FIXME does not work under Windows
185     if( ui.advancedFrame->isVisible() ) {
186         ui.advancedFrame->hide();
187 #ifndef WIN32
188         setMinimumHeight(1);
189         resize( width(), mainHeight );
190 #endif
191     } else {
192 #ifndef WIN32
193         if( mainHeight == 0 )
194             mainHeight = height();
195 #endif
196
197         ui.advancedFrame->show();
198 #ifndef WIN32
199         if( advHeight == 0 ) {
200             advHeight = height() - mainHeight;
201         }
202         resize( width(), mainHeight + advHeight );
203 #endif
204     }
205 }
206
207 /***********
208  * Actions *
209  ***********/
210
211 /* If Cancel is pressed or escaped */
212 void OpenDialog::cancel()
213 {
214     for( int i = 0; i <= OPEN_TAB_MAX; i++ ) 
215         dynamic_cast<OpenPanel*>(ui.Tab->widget( i ))->clear();
216     toggleVisible();
217     if( isModal() ) reject();
218 }
219
220 /* If EnterKey is pressed */
221 void OpenDialog::close()
222 {
223     /* FIXME */
224     if ( !i_action_flag )
225     {
226         play();
227     }
228     else
229     {
230         stream();
231     }
232 }
233
234 /* Play button */
235 void OpenDialog::play()
236 {
237     finish( false );
238 }
239
240 void OpenDialog::enqueue()
241 {
242     finish( true );
243 }
244
245 void OpenDialog::transcode()
246 {
247     stream( true );
248 }
249
250 void OpenDialog::stream( bool b_transcode_only )
251 {
252     /* not finished FIXME */
253     /* Should go through the finish function */
254     THEDP->streamingDialog( mrl, b_transcode_only );
255 }
256
257 void OpenDialog::finish( bool b_enqueue = false )
258 {
259     toggleVisible();
260     mrl = ui.advancedLineInput->text();
261
262     if( !isModal() )
263     {
264         QStringList tempMRL = SeparateEntries( mrl );
265         for( size_t i = 0; i < tempMRL.size(); i++ )
266         {
267             bool b_start = !i && !b_enqueue;
268             input_item_t *p_input;
269
270             p_input = input_ItemNew( p_intf, qtu( tempMRL[i] ), NULL );
271
272             /* Insert options */
273             while( i + 1 < tempMRL.size() && tempMRL[i + 1].startsWith( ":" ) )
274             {
275                 i++;
276                 input_ItemAddOption( p_input, qtu( tempMRL[i] ) );
277             }
278
279             /* Switch between enqueuing and starting the item */
280             if( b_start )
281             {
282                 playlist_AddInput( THEPL, p_input,
283                                    PLAYLIST_APPEND | PLAYLIST_GO,
284                                    PLAYLIST_END, VLC_TRUE, VLC_FALSE );
285             }
286             else
287             {
288                 playlist_AddInput( THEPL, p_input,
289                                    PLAYLIST_APPEND | PLAYLIST_PREPARSE,
290                                    PLAYLIST_END, VLC_TRUE, VLC_FALSE );
291             }
292         }
293     }
294     else
295         accept();
296 }
297
298
299 /* Update the MRL */
300 void OpenDialog::updateMRL( QString tempMRL )
301 {
302     mainMRL = tempMRL;
303     updateMRL();
304 }
305
306 void OpenDialog::updateMRL() {
307     mrl = mainMRL;
308     if( ui.slaveCheckbox->isChecked() ) {
309         mrl += " :input-slave=" + ui.slaveText->text();
310     }
311     int i_cache = config_GetInt( p_intf, qta(storedMethod) );
312     if( i_cache != ui.cacheSpinBox->value() ) {
313         mrl += QString(" :%1=%2").arg(storedMethod).
314                                   arg(ui.cacheSpinBox->value());
315     }
316     if( ui.startTimeSpinBox->value()) {
317         mrl += " :start-time=" + QString("%1").
318             arg(ui.startTimeSpinBox->value());
319     }
320     ui.advancedLineInput->setText(mrl);
321 }
322
323 void OpenDialog::newCachingMethod( QString method )
324 {
325     if( method != storedMethod ) {
326         storedMethod = method;
327         int i_value = config_GetInt( p_intf, qta( storedMethod ) );
328         ui.cacheSpinBox->setValue( i_value );
329     }
330 }
331
332 QStringList OpenDialog::SeparateEntries( QString entries )
333 {
334     bool b_quotes_mode = false;
335
336     QStringList entries_array;
337     QString entry;
338
339     int index = 0;
340     while( index < entries.size() )
341     {
342         int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
343         if( delim_pos < 0 ) delim_pos = entries.size() - 1;
344         entry += entries.mid( index, delim_pos - index + 1 );
345         index = delim_pos + 1;
346
347         if( entry.isEmpty() ) continue;
348
349         if( !b_quotes_mode && entry.endsWith( "\"" ) )
350         {
351             /* Enters quotes mode */
352             entry.truncate( entry.size() - 1 );
353             b_quotes_mode = true;
354         }
355         else if( b_quotes_mode && entry.endsWith( "\"" ) )
356         {
357             /* Finished the quotes mode */
358             entry.truncate( entry.size() - 1 );
359             b_quotes_mode = false;
360         }
361         else if( !b_quotes_mode && !entry.endsWith( "\"" ) )
362         {
363             /* we found a non-quoted standalone string */
364             if( index < entries.size() ||
365                 entry.endsWith( " " ) || entry.endsWith( "\t" ) ||
366                 entry.endsWith( "\r" ) || entry.endsWith( "\n" ) )
367                 entry.truncate( entry.size() - 1 );
368             if( !entry.isEmpty() ) entries_array.append( entry );
369             entry.clear();
370         }
371         else
372         {;}
373     }
374
375     if( !entry.isEmpty() ) entries_array.append( entry );
376
377     return entries_array;
378 }