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/epg.hpp"
57 #include "dialogs/errors.hpp"
60 #include <QApplication>
61 #include <QSignalMapper>
62 #include <QFileDialog>
64 #define I_OP_DIR_WINTITLE I_DIR_OR_FOLDER( N_("Open Directory"), \
67 DialogsProvider* DialogsProvider::instance = NULL;
69 DialogsProvider::DialogsProvider( intf_thread_t *_p_intf ) :
70 QObject( NULL ), p_intf( _p_intf )
74 /* Various signal mappers for the menus */
75 menusMapper = new QSignalMapper();
76 CONNECT( menusMapper, mapped(QObject *), this, menuAction( QObject *) );
78 menusUpdateMapper = new QSignalMapper();
79 CONNECT( menusUpdateMapper, mapped(QObject *),
80 this, menuUpdateAction( QObject *) );
82 SDMapper = new QSignalMapper();
83 CONNECT( SDMapper, mapped (QString), this, SDMenuAction( QString ) );
85 new DialogHandler (p_intf, this );
88 DialogsProvider::~DialogsProvider()
90 PlaylistDialog::killInstance();
91 MediaInfoDialog::killInstance();
92 MessagesDialog::killInstance();
93 ExtendedDialog::killInstance();
94 BookmarksDialog::killInstance();
95 HelpDialog::killInstance();
97 UpdateDialog::killInstance();
99 PluginDialog::killInstance();
102 delete menusUpdateMapper;
106 void DialogsProvider::quit()
109 libvlc_Quit( p_intf->p_libvlc );
112 void DialogsProvider::customEvent( QEvent *event )
114 if( event->type() == (int)DialogEvent_Type )
116 DialogEvent *de = static_cast<DialogEvent*>(event);
117 switch( de->i_dialog )
119 case INTF_DIALOG_FILE_SIMPLE:
120 case INTF_DIALOG_FILE:
122 case INTF_DIALOG_FILE_GENERIC:
123 openFileGenericDialog( de->p_arg ); break;
124 case INTF_DIALOG_DISC:
125 openDiscDialog(); break;
126 case INTF_DIALOG_NET:
127 openNetDialog(); break;
128 case INTF_DIALOG_SAT:
129 case INTF_DIALOG_CAPTURE:
130 openCaptureDialog(); break;
131 case INTF_DIALOG_DIRECTORY:
132 PLAppendDir(); break;
133 case INTF_DIALOG_PLAYLIST:
134 playlistDialog(); break;
135 case INTF_DIALOG_MESSAGES:
136 messagesDialog(); break;
137 case INTF_DIALOG_FILEINFO:
138 mediaInfoDialog(); break;
139 case INTF_DIALOG_PREFS:
140 prefsDialog(); break;
141 case INTF_DIALOG_BOOKMARKS:
142 bookmarksDialog(); break;
143 case INTF_DIALOG_EXTENDED:
144 extendedDialog(); break;
146 case INTF_DIALOG_VLM:
149 case INTF_DIALOG_POPUPMENU:
150 QVLCMenu::PopupMenu( p_intf, (de->i_arg != 0) ); break;
151 case INTF_DIALOG_AUDIOPOPUPMENU:
152 QVLCMenu::AudioPopupMenu( p_intf ); break;
153 case INTF_DIALOG_VIDEOPOPUPMENU:
154 QVLCMenu::VideoPopupMenu( p_intf ); break;
155 case INTF_DIALOG_MISCPOPUPMENU:
156 QVLCMenu::MiscPopupMenu( p_intf ); break;
157 case INTF_DIALOG_WIZARD:
158 case INTF_DIALOG_STREAMWIZARD:
159 openAndStreamingDialogs(); break;
161 case INTF_DIALOG_UPDATEVLC:
162 updateDialog(); break;
164 case INTF_DIALOG_EXIT:
167 msg_Warn( p_intf, "unimplemented dialog" );
172 /****************************************************************************
173 * Individual simple dialogs
174 ****************************************************************************/
175 void DialogsProvider::playlistDialog()
177 PlaylistDialog::getInstance( p_intf )->toggleVisible();
180 void DialogsProvider::prefsDialog()
182 PrefsDialog *p = new PrefsDialog( (QWidget *)p_intf->p_sys->p_mi, p_intf );
186 void DialogsProvider::extendedDialog()
188 if( !ExtendedDialog::getInstance( p_intf )->isVisible() || /* Hidden */
189 ExtendedDialog::getInstance( p_intf )->currentTab() != 0 ) /* wrong tab */
190 ExtendedDialog::getInstance( p_intf )->showTab( 0 );
192 ExtendedDialog::getInstance( p_intf )->hide();
195 void DialogsProvider::synchroDialog()
197 if( !ExtendedDialog::getInstance( p_intf )->isVisible() || /* Hidden */
198 ExtendedDialog::getInstance( p_intf )->currentTab() != 2 ) /* wrong tab */
199 ExtendedDialog::getInstance( p_intf )->showTab( 2 );
201 ExtendedDialog::getInstance( p_intf )->hide();
204 void DialogsProvider::messagesDialog()
206 MessagesDialog::getInstance( p_intf )->toggleVisible();
209 void DialogsProvider::gotoTimeDialog()
211 GotoTimeDialog::getInstance( p_intf )->toggleVisible();
215 void DialogsProvider::vlmDialog()
217 VLMDialog::getInstance( p_intf )->toggleVisible();
221 void DialogsProvider::helpDialog()
223 HelpDialog::getInstance( p_intf )->toggleVisible();
227 void DialogsProvider::updateDialog()
229 UpdateDialog::getInstance( p_intf )->toggleVisible();
233 void DialogsProvider::aboutDialog()
235 AboutDialog::getInstance( p_intf )->toggleVisible();
238 void DialogsProvider::mediaInfoDialog()
240 MediaInfoDialog::getInstance( p_intf )->showTab( 0 );
243 void DialogsProvider::mediaCodecDialog()
245 MediaInfoDialog::getInstance( p_intf )->showTab( 2 );
248 void DialogsProvider::bookmarksDialog()
250 BookmarksDialog::getInstance( p_intf )->toggleVisible();
253 void DialogsProvider::podcastConfigureDialog()
255 PodcastConfigDialog::getInstance( p_intf )->toggleVisible();
258 void DialogsProvider::toolbarDialog()
260 ToolbarEditDialog *toolbarEditor = new ToolbarEditDialog( (QWidget *)p_intf->p_sys->p_mi, p_intf );
261 if( toolbarEditor->exec() == QDialog::Accepted )
262 emit toolBarConfUpdated();
265 void DialogsProvider::pluginDialog()
267 PluginDialog::getInstance( p_intf )->toggleVisible();
270 void DialogsProvider::epgDialog()
272 EpgDialog::getInstance( p_intf )->toggleVisible();
275 /* Generic open file */
276 void DialogsProvider::openFileGenericDialog( intf_dialog_args_t *p_arg )
280 msg_Warn( p_intf, "openFileGenericDialog() called with NULL arg" );
284 /* Replace the extensions to a Qt format */
286 QString extensions = qfu( p_arg->psz_extensions );
287 while ( ( i = extensions.indexOf( "|", i ) ) != -1 )
289 if( ( extensions.count( "|" ) % 2 ) == 0 )
290 extensions.replace( i, 1, ");;" );
292 extensions.replace( i, 1, "(" );
294 extensions.replace( ";*", " *" );
295 extensions.append( ")" );
300 QString file = QFileDialog::getSaveFileName( NULL, p_arg->psz_title,
301 p_intf->p_sys->filepath, extensions );
302 if( !file.isEmpty() )
304 p_arg->i_results = 1;
305 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
306 p_arg->psz_results[0] = strdup( qtu( toNativeSepNoSlash( file ) ) );
309 p_arg->i_results = 0;
311 else /* non-save mode */
313 QStringList files = QFileDialog::getOpenFileNames( NULL,
314 p_arg->psz_title, p_intf->p_sys->filepath,
316 p_arg->i_results = files.count();
317 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
319 foreach( const QString &file, files )
320 p_arg->psz_results[i++] = strdup( qtu( toNativeSepNoSlash( file ) ) );
322 p_intf->p_sys->filepath = QString::fromAscii("");
324 p_intf->p_sys->filepath = qfu( p_arg->psz_results[i-1] );
328 if( p_arg->pf_callback )
329 p_arg->pf_callback( p_arg );
331 /* Clean afterwards */
332 if( p_arg->psz_results )
334 for( i = 0; i < p_arg->i_results; i++ )
335 free( p_arg->psz_results[i] );
336 free( p_arg->psz_results );
338 free( p_arg->psz_title );
339 free( p_arg->psz_extensions );
342 /****************************************************************************
343 * All the open/add stuff
344 * Open Dialog first - Simple Open then
345 ****************************************************************************/
347 void DialogsProvider::openDialog( int i_tab )
349 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf )->showTab( i_tab );
351 void DialogsProvider::openDialog()
353 openDialog( OPEN_FILE_TAB );
355 void DialogsProvider::openFileDialog()
357 openDialog( OPEN_FILE_TAB );
359 void DialogsProvider::openDiscDialog()
361 openDialog( OPEN_DISC_TAB );
363 void DialogsProvider::openNetDialog()
365 openDialog( OPEN_NETWORK_TAB );
367 void DialogsProvider::openCaptureDialog()
369 openDialog( OPEN_CAPTURE_TAB );
372 /* Same as the open one, but force the enqueue */
373 void DialogsProvider::PLAppendDialog( int tab )
375 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
376 OPEN_AND_ENQUEUE )->showTab( tab );
379 void DialogsProvider::MLAppendDialog( int tab )
381 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
382 OPEN_AND_ENQUEUE, false, false )
389 QStringList DialogsProvider::showSimpleOpen( const QString& help,
391 const QString& path )
393 QString fileTypes = "";
394 if( filters & EXT_FILTER_MEDIA ) {
395 ADD_FILTER_MEDIA( fileTypes );
397 if( filters & EXT_FILTER_VIDEO ) {
398 ADD_FILTER_VIDEO( fileTypes );
400 if( filters & EXT_FILTER_AUDIO ) {
401 ADD_FILTER_AUDIO( fileTypes );
403 if( filters & EXT_FILTER_PLAYLIST ) {
404 ADD_FILTER_PLAYLIST( fileTypes );
406 if( filters & EXT_FILTER_SUBTITLE ) {
407 ADD_FILTER_SUBTITLE( fileTypes );
409 ADD_FILTER_ALL( fileTypes );
410 fileTypes.replace( ";*", " *");
412 QStringList files = QFileDialog::getOpenFileNames( NULL,
413 help.isEmpty() ? qtr(I_OP_SEL_FILES ) : help,
414 path.isEmpty() ? p_intf->p_sys->filepath : path,
417 if( !files.isEmpty() ) savedirpathFromFile( files.last() );
424 * pl helps you to choose from playlist or media library,
425 * go to start or enqueue
427 void DialogsProvider::addFromSimple( bool pl, bool go)
429 QStringList files = DialogsProvider::showSimpleOpen();
432 foreach( const QString &file, files )
434 char* psz_uri = make_URI( qtu( toNativeSeparators(file) ) );
435 playlist_Add( THEPL, psz_uri, NULL,
436 go ? ( PLAYLIST_APPEND | ( i ? PLAYLIST_PREPARSE : PLAYLIST_GO ) )
437 : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
438 PLAYLIST_END, pl, pl_Unlocked );
440 RecentsMRL::getInstance( p_intf )->addRecent(
441 toNativeSeparators( file ) );
446 void DialogsProvider::simpleOpenDialog()
448 addFromSimple( true, true ); /* Playlist and Go */
451 void DialogsProvider::simplePLAppendDialog()
453 addFromSimple( true, false );
456 void DialogsProvider::simpleMLAppendDialog()
458 addFromSimple( false, false );
461 /* Url & Clipboard */
464 * If the clipboard contains URLs, the first is automatically 'preselected'.
466 void DialogsProvider::openUrlDialog()
468 OpenUrlDialog *oud = new OpenUrlDialog( p_intf );
469 if( oud->exec() == QDialog::Accepted )
471 QString url = oud->url();
474 playlist_Add( THEPL, qtu( url ),
475 NULL, !oud->shouldEnqueue() ?
476 ( PLAYLIST_APPEND | PLAYLIST_GO )
477 : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
478 PLAYLIST_END, true, false );
479 RecentsMRL::getInstance( p_intf )->addRecent( url );
488 * pl helps you to choose from playlist or media library,
489 * go to start or enqueue
491 static void openDirectory( intf_thread_t *p_intf, bool pl, bool go )
493 QString dir = QFileDialog::getExistingDirectory( NULL, qtr( I_OP_DIR_WINTITLE ), p_intf->p_sys->filepath );
497 QString mrl = (dir.endsWith( "VIDEO_TS", Qt::CaseInsensitive ) ?
498 "dvd://" : "directory://")
499 + toNativeSeparators( dir );
500 input_item_t *p_input = input_item_New( THEPL, qtu( mrl ), NULL );
502 /* FIXME: playlist_AddInput() can fail */
503 playlist_AddInput( THEPL, p_input,
504 go ? ( PLAYLIST_APPEND | PLAYLIST_GO ) : PLAYLIST_APPEND,
505 PLAYLIST_END, pl, pl_Unlocked );
506 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
508 input_Read( THEPL, p_input );
509 vlc_gc_decref( p_input );
513 void DialogsProvider::PLOpenDir()
515 openDirectory( p_intf, true, true );
518 void DialogsProvider::PLAppendDir()
520 openDirectory( p_intf, true, false );
523 void DialogsProvider::MLAppendDir()
525 openDirectory( p_intf, false , false );
531 void DialogsProvider::openAPlaylist()
533 QStringList files = showSimpleOpen( qtr( "Open playlist..." ),
534 EXT_FILTER_PLAYLIST );
535 foreach( const QString &file, files )
537 playlist_Import( THEPL, qtu( toNativeSeparators( file ) ) );
541 void DialogsProvider::saveAPlaylist()
548 { N_("XSPF playlist (*.xspf)"), "export-xspf", },
549 { N_("M3U8 playlist (*.m3u)"), "export-m3u8", },
550 { N_("M3U playlist (*.m3u)"), "export-m3u", },
551 { N_("HTML playlist (*.html)"), "export-html", },
553 QString filters, selected;
555 for( size_t i = 0; i < sizeof (types) / sizeof (types[0]); i++ )
557 if( !filters.isEmpty() )
559 filters += qfu( vlc_gettext( types[i].filter ) );
562 QString file = QFileDialog::getSaveFileName( NULL,
563 qtr( "Save playlist as..." ),
564 p_intf->p_sys->filepath, filters, &selected );
568 for( size_t i = 0; i < sizeof (types) / sizeof (types[0]); i++)
569 if( selected == qfu( vlc_gettext( types[i].filter ) ) )
571 playlist_Export( THEPL, qtu( toNativeSeparators( file ) ),
572 THEPL->p_playing, types[i].module );
577 /****************************************************************************
579 ****************************************************************************/
581 void DialogsProvider::streamingDialog( QWidget *parent,
583 bool b_transcode_only,
584 QStringList options )
589 if( !b_transcode_only )
591 SoutDialog *s = new SoutDialog( parent, p_intf, mrl );
592 if( s->exec() == QDialog::Accepted )
594 soutoption = s->getMrl();
603 ConvertDialog *s = new ConvertDialog( parent, p_intf, mrl );
604 if( s->exec() == QDialog::Accepted )
606 soutoption = s->getMrl();
616 if( !soutoption.isEmpty() )
618 options += soutoption.split( " :");
621 input_item_t *p_input;
622 p_input = input_item_New( p_intf, qtu( mrl ), _("Streaming") );
624 /* Add normal Options */
625 for( int j = 0; j < options.size(); j++ )
627 QString qs = colon_unescape( options[j] );
630 input_item_AddOption( p_input, qtu( qs ),
631 VLC_INPUT_OPTION_TRUSTED );
632 msg_Dbg( p_intf, "Adding option: %s", qtu( qs ) );
636 /* Switch between enqueuing and starting the item */
637 /* FIXME: playlist_AddInput() can fail */
638 playlist_AddInput( THEPL, p_input,
639 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END, true, pl_Unlocked );
640 vlc_gc_decref( p_input );
642 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
646 void DialogsProvider::openAndStreamingDialogs()
648 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false, OPEN_AND_STREAM )
649 ->showTab( OPEN_FILE_TAB );
652 void DialogsProvider::openAndTranscodingDialogs()
654 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, false, OPEN_AND_SAVE )
655 ->showTab( OPEN_FILE_TAB );
658 void DialogsProvider::loadSubtitlesFile()
660 input_thread_t *p_input = THEMIM->getInput();
661 if( !p_input ) return;
663 input_item_t *p_item = input_GetItem( p_input );
664 if( !p_item ) return;
666 char *path = input_item_GetURI( p_item );
667 if( !path ) path = strdup( "" );
669 char *sep = strrchr( path, DIR_SEP_CHAR );
670 if( sep ) *sep = '\0';
672 QStringList qsl = showSimpleOpen( qtr( "Open subtitles..." ),
676 foreach( const QString &qsFile, qsl )
678 if( input_AddSubtitle( p_input, qtu( toNativeSeparators( qsFile ) ),
680 msg_Warn( p_intf, "unable to load subtitles from '%s'",
686 /****************************************************************************
688 ****************************************************************************/
690 void DialogsProvider::menuAction( QObject *data )
692 QVLCMenu::DoAction( data );
695 void DialogsProvider::menuUpdateAction( QObject *data )
697 MenuFunc *func = qobject_cast<MenuFunc *>(data);
699 func->doFunc( p_intf );
702 void DialogsProvider::SDMenuAction( const QString& data )
704 char *psz_sd = strdup( qtu( data ) );
705 if( !playlist_IsServicesDiscoveryLoaded( THEPL, psz_sd ) )
706 playlist_ServicesDiscoveryAdd( THEPL, psz_sd );
708 playlist_ServicesDiscoveryRemove( THEPL, psz_sd );
713 * Play the MRL contained in the Recently played menu.
715 void DialogsProvider::playMRL( const QString &mrl )
717 char* psz_uri = make_URI( qtu(mrl) );
718 playlist_Add( THEPL, psz_uri, NULL,
719 PLAYLIST_APPEND | PLAYLIST_GO , PLAYLIST_END, true, false );
722 RecentsMRL::getInstance( p_intf )->addRecent( mrl );