]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/open.cpp
Qt4 - Open Dialog: Code cosmetic.
[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, newMethod(QString) );
116     CONNECT( netOpenPanel, methodChanged( QString ),
117                                                  this, newMethod(QString) );
118     CONNECT( discOpenPanel, methodChanged( QString ),
119                                                  this, newMethod(QString) );
120     CONNECT( captureOpenPanel, methodChanged( QString ),
121                                                  this, newMethod(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     newMethod("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 /***********
183  * Actions *
184  ***********/
185
186 /* If Cancel is pressed or escaped */
187 void OpenDialog::cancel()
188 {
189     fileOpenPanel->clear();
190     this->toggleVisible();
191     if( isModal() ) reject();
192 }
193
194 /* If EnterKey is pressed */
195 void OpenDialog::close()
196 {
197     /* FIXME */
198     if ( !i_action_flag )
199     {
200         play();
201     }
202     else
203     {
204         stream();
205     }
206 }
207
208 /* Play button */
209 void OpenDialog::play()
210 {
211     finish( false );
212 }
213
214 void OpenDialog::enqueue()
215 {
216     finish( true );
217 }
218
219 void OpenDialog::transcode()
220 {
221     stream( true );
222 }
223
224 void OpenDialog::stream( bool b_transcode_only )
225 {
226     /* not finished FIXME */
227     /* Should go through the finish function */
228     THEDP->streamingDialog( mrl, b_transcode_only );
229 }
230
231 void OpenDialog::finish( bool b_enqueue = false )
232 {
233     this->toggleVisible();
234     mrl = ui.advancedLineInput->text();
235
236     if( !isModal() )
237     {
238         QStringList tempMRL = SeparateEntries( mrl );
239         for( size_t i = 0; i < tempMRL.size(); i++ )
240         {
241             bool b_start = !i && !b_enqueue;
242             input_item_t *p_input;
243             const char *psz_utf8 = qtu( tempMRL[i] );
244
245             p_input = input_ItemNew( p_intf, psz_utf8, NULL );
246
247             /* Insert options */
248             while( i + 1 < tempMRL.size() && tempMRL[i + 1].startsWith( ":" ) )
249             {
250                 i++;
251                 psz_utf8 = qtu( tempMRL[i] );
252                 input_ItemAddOption( p_input, psz_utf8 );
253             }
254
255             if( b_start )
256             {
257                 playlist_AddInput( THEPL, p_input,
258                                    PLAYLIST_APPEND | PLAYLIST_GO,
259                                    PLAYLIST_END, VLC_TRUE, VLC_FALSE );
260             }
261             else
262             {
263                 playlist_AddInput( THEPL, p_input,
264                                    PLAYLIST_APPEND|PLAYLIST_PREPARSE,
265                                    PLAYLIST_END, VLC_TRUE, VLC_FALSE );
266             }
267         }
268     }
269     else
270         accept();
271 }
272
273 void OpenDialog::toggleAdvancedPanel()
274 {
275     //FIXME does not work under Windows
276     if( ui.advancedFrame->isVisible() ) {
277         ui.advancedFrame->hide();
278 #ifndef WIN32
279         setMinimumHeight(1);
280         resize( width(), mainHeight );
281 #endif
282     } else {
283 #ifndef WIN32
284         if( mainHeight == 0 )
285             mainHeight = height();
286 #endif
287
288         ui.advancedFrame->show();
289 #ifndef WIN32
290         if( advHeight == 0 ) {
291             advHeight = height() - mainHeight;
292         }
293         resize( width(), mainHeight + advHeight );
294 #endif
295     }
296 }
297
298 void OpenDialog::updateMRL() {
299     mrl = mainMRL;
300     if( ui.slaveCheckbox->isChecked() ) {
301         mrl += " :input-slave=" + ui.slaveText->text();
302     }
303     int i_cache = config_GetInt( p_intf, qta(storedMethod) );
304     if( i_cache != ui.cacheSpinBox->value() ) {
305         mrl += QString(" :%1=%2").arg(storedMethod).
306                                   arg(ui.cacheSpinBox->value());
307     }
308     if( ui.startTimeSpinBox->value()) {
309         mrl += " :start-time=" + QString("%1").
310             arg(ui.startTimeSpinBox->value());
311     }
312     ui.advancedLineInput->setText(mrl);
313 }
314
315 void OpenDialog::updateMRL(QString tempMRL)
316 {
317     mainMRL = tempMRL;
318     updateMRL();
319 }
320
321 void OpenDialog::newMethod(QString method)
322 {
323     if( method != storedMethod ) {
324         storedMethod = method;
325         int i_value = config_GetInt( p_intf, qta(storedMethod) );
326         ui.cacheSpinBox->setValue(i_value);
327     }
328 }
329
330 QStringList OpenDialog::SeparateEntries( QString entries )
331 {
332     bool b_quotes_mode = false;
333
334     QStringList entries_array;
335     QString entry;
336
337     int index = 0;
338     while( index < entries.size() )
339     {
340         int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
341         if( delim_pos < 0 ) delim_pos = entries.size() - 1;
342         entry += entries.mid( index, delim_pos - index + 1 );
343         index = delim_pos + 1;
344
345         if( entry.isEmpty() ) continue;
346
347         if( !b_quotes_mode && entry.endsWith( "\"" ) )
348         {
349             /* Enters quotes mode */
350             entry.truncate( entry.size() - 1 );
351             b_quotes_mode = true;
352         }
353         else if( b_quotes_mode && entry.endsWith( "\"" ) )
354         {
355             /* Finished the quotes mode */
356             entry.truncate( entry.size() - 1 );
357             b_quotes_mode = false;
358         }
359         else if( !b_quotes_mode && !entry.endsWith( "\"" ) )
360         {
361             /* we found a non-quoted standalone string */
362             if( index < entries.size() ||
363                 entry.endsWith( " " ) || entry.endsWith( "\t" ) ||
364                 entry.endsWith( "\r" ) || entry.endsWith( "\n" ) )
365                 entry.truncate( entry.size() - 1 );
366             if( !entry.isEmpty() ) entries_array.append( entry );
367             entry.clear();
368         }
369         else
370         {;}
371     }
372
373     if( !entry.isEmpty() ) entries_array.append( entry );
374
375     return entries_array;
376 }