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