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