]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/open.cpp
* open dialog: options like :sub-file= are now added to input (ie "use subtitles...
[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
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                                                 QVLCDialog( parent, _p_intf )
42 {
43     setModal( modal );
44     ui.setupUi( this );
45     setWindowTitle( qtr("Open" ) );
46     resize( 500, 300);
47
48     fileOpenPanel = new FileOpenPanel( this , p_intf );
49     diskOpenPanel = new DiskOpenPanel( this , p_intf );
50     netOpenPanel = new NetOpenPanel( this , p_intf );
51     captureOpenPanel = new CaptureOpenPanel( this, p_intf );
52
53     ui.Tab->addTab( fileOpenPanel, qtr( "&File" ) );
54     ui.Tab->addTab( diskOpenPanel, qtr( "&Disc" ) );
55     ui.Tab->addTab( netOpenPanel, qtr( "&Network" ) );
56     ui.Tab->addTab( captureOpenPanel, qtr( "Capture &Device" ) );
57
58     ui.advancedFrame->hide();
59
60     /* Force MRL update on tab change */
61     CONNECT( ui.Tab, currentChanged(int), this, signalCurrent());
62
63     CONNECT( fileOpenPanel, mrlUpdated( QString ), this, updateMRL(QString) );
64     CONNECT( netOpenPanel, mrlUpdated( QString ), this, updateMRL(QString) );
65     CONNECT( diskOpenPanel, mrlUpdated( QString ), this, updateMRL(QString) );
66     CONNECT( captureOpenPanel, mrlUpdated( QString ), this,
67             updateMRL(QString) );
68
69
70     CONNECT( fileOpenPanel, methodChanged( QString ),
71              this, newMethod(QString) );
72     CONNECT( netOpenPanel, methodChanged( QString ),
73              this, newMethod(QString) );
74     CONNECT( diskOpenPanel, methodChanged( QString ),
75              this, newMethod(QString) );
76
77     CONNECT( ui.slaveText, textChanged(QString), this, updateMRL());
78     CONNECT( ui.cacheSpinBox, valueChanged(int), this, updateMRL());
79
80     BUTTONACT( ui.closeButton, play());
81     BUTTONACT( ui.cancelButton, cancel());
82     BUTTONACT( ui.enqueueButton, enqueue());
83     BUTTONACT( ui.advancedCheckBox , toggleAdvancedPanel() );
84
85     /* Initialize caching */
86     storedMethod = "";
87     newMethod("file-caching");
88
89     mainHeight = advHeight = 0;
90 }
91
92 OpenDialog::~OpenDialog()
93 {
94 }
95
96 void OpenDialog::showTab(int i_tab=0)
97 {
98     this->show();
99     ui.Tab->setCurrentIndex(i_tab);
100 }
101
102 void OpenDialog::signalCurrent() {
103     if (ui.Tab->currentWidget() != NULL) {
104         (dynamic_cast<OpenPanel*>(ui.Tab->currentWidget()))->updateMRL();
105     }
106 }
107
108 void OpenDialog::cancel()
109 {
110     fileOpenPanel->clear();
111     this->toggleVisible();
112     if( isModal() )
113         reject();
114 }
115
116 void OpenDialog::play()
117 {
118     playOrEnqueue( false );
119 }
120
121 void OpenDialog::enqueue()
122 {
123     playOrEnqueue( true );
124 }
125
126 void OpenDialog::playOrEnqueue( bool b_enqueue = false )
127 {
128     this->toggleVisible();
129     mrl = ui.advancedLineInput->text();
130
131     if( !isModal() )
132     {
133         QStringList tempMRL = SeparateEntries( mrl );
134         for( size_t i = 0; i < tempMRL.size(); i++ )
135         {
136             bool b_start = !i && !b_enqueue;
137             input_item_t *p_input;
138             const char *psz_utf8 = qtu( tempMRL[i] );
139
140             p_input = input_ItemNew( p_intf, psz_utf8, NULL );
141
142             /* Insert options */
143             while( i + 1 < tempMRL.size() && tempMRL[i + 1].startsWith( ":" ) )
144             {
145                 i++;
146                 psz_utf8 = qtu( tempMRL[i] );
147                 input_ItemAddOption( p_input, psz_utf8 );
148             }
149
150             if( b_start )
151             {
152                 playlist_AddInput( THEPL, p_input,
153                                    PLAYLIST_APPEND | PLAYLIST_GO,
154                                    PLAYLIST_END, VLC_TRUE, VLC_FALSE );
155             }
156             else
157             {
158                 playlist_AddInput( THEPL, p_input,
159                                    PLAYLIST_APPEND|PLAYLIST_PREPARSE,
160                                    PLAYLIST_END, VLC_TRUE, VLC_FALSE );
161             }
162         }
163     }
164     else
165         accept();
166 }
167
168 void OpenDialog::toggleAdvancedPanel()
169 {
170     //FIXME does not work under Windows
171     if (ui.advancedFrame->isVisible()) {
172         ui.advancedFrame->hide();
173         setMinimumHeight(1);
174         resize( width(), mainHeight );
175
176     } else {
177         if( mainHeight == 0 )
178             mainHeight = height();
179
180         ui.advancedFrame->show();
181         if( advHeight == 0 ) {
182             advHeight = height() - mainHeight;
183         }
184         resize( width(), mainHeight + advHeight );
185     }
186 }
187
188 void OpenDialog::updateMRL() {
189     mrl = mainMRL;
190     if( ui.slaveCheckbox->isChecked() ) {
191         mrl += " :input-slave=" + ui.slaveText->text();
192     }
193     int i_cache = config_GetInt( p_intf, qta(storedMethod) );
194     if( i_cache != ui.cacheSpinBox->value() ) {
195         mrl += QString(" :%1=%2").arg(storedMethod).
196                                   arg(ui.cacheSpinBox->value());
197     }
198     ui.advancedLineInput->setText(mrl);
199 }
200
201 void OpenDialog::updateMRL(QString tempMRL)
202 {
203     mainMRL = tempMRL;
204     updateMRL();
205 }
206
207 void OpenDialog::newMethod(QString method)
208 {
209     if( method != storedMethod ) {
210         storedMethod = method;
211         int i_value = config_GetInt( p_intf, qta(storedMethod) );
212         ui.cacheSpinBox->setValue(i_value);
213     }
214 }
215
216 QStringList OpenDialog::SeparateEntries( QString entries )
217 {
218     bool b_quotes_mode = false;
219
220     QStringList entries_array;
221     QString entry;
222
223     int index = 0;
224     while( index < entries.size() )
225     {
226         int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
227         if( delim_pos < 0 ) delim_pos = entries.size() - 1;
228         entry += entries.mid( index, delim_pos - index + 1 );
229         index = delim_pos + 1;
230
231         if( entry.isEmpty() ) continue;
232
233         if( !b_quotes_mode && entry.endsWith( "\"" ) )
234         {
235             /* Enters quotes mode */
236             entry.truncate( entry.size() - 1 );
237             b_quotes_mode = true;
238         }
239         else if( b_quotes_mode && entry.endsWith( "\"" ) )
240         {
241             /* Finished the quotes mode */
242             entry.truncate( entry.size() - 1 );
243             b_quotes_mode = false;
244         }
245         else if( !b_quotes_mode && !entry.endsWith( "\"" ) )
246         {
247             /* we found a non-quoted standalone string */
248             if( index < entries.size() ||
249                 entry.endsWith( " " ) || entry.endsWith( "\t" ) ||
250                 entry.endsWith( "\r" ) || entry.endsWith( "\n" ) )
251                 entry.truncate( entry.size() - 1 );
252             if( !entry.isEmpty() ) entries_array.append( entry );
253             entry.clear();
254         }
255         else
256         {;}
257     }
258
259     if( !entry.isEmpty() ) entries_array.append( entry );
260
261     return entries_array;
262 }