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() == (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 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 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 ExtendedDialog *extDialog = ExtendedDialog::getInstance(p_intf );
196 if( !extDialog->isVisible() || /* Hidden */
197 extDialog->currentTab() != 0 ) /* wrong tab */
198 extDialog->showTab( 0 );
203 void DialogsProvider::synchroDialog()
205 ExtendedDialog *extDialog = ExtendedDialog::getInstance(p_intf );
207 if( !extDialog->isVisible() || /* Hidden */
208 extDialog->currentTab() != 2 ) /* wrong tab */
209 extDialog->showTab( 2 );
214 void DialogsProvider::messagesDialog()
216 MessagesDialog::getInstance( p_intf )->toggleVisible();
219 void DialogsProvider::gotoTimeDialog()
221 GotoTimeDialog::getInstance( p_intf )->toggleVisible();
225 void DialogsProvider::vlmDialog()
227 VLMDialog::getInstance( p_intf )->toggleVisible();
231 void DialogsProvider::helpDialog()
233 HelpDialog::getInstance( p_intf )->toggleVisible();
237 void DialogsProvider::updateDialog()
239 UpdateDialog::getInstance( p_intf )->toggleVisible();
243 void DialogsProvider::aboutDialog()
245 AboutDialog::getInstance( p_intf )->toggleVisible();
248 void DialogsProvider::mediaInfoDialog()
250 MediaInfoDialog::getInstance( p_intf )->showTab( 0 );
253 void DialogsProvider::mediaCodecDialog()
255 MediaInfoDialog::getInstance( p_intf )->showTab( 2 );
258 void DialogsProvider::bookmarksDialog()
260 BookmarksDialog::getInstance( p_intf )->toggleVisible();
263 void DialogsProvider::podcastConfigureDialog()
265 PodcastConfigDialog::getInstance( p_intf )->toggleVisible();
268 void DialogsProvider::toolbarDialog()
270 ToolbarEditDialog *toolbarEditor = new ToolbarEditDialog( (QWidget *)p_intf->p_sys->p_mi, p_intf );
271 if( toolbarEditor->exec() == QDialog::Accepted )
272 emit toolBarConfUpdated();
275 void DialogsProvider::pluginDialog()
277 PluginDialog::getInstance( p_intf )->toggleVisible();
280 void DialogsProvider::epgDialog()
282 EpgDialog::getInstance( p_intf )->toggleVisible();
285 /* Generic open file */
286 void DialogsProvider::openFileGenericDialog( intf_dialog_args_t *p_arg )
290 msg_Warn( p_intf, "openFileGenericDialog() called with NULL arg" );
294 /* Replace the extensions to a Qt format */
296 QString extensions = qfu( p_arg->psz_extensions );
297 while ( ( i = extensions.indexOf( "|", i ) ) != -1 )
299 if( ( extensions.count( "|" ) % 2 ) == 0 )
300 extensions.replace( i, 1, ");;" );
302 extensions.replace( i, 1, "(" );
304 extensions.replace( ";*", " *" );
305 extensions.append( ")" );
310 QString file = QFileDialog::getSaveFileName( NULL, p_arg->psz_title,
311 p_intf->p_sys->filepath, extensions );
312 if( !file.isEmpty() )
314 p_arg->i_results = 1;
315 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
316 p_arg->psz_results[0] = strdup( qtu( toNativeSepNoSlash( file ) ) );
319 p_arg->i_results = 0;
321 else /* non-save mode */
323 QStringList files = QFileDialog::getOpenFileNames( NULL,
324 p_arg->psz_title, p_intf->p_sys->filepath,
326 p_arg->i_results = files.count();
327 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
329 foreach( const QString &file, files )
330 p_arg->psz_results[i++] = strdup( qtu( toNativeSepNoSlash( file ) ) );
332 p_intf->p_sys->filepath = QString::fromAscii("");
334 p_intf->p_sys->filepath = qfu( p_arg->psz_results[i-1] );
338 if( p_arg->pf_callback )
339 p_arg->pf_callback( p_arg );
341 /* Clean afterwards */
342 if( p_arg->psz_results )
344 for( i = 0; i < p_arg->i_results; i++ )
345 free( p_arg->psz_results[i] );
346 free( p_arg->psz_results );
348 free( p_arg->psz_title );
349 free( p_arg->psz_extensions );
352 /****************************************************************************
353 * All the open/add stuff
354 * Open Dialog first - Simple Open then
355 ****************************************************************************/
357 void DialogsProvider::openDialog( int i_tab )
359 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf )->showTab( i_tab );
361 void DialogsProvider::openDialog()
363 openDialog( OPEN_FILE_TAB );
365 void DialogsProvider::openFileDialog()
367 openDialog( OPEN_FILE_TAB );
369 void DialogsProvider::openDiscDialog()
371 openDialog( OPEN_DISC_TAB );
373 void DialogsProvider::openNetDialog()
375 openDialog( OPEN_NETWORK_TAB );
377 void DialogsProvider::openCaptureDialog()
379 openDialog( OPEN_CAPTURE_TAB );
382 /* Same as the open one, but force the enqueue */
383 void DialogsProvider::PLAppendDialog( int tab )
385 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
386 OPEN_AND_ENQUEUE )->showTab( tab );
389 void DialogsProvider::MLAppendDialog( int tab )
391 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
392 OPEN_AND_ENQUEUE, false, false )
399 QStringList DialogsProvider::showSimpleOpen( const QString& help,
401 const QString& path )
403 QString fileTypes = "";
404 if( filters & EXT_FILTER_MEDIA ) {
405 ADD_EXT_FILTER( fileTypes, EXTENSIONS_MEDIA );
407 if( filters & EXT_FILTER_VIDEO ) {
408 ADD_EXT_FILTER( fileTypes, EXTENSIONS_VIDEO );
410 if( filters & EXT_FILTER_AUDIO ) {
411 ADD_EXT_FILTER( fileTypes, EXTENSIONS_AUDIO );
413 if( filters & EXT_FILTER_PLAYLIST ) {
414 ADD_EXT_FILTER( fileTypes, EXTENSIONS_PLAYLIST );
416 if( filters & EXT_FILTER_SUBTITLE ) {
417 ADD_EXT_FILTER( fileTypes, EXTENSIONS_SUBTITLE );
419 ADD_EXT_FILTER( fileTypes, EXTENSIONS_ALL );
420 fileTypes.replace( ";*", " *");
422 QStringList files = QFileDialog::getOpenFileNames( NULL,
423 help.isEmpty() ? qtr(I_OP_SEL_FILES ) : help,
424 path.isEmpty() ? p_intf->p_sys->filepath : path,
427 if( !files.isEmpty() ) savedirpathFromFile( files.last() );
434 * pl helps you to choose from playlist or media library,
435 * go to start or enqueue
437 void DialogsProvider::addFromSimple( bool pl, bool go)
439 QStringList files = DialogsProvider::showSimpleOpen();
440 int mode = go ? PLAYLIST_GO : PLAYLIST_PREPARSE;
443 foreach( const QString &file, files )
445 QString url = toURI( toNativeSeparators( file ) );
446 playlist_Add( THEPL, qtu( url ), NULL, PLAYLIST_APPEND | mode,
447 PLAYLIST_END, pl, pl_Unlocked );
448 RecentsMRL::getInstance( p_intf )->addRecent( url );
449 mode = PLAYLIST_PREPARSE;
453 void DialogsProvider::simpleOpenDialog()
455 addFromSimple( true, true ); /* Playlist and Go */
458 void DialogsProvider::simplePLAppendDialog()
460 addFromSimple( true, false );
463 void DialogsProvider::simpleMLAppendDialog()
465 addFromSimple( false, false );
468 /* Url & Clipboard */
471 * If the clipboard contains URLs, the first is automatically 'preselected'.
473 void DialogsProvider::openUrlDialog()
475 OpenUrlDialog *oud = new OpenUrlDialog( p_intf );
476 if( oud->exec() == QDialog::Accepted )
478 QString url = oud->url();
481 char *uri = make_URI( qtu( url ), NULL );
482 if( likely( uri != NULL ) )
484 playlist_Add( THEPL, uri,
485 NULL, !oud->shouldEnqueue() ?
486 ( PLAYLIST_APPEND | PLAYLIST_GO )
487 : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
488 PLAYLIST_END, true, false );
489 RecentsMRL::getInstance( p_intf )->addRecent( url );
500 * pl helps you to choose from playlist or media library,
501 * go to start or enqueue
503 static void openDirectory( intf_thread_t *p_intf, bool pl, bool go )
505 QString dir = QFileDialog::getExistingDirectory( NULL, qtr( I_OP_DIR_WINTITLE ), p_intf->p_sys->filepath );
510 p_intf->p_sys->filepath = dir;
512 const char *scheme = "directory";
513 if( dir.endsWith( "/VIDEO_TS", Qt::CaseInsensitive ) )
515 else if( dir.endsWith( "/BDMV", Qt::CaseInsensitive ) )
518 dir.remove( "BDMV" );
521 char *uri = make_URI( qtu( toNativeSeparators( dir ) ), scheme );
522 if( unlikely(uri == NULL) )
525 RecentsMRL::getInstance( p_intf )->addRecent( qfu(uri) );
527 input_item_t *p_input = input_item_New( uri, NULL );
529 if( unlikely( p_input == NULL ) )
532 /* FIXME: playlist_AddInput() can fail */
533 playlist_AddInput( THEPL, p_input,
534 go ? ( PLAYLIST_APPEND | PLAYLIST_GO ) : PLAYLIST_APPEND,
535 PLAYLIST_END, pl, pl_Unlocked );
536 vlc_gc_decref( p_input );
539 void DialogsProvider::PLOpenDir()
541 openDirectory( p_intf, true, true );
544 void DialogsProvider::PLAppendDir()
546 openDirectory( p_intf, true, false );
549 void DialogsProvider::MLAppendDir()
551 openDirectory( p_intf, false , false );
557 void DialogsProvider::openAPlaylist()
559 QStringList files = showSimpleOpen( qtr( "Open playlist..." ),
560 EXT_FILTER_PLAYLIST );
561 foreach( const QString &file, files )
563 playlist_Import( THEPL, qtu( toNativeSeparators( file ) ) );
567 void DialogsProvider::saveAPlaylist()
571 char filter_name[14];
572 char filter_patterns[5];
575 { N_("XSPF playlist"), "xspf", "export-xspf", },
576 { N_("M3U playlist"), "m3u", "export-m3u", },
577 { N_("M3U8 playlist"), "m3u8", "export-m3u8", },
578 { N_("HTML playlist"), "html", "export-html", },
582 QString ext = getSettings()->value( "last-playlist-ext" ).toString();
584 for( size_t i = 0; i < sizeof (types) / sizeof (types[0]); i++ )
586 QString tmp = qfu( vlc_gettext( types[i].filter_name ) ) + " (*." + types[i].filter_patterns + ")";
587 if( ext == qfu( types[i].filter_patterns ) )
588 filters.insert( 0, tmp );
590 filters.append( tmp );
594 QString file = QFileDialog::getSaveFileName( NULL,
595 qtr( "Save playlist as..." ),
596 p_intf->p_sys->filepath, filters.join( ";;" ),
601 for( size_t i = 0; i < sizeof (types) / sizeof (types[0]); i++)
602 if( selected == qfu( vlc_gettext( types[i].filter_name ) ) + " (*." + qfu( types[i].filter_patterns ) + ")" )
604 playlist_Export( THEPL, qtu( toNativeSeparators( file ) ),
605 THEPL->p_playing, types[i].module );
606 getSettings()->setValue( "last-playlist-ext", types[i].filter_patterns );
611 /****************************************************************************
613 ****************************************************************************/
615 void DialogsProvider::streamingDialog( QWidget *parent,
617 bool b_transcode_only,
618 QStringList options )
623 if( !b_transcode_only )
625 SoutDialog *s = new SoutDialog( parent, p_intf, mrl );
626 s->setAttribute( Qt::WA_QuitOnClose, false ); // See #4883
627 if( s->exec() == QDialog::Accepted )
629 soutoption = s->getMrl();
638 ConvertDialog *s = new ConvertDialog( parent, p_intf, mrl );
639 s->setAttribute( Qt::WA_QuitOnClose, false ); // See #4883
640 if( s->exec() == QDialog::Accepted )
642 soutoption = s->getMrl();
652 if( !soutoption.isEmpty() )
654 options += soutoption.split( " :");
657 input_item_t *p_input;
658 p_input = input_item_New( qtu( mrl ), _("Streaming") );
660 /* Add normal Options */
661 for( int j = 0; j < options.count(); j++ )
663 QString qs = colon_unescape( options[j] );
666 input_item_AddOption( p_input, qtu( qs ),
667 VLC_INPUT_OPTION_TRUSTED );
668 msg_Dbg( p_intf, "Adding option: %s", qtu( qs ) );
672 /* Switch between enqueuing and starting the item */
673 /* FIXME: playlist_AddInput() can fail */
674 playlist_AddInput( THEPL, p_input,
675 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END, true, pl_Unlocked );
676 vlc_gc_decref( p_input );
678 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
682 void DialogsProvider::openAndStreamingDialogs()
684 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false, OPEN_AND_STREAM )
685 ->showTab( OPEN_FILE_TAB );
688 void DialogsProvider::openAndTranscodingDialogs()
690 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, false, OPEN_AND_SAVE )
691 ->showTab( OPEN_FILE_TAB );
694 void DialogsProvider::loadSubtitlesFile()
696 input_thread_t *p_input = THEMIM->getInput();
697 if( !p_input ) return;
699 input_item_t *p_item = input_GetItem( p_input );
700 if( !p_item ) return;
702 char *path = input_item_GetURI( p_item );
706 path2 = make_path( path );
710 char *sep = strrchr( path2, DIR_SEP_CHAR );
711 if( sep ) *sep = '\0';
715 QStringList qsl = showSimpleOpen( qtr( "Open subtitles..." ),
719 foreach( const QString &qsFile, qsl )
721 if( input_AddSubtitle( p_input, qtu( toNativeSeparators( qsFile ) ),
723 msg_Warn( p_intf, "unable to load subtitles from '%s'",
729 /****************************************************************************
731 ****************************************************************************/
733 void DialogsProvider::menuAction( QObject *data )
735 VLCMenuBar::DoAction( data );
738 void DialogsProvider::menuUpdateAction( QObject *data )
740 MenuFunc *func = qobject_cast<MenuFunc *>(data);
742 func->doFunc( p_intf );
745 void DialogsProvider::SDMenuAction( const QString& data )
747 if( !playlist_IsServicesDiscoveryLoaded( THEPL, qtu( data ) ) )
748 playlist_ServicesDiscoveryAdd( THEPL, qtu( data ) );
750 playlist_ServicesDiscoveryRemove( THEPL, qtu( data ) );
754 * Play the MRL contained in the Recently played menu.
756 void DialogsProvider::playMRL( const QString &mrl )
758 char *uri = make_URI( qtu( mrl ), NULL );
759 if( unlikely( uri == NULL ) )
762 playlist_Add( THEPL, uri, NULL,
763 PLAYLIST_APPEND | PLAYLIST_GO , PLAYLIST_END, true, false );
764 RecentsMRL::getInstance( p_intf )->addRecent( mrl );