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"
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 #include "dialogs/external.hpp"
56 #include "dialogs/errors.hpp"
59 #include <QApplication>
60 #include <QSignalMapper>
61 #include <QFileDialog>
64 DialogsProvider* DialogsProvider::instance = NULL;
66 DialogsProvider::DialogsProvider( intf_thread_t *_p_intf ) :
67 QObject( NULL ), p_intf( _p_intf )
71 /* Various signal mappers for the menus */
72 menusMapper = new QSignalMapper();
73 CONNECT( menusMapper, mapped(QObject *), this, menuAction( QObject *) );
75 menusUpdateMapper = new QSignalMapper();
76 CONNECT( menusUpdateMapper, mapped(QObject *),
77 this, menuUpdateAction( QObject *) );
79 SDMapper = new QSignalMapper();
80 CONNECT( SDMapper, mapped (QString), this, SDMenuAction( QString ) );
82 new DialogHandler (p_intf, this );
85 DialogsProvider::~DialogsProvider()
87 PlaylistDialog::killInstance();
88 MediaInfoDialog::killInstance();
89 MessagesDialog::killInstance();
90 ExtendedDialog::killInstance();
91 BookmarksDialog::killInstance();
92 HelpDialog::killInstance();
94 UpdateDialog::killInstance();
98 delete menusUpdateMapper;
102 void DialogsProvider::quit()
105 libvlc_Quit( p_intf->p_libvlc );
108 void DialogsProvider::customEvent( QEvent *event )
110 if( event->type() == (int)DialogEvent_Type )
112 DialogEvent *de = static_cast<DialogEvent*>(event);
113 switch( de->i_dialog )
115 case INTF_DIALOG_FILE_SIMPLE:
116 case INTF_DIALOG_FILE:
118 case INTF_DIALOG_FILE_GENERIC:
119 openFileGenericDialog( de->p_arg ); break;
120 case INTF_DIALOG_DISC:
121 openDiscDialog(); break;
122 case INTF_DIALOG_NET:
123 openNetDialog(); break;
124 case INTF_DIALOG_SAT:
125 case INTF_DIALOG_CAPTURE:
126 openCaptureDialog(); break;
127 case INTF_DIALOG_DIRECTORY:
128 PLAppendDir(); break;
129 case INTF_DIALOG_PLAYLIST:
130 playlistDialog(); break;
131 case INTF_DIALOG_MESSAGES:
132 messagesDialog(); break;
133 case INTF_DIALOG_FILEINFO:
134 mediaInfoDialog(); break;
135 case INTF_DIALOG_PREFS:
136 prefsDialog(); break;
137 case INTF_DIALOG_BOOKMARKS:
138 bookmarksDialog(); break;
139 case INTF_DIALOG_EXTENDED:
140 extendedDialog(); break;
142 case INTF_DIALOG_VLM:
145 case INTF_DIALOG_POPUPMENU:
146 QVLCMenu::PopupMenu( p_intf, (de->i_arg != 0) ); break;
147 case INTF_DIALOG_AUDIOPOPUPMENU:
148 QVLCMenu::AudioPopupMenu( p_intf ); break;
149 case INTF_DIALOG_VIDEOPOPUPMENU:
150 QVLCMenu::VideoPopupMenu( p_intf ); break;
151 case INTF_DIALOG_MISCPOPUPMENU:
152 QVLCMenu::MiscPopupMenu( p_intf ); break;
153 case INTF_DIALOG_WIZARD:
154 case INTF_DIALOG_STREAMWIZARD:
155 openAndStreamingDialogs(); break;
157 case INTF_DIALOG_UPDATEVLC:
158 updateDialog(); break;
160 case INTF_DIALOG_EXIT:
163 msg_Warn( p_intf, "unimplemented dialog" );
168 /****************************************************************************
169 * Individual simple dialogs
170 ****************************************************************************/
171 void DialogsProvider::playlistDialog()
173 PlaylistDialog::getInstance( p_intf )->toggleVisible();
176 void DialogsProvider::prefsDialog()
178 PrefsDialog *p = new PrefsDialog( (QWidget *)p_intf->p_sys->p_mi, p_intf );
182 void DialogsProvider::extendedDialog()
184 if( !ExtendedDialog::getInstance( p_intf )->isVisible() || /* Hidden */
185 ExtendedDialog::getInstance( p_intf )->currentTab() != 0 ) /* wrong tab */
186 ExtendedDialog::getInstance( p_intf )->showTab( 0 );
188 ExtendedDialog::getInstance( p_intf )->hide();
191 void DialogsProvider::synchroDialog()
193 if( !ExtendedDialog::getInstance( p_intf )->isVisible() || /* Hidden */
194 ExtendedDialog::getInstance( p_intf )->currentTab() != 2 ) /* wrong tab */
195 ExtendedDialog::getInstance( p_intf )->showTab( 2 );
197 ExtendedDialog::getInstance( p_intf )->hide();
200 void DialogsProvider::messagesDialog()
202 MessagesDialog::getInstance( p_intf )->toggleVisible();
205 void DialogsProvider::gotoTimeDialog()
207 GotoTimeDialog::getInstance( p_intf )->toggleVisible();
211 void DialogsProvider::vlmDialog()
213 VLMDialog::getInstance( p_intf )->toggleVisible();
217 void DialogsProvider::helpDialog()
219 HelpDialog::getInstance( p_intf )->toggleVisible();
223 void DialogsProvider::updateDialog()
225 UpdateDialog::getInstance( p_intf )->toggleVisible();
229 void DialogsProvider::aboutDialog()
231 AboutDialog::getInstance( p_intf )->toggleVisible();
234 void DialogsProvider::mediaInfoDialog()
236 MediaInfoDialog::getInstance( p_intf )->showTab( 0 );
239 void DialogsProvider::mediaCodecDialog()
241 MediaInfoDialog::getInstance( p_intf )->showTab( 2 );
244 void DialogsProvider::bookmarksDialog()
246 BookmarksDialog::getInstance( p_intf )->toggleVisible();
249 void DialogsProvider::podcastConfigureDialog()
251 PodcastConfigDialog::getInstance( p_intf )->toggleVisible();
254 void DialogsProvider::toolbarDialog()
256 ToolbarEditDialog *toolbarEditor = new ToolbarEditDialog( (QWidget *)p_intf->p_sys->p_mi, p_intf );
257 if( toolbarEditor->exec() == QDialog::Accepted )
258 emit toolBarConfUpdated();
261 void DialogsProvider::pluginDialog()
263 PluginDialog::getInstance( p_intf )->toggleVisible();
266 /* Generic open file */
267 void DialogsProvider::openFileGenericDialog( intf_dialog_args_t *p_arg )
271 msg_Warn( p_intf, "openFileGenericDialog() called with NULL arg" );
275 /* Replace the extensions to a Qt format */
277 QString extensions = qfu( p_arg->psz_extensions );
278 while ( ( i = extensions.indexOf( "|", i ) ) != -1 )
280 if( ( extensions.count( "|" ) % 2 ) == 0 )
281 extensions.replace( i, 1, ");;" );
283 extensions.replace( i, 1, "(" );
285 extensions.replace( ";*", " *" );
286 extensions.append( ")" );
291 QString file = QFileDialog::getSaveFileName( NULL, p_arg->psz_title,
292 p_intf->p_sys->filepath, extensions );
293 if( !file.isEmpty() )
295 p_arg->i_results = 1;
296 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
297 p_arg->psz_results[0] = strdup( qtu( toNativeSepNoSlash( file ) ) );
300 p_arg->i_results = 0;
302 else /* non-save mode */
304 QStringList files = QFileDialog::getOpenFileNames( NULL,
305 p_arg->psz_title, p_intf->p_sys->filepath,
307 p_arg->i_results = files.count();
308 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
310 foreach( const QString &file, files )
311 p_arg->psz_results[i++] = strdup( qtu( toNativeSepNoSlash( file ) ) );
312 p_intf->p_sys->filepath = qfu( p_arg->psz_results[i] );
316 if( p_arg->pf_callback )
317 p_arg->pf_callback( p_arg );
319 /* Clean afterwards */
320 if( p_arg->psz_results )
322 for( i = 0; i < p_arg->i_results; i++ )
323 free( p_arg->psz_results[i] );
324 free( p_arg->psz_results );
326 free( p_arg->psz_title );
327 free( p_arg->psz_extensions );
330 /****************************************************************************
331 * All the open/add stuff
332 * Open Dialog first - Simple Open then
333 ****************************************************************************/
335 void DialogsProvider::openDialog( int i_tab )
337 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf )->showTab( i_tab );
339 void DialogsProvider::openDialog()
341 openDialog( OPEN_FILE_TAB );
343 void DialogsProvider::openFileDialog()
345 openDialog( OPEN_FILE_TAB );
347 void DialogsProvider::openDiscDialog()
349 openDialog( OPEN_DISC_TAB );
351 void DialogsProvider::openNetDialog()
353 openDialog( OPEN_NETWORK_TAB );
355 void DialogsProvider::openCaptureDialog()
357 openDialog( OPEN_CAPTURE_TAB );
360 /* Same as the open one, but force the enqueue */
361 void DialogsProvider::PLAppendDialog( int tab )
363 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
364 OPEN_AND_ENQUEUE )->showTab( tab );
367 void DialogsProvider::MLAppendDialog( int tab )
369 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
370 OPEN_AND_ENQUEUE, false, false )
377 QStringList DialogsProvider::showSimpleOpen( const QString& help,
379 const QString& path )
381 QString fileTypes = "";
382 if( filters & EXT_FILTER_MEDIA ) {
383 ADD_FILTER_MEDIA( fileTypes );
385 if( filters & EXT_FILTER_VIDEO ) {
386 ADD_FILTER_VIDEO( fileTypes );
388 if( filters & EXT_FILTER_AUDIO ) {
389 ADD_FILTER_AUDIO( fileTypes );
391 if( filters & EXT_FILTER_PLAYLIST ) {
392 ADD_FILTER_PLAYLIST( fileTypes );
394 if( filters & EXT_FILTER_SUBTITLE ) {
395 ADD_FILTER_SUBTITLE( fileTypes );
397 ADD_FILTER_ALL( fileTypes );
398 fileTypes.replace( ";*", " *");
400 QStringList files = QFileDialog::getOpenFileNames( NULL,
401 help.isEmpty() ? qtr(I_OP_SEL_FILES ) : help,
402 path.isEmpty() ? p_intf->p_sys->filepath : path,
405 if( !files.isEmpty() ) savedirpathFromFile( files.last() );
412 * pl helps you to choose from playlist or media library,
413 * go to start or enqueue
415 void DialogsProvider::addFromSimple( bool pl, bool go)
417 QStringList files = DialogsProvider::showSimpleOpen();
420 foreach( const QString &file, files )
422 char* psz_uri = make_URI( qtu( toNativeSeparators(file) ) );
423 playlist_Add( THEPL, psz_uri, NULL,
424 go ? ( PLAYLIST_APPEND | ( i ? PLAYLIST_PREPARSE : PLAYLIST_GO ) )
425 : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
426 PLAYLIST_END, pl, pl_Unlocked );
428 RecentsMRL::getInstance( p_intf )->addRecent(
429 toNativeSeparators( file ) );
434 void DialogsProvider::simpleOpenDialog()
436 addFromSimple( true, true ); /* Playlist and Go */
439 void DialogsProvider::simplePLAppendDialog()
441 addFromSimple( true, false );
444 void DialogsProvider::simpleMLAppendDialog()
446 addFromSimple( false, false );
449 /* Url & Clipboard */
452 * If the clipboard contains URLs, the first is automatically 'preselected'.
454 void DialogsProvider::openUrlDialog()
456 OpenUrlDialog *oud = OpenUrlDialog::getInstance( p_intf );
457 if( oud->exec() == QDialog::Accepted )
459 QString url = oud->url();
462 playlist_Add( THEPL, qtu( url ),
463 NULL, !oud->shouldEnqueue() ?
464 ( PLAYLIST_APPEND | PLAYLIST_GO )
465 : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
466 PLAYLIST_END, true, false );
467 RecentsMRL::getInstance( p_intf )->addRecent( url );
475 * pl helps you to choose from playlist or media library,
476 * go to start or enqueue
478 static void openDirectory( intf_thread_t *p_intf, bool pl, bool go )
480 QString dir = QFileDialog::getExistingDirectory( NULL, qtr("Open Directory"), p_intf->p_sys->filepath );
484 QString mrl = (dir.endsWith( "VIDEO_TS", Qt::CaseInsensitive ) ?
485 "dvd://" : "directory://")
486 + toNativeSeparators( dir );
487 input_item_t *p_input = input_item_New( THEPL, qtu( mrl ), NULL );
489 /* FIXME: playlist_AddInput() can fail */
490 playlist_AddInput( THEPL, p_input,
491 go ? ( PLAYLIST_APPEND | PLAYLIST_GO ) : PLAYLIST_APPEND,
492 PLAYLIST_END, pl, pl_Unlocked );
493 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
495 input_Read( THEPL, p_input );
496 vlc_gc_decref( p_input );
500 void DialogsProvider::PLOpenDir()
502 openDirectory( p_intf, true, true );
505 void DialogsProvider::PLAppendDir()
507 openDirectory( p_intf, true, false );
510 void DialogsProvider::MLAppendDir()
512 openDirectory( p_intf, false , false );
518 void DialogsProvider::openAPlaylist()
520 QStringList files = showSimpleOpen( qtr( "Open playlist..." ),
521 EXT_FILTER_PLAYLIST );
522 foreach( const QString &file, files )
524 playlist_Import( THEPL, qtu( toNativeSeparators( file ) ) );
528 void DialogsProvider::saveAPlaylist()
535 { N_("XSPF playlist (*.xspf)"), "export-xspf", },
536 { N_("M3U8 playlist (*.m3u)"), "export-m3u8", },
537 { N_("M3U playlist (*.m3u)"), "export-m3u", },
538 { N_("HTML playlist (*.html)"), "export-html", },
540 QString filters, selected;
542 for( size_t i = 0; i < sizeof (types) / sizeof (types[0]); i++ )
544 if( !filters.isEmpty() )
546 filters += qfu( vlc_gettext( types[i].filter ) );
549 QString file = QFileDialog::getSaveFileName( NULL,
550 qtr( "Save playlist as..." ),
551 p_intf->p_sys->filepath, filters, &selected );
555 for( size_t i = 0; i < sizeof (types) / sizeof (types[0]); i++)
556 if( selected == qfu( vlc_gettext( types[i].filter ) ) )
558 playlist_Export( THEPL, qtu( toNativeSeparators( file ) ),
559 THEPL->p_local_category, types[i].module );
564 /****************************************************************************
566 ****************************************************************************/
568 void DialogsProvider::streamingDialog( QWidget *parent,
570 bool b_transcode_only,
571 QStringList options )
576 if( !b_transcode_only )
578 SoutDialog *s = new SoutDialog( parent, p_intf, mrl );
579 if( s->exec() == QDialog::Accepted )
581 soutoption = s->getMrl();
590 ConvertDialog *s = new ConvertDialog( parent, p_intf, mrl );
591 if( s->exec() == QDialog::Accepted )
593 soutoption = s->getMrl();
603 if( !soutoption.isEmpty() )
605 options += soutoption.split( " :");
608 input_item_t *p_input;
609 p_input = input_item_New( p_intf, qtu( mrl ), _("Streaming") );
611 /* Add normal Options */
612 for( int j = 0; j < options.size(); j++ )
614 QString qs = colon_unescape( options[j] );
617 input_item_AddOption( p_input, qtu( qs ),
618 VLC_INPUT_OPTION_TRUSTED );
619 msg_Dbg( p_intf, "Adding option: %s", qtu( qs ) );
623 /* Switch between enqueuing and starting the item */
624 /* FIXME: playlist_AddInput() can fail */
625 playlist_AddInput( THEPL, p_input,
626 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END, true, pl_Unlocked );
627 vlc_gc_decref( p_input );
629 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
633 void DialogsProvider::openAndStreamingDialogs()
635 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false, OPEN_AND_STREAM )
636 ->showTab( OPEN_FILE_TAB );
639 void DialogsProvider::openAndTranscodingDialogs()
641 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, false, OPEN_AND_SAVE )
642 ->showTab( OPEN_FILE_TAB );
645 void DialogsProvider::loadSubtitlesFile()
647 input_thread_t *p_input = THEMIM->getInput();
648 if( !p_input ) return;
650 input_item_t *p_item = input_GetItem( p_input );
651 if( !p_item ) return;
653 char *path = input_item_GetURI( p_item );
654 if( !path ) path = strdup( "" );
656 char *sep = strrchr( path, DIR_SEP_CHAR );
657 if( sep ) *sep = '\0';
659 QStringList qsl = showSimpleOpen( qtr( "Open subtitles..." ),
663 foreach( const QString &qsFile, qsl )
665 if( input_AddSubtitle( p_input, qtu( toNativeSeparators( qsFile ) ),
667 msg_Warn( p_intf, "unable to load subtitles from '%s'",
673 /****************************************************************************
675 ****************************************************************************/
677 void DialogsProvider::menuAction( QObject *data )
679 QVLCMenu::DoAction( data );
682 void DialogsProvider::menuUpdateAction( QObject *data )
684 MenuFunc *func = qobject_cast<MenuFunc *>(data);
686 func->doFunc( p_intf );
689 void DialogsProvider::SDMenuAction( const QString& data )
691 char *psz_sd = strdup( qtu( data ) );
692 if( !playlist_IsServicesDiscoveryLoaded( THEPL, psz_sd ) )
693 playlist_ServicesDiscoveryAdd( THEPL, psz_sd );
695 playlist_ServicesDiscoveryRemove( THEPL, psz_sd );
700 * Play the MRL contained in the Recently played menu.
702 void DialogsProvider::playMRL( const QString &mrl )
704 char* psz_uri = make_URI( qtu(mrl) );
705 playlist_Add( THEPL, psz_uri, NULL,
706 PLAYLIST_APPEND | PLAYLIST_GO , PLAYLIST_END, true, false );
709 RecentsMRL::getInstance( p_intf )->addRecent( mrl );