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