1 /*****************************************************************************
2 * dialogs_provider.cpp : Dialog Provider
3 *****************************************************************************
4 * Copyright (C) 2006-2009 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
8 * Jean-Baptiste Kempf <jb@videolan.org>
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.
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.
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 *****************************************************************************/
29 #include <vlc_intf_strings.h>
32 #include "dialogs_provider.hpp"
33 #include "input_manager.hpp" /* Load Subtitles */
35 #include "recents.hpp"
36 #include "util/qt_dirs.hpp"
37 #include "main_interface.hpp"
40 #include "dialogs/playlist.hpp"
41 #include "dialogs/bookmarks.hpp"
42 #include "dialogs/preferences.hpp"
43 #include "dialogs/mediainfo.hpp"
44 #include "dialogs/messages.hpp"
45 #include "dialogs/extended.hpp"
46 #include "dialogs/vlm.hpp"
47 #include "dialogs/sout.hpp"
48 #include "dialogs/convert.hpp"
49 #include "dialogs/open.hpp"
50 #include "dialogs/openurl.hpp"
51 #include "dialogs/help.hpp"
52 #include "dialogs/gototime.hpp"
53 #include "dialogs/podcast_configuration.hpp"
54 #include "dialogs/toolbar.hpp"
55 #include "dialogs/plugins.hpp"
56 #include "dialogs/external.hpp"
57 #include "dialogs/epg.hpp"
58 #include "dialogs/errors.hpp"
61 #include <QApplication>
62 #include <QSignalMapper>
63 #include <QFileDialog>
65 #define I_OP_DIR_WINTITLE I_DIR_OR_FOLDER( N_("Open Directory"), \
68 DialogsProvider* DialogsProvider::instance = NULL;
70 DialogsProvider::DialogsProvider( intf_thread_t *_p_intf ) :
71 QObject( NULL ), p_intf( _p_intf )
75 /* Various signal mappers for the menus */
76 menusMapper = new QSignalMapper();
77 CONNECT( menusMapper, mapped(QObject *), this, menuAction( QObject *) );
79 menusUpdateMapper = new QSignalMapper();
80 CONNECT( menusUpdateMapper, mapped(QObject *),
81 this, menuUpdateAction( QObject *) );
83 SDMapper = new QSignalMapper();
84 CONNECT( SDMapper, mapped (QString), this, SDMenuAction( QString ) );
86 new DialogHandler (p_intf, this );
89 DialogsProvider::~DialogsProvider()
91 PlaylistDialog::killInstance();
92 MediaInfoDialog::killInstance();
93 MessagesDialog::killInstance();
94 ExtendedDialog::killInstance();
95 BookmarksDialog::killInstance();
96 HelpDialog::killInstance();
98 UpdateDialog::killInstance();
100 PluginDialog::killInstance();
103 delete menusUpdateMapper;
106 QVLCMenu::PopupMenu( p_intf, false );
107 QVLCMenu::AudioPopupMenu( p_intf, false );
108 QVLCMenu::VideoPopupMenu( p_intf, false );
109 QVLCMenu::MiscPopupMenu( p_intf, false );
112 void DialogsProvider::quit()
115 libvlc_Quit( p_intf->p_libvlc );
118 void DialogsProvider::customEvent( QEvent *event )
120 if( event->type() == (int)DialogEvent_Type )
122 DialogEvent *de = static_cast<DialogEvent*>(event);
123 switch( de->i_dialog )
125 case INTF_DIALOG_FILE_SIMPLE:
126 case INTF_DIALOG_FILE:
128 case INTF_DIALOG_FILE_GENERIC:
129 openFileGenericDialog( de->p_arg ); break;
130 case INTF_DIALOG_DISC:
131 openDiscDialog(); break;
132 case INTF_DIALOG_NET:
133 openNetDialog(); break;
134 case INTF_DIALOG_SAT:
135 case INTF_DIALOG_CAPTURE:
136 openCaptureDialog(); break;
137 case INTF_DIALOG_DIRECTORY:
138 PLAppendDir(); break;
139 case INTF_DIALOG_PLAYLIST:
140 playlistDialog(); break;
141 case INTF_DIALOG_MESSAGES:
142 messagesDialog(); break;
143 case INTF_DIALOG_FILEINFO:
144 mediaInfoDialog(); break;
145 case INTF_DIALOG_PREFS:
146 prefsDialog(); break;
147 case INTF_DIALOG_BOOKMARKS:
148 bookmarksDialog(); break;
149 case INTF_DIALOG_EXTENDED:
150 extendedDialog(); break;
152 case INTF_DIALOG_VLM:
155 case INTF_DIALOG_POPUPMENU:
156 QVLCMenu::PopupMenu( p_intf, (de->i_arg != 0) ); break;
157 case INTF_DIALOG_AUDIOPOPUPMENU:
158 QVLCMenu::AudioPopupMenu( p_intf, (de->i_arg != 0) ); break;
159 case INTF_DIALOG_VIDEOPOPUPMENU:
160 QVLCMenu::VideoPopupMenu( p_intf, (de->i_arg != 0) ); break;
161 case INTF_DIALOG_MISCPOPUPMENU:
162 QVLCMenu::MiscPopupMenu( p_intf, (de->i_arg != 0) ); break;
163 case INTF_DIALOG_WIZARD:
164 case INTF_DIALOG_STREAMWIZARD:
165 openAndStreamingDialogs(); break;
167 case INTF_DIALOG_UPDATEVLC:
168 updateDialog(); break;
170 case INTF_DIALOG_EXIT:
173 msg_Warn( p_intf, "unimplemented dialog" );
178 /****************************************************************************
179 * Individual simple dialogs
180 ****************************************************************************/
181 void DialogsProvider::playlistDialog()
183 PlaylistDialog::getInstance( p_intf )->toggleVisible();
186 void DialogsProvider::prefsDialog()
188 PrefsDialog *p = new PrefsDialog( (QWidget *)p_intf->p_sys->p_mi, p_intf );
192 void DialogsProvider::extendedDialog()
194 if( !ExtendedDialog::getInstance( p_intf )->isVisible() || /* Hidden */
195 ExtendedDialog::getInstance( p_intf )->currentTab() != 0 ) /* wrong tab */
196 ExtendedDialog::getInstance( p_intf )->showTab( 0 );
198 ExtendedDialog::getInstance( p_intf )->hide();
201 void DialogsProvider::synchroDialog()
203 if( !ExtendedDialog::getInstance( p_intf )->isVisible() || /* Hidden */
204 ExtendedDialog::getInstance( p_intf )->currentTab() != 2 ) /* wrong tab */
205 ExtendedDialog::getInstance( p_intf )->showTab( 2 );
207 ExtendedDialog::getInstance( p_intf )->hide();
210 void DialogsProvider::messagesDialog()
212 MessagesDialog::getInstance( p_intf )->toggleVisible();
215 void DialogsProvider::gotoTimeDialog()
217 GotoTimeDialog::getInstance( p_intf )->toggleVisible();
221 void DialogsProvider::vlmDialog()
223 VLMDialog::getInstance( p_intf )->toggleVisible();
227 void DialogsProvider::helpDialog()
229 HelpDialog::getInstance( p_intf )->toggleVisible();
233 void DialogsProvider::updateDialog()
235 UpdateDialog::getInstance( p_intf )->toggleVisible();
239 void DialogsProvider::aboutDialog()
241 AboutDialog::getInstance( p_intf )->toggleVisible();
244 void DialogsProvider::mediaInfoDialog()
246 MediaInfoDialog::getInstance( p_intf )->showTab( 0 );
249 void DialogsProvider::mediaCodecDialog()
251 MediaInfoDialog::getInstance( p_intf )->showTab( 2 );
254 void DialogsProvider::bookmarksDialog()
256 BookmarksDialog::getInstance( p_intf )->toggleVisible();
259 void DialogsProvider::podcastConfigureDialog()
261 PodcastConfigDialog::getInstance( p_intf )->toggleVisible();
264 void DialogsProvider::toolbarDialog()
266 ToolbarEditDialog *toolbarEditor = new ToolbarEditDialog( (QWidget *)p_intf->p_sys->p_mi, p_intf );
267 if( toolbarEditor->exec() == QDialog::Accepted )
268 emit toolBarConfUpdated();
271 void DialogsProvider::pluginDialog()
273 PluginDialog::getInstance( p_intf )->toggleVisible();
276 void DialogsProvider::epgDialog()
278 EpgDialog::getInstance( p_intf )->toggleVisible();
281 /* Generic open file */
282 void DialogsProvider::openFileGenericDialog( intf_dialog_args_t *p_arg )
286 msg_Warn( p_intf, "openFileGenericDialog() called with NULL arg" );
290 /* Replace the extensions to a Qt format */
292 QString extensions = qfu( p_arg->psz_extensions );
293 while ( ( i = extensions.indexOf( "|", i ) ) != -1 )
295 if( ( extensions.count( "|" ) % 2 ) == 0 )
296 extensions.replace( i, 1, ");;" );
298 extensions.replace( i, 1, "(" );
300 extensions.replace( ";*", " *" );
301 extensions.append( ")" );
306 QString file = QFileDialog::getSaveFileName( NULL, p_arg->psz_title,
307 p_intf->p_sys->filepath, extensions );
308 if( !file.isEmpty() )
310 p_arg->i_results = 1;
311 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
312 p_arg->psz_results[0] = strdup( qtu( toNativeSepNoSlash( file ) ) );
315 p_arg->i_results = 0;
317 else /* non-save mode */
319 QStringList files = QFileDialog::getOpenFileNames( NULL,
320 p_arg->psz_title, p_intf->p_sys->filepath,
322 p_arg->i_results = files.count();
323 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
325 foreach( const QString &file, files )
326 p_arg->psz_results[i++] = strdup( qtu( toNativeSepNoSlash( file ) ) );
328 p_intf->p_sys->filepath = QString::fromAscii("");
330 p_intf->p_sys->filepath = qfu( p_arg->psz_results[i-1] );
334 if( p_arg->pf_callback )
335 p_arg->pf_callback( p_arg );
337 /* Clean afterwards */
338 if( p_arg->psz_results )
340 for( i = 0; i < p_arg->i_results; i++ )
341 free( p_arg->psz_results[i] );
342 free( p_arg->psz_results );
344 free( p_arg->psz_title );
345 free( p_arg->psz_extensions );
348 /****************************************************************************
349 * All the open/add stuff
350 * Open Dialog first - Simple Open then
351 ****************************************************************************/
353 void DialogsProvider::openDialog( int i_tab )
355 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf )->showTab( i_tab );
357 void DialogsProvider::openDialog()
359 openDialog( OPEN_FILE_TAB );
361 void DialogsProvider::openFileDialog()
363 openDialog( OPEN_FILE_TAB );
365 void DialogsProvider::openDiscDialog()
367 openDialog( OPEN_DISC_TAB );
369 void DialogsProvider::openNetDialog()
371 openDialog( OPEN_NETWORK_TAB );
373 void DialogsProvider::openCaptureDialog()
375 openDialog( OPEN_CAPTURE_TAB );
378 /* Same as the open one, but force the enqueue */
379 void DialogsProvider::PLAppendDialog( int tab )
381 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
382 OPEN_AND_ENQUEUE )->showTab( tab );
385 void DialogsProvider::MLAppendDialog( int tab )
387 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
388 OPEN_AND_ENQUEUE, false, false )
395 QStringList DialogsProvider::showSimpleOpen( const QString& help,
397 const QString& path )
399 QString fileTypes = "";
400 if( filters & EXT_FILTER_MEDIA ) {
401 ADD_FILTER_MEDIA( fileTypes );
403 if( filters & EXT_FILTER_VIDEO ) {
404 ADD_FILTER_VIDEO( fileTypes );
406 if( filters & EXT_FILTER_AUDIO ) {
407 ADD_FILTER_AUDIO( fileTypes );
409 if( filters & EXT_FILTER_PLAYLIST ) {
410 ADD_FILTER_PLAYLIST( fileTypes );
412 if( filters & EXT_FILTER_SUBTITLE ) {
413 ADD_FILTER_SUBTITLE( fileTypes );
415 ADD_FILTER_ALL( fileTypes );
416 fileTypes.replace( ";*", " *");
418 QStringList files = QFileDialog::getOpenFileNames( NULL,
419 help.isEmpty() ? qtr(I_OP_SEL_FILES ) : help,
420 path.isEmpty() ? p_intf->p_sys->filepath : path,
423 if( !files.isEmpty() ) savedirpathFromFile( files.last() );
430 * pl helps you to choose from playlist or media library,
431 * go to start or enqueue
433 void DialogsProvider::addFromSimple( bool pl, bool go)
435 QStringList files = DialogsProvider::showSimpleOpen();
436 int mode = go ? PLAYLIST_GO : PLAYLIST_PREPARSE;
439 foreach( const QString &file, files )
441 QString url = toURI( toNativeSeparators( file ) );
442 playlist_Add( THEPL, qtu( url ), NULL, PLAYLIST_APPEND | mode,
443 PLAYLIST_END, pl, pl_Unlocked );
444 RecentsMRL::getInstance( p_intf )->addRecent( url );
445 mode = PLAYLIST_PREPARSE;
449 void DialogsProvider::simpleOpenDialog()
451 addFromSimple( true, true ); /* Playlist and Go */
454 void DialogsProvider::simplePLAppendDialog()
456 addFromSimple( true, false );
459 void DialogsProvider::simpleMLAppendDialog()
461 addFromSimple( false, false );
464 /* Url & Clipboard */
467 * If the clipboard contains URLs, the first is automatically 'preselected'.
469 void DialogsProvider::openUrlDialog()
471 OpenUrlDialog *oud = new OpenUrlDialog( p_intf );
472 if( oud->exec() == QDialog::Accepted )
474 QString url = oud->url();
477 playlist_Add( THEPL, qtu( url ),
478 NULL, !oud->shouldEnqueue() ?
479 ( PLAYLIST_APPEND | PLAYLIST_GO )
480 : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
481 PLAYLIST_END, true, false );
482 RecentsMRL::getInstance( p_intf )->addRecent( url );
491 * pl helps you to choose from playlist or media library,
492 * go to start or enqueue
494 static void openDirectory( intf_thread_t *p_intf, bool pl, bool go )
496 QString dir = QFileDialog::getExistingDirectory( NULL, qtr( I_OP_DIR_WINTITLE ), p_intf->p_sys->filepath );
501 const char *scheme = "directory";
502 if( dir.endsWith( "/VIDEO_TS", Qt::CaseInsensitive ) )
505 char *uri = make_URI( qtu( toNativeSeparators( dir ) ), scheme );
506 if( unlikely(uri == NULL) )
509 RecentsMRL::getInstance( p_intf )->addRecent( qfu(uri) );
511 input_item_t *p_input = input_item_New( uri, NULL );
513 if( unlikely( p_input == NULL ) )
516 /* FIXME: playlist_AddInput() can fail */
517 playlist_AddInput( THEPL, p_input,
518 go ? ( PLAYLIST_APPEND | PLAYLIST_GO ) : PLAYLIST_APPEND,
519 PLAYLIST_END, pl, pl_Unlocked );
520 vlc_gc_decref( p_input );
523 void DialogsProvider::PLOpenDir()
525 openDirectory( p_intf, true, true );
528 void DialogsProvider::PLAppendDir()
530 openDirectory( p_intf, true, false );
533 void DialogsProvider::MLAppendDir()
535 openDirectory( p_intf, false , false );
541 void DialogsProvider::openAPlaylist()
543 QStringList files = showSimpleOpen( qtr( "Open playlist..." ),
544 EXT_FILTER_PLAYLIST );
545 foreach( const QString &file, files )
547 playlist_Import( THEPL, qtu( toNativeSeparators( file ) ) );
551 void DialogsProvider::saveAPlaylist()
555 char filter_name[14];
556 char filter_patterns[5];
559 { N_("XSPF playlist"), "xspf", "export-xspf", },
560 { N_("M3U playlist"), "m3u", "export-m3u", },
561 { N_("M3U8 playlist"), "m3u8", "export-m3u8", },
562 { N_("HTML playlist"), "html", "export-html", },
566 QString ext = getSettings()->value( "last-playlist-ext" ).toString();
568 for( size_t i = 0; i < sizeof (types) / sizeof (types[0]); i++ )
570 QString tmp = qfu( vlc_gettext( types[i].filter_name ) ) + " (*." + types[i].filter_patterns + ")";
571 if( ext == qfu( types[i].filter_patterns ) )
572 filters.insert( 0, tmp );
574 filters.append( tmp );
578 QString file = QFileDialog::getSaveFileName( NULL,
579 qtr( "Save playlist as..." ),
580 p_intf->p_sys->filepath, filters.join( ";;" ),
585 for( size_t i = 0; i < sizeof (types) / sizeof (types[0]); i++)
586 if( selected == qfu( vlc_gettext( types[i].filter_name ) ) + " (*." + qfu( types[i].filter_patterns ) + ")" )
588 playlist_Export( THEPL, qtu( toNativeSeparators( file ) ),
589 THEPL->p_playing, types[i].module );
590 getSettings()->setValue( "last-playlist-ext", types[i].filter_patterns );
595 /****************************************************************************
597 ****************************************************************************/
599 void DialogsProvider::streamingDialog( QWidget *parent,
601 bool b_transcode_only,
602 QStringList options )
607 if( !b_transcode_only )
609 SoutDialog *s = new SoutDialog( parent, p_intf, mrl );
610 if( s->exec() == QDialog::Accepted )
612 soutoption = s->getMrl();
621 ConvertDialog *s = new ConvertDialog( parent, p_intf, mrl );
622 if( s->exec() == QDialog::Accepted )
624 soutoption = s->getMrl();
634 if( !soutoption.isEmpty() )
636 options += soutoption.split( " :");
639 input_item_t *p_input;
640 p_input = input_item_New( qtu( mrl ), _("Streaming") );
642 /* Add normal Options */
643 for( int j = 0; j < options.count(); j++ )
645 QString qs = colon_unescape( options[j] );
648 input_item_AddOption( p_input, qtu( qs ),
649 VLC_INPUT_OPTION_TRUSTED );
650 msg_Dbg( p_intf, "Adding option: %s", qtu( qs ) );
654 /* Switch between enqueuing and starting the item */
655 /* FIXME: playlist_AddInput() can fail */
656 playlist_AddInput( THEPL, p_input,
657 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END, true, pl_Unlocked );
658 vlc_gc_decref( p_input );
660 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
664 void DialogsProvider::openAndStreamingDialogs()
666 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false, OPEN_AND_STREAM )
667 ->showTab( OPEN_FILE_TAB );
670 void DialogsProvider::openAndTranscodingDialogs()
672 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, false, OPEN_AND_SAVE )
673 ->showTab( OPEN_FILE_TAB );
676 void DialogsProvider::loadSubtitlesFile()
678 input_thread_t *p_input = THEMIM->getInput();
679 if( !p_input ) return;
681 input_item_t *p_item = input_GetItem( p_input );
682 if( !p_item ) return;
684 char *path = input_item_GetURI( p_item );
688 path2 = make_path( path );
692 char *sep = strrchr( path2, DIR_SEP_CHAR );
693 if( sep ) *sep = '\0';
697 QStringList qsl = showSimpleOpen( qtr( "Open subtitles..." ),
701 foreach( const QString &qsFile, qsl )
703 if( input_AddSubtitle( p_input, qtu( toNativeSeparators( qsFile ) ),
705 msg_Warn( p_intf, "unable to load subtitles from '%s'",
711 /****************************************************************************
713 ****************************************************************************/
715 void DialogsProvider::menuAction( QObject *data )
717 QVLCMenu::DoAction( data );
720 void DialogsProvider::menuUpdateAction( QObject *data )
722 MenuFunc *func = qobject_cast<MenuFunc *>(data);
724 func->doFunc( p_intf );
727 void DialogsProvider::SDMenuAction( const QString& data )
729 if( !playlist_IsServicesDiscoveryLoaded( THEPL, qtu( data ) ) )
730 playlist_ServicesDiscoveryAdd( THEPL, qtu( data ) );
732 playlist_ServicesDiscoveryRemove( THEPL, qtu( data ) );
736 * Play the MRL contained in the Recently played menu.
738 void DialogsProvider::playMRL( const QString &mrl )
740 playlist_Add( THEPL, qtu(mrl), NULL,
741 PLAYLIST_APPEND | PLAYLIST_GO , PLAYLIST_END, true, false );
742 RecentsMRL::getInstance( p_intf )->addRecent( mrl );