]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/open.cpp
Qt4 - Open Dialog. Use #defines for tab naming to avoid miscomprehensions. Cosmectics.
[vlc] / modules / gui / qt4 / dialogs / open.cpp
1 /*****************************************************************************
2  * open.cpp : Advanced open dialog
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id: streaminfo.cpp 16816 2006-09-23 20:56:52Z jb $
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 <QTabWidget>
25 #include <QGridLayout>
26 #include <QFileDialog>
27 #include <QRegExp>
28 #include <QMenu>
29 #include "dialogs/open.hpp"
30 #include "components/open.hpp"
31
32 #include "qt4.hpp"
33 #include "util/qvlcframe.hpp"
34
35 #include "input_manager.hpp"
36 #include "dialogs_provider.hpp"
37
38 OpenDialog *OpenDialog::instance = NULL;
39
40 OpenDialog::OpenDialog( QWidget *parent, intf_thread_t *_p_intf, bool modal,
41                         bool _stream_after ) :  QVLCDialog( parent, _p_intf )
42 {
43     setModal( modal );
44     b_stream_after = _stream_after;
45
46     ui.setupUi( this );
47     setWindowTitle( qtr("Open" ) );
48     resize( 500, 300);
49
50     /* Tab definition and creation */
51     fileOpenPanel = new FileOpenPanel( ui.Tab, p_intf );
52     discOpenPanel = new DiscOpenPanel( ui.Tab, p_intf );
53     netOpenPanel = new NetOpenPanel( ui.Tab, p_intf );
54     captureOpenPanel = new CaptureOpenPanel( ui.Tab, p_intf );
55
56     ui.Tab->insertTab( OPEN_FILE_TAB, fileOpenPanel, qtr( "&File" ) );
57     ui.Tab->insertTab( OPEN_DISC_TAB, discOpenPanel, qtr( "&Disc" ) );
58     ui.Tab->insertTab( OPEN_NETWORK_TAB, netOpenPanel, qtr( "&Network" ) );
59     ui.Tab->insertTab( OPEN_CAPTURE_TAB, captureOpenPanel,
60                                 qtr( "Capture &Device" ) );
61
62     /* Hide the advancedPanel */
63     ui.advancedFrame->hide();
64
65     /* Buttons Creation */
66     QSizePolicy buttonSizePolicy( static_cast<QSizePolicy::Policy>(7),
67                                   static_cast<QSizePolicy::Policy>(1) );
68     buttonSizePolicy.setHorizontalStretch(0);
69     buttonSizePolicy.setVerticalStretch(0);
70
71     playButton = new QToolButton();
72     playButton->setText( qtr( "&Play" ) );
73     playButton->setSizePolicy( buttonSizePolicy );
74     playButton->setMinimumSize( QSize(90, 0) );
75     playButton->setPopupMode( QToolButton::MenuButtonPopup );
76     playButton->setToolButtonStyle( Qt::ToolButtonTextOnly );
77
78     cancelButton = new QToolButton();
79     cancelButton->setText( qtr( "&Cancel" ) );
80     cancelButton->setSizePolicy( buttonSizePolicy );
81
82     QMenu * openButtonMenu = new QMenu( "Open" );
83     openButtonMenu->addAction( qtr("&Enqueue"), this, SLOT( enqueue() ),
84                                     QKeySequence( "Alt+E") );
85     openButtonMenu->addAction( qtr("&Play"), this, SLOT( play() ),
86                                     QKeySequence( "Alt+P" ) );
87     openButtonMenu->addAction( qtr("&Stream"), this, SLOT( stream() ) ,
88                                     QKeySequence( "Alt+S" ) );
89
90     playButton->setMenu( openButtonMenu );
91
92     ui.buttonsBox->addButton( playButton, QDialogButtonBox::AcceptRole );
93     ui.buttonsBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
94
95
96     /* Force MRL update on tab change */
97     CONNECT( ui.Tab, currentChanged(int), this, signalCurrent());
98
99     CONNECT( fileOpenPanel, mrlUpdated( QString ), this, updateMRL(QString) );
100     CONNECT( netOpenPanel, mrlUpdated( QString ), this, updateMRL(QString) );
101     CONNECT( discOpenPanel, mrlUpdated( QString ), this, updateMRL(QString) );
102     CONNECT( captureOpenPanel, mrlUpdated( QString ), this,
103             updateMRL(QString) );
104
105     CONNECT( fileOpenPanel, methodChanged( QString ),
106              this, newMethod(QString) );
107     CONNECT( netOpenPanel, methodChanged( QString ),
108              this, newMethod(QString) );
109     CONNECT( discOpenPanel, methodChanged( QString ),
110              this, newMethod(QString) );
111
112     CONNECT( ui.slaveText, textChanged(QString), this, updateMRL());
113     CONNECT( ui.cacheSpinBox, valueChanged(int), this, updateMRL());
114     CONNECT( ui.startTimeSpinBox, valueChanged(int), this, updateMRL());
115
116     /* Buttons action */
117     BUTTONACT( playButton, play());
118     BUTTONACT( cancelButton, cancel());
119
120     if ( b_stream_after ) setAfter();
121
122     BUTTONACT( ui.advancedCheckBox , toggleAdvancedPanel() );
123
124     /* Initialize caching */
125     storedMethod = "";
126     newMethod("file-caching");
127
128     mainHeight = advHeight = 0;
129 }
130
131 OpenDialog::~OpenDialog()
132 {
133 }
134
135 void OpenDialog::setAfter()
136 {
137     if (!b_stream_after )
138     {
139         playButton->setText( qtr("&Play") );
140         BUTTONACT( playButton, play() );
141     }
142     else
143     {
144         playButton->setText( qtr("&Stream") );
145         BUTTONACT( playButton, stream() );
146     }
147 }
148
149 void OpenDialog::showTab(int i_tab=0)
150 {
151     this->show();
152     ui.Tab->setCurrentIndex(i_tab);
153 }
154
155 void OpenDialog::signalCurrent() {
156     if (ui.Tab->currentWidget() != NULL) {
157         (dynamic_cast<OpenPanel*>(ui.Tab->currentWidget()))->updateMRL();
158     }
159 }
160
161 /*********** 
162  * Actions *
163  ***********/
164
165 /* If Cancel is pressed or escaped */
166 void OpenDialog::cancel()
167 {
168     fileOpenPanel->clear();
169     this->toggleVisible();
170     if( isModal() )
171         reject();
172 }
173
174 /* If EnterKey is pressed */
175 void OpenDialog::close()
176 {
177     if ( !b_stream_after )
178     {
179         play();
180     }
181     else
182     {
183         stream();
184     }
185 }
186
187 /* Play button */
188 void OpenDialog::play()
189 {
190     playOrEnqueue( false );
191 }
192
193 void OpenDialog::enqueue()
194 {
195     playOrEnqueue( true );
196 }
197
198 void OpenDialog::stream()
199 {
200     /* not finished FIXME */
201     THEDP->streamingDialog( mrl );
202 }
203
204
205 void OpenDialog::playOrEnqueue( bool b_enqueue = false )
206 {
207     this->toggleVisible();
208     mrl = ui.advancedLineInput->text();
209
210     if( !isModal() )
211     {
212         QStringList tempMRL = SeparateEntries( mrl );
213         for( size_t i = 0; i < tempMRL.size(); i++ )
214         {
215             bool b_start = !i && !b_enqueue;
216             input_item_t *p_input;
217             const char *psz_utf8 = qtu( tempMRL[i] );
218
219             p_input = input_ItemNew( p_intf, psz_utf8, NULL );
220
221             /* Insert options */
222             while( i + 1 < tempMRL.size() && tempMRL[i + 1].startsWith( ":" ) )
223             {
224                 i++;
225                 psz_utf8 = qtu( tempMRL[i] );
226                 input_ItemAddOption( p_input, psz_utf8 );
227             }
228
229             if( b_start )
230             {
231                 playlist_AddInput( THEPL, p_input,
232                                    PLAYLIST_APPEND | PLAYLIST_GO,
233                                    PLAYLIST_END, VLC_TRUE, VLC_FALSE );
234             }
235             else
236             {
237                 playlist_AddInput( THEPL, p_input,
238                                    PLAYLIST_APPEND|PLAYLIST_PREPARSE,
239                                    PLAYLIST_END, VLC_TRUE, VLC_FALSE );
240             }
241         }
242     }
243     else
244         accept();
245 }
246
247 void OpenDialog::toggleAdvancedPanel()
248 {
249     //FIXME does not work under Windows
250     if (ui.advancedFrame->isVisible()) {
251         ui.advancedFrame->hide();
252         setMinimumHeight(1);
253         resize( width(), mainHeight );
254
255     } else {
256         if( mainHeight == 0 )
257             mainHeight = height();
258
259         ui.advancedFrame->show();
260         if( advHeight == 0 ) {
261             advHeight = height() - mainHeight;
262         }
263         resize( width(), mainHeight + advHeight );
264     }
265 }
266
267 void OpenDialog::updateMRL() {
268     mrl = mainMRL;
269     if( ui.slaveCheckbox->isChecked() ) {
270         mrl += " :input-slave=" + ui.slaveText->text();
271     }
272     int i_cache = config_GetInt( p_intf, qta(storedMethod) );
273     if( i_cache != ui.cacheSpinBox->value() ) {
274         mrl += QString(" :%1=%2").arg(storedMethod).
275                                   arg(ui.cacheSpinBox->value());
276     }
277     if( ui.startTimeSpinBox->value()) {
278         mrl += " :start-time=" + QString("%1").
279             arg(ui.startTimeSpinBox->value());
280     }
281     ui.advancedLineInput->setText(mrl);
282 }
283
284 void OpenDialog::updateMRL(QString tempMRL)
285 {
286     mainMRL = tempMRL;
287     updateMRL();
288 }
289
290 void OpenDialog::newMethod(QString method)
291 {
292     if( method != storedMethod ) {
293         storedMethod = method;
294         int i_value = config_GetInt( p_intf, qta(storedMethod) );
295         ui.cacheSpinBox->setValue(i_value);
296     }
297 }
298
299 QStringList OpenDialog::SeparateEntries( QString entries )
300 {
301     bool b_quotes_mode = false;
302
303     QStringList entries_array;
304     QString entry;
305
306     int index = 0;
307     while( index < entries.size() )
308     {
309         int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
310         if( delim_pos < 0 ) delim_pos = entries.size() - 1;
311         entry += entries.mid( index, delim_pos - index + 1 );
312         index = delim_pos + 1;
313
314         if( entry.isEmpty() ) continue;
315
316         if( !b_quotes_mode && entry.endsWith( "\"" ) )
317         {
318             /* Enters quotes mode */
319             entry.truncate( entry.size() - 1 );
320             b_quotes_mode = true;
321         }
322         else if( b_quotes_mode && entry.endsWith( "\"" ) )
323         {
324             /* Finished the quotes mode */
325             entry.truncate( entry.size() - 1 );
326             b_quotes_mode = false;
327         }
328         else if( !b_quotes_mode && !entry.endsWith( "\"" ) )
329         {
330             /* we found a non-quoted standalone string */
331             if( index < entries.size() ||
332                 entry.endsWith( " " ) || entry.endsWith( "\t" ) ||
333                 entry.endsWith( "\r" ) || entry.endsWith( "\n" ) )
334                 entry.truncate( entry.size() - 1 );
335             if( !entry.isEmpty() ) entries_array.append( entry );
336             entry.clear();
337         }
338         else
339         {;}
340     }
341
342     if( !entry.isEmpty() ) entries_array.append( entry );
343
344     return entries_array;
345 }