]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/open.cpp
Qt4 - Coding style fixes by RĂ©mi Duraffort.
[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, selectSlots() );
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         break;
154     case OPEN_AND_SAVE:
155         playButton->setText( qtr( "&Convert / Save" ) );
156         break;
157     case OPEN_AND_ENQUEUE:
158         playButton->setText( qtr( "&Enqueue" ) );
159         break;
160     case OPEN_AND_PLAY:
161     default:
162         playButton->setText( qtr( "&Play" ) );
163    }
164 }
165
166 void OpenDialog::showTab( int i_tab=0 )
167 {
168     this->show();
169     ui.Tab->setCurrentIndex( i_tab );
170 }
171
172 void OpenDialog::signalCurrent() {
173     if (ui.Tab->currentWidget() != NULL)
174         (dynamic_cast<OpenPanel *>( ui.Tab->currentWidget() ))->updateMRL();
175 }
176
177 void OpenDialog::toggleAdvancedPanel()
178 {
179     //FIXME does not work under Windows
180     if( ui.advancedFrame->isVisible() ) {
181         ui.advancedFrame->hide();
182 #ifndef WIN32
183         setMinimumHeight( 1 );
184         resize( width(), mainHeight );
185 #endif
186     } else {
187 #ifndef WIN32
188         if( mainHeight == 0 )
189             mainHeight = height();
190 #endif
191
192         ui.advancedFrame->show();
193 #ifndef WIN32
194         if( advHeight == 0 ) {
195             advHeight = height() - mainHeight;
196         }
197         resize( width(), mainHeight + advHeight );
198 #endif
199     }
200 }
201
202 /***********
203  * Actions *
204  ***********/
205
206 /* If Cancel is pressed or escaped */
207 void OpenDialog::cancel()
208 {
209     for( int i = 0; i < OPEN_TAB_MAX; i++ )
210         dynamic_cast<OpenPanel*>(ui.Tab->widget( i ))->clear();
211     toggleVisible();
212     if( isModal() ) reject();
213 }
214
215 /* If EnterKey is pressed */
216 void OpenDialog::close()
217 {
218     /* FIXME */
219     if ( !i_action_flag )
220     {
221         play();
222     }
223     else
224     {
225         stream();
226     }
227 }
228
229 /* Play button */
230 void OpenDialog::selectSlots()
231 {
232     switch ( i_action_flag )
233     {
234     case OPEN_AND_STREAM:
235         stream();
236         break;
237     case OPEN_AND_SAVE:
238         transcode();
239         break;
240     case OPEN_AND_ENQUEUE:
241         enqueue();
242         break;
243     case OPEN_AND_PLAY:
244     default:
245         play();
246     }
247 }
248
249 void OpenDialog::play()
250 {
251     finish( false );
252 }
253
254 void OpenDialog::enqueue()
255 {
256     finish( true );
257 }
258
259 void OpenDialog::transcode()
260 {
261     stream( true );
262 }
263
264 void OpenDialog::stream( bool b_transcode_only )
265 {
266     /* not finished FIXME */
267     /* Should go through the finish function */
268     THEDP->streamingDialog( mrl, b_transcode_only );
269 }
270
271 void OpenDialog::finish( bool b_enqueue = false )
272 {
273     toggleVisible();
274     mrl = ui.advancedLineInput->text();
275
276     if( !isModal() )
277     {
278         QStringList tempMRL = SeparateEntries( mrl );
279         for( size_t i = 0; i < tempMRL.size(); i++ )
280         {
281             bool b_start = !i && !b_enqueue;
282             input_item_t *p_input;
283
284             p_input = input_ItemNew( p_intf, qtu( tempMRL[i] ), NULL );
285
286             /* Insert options */
287             while( i + 1 < tempMRL.size() && tempMRL[i + 1].startsWith( ":" ) )
288             {
289                 i++;
290                 input_ItemAddOption( p_input, qtu( tempMRL[i] ) );
291             }
292
293             /* Switch between enqueuing and starting the item */
294             if( b_start )
295             {
296                 playlist_AddInput( THEPL, p_input,
297                                    PLAYLIST_APPEND | PLAYLIST_GO,
298                                    PLAYLIST_END, VLC_TRUE, VLC_FALSE );
299             }
300             else
301             {
302                 playlist_AddInput( THEPL, p_input,
303                                    PLAYLIST_APPEND | PLAYLIST_PREPARSE,
304                                    PLAYLIST_END, VLC_TRUE, VLC_FALSE );
305             }
306         }
307     }
308     else
309         accept();
310 }
311
312
313 /* Update the MRL */
314 void OpenDialog::updateMRL( QString tempMRL )
315 {
316     mainMRL = tempMRL;
317     updateMRL();
318 }
319
320 void OpenDialog::updateMRL() {
321     mrl = mainMRL;
322     if( ui.slaveCheckbox->isChecked() ) {
323         mrl += " :input-slave=" + ui.slaveText->text();
324     }
325     int i_cache = config_GetInt( p_intf, qta( storedMethod ) );
326     if( i_cache != ui.cacheSpinBox->value() ) {
327         mrl += QString( " :%1=%2" ).arg( storedMethod ).
328                                   arg( ui.cacheSpinBox->value() );
329     }
330     if( ui.startTimeSpinBox->value() ) {
331         mrl += " :start-time=" + QString( "%1" ).
332             arg( ui.startTimeSpinBox->value() );
333     }
334     ui.advancedLineInput->setText( mrl );
335 }
336
337 void OpenDialog::newCachingMethod( QString method )
338 {
339     if( method != storedMethod ) {
340         storedMethod = method;
341         int i_value = config_GetInt( p_intf, qta( storedMethod ) );
342         ui.cacheSpinBox->setValue( i_value );
343     }
344 }
345
346 QStringList OpenDialog::SeparateEntries( QString entries )
347 {
348     bool b_quotes_mode = false;
349
350     QStringList entries_array;
351     QString entry;
352
353     int index = 0;
354     while( index < entries.size() )
355     {
356         int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
357         if( delim_pos < 0 ) delim_pos = entries.size() - 1;
358         entry += entries.mid( index, delim_pos - index + 1 );
359         index = delim_pos + 1;
360
361         if( entry.isEmpty() ) continue;
362
363         if( !b_quotes_mode && entry.endsWith( "\"" ) )
364         {
365             /* Enters quotes mode */
366             entry.truncate( entry.size() - 1 );
367             b_quotes_mode = true;
368         }
369         else if( b_quotes_mode && entry.endsWith( "\"" ) )
370         {
371             /* Finished the quotes mode */
372             entry.truncate( entry.size() - 1 );
373             b_quotes_mode = false;
374         }
375         else if( !b_quotes_mode && !entry.endsWith( "\"" ) )
376         {
377             /* we found a non-quoted standalone string */
378             if( index < entries.size() ||
379                 entry.endsWith( " " ) || entry.endsWith( "\t" ) ||
380                 entry.endsWith( "\r" ) || entry.endsWith( "\n" ) )
381                 entry.truncate( entry.size() - 1 );
382             if( !entry.isEmpty() ) entries_array.append( entry );
383             entry.clear();
384         }
385         else
386         {;}
387     }
388
389     if( !entry.isEmpty() ) entries_array.append( entry );
390
391     return entries_array;
392 }