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