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