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