QT4: implement the open dialog from the playlist
[vlc.git] / modules / gui / qt4 / dialogs / open.cpp
1 /*****************************************************************************
2  * open.cpp : Advanced open dialog
3  *****************************************************************************
4  * Copyright (C) 2006-2007 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
37 OpenDialog *OpenDialog::instance = NULL;
38
39 OpenDialog::OpenDialog( QWidget *parent, intf_thread_t *_p_intf, bool modal,
40                         int _action_flag )  :  QVLCDialog( parent, _p_intf )
41 {
42     setModal( modal );
43     i_action_flag = _action_flag;
44
45     ui.setupUi( this );
46     setWindowTitle( qtr("Open" ) );
47     resize( 410, 300);
48
49     /* Tab definition and creation */
50     fileOpenPanel = new FileOpenPanel( ui.Tab, p_intf );
51     discOpenPanel = new DiscOpenPanel( ui.Tab, p_intf );
52     netOpenPanel = new NetOpenPanel( ui.Tab, p_intf );
53     captureOpenPanel = new CaptureOpenPanel( ui.Tab, p_intf );
54
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" ) );
60
61     /* Hide the advancedPanel */
62     ui.advancedFrame->hide();
63
64     /* Buttons Creation */
65     QSizePolicy buttonSizePolicy( static_cast<QSizePolicy::Policy>(7),
66                                   static_cast<QSizePolicy::Policy>(1) );
67     buttonSizePolicy.setHorizontalStretch(0);
68     buttonSizePolicy.setVerticalStretch(0);
69
70     playButton = new QToolButton( this );
71     playButton->setText( qtr( "&Play" ) );
72     playButton->setSizePolicy( buttonSizePolicy );
73     playButton->setMinimumSize( QSize(90, 0) );
74     playButton->setPopupMode( QToolButton::MenuButtonPopup );
75     playButton->setToolButtonStyle( Qt::ToolButtonTextOnly );
76
77     cancelButton = new QToolButton();
78     cancelButton->setText( qtr( "&Cancel" ) );
79     cancelButton->setSizePolicy( buttonSizePolicy );
80
81     QMenu * openButtonMenu = new QMenu( "Open" );
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     openButtonMenu->addAction( qtr("&Convert"), this, SLOT( transcode( ) ) ,
89                                     QKeySequence( "Alt+C" ) );
90
91     playButton->setMenu( openButtonMenu );
92
93     ui.buttonsBox->addButton( playButton, QDialogButtonBox::AcceptRole );
94     ui.buttonsBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
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     /* FIXME CAPTURE */
112
113     /* Advanced frame Connects */
114     CONNECT( ui.slaveText, textChanged(QString), this, updateMRL());
115     CONNECT( ui.cacheSpinBox, valueChanged(int), this, updateMRL());
116     CONNECT( ui.startTimeSpinBox, valueChanged(int), this, updateMRL());
117     BUTTONACT( ui.advancedCheckBox , toggleAdvancedPanel() );
118
119     /* Buttons action */
120     BUTTONACT( playButton, play());
121     BUTTONACT( cancelButton, cancel());
122
123     /* At creation time, modify the default buttons */
124     if ( i_action_flag ) setMenuAction();
125
126
127     /* Initialize caching */
128     storedMethod = "";
129     newMethod("file-caching");
130
131     mainHeight = advHeight = 0;
132 }
133
134 OpenDialog::~OpenDialog()
135 {
136 }
137
138 /* Finish the dialog and decide if you open another one after */
139 void OpenDialog::setMenuAction()
140 {
141     switch ( i_action_flag )
142     {
143         case OPEN_AND_STREAM:
144             playButton->setText( qtr("&Stream") );
145             BUTTONACT( playButton, stream() );
146             break;
147         case OPEN_AND_SAVE:
148             playButton->setText( qtr("&Convert / Save") );
149             BUTTONACT( playButton, stream( true ) );
150             break;
151         case ENQUEUE:
152             playButton->setText( qtr("&Enqueue") );
153             BUTTONACT( playButton, enqueue() );
154             break;
155         case OPEN_AND_PLAY:
156         default:
157             playButton->setText( qtr("&Play") );
158             BUTTONACT( playButton, play() );
159    }
160 }
161
162 void OpenDialog::showTab(int i_tab=0)
163 {
164     this->show();
165     ui.Tab->setCurrentIndex(i_tab);
166 }
167
168 void OpenDialog::signalCurrent() {
169     if (ui.Tab->currentWidget() != NULL) {
170         (dynamic_cast<OpenPanel*>(ui.Tab->currentWidget()))->updateMRL();
171     }
172 }
173
174 /***********
175  * Actions *
176  ***********/
177
178 /* If Cancel is pressed or escaped */
179 void OpenDialog::cancel()
180 {
181     fileOpenPanel->clear();
182     this->toggleVisible();
183     if( isModal() )
184         reject();
185 }
186
187 /* If EnterKey is pressed */
188 void OpenDialog::close()
189 {
190     /* FIXME */
191     if ( !i_action_flag )
192     {
193         play();
194     }
195     else
196     {
197         stream();
198     }
199 }
200
201 /* Play button */
202 void OpenDialog::play()
203 {
204     finish( false );
205 }
206
207 void OpenDialog::enqueue()
208 {
209     finish( true );
210 }
211
212 void OpenDialog::transcode()
213 {
214     stream( true );
215 }
216
217 void OpenDialog::stream( bool b_transcode_only )
218 {
219     /* not finished FIXME */
220     /* Should go through the finish function */
221     THEDP->streamingDialog( mrl, b_transcode_only );
222 }
223
224 void OpenDialog::finish( bool b_enqueue = false )
225 {
226     this->toggleVisible();
227     mrl = ui.advancedLineInput->text();
228
229     if( !isModal() )
230     {
231         QStringList tempMRL = SeparateEntries( mrl );
232         for( size_t i = 0; i < tempMRL.size(); i++ )
233         {
234             bool b_start = !i && !b_enqueue;
235             input_item_t *p_input;
236             const char *psz_utf8 = qtu( tempMRL[i] );
237
238             p_input = input_ItemNew( p_intf, psz_utf8, NULL );
239
240             /* Insert options */
241             while( i + 1 < tempMRL.size() && tempMRL[i + 1].startsWith( ":" ) )
242             {
243                 i++;
244                 psz_utf8 = qtu( tempMRL[i] );
245                 input_ItemAddOption( p_input, psz_utf8 );
246             }
247
248             if( b_start )
249             {
250                 playlist_AddInput( THEPL, p_input,
251                                    PLAYLIST_APPEND | PLAYLIST_GO,
252                                    PLAYLIST_END, VLC_TRUE, VLC_FALSE );
253             }
254             else
255             {
256                 playlist_AddInput( THEPL, p_input,
257                                    PLAYLIST_APPEND|PLAYLIST_PREPARSE,
258                                    PLAYLIST_END, VLC_TRUE, VLC_FALSE );
259             }
260         }
261     }
262     else
263         accept();
264 }
265
266 void OpenDialog::toggleAdvancedPanel()
267 {
268     //FIXME does not work under Windows
269     if (ui.advancedFrame->isVisible()) {
270         ui.advancedFrame->hide();
271         setMinimumHeight(1);
272         resize( width(), mainHeight );
273
274     } else {
275         if( mainHeight == 0 )
276             mainHeight = height();
277
278         ui.advancedFrame->show();
279         if( advHeight == 0 ) {
280             advHeight = height() - mainHeight;
281         }
282         resize( width(), mainHeight + advHeight );
283     }
284 }
285
286 void OpenDialog::updateMRL() {
287     mrl = mainMRL;
288     if( ui.slaveCheckbox->isChecked() ) {
289         mrl += " :input-slave=" + ui.slaveText->text();
290     }
291     int i_cache = config_GetInt( p_intf, qta(storedMethod) );
292     if( i_cache != ui.cacheSpinBox->value() ) {
293         mrl += QString(" :%1=%2").arg(storedMethod).
294                                   arg(ui.cacheSpinBox->value());
295     }
296     if( ui.startTimeSpinBox->value()) {
297         mrl += " :start-time=" + QString("%1").
298             arg(ui.startTimeSpinBox->value());
299     }
300     ui.advancedLineInput->setText(mrl);
301 }
302
303 void OpenDialog::updateMRL(QString tempMRL)
304 {
305     mainMRL = tempMRL;
306     updateMRL();
307 }
308
309 void OpenDialog::newMethod(QString method)
310 {
311     if( method != storedMethod ) {
312         storedMethod = method;
313         int i_value = config_GetInt( p_intf, qta(storedMethod) );
314         ui.cacheSpinBox->setValue(i_value);
315     }
316 }
317
318 QStringList OpenDialog::SeparateEntries( QString entries )
319 {
320     bool b_quotes_mode = false;
321
322     QStringList entries_array;
323     QString entry;
324
325     int index = 0;
326     while( index < entries.size() )
327     {
328         int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
329         if( delim_pos < 0 ) delim_pos = entries.size() - 1;
330         entry += entries.mid( index, delim_pos - index + 1 );
331         index = delim_pos + 1;
332
333         if( entry.isEmpty() ) continue;
334
335         if( !b_quotes_mode && entry.endsWith( "\"" ) )
336         {
337             /* Enters quotes mode */
338             entry.truncate( entry.size() - 1 );
339             b_quotes_mode = true;
340         }
341         else if( b_quotes_mode && entry.endsWith( "\"" ) )
342         {
343             /* Finished the quotes mode */
344             entry.truncate( entry.size() - 1 );
345             b_quotes_mode = false;
346         }
347         else if( !b_quotes_mode && !entry.endsWith( "\"" ) )
348         {
349             /* we found a non-quoted standalone string */
350             if( index < entries.size() ||
351                 entry.endsWith( " " ) || entry.endsWith( "\t" ) ||
352                 entry.endsWith( "\r" ) || entry.endsWith( "\n" ) )
353                 entry.truncate( entry.size() - 1 );
354             if( !entry.isEmpty() ) entries_array.append( entry );
355             entry.clear();
356         }
357         else
358         {;}
359     }
360
361     if( !entry.isEmpty() ) entries_array.append( entry );
362
363     return entries_array;
364 }