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