Moves volume icon update in the timer
[vlc.git] / modules / gui / qt4 / dialogs_provider.cpp
1 /*****************************************************************************
2  * main_inteface.cpp : Main interface
3  *****************************************************************************
4  * Copyright (C) 2006-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include <QEvent>
26 #include <QApplication>
27 #include <QSignalMapper>
28 #include <QFileDialog>
29
30 #include "qt4.hpp"
31 #include "dialogs_provider.hpp"
32 #include "main_interface.hpp"
33 #include "menus.hpp"
34 #include <vlc_intf_strings.h>
35
36 /* The dialogs */
37 #include "dialogs/playlist.hpp"
38 #include "dialogs/preferences.hpp"
39 #include "dialogs/mediainfo.hpp"
40 #include "dialogs/messages.hpp"
41 #include "dialogs/extended.hpp"
42 #include "dialogs/sout.hpp"
43 #include "dialogs/open.hpp"
44 #include "dialogs/help.hpp"
45 #include "dialogs/gototime.hpp"
46 #include "dialogs/podcast_configuration.hpp"
47
48 DialogsProvider* DialogsProvider::instance = NULL;
49
50 DialogsProvider::DialogsProvider( intf_thread_t *_p_intf ) :
51                                   QObject( NULL ), p_intf( _p_intf )
52 {
53     fixed_timer = new QTimer( this );
54     fixed_timer->start( 150 /* milliseconds */ );
55
56     menusMapper = new QSignalMapper();
57     CONNECT( menusMapper, mapped(QObject *), this, menuAction( QObject *) );
58
59     menusUpdateMapper = new QSignalMapper();
60     CONNECT( menusUpdateMapper, mapped(QObject *),
61              this, menuUpdateAction( QObject *) );
62
63     SDMapper = new QSignalMapper();
64     CONNECT( SDMapper, mapped (QString), this, SDMenuAction( QString ) );
65 }
66
67 DialogsProvider::~DialogsProvider()
68 {
69     PlaylistDialog::killInstance();
70     MediaInfoDialog::killInstance();
71 }
72
73 void DialogsProvider::quit()
74 {
75     vlc_object_kill( p_intf );
76     QApplication::quit();
77 }
78
79 void DialogsProvider::customEvent( QEvent *event )
80 {
81     if( event->type() == DialogEvent_Type )
82     {
83         DialogEvent *de = static_cast<DialogEvent*>(event);
84         switch( de->i_dialog )
85         {
86             case INTF_DIALOG_FILE_SIMPLE:
87             case INTF_DIALOG_FILE:
88                 openDialog(); break;
89             case INTF_DIALOG_DISC:
90                 openDiscDialog(); break;
91             case INTF_DIALOG_NET:
92                 openNetDialog(); break;
93             case INTF_DIALOG_SAT:
94             case INTF_DIALOG_CAPTURE:
95                 openCaptureDialog(); break;
96             case INTF_DIALOG_PLAYLIST:
97                 playlistDialog(); break;
98             case INTF_DIALOG_MESSAGES:
99                 messagesDialog(); break;
100             case INTF_DIALOG_FILEINFO:
101                mediaInfoDialog(); break;
102             case INTF_DIALOG_PREFS:
103                prefsDialog(); break;
104             case INTF_DIALOG_BOOKMARKS:
105                bookmarksDialog(); break;
106             case INTF_DIALOG_EXTENDED:
107                extendedDialog(); break;
108                /* We might want to make it better with custom functions */
109             case INTF_DIALOG_POPUPMENU:
110                QVLCMenu::PopupMenu( p_intf, (de->i_arg != 0) ); break;
111             case INTF_DIALOG_AUDIOPOPUPMENU:
112                QVLCMenu::AudioPopupMenu( p_intf ); break;
113             case INTF_DIALOG_VIDEOPOPUPMENU:
114                QVLCMenu::VideoPopupMenu( p_intf ); break;
115             case INTF_DIALOG_MISCPOPUPMENU:
116                QVLCMenu::MiscPopupMenu( p_intf ); break;
117             case INTF_DIALOG_INTERACTION:
118                doInteraction( de->p_arg ); break;
119             case INTF_DIALOG_VLM:
120                vlmDialog(); break;
121             case INTF_DIALOG_WIZARD:
122             case INTF_DIALOG_UPDATEVLC:
123             case INTF_DIALOG_EXIT:
124             default:
125                msg_Warn( p_intf, "unimplemented dialog\n" );
126         }
127     }
128 }
129
130 /****************************************************************************
131  * Individual simple dialogs
132  ****************************************************************************/
133 void DialogsProvider::playlistDialog()
134 {
135     PlaylistDialog::getInstance( p_intf )->toggleVisible();
136 }
137
138 void DialogsProvider::prefsDialog()
139 {
140     PrefsDialog::getInstance( p_intf )->toggleVisible();
141 }
142 void DialogsProvider::extendedDialog()
143 {
144     ExtendedDialog::getInstance( p_intf )->toggleVisible();
145 }
146
147 void DialogsProvider::messagesDialog()
148 {
149     MessagesDialog::getInstance( p_intf )->toggleVisible();
150 }
151
152 void DialogsProvider::gotoTimeDialog()
153 {
154     GotoTimeDialog::getInstance( p_intf )->toggleVisible();
155 }
156
157 void DialogsProvider::vlmDialog()
158 {
159     /* FIXME - Implement me */
160     /*  VLMDialog::getInstance( p_intf )->toggleVisible(); */
161 }
162
163 void DialogsProvider::helpDialog()
164 {
165     HelpDialog::getInstance( p_intf )->toggleVisible();
166 }
167
168 void DialogsProvider::aboutDialog()
169 {
170     AboutDialog::getInstance( p_intf )->toggleVisible();
171 }
172
173 void DialogsProvider::mediaInfoDialog()
174 {
175     MediaInfoDialog::getInstance( p_intf )->toggleVisible();
176 }
177
178 void DialogsProvider::mediaCodecDialog()
179 {
180     MediaInfoDialog::getInstance( p_intf )->showTab( 2 );
181 }
182
183 void DialogsProvider::bookmarksDialog()
184 {
185     /* FIXME - Implement me */
186     /*  BookmarkDialog::getInstance( p_intf )->toggleVisible(); */
187 }
188
189 /****************************************************************************
190  * All the open/add stuff
191  * Open Dialog first - Simple Open then
192  ****************************************************************************/
193
194 void DialogsProvider::openDialog( int i_tab )
195 {
196     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf )->showTab( i_tab );
197 }
198 void DialogsProvider::openDialog()
199 {
200     openDialog( OPEN_FILE_TAB );
201 }
202 void DialogsProvider::openFileDialog()
203 {
204     openDialog( OPEN_FILE_TAB );
205 }
206 void DialogsProvider::openDiscDialog()
207 {
208     openDialog( OPEN_DISC_TAB );
209 }
210 void DialogsProvider::openNetDialog()
211 {
212     openDialog( OPEN_NETWORK_TAB );
213 }
214 void DialogsProvider::openCaptureDialog()
215 {
216     openDialog( OPEN_CAPTURE_TAB );
217 }
218
219 /* Same as the open one, but force the enqueue */
220 void DialogsProvider::PLAppendDialog()
221 {
222     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, ENQUEUE)->showTab(0);
223 }
224
225 /* Unimplemmented yet - Usefull ? */
226 void DialogsProvider::MLAppendDialog()
227 {
228 }
229
230 /**
231  * Simple open
232  * Not used anymore. Let the code until we are sure we don't want it
233  * Two opens make it confusing for the user.
234  ***/
235 QStringList DialogsProvider::showSimpleOpen( QString help,
236                                              int filters,
237                                              QString path )
238 {
239     QString fileTypes = "";
240     if( filters & EXT_FILTER_MEDIA ) {
241         ADD_FILTER_MEDIA( fileTypes );
242     }
243     if( filters & EXT_FILTER_VIDEO ) {
244         ADD_FILTER_VIDEO( fileTypes );
245     }
246     if( filters & EXT_FILTER_AUDIO ) {
247         ADD_FILTER_AUDIO( fileTypes );
248     }
249     if( filters & EXT_FILTER_PLAYLIST ) {
250         ADD_FILTER_PLAYLIST( fileTypes );
251     }
252     if( filters & EXT_FILTER_SUBTITLE ) {
253         ADD_FILTER_SUBTITLE( fileTypes );
254     }
255     ADD_FILTER_ALL( fileTypes );
256     fileTypes.replace(QString(";*"), QString(" *"));
257     return QFileDialog::getOpenFileNames( NULL,
258         help.isNull() ? qfu(I_OP_SEL_FILES ) : help,
259         path.isNull() ? qfu( p_intf->p_libvlc->psz_homedir ) : path,
260         fileTypes );
261 }
262
263 void DialogsProvider::addFromSimple( bool pl, bool go)
264 {
265     QStringList files = DialogsProvider::showSimpleOpen();
266     int i = 0;
267     foreach( QString file, files )
268     {
269         const char * psz_utf8 = qtu( file );
270         playlist_Add( THEPL, psz_utf8, NULL,
271                       go ? ( PLAYLIST_APPEND | ( i ? 0 : PLAYLIST_GO ) |
272                                                ( i ? PLAYLIST_PREPARSE : 0 ) )
273                          : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
274                       PLAYLIST_END,
275                       pl ? VLC_TRUE : VLC_FALSE, VLC_FALSE );
276         i++;
277     }
278 }
279
280 void DialogsProvider::simplePLAppendDialog()
281 {
282     addFromSimple( true, false );
283 }
284
285 void DialogsProvider::simpleMLAppendDialog()
286 {
287     addFromSimple( false, false );
288 }
289
290 void DialogsProvider::simpleOpenDialog()
291 {
292     addFromSimple( true, true );
293 }
294
295 /* Directory */
296
297 /**
298  * Open a directory,
299  * pl helps you to choose from playlist or media library,
300  * go to start or enqueue
301  **/
302 static void openDirectory( intf_thread_t* p_intf, bool pl, bool go )
303 {
304     QString dir = QFileDialog::getExistingDirectory ( 0, qtr("Open directory") );
305     if (!dir.isEmpty()) {
306         input_item_t *p_input = input_ItemNewExt( THEPL, qtu(dir), NULL,
307                                                0, NULL, -1 );
308
309         playlist_AddInput( THEPL, p_input,
310                        go ? ( PLAYLIST_APPEND | PLAYLIST_GO ) : PLAYLIST_APPEND,
311                        PLAYLIST_END, pl, VLC_FALSE );
312         input_Read( THEPL, p_input, VLC_FALSE );
313     }
314 }
315
316 void DialogsProvider::PLAppendDir()
317 {
318     openDirectory( p_intf, true, false );
319 }
320
321 void DialogsProvider::MLAppendDir()
322 {
323     openDirectory( p_intf, false , false );
324 }
325
326 /****************
327  * Playlist     *
328  ****************/
329 void DialogsProvider::openPlaylist()
330 {
331     QStringList files = showSimpleOpen( qtr( "Open playlist file" ),
332                                         EXT_FILTER_PLAYLIST );
333     foreach( QString file, files )
334     {
335         playlist_Import( THEPL, qtu(file) );
336     }
337 }
338
339 void DialogsProvider::savePlaylist()
340 {
341     QFileDialog *qfd = new QFileDialog( NULL,
342                                    qtr("Choose a filename to save playlist"),
343                                    qfu( p_intf->p_libvlc->psz_homedir ),
344                                    qtr("XSPF playlist (*.xspf);; ") +
345                                    qtr("M3U playlist (*.m3u);; Any (*.*) ") );
346     qfd->setFileMode( QFileDialog::AnyFile );
347     qfd->setAcceptMode( QFileDialog::AcceptSave );
348     qfd->setConfirmOverwrite( true );
349
350     if( qfd->exec() == QDialog::Accepted )
351     {
352         if( qfd->selectedFiles().count() > 0 )
353         {
354             static const char psz_xspf[] = "export-xspf",
355                               psz_m3u[] = "esport-m3u";
356             const char *psz_module;
357
358             QString file = qfd->selectedFiles().first();
359             QString filter = qfd->selectedFilter();
360
361             if( file.contains(".xsp") ||
362                 ( filter.contains(".xspf") && !file.contains(".m3u") ) )
363             {
364                 psz_module = psz_xspf;
365                 if( !file.contains( ".xsp" ) )
366                     file.append( ".xspf" );
367             }
368             else
369             {
370                 psz_module = psz_m3u;
371                 if( !file.contains( ".m3u" ) )
372                     file.append( ".m3u" );
373             }
374
375             playlist_Export( THEPL, qtu(file), THEPL->p_local_category,
376                              psz_module);
377         }
378     }
379     delete qfd;
380 }
381
382
383 /****************************************************************************
384  * Sout emulation
385  ****************************************************************************/
386
387 //FIXME !!
388 void DialogsProvider::streamingDialog( QString mrl, bool b_transcode_only )
389 {
390     SoutDialog *s = new SoutDialog( p_intf->p_sys->p_mi, p_intf,
391                                                     b_transcode_only );
392     if( s->exec() == QDialog::Accepted )
393     {
394         msg_Err( p_intf, "mrl %s\n", qta( s->getMrl() ) );
395         /* Just do it */
396         int i_len = strlen( qtu( s->getMrl() ) ) + 10;
397         char *psz_option = (char*)malloc(i_len);
398         snprintf( psz_option, i_len - 1, "%s", qtu( s->getMrl() ) );
399
400         playlist_AddExt( THEPL, qtu( mrl ), "Streaming",
401                          PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END,
402                         -1, &psz_option, 1, VLC_TRUE, VLC_FALSE );
403     }
404     delete s;
405 }
406
407 void DialogsProvider::openThenStreamingDialogs()
408 {
409     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, OPEN_AND_STREAM )
410                                 ->showTab( 0 );
411 }
412
413 void DialogsProvider::openThenTranscodingDialogs()
414 {
415     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, OPEN_AND_SAVE )
416                                 ->showTab( 0 );
417 }
418 /*
419 void DialogsProvider::streamingDialog()
420 {
421     OpenDialog *o = new OpenDialog( p_intf->p_sys->p_mi, p_intf, true );
422     if ( o->exec() == QDialog::Accepted )
423     {
424         SoutDialog *s = new SoutDialog( p_intf->p_sys->p_mi, p_intf );
425         if( s->exec() == QDialog::Accepted )
426         {
427             msg_Err(p_intf, "mrl %s\n", qta(s->mrl));
428             /* Just do it
429             int i_len = strlen( qtu(s->mrl) ) + 10;
430             char *psz_option = (char*)malloc(i_len);
431             snprintf( psz_option, i_len - 1, ":sout=%s", qtu(s->mrl));
432
433             playlist_AddExt( THEPL, qtu( o->mrl ), "Streaming",
434                              PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END,
435                              -1, &psz_option, 1, VLC_TRUE, VLC_FALSE );
436         }
437         delete s;
438     }
439     delete o;
440 }*/
441
442
443
444 /****************************************************************************
445  * Menus / Interaction
446  ****************************************************************************/
447
448 void DialogsProvider::menuAction( QObject *data )
449 {
450     QVLCMenu::DoAction( p_intf, data );
451 }
452
453 void DialogsProvider::menuUpdateAction( QObject *data )
454 {
455     MenuFunc * f = qobject_cast<MenuFunc *>(data);
456     f->doFunc( p_intf );
457 }
458
459 void DialogsProvider::SDMenuAction( QString data )
460 {
461     char *psz_sd = strdup( qtu( data ) );
462     if( !playlist_IsServicesDiscoveryLoaded( THEPL, psz_sd ) )
463         playlist_ServicesDiscoveryAdd( THEPL, psz_sd );
464     else
465         playlist_ServicesDiscoveryRemove( THEPL, psz_sd );
466     free( psz_sd );
467 }
468
469 void DialogsProvider::doInteraction( intf_dialog_args_t *p_arg )
470 {
471     InteractionDialog *qdialog;
472     interaction_dialog_t *p_dialog = p_arg->p_dialog;
473     switch( p_dialog->i_action )
474     {
475     case INTERACT_NEW:
476         qdialog = new InteractionDialog( p_intf, p_dialog );
477         p_dialog->p_private = (void*)qdialog;
478         if( !(p_dialog->i_status == ANSWERED_DIALOG) )
479             qdialog->show();
480         break;
481     case INTERACT_UPDATE:
482         qdialog = (InteractionDialog*)(p_dialog->p_private);
483         if( qdialog)
484             qdialog->update();
485         break;
486     case INTERACT_HIDE:
487         qdialog = (InteractionDialog*)(p_dialog->p_private);
488         if( qdialog )
489             qdialog->hide();
490         p_dialog->i_status = HIDDEN_DIALOG;
491         break;
492     case INTERACT_DESTROY:
493         qdialog = (InteractionDialog*)(p_dialog->p_private);
494         if( !p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
495             delete qdialog;
496         p_dialog->i_status = DESTROYED_DIALOG;
497         break;
498     }
499 }
500
501 void DialogsProvider::podcastConfigureDialog()
502 {
503     PodcastConfigurationDialog c( p_intf );
504     c.exec();
505 }
506
507 void DialogsProvider::switchToSkins()
508 {
509     var_SetString( p_intf, "intf-switch", "skins2" );
510 }