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