Fix segfault when canceling the open dialog. Patch by RĂ©mi Duraffort.
[vlc/vlc-skelet.git] / modules / gui / qt4 / dialogs / open.cpp
blob3449a820b156123885ff71a6ada63bf4ea37dfe9
1 /*****************************************************************************
2 * open.cpp : Advanced open dialog
3 *****************************************************************************
4 * Copyright (C) 2006-2007 the VideoLAN team
5 * $Id$
7 * Authors: Jean-Baptiste Kempf <jb@videolan.org>
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.
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.
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 *****************************************************************************/
24 #include "input_manager.hpp"
26 #include "dialogs/open.hpp"
27 #include "components/open.hpp"
29 #include <QTabWidget>
30 #include <QGridLayout>
31 #include <QFileDialog>
32 #include <QRegExp>
33 #include <QMenu>
35 OpenDialog *OpenDialog::instance = NULL;
37 OpenDialog::OpenDialog( QWidget *parent, intf_thread_t *_p_intf, bool modal,
38 int _action_flag ) : QVLCDialog( parent, _p_intf )
40 setModal( modal );
41 i_action_flag = _action_flag;
43 /* Basic Creation of the Window */
44 ui.setupUi( this );
45 setWindowTitle( qtr("Open" ) );
46 resize( 410, 300);
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 );
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" ) );
61 /* Hide the Slave input widgets */
62 ui.slaveLabel->hide();
63 ui.slaveText->hide();
64 ui.slaveBrowseButton->hide();
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 );
72 /* Buttons Creation */
73 QSizePolicy buttonSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
74 buttonSizePolicy.setHorizontalStretch(0);
75 buttonSizePolicy.setVerticalStretch(0);
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 );
85 /* Cancel Button */
86 cancelButton = new QPushButton();
87 cancelButton->setText( qtr( "&Cancel" ) );
88 cancelButton->setSizePolicy( buttonSizePolicy );
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" ) );
101 playButton->setMenu( openButtonMenu );
103 ui.buttonsBox->addButton( playButton, QDialogButtonBox::AcceptRole );
104 ui.buttonsBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
106 /* Force MRL update on tab change */
107 CONNECT( ui.Tab, currentChanged(int), this, signalCurrent());
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) );
114 CONNECT( fileOpenPanel, methodChanged( QString ),
115 this, newCachingMethod(QString) );
116 CONNECT( netOpenPanel, methodChanged( QString ),
117 this, newCachingMethod(QString) );
118 CONNECT( discOpenPanel, methodChanged( QString ),
119 this, newCachingMethod(QString) );
120 CONNECT( captureOpenPanel, methodChanged( QString ),
121 this, newCachingMethod(QString) );
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() );
129 /* Buttons action */
130 BUTTONACT( playButton, play());
131 BUTTONACT( cancelButton, cancel());
133 /* At creation time, modify the default buttons */
134 if ( i_action_flag ) setMenuAction();
136 /* Initialize caching */
137 storedMethod = "";
138 newCachingMethod("file-caching");
140 mainHeight = advHeight = 0;
143 OpenDialog::~OpenDialog()
147 /* Finish the dialog and decide if you open another one after */
148 void OpenDialog::setMenuAction()
150 switch ( i_action_flag )
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() );
171 void OpenDialog::showTab( int i_tab=0 )
173 this->show();
174 ui.Tab->setCurrentIndex( i_tab );
177 void OpenDialog::signalCurrent() {
178 if (ui.Tab->currentWidget() != NULL)
179 (dynamic_cast<OpenPanel *>( ui.Tab->currentWidget() ))->updateMRL();
182 void OpenDialog::toggleAdvancedPanel()
184 //FIXME does not work under Windows
185 if( ui.advancedFrame->isVisible() ) {
186 ui.advancedFrame->hide();
187 #ifndef WIN32
188 setMinimumHeight(1);
189 resize( width(), mainHeight );
190 #endif
191 } else {
192 #ifndef WIN32
193 if( mainHeight == 0 )
194 mainHeight = height();
195 #endif
197 ui.advancedFrame->show();
198 #ifndef WIN32
199 if( advHeight == 0 ) {
200 advHeight = height() - mainHeight;
202 resize( width(), mainHeight + advHeight );
203 #endif
207 /***********
208 * Actions *
209 ***********/
211 /* If Cancel is pressed or escaped */
212 void OpenDialog::cancel()
214 for( int i = 0; i < OPEN_TAB_MAX; i++ )
215 dynamic_cast<OpenPanel*>(ui.Tab->widget( i ))->clear();
216 toggleVisible();
217 if( isModal() ) reject();
220 /* If EnterKey is pressed */
221 void OpenDialog::close()
223 /* FIXME */
224 if ( !i_action_flag )
226 play();
228 else
230 stream();
234 /* Play button */
235 void OpenDialog::play()
237 finish( false );
240 void OpenDialog::enqueue()
242 finish( true );
245 void OpenDialog::transcode()
247 stream( true );
250 void OpenDialog::stream( bool b_transcode_only )
252 /* not finished FIXME */
253 /* Should go through the finish function */
254 THEDP->streamingDialog( mrl, b_transcode_only );
257 void OpenDialog::finish( bool b_enqueue = false )
259 toggleVisible();
260 mrl = ui.advancedLineInput->text();
262 if( !isModal() )
264 QStringList tempMRL = SeparateEntries( mrl );
265 for( size_t i = 0; i < tempMRL.size(); i++ )
267 bool b_start = !i && !b_enqueue;
268 input_item_t *p_input;
270 p_input = input_ItemNew( p_intf, qtu( tempMRL[i] ), NULL );
272 /* Insert options */
273 while( i + 1 < tempMRL.size() && tempMRL[i + 1].startsWith( ":" ) )
275 i++;
276 input_ItemAddOption( p_input, qtu( tempMRL[i] ) );
279 /* Switch between enqueuing and starting the item */
280 if( b_start )
282 playlist_AddInput( THEPL, p_input,
283 PLAYLIST_APPEND | PLAYLIST_GO,
284 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
286 else
288 playlist_AddInput( THEPL, p_input,
289 PLAYLIST_APPEND | PLAYLIST_PREPARSE,
290 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
294 else
295 accept();
299 /* Update the MRL */
300 void OpenDialog::updateMRL( QString tempMRL )
302 mainMRL = tempMRL;
303 updateMRL();
306 void OpenDialog::updateMRL() {
307 mrl = mainMRL;
308 if( ui.slaveCheckbox->isChecked() ) {
309 mrl += " :input-slave=" + ui.slaveText->text();
311 int i_cache = config_GetInt( p_intf, qta(storedMethod) );
312 if( i_cache != ui.cacheSpinBox->value() ) {
313 mrl += QString(" :%1=%2").arg(storedMethod).
314 arg(ui.cacheSpinBox->value());
316 if( ui.startTimeSpinBox->value()) {
317 mrl += " :start-time=" + QString("%1").
318 arg(ui.startTimeSpinBox->value());
320 ui.advancedLineInput->setText(mrl);
323 void OpenDialog::newCachingMethod( QString method )
325 if( method != storedMethod ) {
326 storedMethod = method;
327 int i_value = config_GetInt( p_intf, qta( storedMethod ) );
328 ui.cacheSpinBox->setValue( i_value );
332 QStringList OpenDialog::SeparateEntries( QString entries )
334 bool b_quotes_mode = false;
336 QStringList entries_array;
337 QString entry;
339 int index = 0;
340 while( index < entries.size() )
342 int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
343 if( delim_pos < 0 ) delim_pos = entries.size() - 1;
344 entry += entries.mid( index, delim_pos - index + 1 );
345 index = delim_pos + 1;
347 if( entry.isEmpty() ) continue;
349 if( !b_quotes_mode && entry.endsWith( "\"" ) )
351 /* Enters quotes mode */
352 entry.truncate( entry.size() - 1 );
353 b_quotes_mode = true;
355 else if( b_quotes_mode && entry.endsWith( "\"" ) )
357 /* Finished the quotes mode */
358 entry.truncate( entry.size() - 1 );
359 b_quotes_mode = false;
361 else if( !b_quotes_mode && !entry.endsWith( "\"" ) )
363 /* we found a non-quoted standalone string */
364 if( index < entries.size() ||
365 entry.endsWith( " " ) || entry.endsWith( "\t" ) ||
366 entry.endsWith( "\r" ) || entry.endsWith( "\n" ) )
367 entry.truncate( entry.size() - 1 );
368 if( !entry.isEmpty() ) entries_array.append( entry );
369 entry.clear();
371 else
375 if( !entry.isEmpty() ) entries_array.append( entry );
377 return entries_array;