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 BookmarksDialog::killInstance();
95 HelpDialog::killInstance();
97 UpdateDialog::killInstance();
99 PluginDialog::killInstance();
100 EpgDialog::killInstance();
103 delete menusUpdateMapper;
106 VLCMenuBar::PopupMenu( p_intf, false );
107 VLCMenuBar::AudioPopupMenu( p_intf, false );
108 VLCMenuBar::VideoPopupMenu( p_intf, false );
109 VLCMenuBar::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() == DialogEvent::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 VLCMenuBar::PopupMenu( p_intf, (de->i_arg != 0) ); break;
157 case INTF_DIALOG_AUDIOPOPUPMENU:
158 VLCMenuBar::AudioPopupMenu( p_intf, (de->i_arg != 0) ); break;
159 case INTF_DIALOG_VIDEOPOPUPMENU:
160 VLCMenuBar::VideoPopupMenu( p_intf, (de->i_arg != 0) ); break;
161 case INTF_DIALOG_MISCPOPUPMENU:
162 VLCMenuBar::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 const QEvent::Type DialogEvent::DialogEvent_Type =
182 (QEvent::Type)QEvent::registerEventType();
184 void DialogsProvider::playlistDialog()
186 PlaylistDialog::getInstance( p_intf )->toggleVisible();
189 void DialogsProvider::prefsDialog()
191 PrefsDialog *p = new PrefsDialog( (QWidget *)p_intf->p_sys->p_mi, p_intf );
195 void DialogsProvider::extendedDialog()
197 ExtendedDialog *extDialog = ExtendedDialog::getInstance(p_intf );
199 if( !extDialog->isVisible() || /* Hidden */
200 extDialog->currentTab() != 0 ) /* wrong tab */
201 extDialog->showTab( 0 );
206 void DialogsProvider::synchroDialog()
208 ExtendedDialog *extDialog = ExtendedDialog::getInstance(p_intf );
210 if( !extDialog->isVisible() || /* Hidden */
211 extDialog->currentTab() != 2 ) /* wrong tab */
212 extDialog->showTab( 2 );
217 void DialogsProvider::messagesDialog()
219 MessagesDialog::getInstance( p_intf )->toggleVisible();
222 void DialogsProvider::gotoTimeDialog()
224 GotoTimeDialog::getInstance( p_intf )->toggleVisible();
228 void DialogsProvider::vlmDialog()
230 VLMDialog::getInstance( p_intf )->toggleVisible();
234 void DialogsProvider::helpDialog()
236 HelpDialog::getInstance( p_intf )->toggleVisible();
240 void DialogsProvider::updateDialog()
242 UpdateDialog::getInstance( p_intf )->toggleVisible();
246 void DialogsProvider::aboutDialog()
248 AboutDialog::getInstance( p_intf )->toggleVisible();
251 void DialogsProvider::mediaInfoDialog()
253 MediaInfoDialog::getInstance( p_intf )->showTab( MediaInfoDialog::META_PANEL );
256 void DialogsProvider::mediaCodecDialog()
258 MediaInfoDialog::getInstance( p_intf )->showTab( MediaInfoDialog::INFO_PANEL );
261 void DialogsProvider::bookmarksDialog()
263 BookmarksDialog::getInstance( p_intf )->toggleVisible();
266 void DialogsProvider::podcastConfigureDialog()
268 PodcastConfigDialog::getInstance( p_intf )->toggleVisible();
271 void DialogsProvider::toolbarDialog()
273 ToolbarEditDialog *toolbarEditor = new ToolbarEditDialog( (QWidget *)p_intf->p_sys->p_mi, p_intf );
274 if( toolbarEditor->exec() == QDialog::Accepted )
275 emit toolBarConfUpdated();
278 void DialogsProvider::pluginDialog()
280 PluginDialog::getInstance( p_intf )->toggleVisible();
283 void DialogsProvider::epgDialog()
285 EpgDialog::getInstance( p_intf )->toggleVisible();
288 /* Generic open file */
289 void DialogsProvider::openFileGenericDialog( intf_dialog_args_t *p_arg )
293 msg_Warn( p_intf, "openFileGenericDialog() called with NULL arg" );
297 /* Replace the extensions to a Qt format */
299 QString extensions = qfu( p_arg->psz_extensions );
300 while ( ( i = extensions.indexOf( "|", i ) ) != -1 )
302 if( ( extensions.count( "|" ) % 2 ) == 0 )
303 extensions.replace( i, 1, ");;" );
305 extensions.replace( i, 1, "(" );
307 extensions.replace( ";*", " *" );
308 extensions.append( ")" );
313 QString file = QFileDialog::getSaveFileName( NULL, p_arg->psz_title,
314 p_intf->p_sys->filepath, extensions );
315 if( !file.isEmpty() )
317 p_arg->i_results = 1;
318 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
319 p_arg->psz_results[0] = strdup( qtu( toNativeSepNoSlash( file ) ) );
322 p_arg->i_results = 0;
324 else /* non-save mode */
326 QStringList files = QFileDialog::getOpenFileNames( NULL,
327 p_arg->psz_title, p_intf->p_sys->filepath,
329 p_arg->i_results = files.count();
330 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
332 foreach( const QString &file, files )
333 p_arg->psz_results[i++] = strdup( qtu( toNativeSepNoSlash( file ) ) );
335 p_intf->p_sys->filepath = QString::fromAscii("");
337 p_intf->p_sys->filepath = qfu( p_arg->psz_results[i-1] );
341 if( p_arg->pf_callback )
342 p_arg->pf_callback( p_arg );
344 /* Clean afterwards */
345 if( p_arg->psz_results )
347 for( i = 0; i < p_arg->i_results; i++ )
348 free( p_arg->psz_results[i] );
349 free( p_arg->psz_results );
351 free( p_arg->psz_title );
352 free( p_arg->psz_extensions );
355 /****************************************************************************
356 * All the open/add stuff
357 * Open Dialog first - Simple Open then
358 ****************************************************************************/
360 void DialogsProvider::openDialog( int i_tab )
362 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf )->showTab( i_tab );
364 void DialogsProvider::openDialog()
366 openDialog( OPEN_FILE_TAB );
368 void DialogsProvider::openFileDialog()
370 openDialog( OPEN_FILE_TAB );
372 void DialogsProvider::openDiscDialog()
374 openDialog( OPEN_DISC_TAB );
376 void DialogsProvider::openNetDialog()
378 openDialog( OPEN_NETWORK_TAB );
380 void DialogsProvider::openCaptureDialog()
382 openDialog( OPEN_CAPTURE_TAB );
385 /* Same as the open one, but force the enqueue */
386 void DialogsProvider::PLAppendDialog( int tab )
388 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
389 OPEN_AND_ENQUEUE )->showTab( tab );
392 void DialogsProvider::MLAppendDialog( int tab )
394 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
395 OPEN_AND_ENQUEUE, false, false )
402 QStringList DialogsProvider::showSimpleOpen( const QString& help,
404 const QString& path )
406 QString fileTypes = "";
407 if( filters & EXT_FILTER_MEDIA ) {
408 ADD_EXT_FILTER( fileTypes, EXTENSIONS_MEDIA );
410 if( filters & EXT_FILTER_VIDEO ) {
411 ADD_EXT_FILTER( fileTypes, EXTENSIONS_VIDEO );
413 if( filters & EXT_FILTER_AUDIO ) {
414 ADD_EXT_FILTER( fileTypes, EXTENSIONS_AUDIO );
416 if( filters & EXT_FILTER_PLAYLIST ) {
417 ADD_EXT_FILTER( fileTypes, EXTENSIONS_PLAYLIST );
419 if( filters & EXT_FILTER_SUBTITLE ) {
420 ADD_EXT_FILTER( fileTypes, EXTENSIONS_SUBTITLE );
422 ADD_EXT_FILTER( fileTypes, EXTENSIONS_ALL );
423 fileTypes.replace( ";*", " *");
425 QStringList files = QFileDialog::getOpenFileNames( NULL,
426 help.isEmpty() ? qtr(I_OP_SEL_FILES ) : help,
427 path.isEmpty() ? p_intf->p_sys->filepath : path,
430 if( !files.isEmpty() ) savedirpathFromFile( files.last() );
437 * pl helps you to choose from playlist or media library,
438 * go to start or enqueue
440 void DialogsProvider::addFromSimple( bool pl, bool go)
442 QStringList files = DialogsProvider::showSimpleOpen();
443 int mode = go ? PLAYLIST_GO : PLAYLIST_PREPARSE;
446 foreach( const QString &file, files )
448 QString url = toURI( toNativeSeparators( file ) );
449 playlist_Add( THEPL, qtu( url ), NULL, PLAYLIST_APPEND | mode,
450 PLAYLIST_END, pl, pl_Unlocked );
451 RecentsMRL::getInstance( p_intf )->addRecent( url );
452 mode = PLAYLIST_PREPARSE;
456 void DialogsProvider::simpleOpenDialog()
458 addFromSimple( true, true ); /* Playlist and Go */
461 void DialogsProvider::simplePLAppendDialog()
463 addFromSimple( true, false );
466 void DialogsProvider::simpleMLAppendDialog()
468 addFromSimple( false, false );
471 /* Url & Clipboard */
474 * If the clipboard contains URLs, the first is automatically 'preselected'.
476 void DialogsProvider::openUrlDialog()
478 OpenUrlDialog *oud = new OpenUrlDialog( p_intf );
479 if( oud->exec() == QDialog::Accepted )
481 QString url = oud->url();
484 char *uri = make_URI( qtu( url ), NULL );
485 if( likely( uri != NULL ) )
487 playlist_Add( THEPL, uri,
488 NULL, !oud->shouldEnqueue() ?
489 ( PLAYLIST_APPEND | PLAYLIST_GO )
490 : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
491 PLAYLIST_END, true, false );
492 RecentsMRL::getInstance( p_intf )->addRecent( url );
503 * pl helps you to choose from playlist or media library,
504 * go to start or enqueue
506 static void openDirectory( intf_thread_t *p_intf, bool pl, bool go )
508 QString dir = QFileDialog::getExistingDirectory( NULL, qtr( I_OP_DIR_WINTITLE ), p_intf->p_sys->filepath );
513 p_intf->p_sys->filepath = dir;
515 const char *scheme = "directory";
516 if( dir.endsWith( "/VIDEO_TS", Qt::CaseInsensitive ) )
518 else if( dir.endsWith( "/BDMV", Qt::CaseInsensitive ) )
521 dir.remove( "BDMV" );
524 char *uri = make_URI( qtu( toNativeSeparators( dir ) ), scheme );
525 if( unlikely(uri == NULL) )
528 RecentsMRL::getInstance( p_intf )->addRecent( qfu(uri) );
530 input_item_t *p_input = input_item_New( uri, NULL );
532 if( unlikely( p_input == NULL ) )
535 /* FIXME: playlist_AddInput() can fail */
536 playlist_AddInput( THEPL, p_input,
537 go ? ( PLAYLIST_APPEND | PLAYLIST_GO ) : PLAYLIST_APPEND,
538 PLAYLIST_END, pl, pl_Unlocked );
539 vlc_gc_decref( p_input );
542 void DialogsProvider::PLOpenDir()
544 openDirectory( p_intf, true, true );
547 void DialogsProvider::PLAppendDir()
549 openDirectory( p_intf, true, false );
552 void DialogsProvider::MLAppendDir()
554 openDirectory( p_intf, false , false );
560 void DialogsProvider::openAPlaylist()
562 QStringList files = showSimpleOpen( qtr( "Open playlist..." ),
563 EXT_FILTER_PLAYLIST );
564 foreach( const QString &file, files )
566 playlist_Import( THEPL, qtu( toNativeSeparators( file ) ) );
570 void DialogsProvider::saveAPlaylist()
574 char filter_name[14];
575 char filter_patterns[5];
578 { N_("XSPF playlist"), "xspf", "export-xspf", },
579 { N_("M3U playlist"), "m3u", "export-m3u", },
580 { N_("M3U8 playlist"), "m3u8", "export-m3u8", },
581 { N_("HTML playlist"), "html", "export-html", },
585 QString ext = getSettings()->value( "last-playlist-ext" ).toString();
587 for( size_t i = 0; i < sizeof (types) / sizeof (types[0]); i++ )
589 QString tmp = qfu( vlc_gettext( types[i].filter_name ) ) + " (*." + types[i].filter_patterns + ")";
590 if( ext == qfu( types[i].filter_patterns ) )
591 filters.insert( 0, tmp );
593 filters.append( tmp );
597 QString file = QFileDialog::getSaveFileName( NULL,
598 qtr( "Save playlist as..." ),
599 p_intf->p_sys->filepath, filters.join( ";;" ),
604 for( size_t i = 0; i < sizeof (types) / sizeof (types[0]); i++)
605 if( selected == qfu( vlc_gettext( types[i].filter_name ) ) + " (*." + qfu( types[i].filter_patterns ) + ")" )
607 playlist_Export( THEPL, qtu( toNativeSeparators( file ) ),
608 THEPL->p_playing, types[i].module );
609 getSettings()->setValue( "last-playlist-ext", types[i].filter_patterns );
614 /****************************************************************************
616 ****************************************************************************/
618 void DialogsProvider::streamingDialog( QWidget *parent,
620 bool b_transcode_only,
621 QStringList options )
626 if( !b_transcode_only )
628 SoutDialog *s = new SoutDialog( parent, p_intf, mrl );
629 s->setAttribute( Qt::WA_QuitOnClose, false ); // See #4883
630 if( s->exec() == QDialog::Accepted )
632 soutoption = s->getMrl();
641 ConvertDialog *s = new ConvertDialog( parent, p_intf, mrl );
642 s->setAttribute( Qt::WA_QuitOnClose, false ); // See #4883
643 if( s->exec() == QDialog::Accepted )
645 soutoption = s->getMrl();
655 if( !soutoption.isEmpty() )
657 options += soutoption.split( " :");
660 input_item_t *p_input;
661 p_input = input_item_New( qtu( mrl ), _("Streaming") );
663 /* Add normal Options */
664 for( int j = 0; j < options.count(); j++ )
666 QString qs = colon_unescape( options[j] );
669 input_item_AddOption( p_input, qtu( qs ),
670 VLC_INPUT_OPTION_TRUSTED );
671 msg_Dbg( p_intf, "Adding option: %s", qtu( qs ) );
675 /* Switch between enqueuing and starting the item */
676 /* FIXME: playlist_AddInput() can fail */
677 playlist_AddInput( THEPL, p_input,
678 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END, true, pl_Unlocked );
679 vlc_gc_decref( p_input );
681 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
685 void DialogsProvider::openAndStreamingDialogs()
687 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false, OPEN_AND_STREAM )
688 ->showTab( OPEN_FILE_TAB );
691 void DialogsProvider::openAndTranscodingDialogs()
693 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, false, OPEN_AND_SAVE )
694 ->showTab( OPEN_FILE_TAB );
697 void DialogsProvider::loadSubtitlesFile()
699 input_thread_t *p_input = THEMIM->getInput();
700 if( !p_input ) return;
702 input_item_t *p_item = input_GetItem( p_input );
703 if( !p_item ) return;
705 char *path = input_item_GetURI( p_item );
709 path2 = make_path( path );
713 char *sep = strrchr( path2, DIR_SEP_CHAR );
714 if( sep ) *sep = '\0';
718 QStringList qsl = showSimpleOpen( qtr( "Open subtitles..." ),
722 foreach( const QString &qsFile, qsl )
724 if( input_AddSubtitle( p_input, qtu( toNativeSeparators( qsFile ) ),
726 msg_Warn( p_intf, "unable to load subtitles from '%s'",
732 /****************************************************************************
734 ****************************************************************************/
736 void DialogsProvider::menuAction( QObject *data )
738 VLCMenuBar::DoAction( data );
741 void DialogsProvider::menuUpdateAction( QObject *data )
743 MenuFunc *func = qobject_cast<MenuFunc *>(data);
745 func->doFunc( p_intf );
748 void DialogsProvider::SDMenuAction( const QString& data )
750 if( !playlist_IsServicesDiscoveryLoaded( THEPL, qtu( data ) ) )
751 playlist_ServicesDiscoveryAdd( THEPL, qtu( data ) );
753 playlist_ServicesDiscoveryRemove( THEPL, qtu( data ) );
757 * Play the MRL contained in the Recently played menu.
759 void DialogsProvider::playMRL( const QString &mrl )
761 char *uri = make_URI( qtu( mrl ), NULL );
762 if( unlikely( uri == NULL ) )
765 playlist_Add( THEPL, uri, NULL,
766 PLAYLIST_APPEND | PLAYLIST_GO , PLAYLIST_END, true, false );
767 RecentsMRL::getInstance( p_intf )->addRecent( mrl );