]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/open.cpp
Qt4 - Don't use things that can change from one version to another one of Qt.
[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 advancedPanel */
62     if(! config_GetInt( p_intf, "qt-adv-options") )
63     {
64         ui.advancedFrame->hide();
65     }
66     else
67     {
68         ui.advancedCheckBox->setCheckState( Qt::Checked );
69     }
70
71     ui.slaveLabel->hide();
72     ui.slaveText->hide();
73     ui.slaveBrowseButton->hide();
74
75     /* Buttons Creation */
76     QSizePolicy buttonSizePolicy( QSizePolicy::Expanding, QSizePolicy::GrowFlag );
77     buttonSizePolicy.setHorizontalStretch(0);
78     buttonSizePolicy.setVerticalStretch(0);
79
80     playButton = new QToolButton( this );
81     playButton->setText( qtr( "&Play" ) );
82     playButton->setSizePolicy( buttonSizePolicy );
83     playButton->setMinimumSize( QSize(90, 0) );
84     playButton->setPopupMode( QToolButton::MenuButtonPopup );
85     playButton->setToolButtonStyle( Qt::ToolButtonTextOnly );
86
87     cancelButton = new QPushButton();
88     cancelButton->setText( qtr( "&Cancel" ) );
89     cancelButton->setSizePolicy( buttonSizePolicy );
90
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,
113                                                          updateMRL(QString) );
114
115     CONNECT( fileOpenPanel, methodChanged( QString ),
116                                                  this, newMethod(QString) );
117     CONNECT( netOpenPanel, methodChanged( QString ),
118                                                  this, newMethod(QString) );
119     CONNECT( discOpenPanel, methodChanged( QString ),
120                                                  this, newMethod(QString) );
121     CONNECT( captureOpenPanel, methodChanged( QString ),
122                                                  this, newMethod(QString) );
123
124     /* Advanced frame Connects */
125     CONNECT( ui.slaveText, textChanged(QString), this, updateMRL());
126     CONNECT( ui.cacheSpinBox, valueChanged(int), this, updateMRL());
127     CONNECT( ui.startTimeSpinBox, valueChanged(int), this, updateMRL());
128     BUTTONACT( ui.advancedCheckBox , toggleAdvancedPanel() );
129
130     /* Buttons action */
131     BUTTONACT( playButton, play());
132     BUTTONACT( cancelButton, cancel());
133
134     /* At creation time, modify the default buttons */
135     if ( i_action_flag ) setMenuAction();
136
137
138     /* Initialize caching */
139     storedMethod = "";
140     newMethod("file-caching");
141
142     mainHeight = advHeight = 0;
143 }
144
145 OpenDialog::~OpenDialog()
146 {
147 }
148
149 /* Finish the dialog and decide if you open another one after */
150 void OpenDialog::setMenuAction()
151 {
152     switch ( i_action_flag )
153     {
154         case OPEN_AND_STREAM:
155             playButton->setText( qtr("&Stream") );
156             BUTTONACT( playButton, stream() );
157             break;
158         case OPEN_AND_SAVE:
159             playButton->setText( qtr("&Convert / Save") );
160             BUTTONACT( playButton, transcode() );
161             break;
162         case ENQUEUE:
163             playButton->setText( qtr("&Enqueue") );
164             BUTTONACT( playButton, enqueue() );
165             break;
166         case OPEN_AND_PLAY:
167         default:
168             playButton->setText( qtr("&Play") );
169             BUTTONACT( playButton, play() );
170    }
171 }
172
173 void OpenDialog::showTab(int i_tab=0)
174 {
175     this->show();
176     ui.Tab->setCurrentIndex(i_tab);
177 }
178
179 void OpenDialog::signalCurrent() {
180     if (ui.Tab->currentWidget() != NULL) {
181         (dynamic_cast<OpenPanel*>(ui.Tab->currentWidget()))->updateMRL();
182     }
183 }
184
185 /***********
186  * Actions *
187  ***********/
188
189 /* If Cancel is pressed or escaped */
190 void OpenDialog::cancel()
191 {
192     fileOpenPanel->clear();
193     this->toggleVisible();
194     if( isModal() )
195         reject();
196 }
197
198 /* If EnterKey is pressed */
199 void OpenDialog::close()
200 {
201     /* FIXME */
202     if ( !i_action_flag )
203     {
204         play();
205     }
206     else
207     {
208         stream();
209     }
210 }
211
212 /* Play button */
213 void OpenDialog::play()
214 {
215     finish( false );
216 }
217
218 void OpenDialog::enqueue()
219 {
220     finish( true );
221 }
222
223 void OpenDialog::transcode()
224 {
225     stream( true );
226 }
227
228 void OpenDialog::stream( bool b_transcode_only )
229 {
230     /* not finished FIXME */
231     /* Should go through the finish function */
232     THEDP->streamingDialog( mrl, b_transcode_only );
233 }
234
235 void OpenDialog::finish( bool b_enqueue = false )
236 {
237     this->toggleVisible();
238     mrl = ui.advancedLineInput->text();
239
240     if( !isModal() )
241     {
242         QStringList tempMRL = SeparateEntries( mrl );
243         for( size_t i = 0; i < tempMRL.size(); i++ )
244         {
245             bool b_start = !i && !b_enqueue;
246             input_item_t *p_input;
247             const char *psz_utf8 = qtu( tempMRL[i] );
248
249             p_input = input_ItemNew( p_intf, psz_utf8, NULL );
250
251             /* Insert options */
252             while( i + 1 < tempMRL.size() && tempMRL[i + 1].startsWith( ":" ) )
253             {
254                 i++;
255                 psz_utf8 = qtu( tempMRL[i] );
256                 input_ItemAddOption( p_input, psz_utf8 );
257             }
258
259             if( b_start )
260             {
261                 playlist_AddInput( THEPL, p_input,
262                                    PLAYLIST_APPEND | PLAYLIST_GO,
263                                    PLAYLIST_END, VLC_TRUE, VLC_FALSE );
264             }
265             else
266             {
267                 playlist_AddInput( THEPL, p_input,
268                                    PLAYLIST_APPEND|PLAYLIST_PREPARSE,
269                                    PLAYLIST_END, VLC_TRUE, VLC_FALSE );
270             }
271         }
272     }
273     else
274         accept();
275 }
276
277 void OpenDialog::toggleAdvancedPanel()
278 {
279     //FIXME does not work under Windows
280     if( ui.advancedFrame->isVisible() ) {
281         ui.advancedFrame->hide();
282 #ifndef WIN32
283         setMinimumHeight(1);
284         resize( width(), mainHeight );
285 #endif
286     } else {
287 #ifndef WIN32
288         if( mainHeight == 0 )
289             mainHeight = height();
290 #endif
291
292         ui.advancedFrame->show();
293 #ifndef WIN32
294         if( advHeight == 0 ) {
295             advHeight = height() - mainHeight;
296         }
297         resize( width(), mainHeight + advHeight );
298 #endif
299     }
300 }
301
302 void OpenDialog::updateMRL() {
303     mrl = mainMRL;
304     if( ui.slaveCheckbox->isChecked() ) {
305         mrl += " :input-slave=" + ui.slaveText->text();
306     }
307     int i_cache = config_GetInt( p_intf, qta(storedMethod) );
308     if( i_cache != ui.cacheSpinBox->value() ) {
309         mrl += QString(" :%1=%2").arg(storedMethod).
310                                   arg(ui.cacheSpinBox->value());
311     }
312     if( ui.startTimeSpinBox->value()) {
313         mrl += " :start-time=" + QString("%1").
314             arg(ui.startTimeSpinBox->value());
315     }
316     ui.advancedLineInput->setText(mrl);
317 }
318
319 void OpenDialog::updateMRL(QString tempMRL)
320 {
321     mainMRL = tempMRL;
322     updateMRL();
323 }
324
325 void OpenDialog::newMethod(QString method)
326 {
327     if( method != storedMethod ) {
328         storedMethod = method;
329         int i_value = config_GetInt( p_intf, qta(storedMethod) );
330         ui.cacheSpinBox->setValue(i_value);
331     }
332 }
333
334 QStringList OpenDialog::SeparateEntries( QString entries )
335 {
336     bool b_quotes_mode = false;
337
338     QStringList entries_array;
339     QString entry;
340
341     int index = 0;
342     while( index < entries.size() )
343     {
344         int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
345         if( delim_pos < 0 ) delim_pos = entries.size() - 1;
346         entry += entries.mid( index, delim_pos - index + 1 );
347         index = delim_pos + 1;
348
349         if( entry.isEmpty() ) continue;
350
351         if( !b_quotes_mode && entry.endsWith( "\"" ) )
352         {
353             /* Enters quotes mode */
354             entry.truncate( entry.size() - 1 );
355             b_quotes_mode = true;
356         }
357         else if( b_quotes_mode && entry.endsWith( "\"" ) )
358         {
359             /* Finished the quotes mode */
360             entry.truncate( entry.size() - 1 );
361             b_quotes_mode = false;
362         }
363         else if( !b_quotes_mode && !entry.endsWith( "\"" ) )
364         {
365             /* we found a non-quoted standalone string */
366             if( index < entries.size() ||
367                 entry.endsWith( " " ) || entry.endsWith( "\t" ) ||
368                 entry.endsWith( "\r" ) || entry.endsWith( "\n" ) )
369                 entry.truncate( entry.size() - 1 );
370             if( !entry.isEmpty() ) entries_array.append( entry );
371             entry.clear();
372         }
373         else
374         {;}
375     }
376
377     if( !entry.isEmpty() ) entries_array.append( entry );
378
379     return entries_array;
380 }