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