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