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 "util/customwidgets.hpp" /* VLCKeyToString() */
38 #include "main_interface.hpp"
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/convert.hpp"
50 #include "dialogs/open.hpp"
51 #include "dialogs/openurl.hpp"
52 #include "dialogs/help.hpp"
53 #include "dialogs/gototime.hpp"
54 #include "dialogs/podcast_configuration.hpp"
55 #include "dialogs/toolbar.hpp"
56 #include "dialogs/plugins.hpp"
57 #include "dialogs/external.hpp"
58 #include "dialogs/epg.hpp"
59 #include "dialogs/errors.hpp"
62 #include <QApplication>
63 #include <QSignalMapper>
64 #include <QFileDialog>
66 #define I_OP_DIR_WINTITLE I_DIR_OR_FOLDER( N_("Open Directory"), \
69 DialogsProvider* DialogsProvider::instance = NULL;
71 DialogsProvider::DialogsProvider( intf_thread_t *_p_intf ) :
72 QObject( NULL ), p_intf( _p_intf ),
74 videoPopupMenu( NULL ),
75 audioPopupMenu( NULL ),
80 /* Various signal mappers for the menus */
81 menusMapper = new QSignalMapper();
82 CONNECT( menusMapper, mapped(QObject *), this, menuAction( QObject *) );
84 menusUpdateMapper = new QSignalMapper();
85 CONNECT( menusUpdateMapper, mapped(QObject *),
86 this, menuUpdateAction( QObject *) );
88 SDMapper = new QSignalMapper();
89 CONNECT( SDMapper, mapped (QString), this, SDMenuAction( QString ) );
91 new DialogHandler (p_intf, this );
94 DialogsProvider::~DialogsProvider()
96 PlaylistDialog::killInstance();
97 MediaInfoDialog::killInstance();
98 MessagesDialog::killInstance();
99 BookmarksDialog::killInstance();
100 HelpDialog::killInstance();
102 UpdateDialog::killInstance();
104 PluginDialog::killInstance();
105 EpgDialog::killInstance();
108 delete menusUpdateMapper;
112 delete videoPopupMenu;
113 delete audioPopupMenu;
114 delete miscPopupMenu;
117 void DialogsProvider::quit()
120 libvlc_Quit( p_intf->p_libvlc );
123 void DialogsProvider::customEvent( QEvent *event )
125 if( event->type() == DialogEvent::DialogEvent_Type )
127 DialogEvent *de = static_cast<DialogEvent*>(event);
128 switch( de->i_dialog )
130 case INTF_DIALOG_FILE_SIMPLE:
131 case INTF_DIALOG_FILE:
133 case INTF_DIALOG_FILE_GENERIC:
134 openFileGenericDialog( de->p_arg ); break;
135 case INTF_DIALOG_DISC:
136 openDiscDialog(); break;
137 case INTF_DIALOG_NET:
138 openNetDialog(); break;
139 case INTF_DIALOG_SAT:
140 case INTF_DIALOG_CAPTURE:
141 openCaptureDialog(); break;
142 case INTF_DIALOG_DIRECTORY:
143 PLAppendDir(); break;
144 case INTF_DIALOG_PLAYLIST:
145 playlistDialog(); break;
146 case INTF_DIALOG_MESSAGES:
147 messagesDialog(); break;
148 case INTF_DIALOG_FILEINFO:
149 mediaInfoDialog(); break;
150 case INTF_DIALOG_PREFS:
151 prefsDialog(); break;
152 case INTF_DIALOG_BOOKMARKS:
153 bookmarksDialog(); break;
154 case INTF_DIALOG_EXTENDED:
155 extendedDialog(); break;
156 case INTF_DIALOG_SENDKEY:
157 sendKey( de->i_arg ); break;
159 case INTF_DIALOG_VLM:
162 case INTF_DIALOG_POPUPMENU:
164 delete popupMenu; popupMenu = NULL;
165 bool show = (de->i_arg != 0);
167 popupMenu = VLCMenuBar::PopupMenu( p_intf, show );
170 case INTF_DIALOG_AUDIOPOPUPMENU:
172 delete audioPopupMenu; audioPopupMenu = NULL;
173 bool show = (de->i_arg != 0);
175 audioPopupMenu = VLCMenuBar::AudioPopupMenu( p_intf, show );
178 case INTF_DIALOG_VIDEOPOPUPMENU:
180 delete videoPopupMenu; videoPopupMenu = NULL;
181 bool show = (de->i_arg != 0);
183 videoPopupMenu = VLCMenuBar::VideoPopupMenu( p_intf, show );
186 case INTF_DIALOG_MISCPOPUPMENU:
188 delete miscPopupMenu; miscPopupMenu = NULL;
189 bool show = (de->i_arg != 0);
191 miscPopupMenu = VLCMenuBar::MiscPopupMenu( p_intf, show );
194 case INTF_DIALOG_WIZARD:
195 case INTF_DIALOG_STREAMWIZARD:
196 openAndStreamingDialogs(); break;
198 case INTF_DIALOG_UPDATEVLC:
199 updateDialog(); break;
201 case INTF_DIALOG_EXIT:
204 msg_Warn( p_intf, "unimplemented dialog" );
209 /****************************************************************************
210 * Individual simple dialogs
211 ****************************************************************************/
212 const QEvent::Type DialogEvent::DialogEvent_Type =
213 (QEvent::Type)QEvent::registerEventType();
215 void DialogsProvider::playlistDialog()
217 PlaylistDialog::getInstance( p_intf )->toggleVisible();
220 void DialogsProvider::prefsDialog()
222 PrefsDialog *p = new PrefsDialog( (QWidget *)p_intf->p_sys->p_mi, p_intf );
226 void DialogsProvider::extendedDialog()
228 ExtendedDialog *extDialog = ExtendedDialog::getInstance(p_intf );
230 if( !extDialog->isVisible() || /* Hidden */
231 extDialog->currentTab() != 0 ) /* wrong tab */
232 extDialog->showTab( 0 );
237 void DialogsProvider::synchroDialog()
239 ExtendedDialog *extDialog = ExtendedDialog::getInstance(p_intf );
241 if( !extDialog->isVisible() || /* Hidden */
242 extDialog->currentTab() != 2 ) /* wrong tab */
243 extDialog->showTab( 2 );
248 void DialogsProvider::messagesDialog()
250 MessagesDialog::getInstance( p_intf )->toggleVisible();
253 void DialogsProvider::gotoTimeDialog()
255 GotoTimeDialog::getInstance( p_intf )->toggleVisible();
259 void DialogsProvider::vlmDialog()
261 VLMDialog::getInstance( p_intf )->toggleVisible();
265 void DialogsProvider::helpDialog()
267 HelpDialog::getInstance( p_intf )->toggleVisible();
271 void DialogsProvider::updateDialog()
273 UpdateDialog::getInstance( p_intf )->toggleVisible();
277 void DialogsProvider::aboutDialog()
279 AboutDialog::getInstance( p_intf )->toggleVisible();
282 void DialogsProvider::mediaInfoDialog()
284 MediaInfoDialog::getInstance( p_intf )->showTab( MediaInfoDialog::META_PANEL );
287 void DialogsProvider::mediaCodecDialog()
289 MediaInfoDialog::getInstance( p_intf )->showTab( MediaInfoDialog::INFO_PANEL );
292 void DialogsProvider::bookmarksDialog()
294 BookmarksDialog::getInstance( p_intf )->toggleVisible();
297 void DialogsProvider::podcastConfigureDialog()
299 PodcastConfigDialog::getInstance( p_intf )->toggleVisible();
302 void DialogsProvider::toolbarDialog()
304 ToolbarEditDialog *toolbarEditor = new ToolbarEditDialog( (QWidget *)p_intf->p_sys->p_mi, p_intf );
305 if( toolbarEditor->exec() == QDialog::Accepted )
306 emit toolBarConfUpdated();
309 void DialogsProvider::pluginDialog()
311 PluginDialog::getInstance( p_intf )->toggleVisible();
314 void DialogsProvider::epgDialog()
316 EpgDialog::getInstance( p_intf )->toggleVisible();
319 void DialogsProvider::setPopupMenu()
322 popupMenu = VLCMenuBar::PopupMenu( p_intf, true );
325 void DialogsProvider::destroyPopupMenu()
331 /* Generic open file */
332 void DialogsProvider::openFileGenericDialog( intf_dialog_args_t *p_arg )
336 msg_Warn( p_intf, "openFileGenericDialog() called with NULL arg" );
340 /* Replace the extensions to a Qt format */
342 QString extensions = qfu( p_arg->psz_extensions );
343 while ( ( i = extensions.indexOf( "|", i ) ) != -1 )
345 if( ( extensions.count( "|" ) % 2 ) == 0 )
346 extensions.replace( i, 1, ");;" );
348 extensions.replace( i, 1, "(" );
350 extensions.replace( ";*", " *" );
351 extensions.append( ")" );
356 QString file = QFileDialog::getSaveFileName( NULL,
357 qfu( p_arg->psz_title ),
358 p_intf->p_sys->filepath, extensions );
359 if( !file.isEmpty() )
361 p_arg->i_results = 1;
362 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
363 p_arg->psz_results[0] = strdup( qtu( toNativeSepNoSlash( file ) ) );
366 p_arg->i_results = 0;
368 else /* non-save mode */
370 QStringList files = QFileDialog::getOpenFileNames( NULL,
371 qfu( p_arg->psz_title ), p_intf->p_sys->filepath,
373 p_arg->i_results = files.count();
374 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
376 foreach( const QString &file, files )
377 p_arg->psz_results[i++] = strdup( qtu( toNativeSepNoSlash( file ) ) );
379 p_intf->p_sys->filepath = QString::fromLatin1("");
381 p_intf->p_sys->filepath = qfu( p_arg->psz_results[i-1] );
385 if( p_arg->pf_callback )
386 p_arg->pf_callback( p_arg );
388 /* Clean afterwards */
389 if( p_arg->psz_results )
391 for( i = 0; i < p_arg->i_results; i++ )
392 free( p_arg->psz_results[i] );
393 free( p_arg->psz_results );
395 free( p_arg->psz_title );
396 free( p_arg->psz_extensions );
399 /****************************************************************************
400 * All the open/add stuff
401 * Open Dialog first - Simple Open then
402 ****************************************************************************/
404 void DialogsProvider::openDialog( int i_tab )
406 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf )->showTab( i_tab );
408 void DialogsProvider::openDialog()
410 openDialog( OPEN_FILE_TAB );
412 void DialogsProvider::openFileDialog()
414 openDialog( OPEN_FILE_TAB );
416 void DialogsProvider::openDiscDialog()
418 openDialog( OPEN_DISC_TAB );
420 void DialogsProvider::openNetDialog()
422 openDialog( OPEN_NETWORK_TAB );
424 void DialogsProvider::openCaptureDialog()
426 openDialog( OPEN_CAPTURE_TAB );
429 /* Same as the open one, but force the enqueue */
430 void DialogsProvider::PLAppendDialog( int tab )
432 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
433 OPEN_AND_ENQUEUE )->showTab( tab );
436 void DialogsProvider::MLAppendDialog( int tab )
438 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
439 OPEN_AND_ENQUEUE, false, false )
446 QStringList DialogsProvider::showSimpleOpen( const QString& help,
448 const QString& path )
450 QString fileTypes = "";
451 if( filters & EXT_FILTER_MEDIA ) {
452 ADD_EXT_FILTER( fileTypes, EXTENSIONS_MEDIA );
454 if( filters & EXT_FILTER_VIDEO ) {
455 ADD_EXT_FILTER( fileTypes, EXTENSIONS_VIDEO );
457 if( filters & EXT_FILTER_AUDIO ) {
458 ADD_EXT_FILTER( fileTypes, EXTENSIONS_AUDIO );
460 if( filters & EXT_FILTER_PLAYLIST ) {
461 ADD_EXT_FILTER( fileTypes, EXTENSIONS_PLAYLIST );
463 if( filters & EXT_FILTER_SUBTITLE ) {
464 ADD_EXT_FILTER( fileTypes, EXTENSIONS_SUBTITLE );
466 ADD_EXT_FILTER( fileTypes, EXTENSIONS_ALL );
467 fileTypes.replace( ";*", " *");
469 QStringList files = QFileDialog::getOpenFileNames( NULL,
470 help.isEmpty() ? qtr(I_OP_SEL_FILES ) : help,
471 path.isEmpty() ? p_intf->p_sys->filepath : path,
474 if( !files.isEmpty() ) savedirpathFromFile( files.last() );
481 * pl helps you to choose from playlist or media library,
482 * go to start or enqueue
484 void DialogsProvider::addFromSimple( bool pl, bool go)
486 QStringList files = DialogsProvider::showSimpleOpen();
490 foreach( const QString &file, files )
492 QString url = toURI( toNativeSeparators( file ) );
493 Open::openMRL( p_intf, url, first, pl);
498 void DialogsProvider::simpleOpenDialog()
500 addFromSimple( true, true ); /* Playlist and Go */
503 /* Url & Clipboard */
506 * If the clipboard contains URLs, the first is automatically 'preselected'.
508 void DialogsProvider::openUrlDialog()
510 OpenUrlDialog oud( p_intf );
511 if( oud.exec() != QDialog::Accepted )
514 QString url = oud.url();
518 if( !url.contains( qfu( "://" ) ) )
520 char *uri = vlc_path2uri( qtu( url ), NULL );
527 Open::openMRL( p_intf, qtu(url), !oud.shouldEnqueue() );
533 * pl helps you to choose from playlist or media library,
534 * go to start or enqueue
536 static void openDirectory( intf_thread_t *p_intf, bool pl, bool go )
538 QString uri = DialogsProvider::getDirectoryDialog( p_intf );
540 Open::openMRL( p_intf, uri, go, pl );
543 QString DialogsProvider::getDirectoryDialog( intf_thread_t *p_intf )
545 QString dir = QFileDialog::getExistingDirectory( NULL,
546 qtr( I_OP_DIR_WINTITLE ), p_intf->p_sys->filepath );
548 if( dir.isEmpty() ) return QString();
550 p_intf->p_sys->filepath = dir;
552 const char *scheme = "directory";
553 if( dir.endsWith( DIR_SEP "VIDEO_TS", Qt::CaseInsensitive ) )
555 else if( dir.endsWith( DIR_SEP "BDMV", Qt::CaseInsensitive ) )
558 dir.remove( "BDMV" );
561 char *uri = vlc_path2uri( qtu( toNativeSeparators( dir ) ), scheme );
562 if( unlikely(uri == NULL) )
568 RecentsMRL::getInstance( p_intf )->addRecent( dir );
573 void DialogsProvider::PLOpenDir()
575 openDirectory( p_intf, true, true );
578 void DialogsProvider::PLAppendDir()
580 openDirectory( p_intf, true, false );
586 void DialogsProvider::openAPlaylist()
588 QStringList files = showSimpleOpen( qtr( "Open playlist..." ),
589 EXT_FILTER_PLAYLIST );
590 foreach( const QString &file, files )
592 playlist_Import( THEPL, qtu( toNativeSeparators( file ) ) );
596 void DialogsProvider::saveAPlaylist(playlist_t *p_playlist, playlist_item_t *p_node)
600 char filter_name[14];
601 char filter_patterns[5];
604 { N_("XSPF playlist"), "xspf", "export-xspf", },
605 { N_("M3U playlist"), "m3u", "export-m3u", },
606 { N_("M3U8 playlist"), "m3u8", "export-m3u8", },
607 { N_("HTML playlist"), "html", "export-html", },
611 QString ext = getSettings()->value( "last-playlist-ext" ).toString();
613 for( size_t i = 0; i < sizeof (types) / sizeof (types[0]); i++ )
615 QString tmp = qfu( vlc_gettext( types[i].filter_name ) ) + " (*." + types[i].filter_patterns + ")";
616 if( ext == qfu( types[i].filter_patterns ) )
617 filters.insert( 0, tmp );
619 filters.append( tmp );
623 QString file = QFileDialog::getSaveFileName( NULL,
624 qtr( "Save playlist as..." ),
625 p_intf->p_sys->filepath, filters.join( ";;" ),
627 const char *psz_selected_module = NULL;
628 const char *psz_last_playlist_ext = NULL;
633 /* First test if the file extension is set, and different to selected filter */
634 for( size_t i = 0; i < sizeof (types) / sizeof (types[0]); i++)
636 if ( file.endsWith( QString( "." ) + qfu( types[i].filter_patterns ) ) )
638 psz_selected_module = types[i].module;
639 psz_last_playlist_ext = types[i].filter_patterns;
644 /* otherwise apply the selected extension */
645 if ( !psz_last_playlist_ext )
647 for( size_t i = 0; i < sizeof (types) / sizeof (types[0]); i++)
649 if ( selected.startsWith( qfu( vlc_gettext( types[i].filter_name ) ) ) )
651 psz_selected_module = types[i].module;
652 psz_last_playlist_ext = types[i].filter_patterns;
653 /* Fix file extension */
654 file = file.append( QString( "." ) + qfu( psz_last_playlist_ext ) );
660 if ( psz_selected_module )
662 playlist_Export( p_playlist, qtu( toNativeSeparators( file ) ),
663 p_node, psz_selected_module );
664 getSettings()->setValue( "last-playlist-ext", psz_last_playlist_ext );
668 void DialogsProvider::savePlayingToPlaylist()
670 saveAPlaylist(THEPL, THEPL->p_playing);
673 void DialogsProvider::saveRecentsToPlaylist()
675 playlist_item_t *p_node_recents = RecentsMRL::getInstance(p_intf)->toPlaylist(0);
677 if (p_node_recents == NULL)
679 msg_Warn(p_intf, "cannot create playlist from recents");
683 saveAPlaylist(THEPL, p_node_recents);
684 playlist_NodeDelete(THEPL, p_node_recents, true, false);
687 /****************************************************************************
689 ****************************************************************************/
691 void DialogsProvider::streamingDialog( QWidget *parent,
693 bool b_transcode_only,
694 QStringList options )
699 if( !b_transcode_only )
701 SoutDialog *s = new SoutDialog( parent, p_intf, mrl );
702 s->setAttribute( Qt::WA_QuitOnClose, false ); // See #4883
703 if( s->exec() == QDialog::Accepted )
705 soutoption = s->getMrl();
714 ConvertDialog *s = new ConvertDialog( parent, p_intf, mrl );
715 s->setAttribute( Qt::WA_QuitOnClose, false ); // See #4883
716 if( s->exec() == QDialog::Accepted )
718 soutoption = s->getMrl();
728 if( !soutoption.isEmpty() )
730 options += soutoption.split( " :");
732 Open::openMRLwithOptions( p_intf, mrl, &options, true, true, _("Streaming") );
736 void DialogsProvider::openAndStreamingDialogs()
738 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false, OPEN_AND_STREAM )
739 ->showTab( OPEN_FILE_TAB );
742 void DialogsProvider::openAndTranscodingDialogs()
744 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, false, OPEN_AND_SAVE )
745 ->showTab( OPEN_FILE_TAB );
748 void DialogsProvider::loadSubtitlesFile()
750 input_thread_t *p_input = THEMIM->getInput();
751 if( !p_input ) return;
753 input_item_t *p_item = input_GetItem( p_input );
754 if( !p_item ) return;
756 char *path = input_item_GetURI( p_item );
760 path2 = make_path( path );
764 char *sep = strrchr( path2, DIR_SEP_CHAR );
765 if( sep ) *sep = '\0';
769 QStringList qsl = showSimpleOpen( qtr( "Open subtitles..." ),
773 foreach( const QString &qsFile, qsl )
775 if( input_AddSubtitleOSD( p_input, qtu( toNativeSeparators( qsFile ) ),
777 msg_Warn( p_intf, "unable to load subtitles from '%s'",
783 /****************************************************************************
785 ****************************************************************************/
787 void DialogsProvider::menuAction( QObject *data )
789 VLCMenuBar::DoAction( data );
792 void DialogsProvider::menuUpdateAction( QObject *data )
794 MenuFunc *func = qobject_cast<MenuFunc *>(data);
796 func->doFunc( p_intf );
799 void DialogsProvider::SDMenuAction( const QString& data )
801 if( !playlist_IsServicesDiscoveryLoaded( THEPL, qtu( data ) ) )
802 playlist_ServicesDiscoveryAdd( THEPL, qtu( data ) );
804 playlist_ServicesDiscoveryRemove( THEPL, qtu( data ) );
807 void DialogsProvider::sendKey( int key )
809 // translate from a vlc keycode into a Qt sequence
810 QKeySequence kseq0( VLCKeyToString( key, true ) );
812 if( popupMenu == NULL )
814 // make sure at least a non visible popupmenu is available
815 popupMenu = VLCMenuBar::PopupMenu( p_intf, false );
816 if( unlikely( popupMenu == NULL ) )
820 // test against key accelerators from the popupmenu
821 QList<QAction*> actions = popupMenu->findChildren<QAction*>();
822 for( int i = 0; i < actions.size(); i++ )
824 QAction* action = actions.at(i);
825 QKeySequence kseq = action->shortcut();
833 // forward key to vlc core when not a key accelerator
834 var_SetInteger( p_intf->p_libvlc, "key-pressed", key );