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