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