]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs_provider.cpp
[Qt] Remove QTimer wher we can.
[vlc] / modules / gui / qt4 / dialogs_provider.cpp
1 /*****************************************************************************
2  * dialogs_provider.cpp : Dialog Provider
3  *****************************************************************************
4  * Copyright (C) 2006-2008 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_common.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 #include "input_manager.hpp"
40
41 /* The dialogs */
42 #include "dialogs/playlist.hpp"
43 #include "dialogs/bookmarks.hpp"
44 #include "dialogs/preferences.hpp"
45 #include "dialogs/mediainfo.hpp"
46 #include "dialogs/messages.hpp"
47 #include "dialogs/extended.hpp"
48 #include "dialogs/vlm.hpp"
49 #include "dialogs/sout.hpp"
50 #include "dialogs/open.hpp"
51 #include "dialogs/help.hpp"
52 #include "dialogs/gototime.hpp"
53 #include "dialogs/podcast_configuration.hpp"
54
55 DialogsProvider* DialogsProvider::instance = NULL;
56
57 DialogsProvider::DialogsProvider( intf_thread_t *_p_intf ) :
58                                   QObject( NULL ), p_intf( _p_intf )
59 {
60     b_isDying = false;
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     delete menusMapper;
87     delete menusUpdateMapper;
88     delete SDMapper;
89 }
90
91 void DialogsProvider::quit()
92 {
93     /* Stop the playlist */
94     playlist_Stop( THEPL );
95     b_isDying = true;
96     vlc_object_kill( p_intf->p_libvlc );
97     QApplication::closeAllWindows();
98     QApplication::quit();
99 }
100
101 void DialogsProvider::customEvent( QEvent *event )
102 {
103     if( event->type() == DialogEvent_Type )
104     {
105         DialogEvent *de = static_cast<DialogEvent*>(event);
106         switch( de->i_dialog )
107         {
108         case INTF_DIALOG_FILE_SIMPLE:
109         case INTF_DIALOG_FILE:
110             openDialog(); break;
111         case INTF_DIALOG_FILE_GENERIC:
112             openFileGenericDialog( de->p_arg ); break;
113         case INTF_DIALOG_DISC:
114             openDiscDialog(); break;
115         case INTF_DIALOG_NET:
116             openNetDialog(); break;
117         case INTF_DIALOG_SAT:
118         case INTF_DIALOG_CAPTURE:
119             openCaptureDialog(); break;
120         case INTF_DIALOG_DIRECTORY:
121             PLAppendDir(); break;
122         case INTF_DIALOG_PLAYLIST:
123             playlistDialog(); break;
124         case INTF_DIALOG_MESSAGES:
125             messagesDialog(); break;
126         case INTF_DIALOG_FILEINFO:
127            mediaInfoDialog(); break;
128         case INTF_DIALOG_PREFS:
129            prefsDialog(); break;
130         case INTF_DIALOG_BOOKMARKS:
131            bookmarksDialog(); break;
132         case INTF_DIALOG_EXTENDED:
133            extendedDialog(); break;
134 #ifdef ENABLE_VLM
135         case INTF_DIALOG_VLM:
136            vlmDialog(); break;
137 #endif
138         case INTF_DIALOG_INTERACTION:
139            doInteraction( de->p_arg ); break;
140         case INTF_DIALOG_POPUPMENU:
141            QVLCMenu::PopupMenu( p_intf, (de->i_arg != 0) ); break;
142         case INTF_DIALOG_AUDIOPOPUPMENU:
143            QVLCMenu::AudioPopupMenu( p_intf ); break;
144         case INTF_DIALOG_VIDEOPOPUPMENU:
145            QVLCMenu::VideoPopupMenu( p_intf ); break;
146         case INTF_DIALOG_MISCPOPUPMENU:
147            QVLCMenu::MiscPopupMenu( p_intf ); break;
148         case INTF_DIALOG_WIZARD:
149         case INTF_DIALOG_STREAMWIZARD:
150             openThenStreamingDialogs(); break;
151 #ifdef UPDATE_CHECK
152         case INTF_DIALOG_UPDATEVLC:
153             updateDialog(); break;
154 #endif
155         case INTF_DIALOG_EXIT:
156             quit(); break;
157         default:
158            msg_Warn( p_intf, "unimplemented dialog" );
159         }
160     }
161 }
162
163 /****************************************************************************
164  * Individual simple dialogs
165  ****************************************************************************/
166 void DialogsProvider::playlistDialog()
167 {
168     PlaylistDialog::getInstance( p_intf )->toggleVisible();
169 }
170
171 void DialogsProvider::prefsDialog()
172 {
173     PrefsDialog::getInstance( p_intf )->toggleVisible();
174 }
175
176 void DialogsProvider::extendedDialog()
177 {
178     ExtendedDialog::getInstance( p_intf )->toggleVisible();
179 }
180
181 void DialogsProvider::messagesDialog()
182 {
183     MessagesDialog::getInstance( p_intf )->toggleVisible();
184 }
185
186 void DialogsProvider::gotoTimeDialog()
187 {
188     GotoTimeDialog::getInstance( p_intf )->toggleVisible();
189 }
190
191 #ifdef ENABLE_VLM
192 void DialogsProvider::vlmDialog()
193 {
194     VLMDialog::getInstance( p_intf )->toggleVisible();
195 }
196 #endif
197
198 void DialogsProvider::helpDialog()
199 {
200     HelpDialog::getInstance( p_intf )->toggleVisible();
201 }
202
203 #ifdef UPDATE_CHECK
204 void DialogsProvider::updateDialog()
205 {
206     UpdateDialog::getInstance( p_intf )->toggleVisible();
207 }
208 #endif
209
210 void DialogsProvider::aboutDialog()
211 {
212     AboutDialog::getInstance( p_intf )->toggleVisible();
213 }
214
215 void DialogsProvider::mediaInfoDialog()
216 {
217     MediaInfoDialog::getInstance( p_intf )->toggleVisible();
218 }
219
220 void DialogsProvider::mediaCodecDialog()
221 {
222     MediaInfoDialog::getInstance( p_intf )->showTab( 2 );
223 }
224
225 void DialogsProvider::bookmarksDialog()
226 {
227     BookmarksDialog::getInstance( p_intf )->toggleVisible();
228 }
229
230 void DialogsProvider::podcastConfigureDialog()
231 {
232     PodcastConfigDialog::getInstance( p_intf )->toggleVisible();
233 }
234
235
236 /****************************************************************************
237  * All the open/add stuff
238  * Open Dialog first - Simple Open then
239  ****************************************************************************/
240
241 void DialogsProvider::openDialog( int i_tab )
242 {
243     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf )->showTab( i_tab );
244 }
245 void DialogsProvider::openDialog()
246 {
247     openDialog( OPEN_FILE_TAB );
248 }
249 void DialogsProvider::openFileGenericDialog( intf_dialog_args_t *p_arg )
250 {
251     if( p_arg == NULL )
252     {
253         msg_Warn( p_intf, "openFileGenericDialog() called with NULL arg" );
254         return;
255     }
256
257     /* Replace the extensions to a Qt format */
258     int i = 0;
259     QString extensions = qfu( p_arg->psz_extensions );
260     while ( ( i = extensions.indexOf( "|", i ) ) != -1 )
261     {
262         if( ( extensions.count( "|" ) % 2 ) == 0 )
263             extensions.replace( i, 1, ");;" );
264         else
265             extensions.replace( i, 1, "(" );
266     }
267     extensions.replace(QString(";*"), QString(" *"));
268     extensions.append( ")" );
269
270     /* Save */
271     if( p_arg->b_save )
272     {
273         QString file = QFileDialog::getSaveFileName( NULL, p_arg->psz_title,
274                             qfu( p_intf->p_sys->psz_filepath ), extensions );
275         if( !file.isEmpty() )
276         {
277             p_arg->i_results = 1;
278             p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
279             p_arg->psz_results[0] = strdup( qtu( toNativeSepNoSlash( file ) ) );
280         }
281         else
282             p_arg->i_results = 0;
283     }
284     else /* non-save mode */
285     {
286         QStringList files = QFileDialog::getOpenFileNames( NULL,
287                 p_arg->psz_title, qfu( p_intf->p_sys->psz_filepath ),
288                 extensions );
289         p_arg->i_results = files.count();
290         p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
291         i = 0;
292         foreach( QString file, files )
293             p_arg->psz_results[i++] = strdup( qtu( toNativeSepNoSlash( file ) ) );
294     }
295
296     /* Callback */
297     if( p_arg->pf_callback )
298         p_arg->pf_callback( p_arg );
299
300     /* Clean afterwards */
301     if( p_arg->psz_results )
302     {
303         for( i = 0; i < p_arg->i_results; i++ )
304             free( p_arg->psz_results[i] );
305         free( p_arg->psz_results );
306     }
307     free( p_arg->psz_title );
308     free( p_arg->psz_extensions );
309     free( p_arg );
310 }
311
312 void DialogsProvider::openFileDialog()
313 {
314     openDialog( OPEN_FILE_TAB );
315 }
316 void DialogsProvider::openDiscDialog()
317 {
318     openDialog( OPEN_DISC_TAB );
319 }
320 void DialogsProvider::openNetDialog()
321 {
322     openDialog( OPEN_NETWORK_TAB );
323 }
324 void DialogsProvider::openCaptureDialog()
325 {
326     openDialog( OPEN_CAPTURE_TAB );
327 }
328
329 /* Same as the open one, but force the enqueue */
330 void DialogsProvider::PLAppendDialog()
331 {
332     OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false, OPEN_AND_ENQUEUE)
333                             ->showTab( OPEN_FILE_TAB );
334 }
335
336 void DialogsProvider::MLAppendDialog()
337 {
338     OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
339                             OPEN_AND_ENQUEUE, false, false )
340                                     ->showTab( OPEN_FILE_TAB );
341 }
342
343 /**
344  * Simple open
345  ***/
346 QStringList DialogsProvider::showSimpleOpen( QString help,
347                                              int filters,
348                                              QString path )
349 {
350     QString fileTypes = "";
351     if( filters & EXT_FILTER_MEDIA ) {
352         ADD_FILTER_MEDIA( fileTypes );
353     }
354     if( filters & EXT_FILTER_VIDEO ) {
355         ADD_FILTER_VIDEO( fileTypes );
356     }
357     if( filters & EXT_FILTER_AUDIO ) {
358         ADD_FILTER_AUDIO( fileTypes );
359     }
360     if( filters & EXT_FILTER_PLAYLIST ) {
361         ADD_FILTER_PLAYLIST( fileTypes );
362     }
363     if( filters & EXT_FILTER_SUBTITLE ) {
364         ADD_FILTER_SUBTITLE( fileTypes );
365     }
366     ADD_FILTER_ALL( fileTypes );
367     fileTypes.replace(QString(";*"), QString(" *"));
368
369     return QFileDialog::getOpenFileNames( NULL,
370         help.isEmpty() ? qfu(I_OP_SEL_FILES ) : help,
371         path.isEmpty() ? qfu( p_intf->p_sys->psz_filepath ) : path,
372         fileTypes );
373 }
374
375 /**
376  * Open a file,
377  * pl helps you to choose from playlist or media library,
378  * go to start or enqueue
379  **/
380 void DialogsProvider::addFromSimple( bool pl, bool go)
381 {
382     QStringList files = DialogsProvider::showSimpleOpen();
383     int i = 0;
384     foreach( QString file, files )
385     {
386         playlist_Add( THEPL, qtu( toNativeSeparators( file ) ), NULL,
387                       go ? ( PLAYLIST_APPEND | ( i ? 0 : PLAYLIST_GO ) |
388                                                ( i ? PLAYLIST_PREPARSE : 0 ) )
389                          : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
390                       PLAYLIST_END,
391                       pl ? true : false, false );
392         i++;
393     }
394 }
395
396 void DialogsProvider::simpleOpenDialog()
397 {
398     addFromSimple( true, true ); /* Playlist and Go */
399 }
400
401 void DialogsProvider::simplePLAppendDialog()
402 {
403     addFromSimple( true, false );
404 }
405
406 void DialogsProvider::simpleMLAppendDialog()
407 {
408     addFromSimple( false, false );
409 }
410
411 /* Directory */
412 /**
413  * Open a directory,
414  * pl helps you to choose from playlist or media library,
415  * go to start or enqueue
416  **/
417 static void openDirectory( intf_thread_t *p_intf, bool pl, bool go )
418 {
419     QString dir = QFileDialog::getExistingDirectory( NULL, qtr("Open Directory") );
420
421     if (!dir.isEmpty() )
422     {
423         input_item_t *p_input = input_item_NewExt( THEPL,
424                               qtu( "directory://" + toNativeSeparators(dir) ),
425                               NULL, 0, NULL, -1 );
426
427         /* FIXME: playlist_AddInput() can fail */
428         playlist_AddInput( THEPL, p_input,
429                        go ? ( PLAYLIST_APPEND | PLAYLIST_GO ) : PLAYLIST_APPEND,
430                        PLAYLIST_END, pl, pl_Unlocked );
431         if( !go )
432             input_Read( THEPL, p_input, true );
433         vlc_gc_decref( p_input );
434     }
435 }
436
437 void DialogsProvider::PLOpenDir()
438 {
439     openDirectory( p_intf, true, true );
440 }
441
442 void DialogsProvider::PLAppendDir()
443 {
444     openDirectory( p_intf, true, false );
445 }
446
447 void DialogsProvider::MLAppendDir()
448 {
449     openDirectory( p_intf, false , false );
450 }
451
452 /****************
453  * Playlist     *
454  ****************/
455 void DialogsProvider::openAPlaylist()
456 {
457     QStringList files = showSimpleOpen( qtr( "Open playlist..." ),
458                                         EXT_FILTER_PLAYLIST );
459     foreach( QString file, files )
460     {
461         playlist_Import( THEPL, qtu( toNativeSeparators( file ) ) );
462     }
463 }
464
465 void DialogsProvider::saveAPlaylist()
466 {
467     QFileDialog *qfd = new QFileDialog( NULL,
468                                    qtr( "Save playlist as..." ),
469                                    qfu( p_intf->p_sys->psz_filepath ),
470                                    qtr( "XSPF playlist (*.xspf);; " ) +
471                                    qtr( "M3U playlist (*.m3u);; Any (*.*) " ) );
472     qfd->setFileMode( QFileDialog::AnyFile );
473     qfd->setAcceptMode( QFileDialog::AcceptSave );
474     qfd->setConfirmOverwrite( true );
475
476     if( qfd->exec() == QDialog::Accepted )
477     {
478         if( qfd->selectedFiles().count() > 0 )
479         {
480             static const char psz_xspf[] = "export-xspf",
481                               psz_m3u[] = "export-m3u";
482             const char *psz_module;
483
484             QString file = qfd->selectedFiles().first();
485             QString filter = qfd->selectedFilter();
486
487             if( file.contains( ".xsp" ) ||
488                 ( filter.contains( ".xspf" ) && !file.contains( ".m3u" ) ) )
489             {
490                 psz_module = psz_xspf;
491                 if( !file.contains( ".xsp" ) )
492                     file.append( ".xspf" );
493             }
494             else
495             {
496                 psz_module = psz_m3u;
497                 if( !file.contains( ".m3u" ) )
498                     file.append( ".m3u" );
499             }
500
501             playlist_Export( THEPL, qtu( toNativeSeparators( file ) ),
502                         THEPL->p_local_category, psz_module);
503         }
504     }
505     delete qfd;
506 }
507
508
509 /****************************************************************************
510  * Sout emulation
511  ****************************************************************************/
512
513 void DialogsProvider::streamingDialog( QWidget *parent, QString mrl,
514                                        bool b_transcode_only )
515 {
516     SoutDialog *s = SoutDialog::getInstance( parent, p_intf, b_transcode_only );
517
518     if( s->exec() == QDialog::Accepted )
519     {
520         msg_Dbg( p_intf, "Sout mrl %s", qta( s->getMrl() ) );
521         /* Just do it */
522         int i_len = strlen( qtu( s->getMrl() ) ) + 10;
523         char *psz_option = (char*)malloc( i_len );
524         snprintf( psz_option, i_len - 1, "%s", qtu( s->getMrl() ) );
525
526         playlist_AddExt( THEPL, qtu( mrl ), "Streaming",
527                          PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END,
528                         -1, &psz_option, 1, true, pl_Unlocked );
529     }
530 }
531
532 void DialogsProvider::openThenStreamingDialogs()
533 {
534     OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false, OPEN_AND_STREAM )
535                                 ->showTab( OPEN_FILE_TAB );
536 }
537
538 void DialogsProvider::openThenTranscodingDialogs()
539 {
540     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, false, OPEN_AND_SAVE )
541                                 ->showTab( OPEN_FILE_TAB );
542 }
543
544 /****************************************************************************
545  * Menus / Interaction
546  ****************************************************************************/
547
548 void DialogsProvider::menuAction( QObject *data )
549 {
550     QVLCMenu::DoAction( p_intf, data );
551 }
552
553 void DialogsProvider::menuUpdateAction( QObject *data )
554 {
555     MenuFunc * f = qobject_cast<MenuFunc *>(data);
556     f->doFunc( p_intf );
557 }
558
559 void DialogsProvider::SDMenuAction( QString data )
560 {
561     char *psz_sd = strdup( qtu( data ) );
562     if( !playlist_IsServicesDiscoveryLoaded( THEPL, psz_sd ) )
563         playlist_ServicesDiscoveryAdd( THEPL, psz_sd );
564     else
565         playlist_ServicesDiscoveryRemove( THEPL, psz_sd );
566     free( psz_sd );
567 }
568
569 void DialogsProvider::doInteraction( intf_dialog_args_t *p_arg )
570 {
571     InteractionDialog *qdialog;
572     interaction_dialog_t *p_dialog = p_arg->p_dialog;
573     switch( p_dialog->i_action )
574     {
575     case INTERACT_NEW:
576         qdialog = new InteractionDialog( p_intf, p_dialog );
577         p_dialog->p_private = (void*)qdialog;
578         if( !(p_dialog->i_status == ANSWERED_DIALOG) )
579             qdialog->show();
580         break;
581     case INTERACT_UPDATE:
582         qdialog = (InteractionDialog*)(p_dialog->p_private);
583         if( qdialog )
584             qdialog->update();
585         else
586         {
587             /* The INTERACT_NEW message was forgotten
588                so we must create the dialog and update it*/
589             qdialog = new InteractionDialog( p_intf, p_dialog );
590             p_dialog->p_private = (void*)qdialog;
591             if( !(p_dialog->i_status == ANSWERED_DIALOG) )
592                 qdialog->show();
593             if( qdialog )
594                 qdialog->update();
595         }
596         break;
597     case INTERACT_HIDE:
598         msg_Dbg( p_intf, "Hide the Interaction Dialog" );
599         qdialog = (InteractionDialog*)(p_dialog->p_private);
600         if( qdialog )
601             qdialog->hide();
602         p_dialog->i_status = HIDDEN_DIALOG;
603         break;
604     case INTERACT_DESTROY:
605         msg_Dbg( p_intf, "Destroy the Interaction Dialog" );
606         qdialog = (InteractionDialog*)(p_dialog->p_private);
607         if( !p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
608             delete qdialog;
609         p_dialog->i_status = DESTROYED_DIALOG;
610         break;
611     }
612 }
613
614 void DialogsProvider::loadSubtitlesFile()
615 {
616     input_thread_t *p_input = THEMIM->getInput();
617     if( !p_input )
618         return;
619     input_item_t *p_item = input_GetItem( p_input );
620     if( !p_item )
621         return;
622     char *path = input_item_GetURI( p_item );
623     if( !path )
624         path = strdup( "" );
625     char *sep = strrchr( path, DIR_SEP_CHAR );
626     if( sep )
627         *sep = '\0';
628     QStringList qsl = showSimpleOpen( qtr( "Open subtitles..." ),
629                                       EXT_FILTER_SUBTITLE,
630                                       path );
631     free( path );
632     QString qsFile;
633     foreach( qsFile, qsl )
634     {
635         if( !input_AddSubtitles( p_input, qtu( toNativeSeparators( qsFile ) ),
636                     true ) )
637             msg_Warn( p_intf, "unable to load subtitles from '%s'",
638                       qtu( qsFile ) );
639     }
640 }