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