]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/open.cpp
Qt4 - Make Open dialog KDE/GNOME/WIN32 compliant for buttons...
[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
113     /* Buttons action */
114     BUTTONACT( playButton, play());
115     BUTTONACT( cancelButton, cancel());
116
117     if ( b_stream_after ) setAfter();
118
119     BUTTONACT( ui.advancedCheckBox , toggleAdvancedPanel() );
120
121     /* Initialize caching */
122     storedMethod = "";
123     newMethod("file-caching");
124
125     mainHeight = advHeight = 0;
126 }
127
128 OpenDialog::~OpenDialog()
129 {
130 }
131
132 void OpenDialog::setAfter()
133 {
134     if (!b_stream_after )
135     {
136         playButton->setText( qtr("&Play") );
137         BUTTONACT( playButton, play() );
138     }
139     else
140     {
141         playButton->setText( qtr("&Stream") );
142         BUTTONACT( playButton, stream() );
143     }
144 }
145
146 void OpenDialog::showTab(int i_tab=0)
147 {
148     this->show();
149     ui.Tab->setCurrentIndex(i_tab);
150 }
151
152 void OpenDialog::signalCurrent() {
153     if (ui.Tab->currentWidget() != NULL) {
154         (dynamic_cast<OpenPanel*>(ui.Tab->currentWidget()))->updateMRL();
155     }
156 }
157
158 /*********** 
159  * Actions *
160  ***********/
161
162 /* If Cancel is pressed or escaped */
163 void OpenDialog::cancel()
164 {
165     fileOpenPanel->clear();
166     this->toggleVisible();
167     if( isModal() )
168         reject();
169 }
170
171 /* If EnterKey is pressed */
172 void OpenDialog::close()
173 {
174     if ( !b_stream_after )
175     {
176         play();
177     }
178     else
179     {
180         stream();
181     }
182 }
183
184 /* Play button */
185 void OpenDialog::play()
186 {
187     playOrEnqueue( false );
188 }
189
190 void OpenDialog::enqueue()
191 {
192     playOrEnqueue( true );
193 }
194
195 void OpenDialog::stream()
196 {
197     /* not finished FIXME */
198     THEDP->streamingDialog( mrl );
199 }
200
201
202 void OpenDialog::playOrEnqueue( bool b_enqueue = false )
203 {
204     this->toggleVisible();
205     mrl = ui.advancedLineInput->text();
206
207     if( !isModal() )
208     {
209         QStringList tempMRL = SeparateEntries( mrl );
210         for( size_t i = 0; i < tempMRL.size(); i++ )
211         {
212             bool b_start = !i && !b_enqueue;
213             input_item_t *p_input;
214             const char *psz_utf8 = qtu( tempMRL[i] );
215
216             p_input = input_ItemNew( p_intf, psz_utf8, NULL );
217
218             /* Insert options */
219             while( i + 1 < tempMRL.size() && tempMRL[i + 1].startsWith( ":" ) )
220             {
221                 i++;
222                 psz_utf8 = qtu( tempMRL[i] );
223                 input_ItemAddOption( p_input, psz_utf8 );
224             }
225
226             if( b_start )
227             {
228                 playlist_AddInput( THEPL, p_input,
229                                    PLAYLIST_APPEND | PLAYLIST_GO,
230                                    PLAYLIST_END, VLC_TRUE, VLC_FALSE );
231             }
232             else
233             {
234                 playlist_AddInput( THEPL, p_input,
235                                    PLAYLIST_APPEND|PLAYLIST_PREPARSE,
236                                    PLAYLIST_END, VLC_TRUE, VLC_FALSE );
237             }
238         }
239     }
240     else
241         accept();
242 }
243
244 void OpenDialog::toggleAdvancedPanel()
245 {
246     //FIXME does not work under Windows
247     if (ui.advancedFrame->isVisible()) {
248         ui.advancedFrame->hide();
249         setMinimumHeight(1);
250         resize( width(), mainHeight );
251
252     } else {
253         if( mainHeight == 0 )
254             mainHeight = height();
255
256         ui.advancedFrame->show();
257         if( advHeight == 0 ) {
258             advHeight = height() - mainHeight;
259         }
260         resize( width(), mainHeight + advHeight );
261     }
262 }
263
264 void OpenDialog::updateMRL() {
265     mrl = mainMRL;
266     if( ui.slaveCheckbox->isChecked() ) {
267         mrl += " :input-slave=" + ui.slaveText->text();
268     }
269     int i_cache = config_GetInt( p_intf, qta(storedMethod) );
270     if( i_cache != ui.cacheSpinBox->value() ) {
271         mrl += QString(" :%1=%2").arg(storedMethod).
272                                   arg(ui.cacheSpinBox->value());
273     }
274     ui.advancedLineInput->setText(mrl);
275 }
276
277 void OpenDialog::updateMRL(QString tempMRL)
278 {
279     mainMRL = tempMRL;
280     updateMRL();
281 }
282
283 void OpenDialog::newMethod(QString method)
284 {
285     if( method != storedMethod ) {
286         storedMethod = method;
287         int i_value = config_GetInt( p_intf, qta(storedMethod) );
288         ui.cacheSpinBox->setValue(i_value);
289     }
290 }
291
292 QStringList OpenDialog::SeparateEntries( QString entries )
293 {
294     bool b_quotes_mode = false;
295
296     QStringList entries_array;
297     QString entry;
298
299     int index = 0;
300     while( index < entries.size() )
301     {
302         int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
303         if( delim_pos < 0 ) delim_pos = entries.size() - 1;
304         entry += entries.mid( index, delim_pos - index + 1 );
305         index = delim_pos + 1;
306
307         if( entry.isEmpty() ) continue;
308
309         if( !b_quotes_mode && entry.endsWith( "\"" ) )
310         {
311             /* Enters quotes mode */
312             entry.truncate( entry.size() - 1 );
313             b_quotes_mode = true;
314         }
315         else if( b_quotes_mode && entry.endsWith( "\"" ) )
316         {
317             /* Finished the quotes mode */
318             entry.truncate( entry.size() - 1 );
319             b_quotes_mode = false;
320         }
321         else if( !b_quotes_mode && !entry.endsWith( "\"" ) )
322         {
323             /* we found a non-quoted standalone string */
324             if( index < entries.size() ||
325                 entry.endsWith( " " ) || entry.endsWith( "\t" ) ||
326                 entry.endsWith( "\r" ) || entry.endsWith( "\n" ) )
327                 entry.truncate( entry.size() - 1 );
328             if( !entry.isEmpty() ) entries_array.append( entry );
329             entry.clear();
330         }
331         else
332         {;}
333     }
334
335     if( !entry.isEmpty() ) entries_array.append( entry );
336
337     return entries_array;
338 }