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"
57 #include "dialogs/epg.hpp"
60 #include <QApplication>
61 #include <QSignalMapper>
62 #include <QFileDialog>
65 DialogsProvider* DialogsProvider::instance = NULL;
67 DialogsProvider::DialogsProvider( intf_thread_t *_p_intf ) :
68 QObject( NULL ), p_intf( _p_intf )
72 /* Various signal mappers for the menus */
73 menusMapper = new QSignalMapper();
74 CONNECT( menusMapper, mapped(QObject *), this, menuAction( QObject *) );
76 menusUpdateMapper = new QSignalMapper();
77 CONNECT( menusUpdateMapper, mapped(QObject *),
78 this, menuUpdateAction( QObject *) );
80 SDMapper = new QSignalMapper();
81 CONNECT( SDMapper, mapped (QString), this, SDMenuAction( QString ) );
83 new DialogHandler (p_intf, this );
86 DialogsProvider::~DialogsProvider()
88 PlaylistDialog::killInstance();
89 MediaInfoDialog::killInstance();
90 MessagesDialog::killInstance();
91 ExtendedDialog::killInstance();
92 BookmarksDialog::killInstance();
93 HelpDialog::killInstance();
95 UpdateDialog::killInstance();
97 PluginDialog::killInstance();
100 delete menusUpdateMapper;
104 void DialogsProvider::quit()
107 libvlc_Quit( p_intf->p_libvlc );
110 void DialogsProvider::customEvent( QEvent *event )
112 if( event->type() == (int)DialogEvent_Type )
114 DialogEvent *de = static_cast<DialogEvent*>(event);
115 switch( de->i_dialog )
117 case INTF_DIALOG_FILE_SIMPLE:
118 case INTF_DIALOG_FILE:
120 case INTF_DIALOG_FILE_GENERIC:
121 openFileGenericDialog( de->p_arg ); break;
122 case INTF_DIALOG_DISC:
123 openDiscDialog(); break;
124 case INTF_DIALOG_NET:
125 openNetDialog(); break;
126 case INTF_DIALOG_SAT:
127 case INTF_DIALOG_CAPTURE:
128 openCaptureDialog(); break;
129 case INTF_DIALOG_DIRECTORY:
130 PLAppendDir(); break;
131 case INTF_DIALOG_PLAYLIST:
132 playlistDialog(); break;
133 case INTF_DIALOG_MESSAGES:
134 messagesDialog(); break;
135 case INTF_DIALOG_FILEINFO:
136 mediaInfoDialog(); break;
137 case INTF_DIALOG_PREFS:
138 prefsDialog(); break;
139 case INTF_DIALOG_BOOKMARKS:
140 bookmarksDialog(); break;
141 case INTF_DIALOG_EXTENDED:
142 extendedDialog(); break;
144 case INTF_DIALOG_VLM:
147 case INTF_DIALOG_POPUPMENU:
148 QVLCMenu::PopupMenu( p_intf, (de->i_arg != 0) ); break;
149 case INTF_DIALOG_AUDIOPOPUPMENU:
150 QVLCMenu::AudioPopupMenu( p_intf ); break;
151 case INTF_DIALOG_VIDEOPOPUPMENU:
152 QVLCMenu::VideoPopupMenu( p_intf ); break;
153 case INTF_DIALOG_MISCPOPUPMENU:
154 QVLCMenu::MiscPopupMenu( p_intf ); break;
155 case INTF_DIALOG_WIZARD:
156 case INTF_DIALOG_STREAMWIZARD:
157 openAndStreamingDialogs(); break;
159 case INTF_DIALOG_UPDATEVLC:
160 updateDialog(); break;
162 case INTF_DIALOG_EXIT:
165 msg_Warn( p_intf, "unimplemented dialog" );
170 /****************************************************************************
171 * Individual simple dialogs
172 ****************************************************************************/
173 void DialogsProvider::playlistDialog()
175 PlaylistDialog::getInstance( p_intf )->toggleVisible();
178 void DialogsProvider::prefsDialog()
180 PrefsDialog *p = new PrefsDialog( (QWidget *)p_intf->p_sys->p_mi, p_intf );
184 void DialogsProvider::extendedDialog()
186 if( !ExtendedDialog::getInstance( p_intf )->isVisible() || /* Hidden */
187 ExtendedDialog::getInstance( p_intf )->currentTab() != 0 ) /* wrong tab */
188 ExtendedDialog::getInstance( p_intf )->showTab( 0 );
190 ExtendedDialog::getInstance( p_intf )->hide();
193 void DialogsProvider::synchroDialog()
195 if( !ExtendedDialog::getInstance( p_intf )->isVisible() || /* Hidden */
196 ExtendedDialog::getInstance( p_intf )->currentTab() != 2 ) /* wrong tab */
197 ExtendedDialog::getInstance( p_intf )->showTab( 2 );
199 ExtendedDialog::getInstance( p_intf )->hide();
202 void DialogsProvider::messagesDialog()
204 MessagesDialog::getInstance( p_intf )->toggleVisible();
207 void DialogsProvider::gotoTimeDialog()
209 GotoTimeDialog::getInstance( p_intf )->toggleVisible();
213 void DialogsProvider::vlmDialog()
215 VLMDialog::getInstance( p_intf )->toggleVisible();
219 void DialogsProvider::helpDialog()
221 HelpDialog::getInstance( p_intf )->toggleVisible();
225 void DialogsProvider::updateDialog()
227 UpdateDialog::getInstance( p_intf )->toggleVisible();
231 void DialogsProvider::aboutDialog()
233 AboutDialog::getInstance( p_intf )->toggleVisible();
236 void DialogsProvider::mediaInfoDialog()
238 MediaInfoDialog::getInstance( p_intf )->showTab( 0 );
241 void DialogsProvider::mediaCodecDialog()
243 MediaInfoDialog::getInstance( p_intf )->showTab( 2 );
246 void DialogsProvider::bookmarksDialog()
248 BookmarksDialog::getInstance( p_intf )->toggleVisible();
251 void DialogsProvider::podcastConfigureDialog()
253 PodcastConfigDialog::getInstance( p_intf )->toggleVisible();
256 void DialogsProvider::toolbarDialog()
258 ToolbarEditDialog *toolbarEditor = new ToolbarEditDialog( (QWidget *)p_intf->p_sys->p_mi, p_intf );
259 if( toolbarEditor->exec() == QDialog::Accepted )
260 emit toolBarConfUpdated();
263 void DialogsProvider::pluginDialog()
265 PluginDialog::getInstance( p_intf )->toggleVisible();
268 void DialogsProvider::epgDialog()
270 EpgDialog::getInstance( p_intf )->toggleVisible();
273 /* Generic open file */
274 void DialogsProvider::openFileGenericDialog( intf_dialog_args_t *p_arg )
278 msg_Warn( p_intf, "openFileGenericDialog() called with NULL arg" );
282 /* Replace the extensions to a Qt format */
284 QString extensions = qfu( p_arg->psz_extensions );
285 while ( ( i = extensions.indexOf( "|", i ) ) != -1 )
287 if( ( extensions.count( "|" ) % 2 ) == 0 )
288 extensions.replace( i, 1, ");;" );
290 extensions.replace( i, 1, "(" );
292 extensions.replace( ";*", " *" );
293 extensions.append( ")" );
298 QString file = QFileDialog::getSaveFileName( NULL, p_arg->psz_title,
299 p_intf->p_sys->filepath, extensions );
300 if( !file.isEmpty() )
302 p_arg->i_results = 1;
303 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
304 p_arg->psz_results[0] = strdup( qtu( toNativeSepNoSlash( file ) ) );
307 p_arg->i_results = 0;
309 else /* non-save mode */
311 QStringList files = QFileDialog::getOpenFileNames( NULL,
312 p_arg->psz_title, p_intf->p_sys->filepath,
314 p_arg->i_results = files.count();
315 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
317 foreach( const QString &file, files )
318 p_arg->psz_results[i++] = strdup( qtu( toNativeSepNoSlash( file ) ) );
320 p_intf->p_sys->filepath = QString::fromAscii("");
322 p_intf->p_sys->filepath = qfu( p_arg->psz_results[i-1] );
326 if( p_arg->pf_callback )
327 p_arg->pf_callback( p_arg );
329 /* Clean afterwards */
330 if( p_arg->psz_results )
332 for( i = 0; i < p_arg->i_results; i++ )
333 free( p_arg->psz_results[i] );
334 free( p_arg->psz_results );
336 free( p_arg->psz_title );
337 free( p_arg->psz_extensions );
340 /****************************************************************************
341 * All the open/add stuff
342 * Open Dialog first - Simple Open then
343 ****************************************************************************/
345 void DialogsProvider::openDialog( int i_tab )
347 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf )->showTab( i_tab );
349 void DialogsProvider::openDialog()
351 openDialog( OPEN_FILE_TAB );
353 void DialogsProvider::openFileDialog()
355 openDialog( OPEN_FILE_TAB );
357 void DialogsProvider::openDiscDialog()
359 openDialog( OPEN_DISC_TAB );
361 void DialogsProvider::openNetDialog()
363 openDialog( OPEN_NETWORK_TAB );
365 void DialogsProvider::openCaptureDialog()
367 openDialog( OPEN_CAPTURE_TAB );
370 /* Same as the open one, but force the enqueue */
371 void DialogsProvider::PLAppendDialog( int tab )
373 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
374 OPEN_AND_ENQUEUE )->showTab( tab );
377 void DialogsProvider::MLAppendDialog( int tab )
379 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
380 OPEN_AND_ENQUEUE, false, false )
387 QStringList DialogsProvider::showSimpleOpen( const QString& help,
389 const QString& path )
391 QString fileTypes = "";
392 if( filters & EXT_FILTER_MEDIA ) {
393 ADD_FILTER_MEDIA( fileTypes );
395 if( filters & EXT_FILTER_VIDEO ) {
396 ADD_FILTER_VIDEO( fileTypes );
398 if( filters & EXT_FILTER_AUDIO ) {
399 ADD_FILTER_AUDIO( fileTypes );
401 if( filters & EXT_FILTER_PLAYLIST ) {
402 ADD_FILTER_PLAYLIST( fileTypes );
404 if( filters & EXT_FILTER_SUBTITLE ) {
405 ADD_FILTER_SUBTITLE( fileTypes );
407 ADD_FILTER_ALL( fileTypes );
408 fileTypes.replace( ";*", " *");
410 QStringList files = QFileDialog::getOpenFileNames( NULL,
411 help.isEmpty() ? qtr(I_OP_SEL_FILES ) : help,
412 path.isEmpty() ? p_intf->p_sys->filepath : path,
415 if( !files.isEmpty() ) savedirpathFromFile( files.last() );
422 * pl helps you to choose from playlist or media library,
423 * go to start or enqueue
425 void DialogsProvider::addFromSimple( bool pl, bool go)
427 QStringList files = DialogsProvider::showSimpleOpen();
430 foreach( const QString &file, files )
432 char* psz_uri = make_URI( qtu( toNativeSeparators(file) ) );
433 playlist_Add( THEPL, psz_uri, NULL,
434 go ? ( PLAYLIST_APPEND | ( i ? PLAYLIST_PREPARSE : PLAYLIST_GO ) )
435 : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
436 PLAYLIST_END, pl, pl_Unlocked );
438 RecentsMRL::getInstance( p_intf )->addRecent(
439 toNativeSeparators( file ) );
444 void DialogsProvider::simpleOpenDialog()
446 addFromSimple( true, true ); /* Playlist and Go */
449 void DialogsProvider::simplePLAppendDialog()
451 addFromSimple( true, false );
454 void DialogsProvider::simpleMLAppendDialog()
456 addFromSimple( false, false );
459 /* Url & Clipboard */
462 * If the clipboard contains URLs, the first is automatically 'preselected'.
464 void DialogsProvider::openUrlDialog()
466 OpenUrlDialog *oud = OpenUrlDialog::getInstance( p_intf );
467 if( oud->exec() == QDialog::Accepted )
469 QString url = oud->url();
472 playlist_Add( THEPL, qtu( url ),
473 NULL, !oud->shouldEnqueue() ?
474 ( PLAYLIST_APPEND | PLAYLIST_GO )
475 : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
476 PLAYLIST_END, true, false );
477 RecentsMRL::getInstance( p_intf )->addRecent( url );
485 * pl helps you to choose from playlist or media library,
486 * go to start or enqueue
488 static void openDirectory( intf_thread_t *p_intf, bool pl, bool go )
490 QString dir = QFileDialog::getExistingDirectory( NULL, qtr("Open Directory"), p_intf->p_sys->filepath );
494 QString mrl = (dir.endsWith( "VIDEO_TS", Qt::CaseInsensitive ) ?
495 "dvd://" : "directory://")
496 + toNativeSeparators( dir );
497 input_item_t *p_input = input_item_New( THEPL, qtu( mrl ), NULL );
499 /* FIXME: playlist_AddInput() can fail */
500 playlist_AddInput( THEPL, p_input,
501 go ? ( PLAYLIST_APPEND | PLAYLIST_GO ) : PLAYLIST_APPEND,
502 PLAYLIST_END, pl, pl_Unlocked );
503 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
505 input_Read( THEPL, p_input );
506 vlc_gc_decref( p_input );
510 void DialogsProvider::PLOpenDir()
512 openDirectory( p_intf, true, true );
515 void DialogsProvider::PLAppendDir()
517 openDirectory( p_intf, true, false );
520 void DialogsProvider::MLAppendDir()
522 openDirectory( p_intf, false , false );
528 void DialogsProvider::openAPlaylist()
530 QStringList files = showSimpleOpen( qtr( "Open playlist..." ),
531 EXT_FILTER_PLAYLIST );
532 foreach( const QString &file, files )
534 playlist_Import( THEPL, qtu( toNativeSeparators( file ) ) );
538 void DialogsProvider::saveAPlaylist()
545 { N_("XSPF playlist (*.xspf)"), "export-xspf", },
546 { N_("M3U8 playlist (*.m3u)"), "export-m3u8", },
547 { N_("M3U playlist (*.m3u)"), "export-m3u", },
548 { N_("HTML playlist (*.html)"), "export-html", },
550 QString filters, selected;
552 for( size_t i = 0; i < sizeof (types) / sizeof (types[0]); i++ )
554 if( !filters.isEmpty() )
556 filters += qfu( vlc_gettext( types[i].filter ) );
559 QString file = QFileDialog::getSaveFileName( NULL,
560 qtr( "Save playlist as..." ),
561 p_intf->p_sys->filepath, filters, &selected );
565 for( size_t i = 0; i < sizeof (types) / sizeof (types[0]); i++)
566 if( selected == qfu( vlc_gettext( types[i].filter ) ) )
568 playlist_Export( THEPL, qtu( toNativeSeparators( file ) ),
569 THEPL->p_playing, types[i].module );
574 /****************************************************************************
576 ****************************************************************************/
578 void DialogsProvider::streamingDialog( QWidget *parent,
580 bool b_transcode_only,
581 QStringList options )
586 if( !b_transcode_only )
588 SoutDialog *s = new SoutDialog( parent, p_intf, mrl );
589 if( s->exec() == QDialog::Accepted )
591 soutoption = s->getMrl();
600 ConvertDialog *s = new ConvertDialog( parent, p_intf, mrl );
601 if( s->exec() == QDialog::Accepted )
603 soutoption = s->getMrl();
613 if( !soutoption.isEmpty() )
615 options += soutoption.split( " :");
618 input_item_t *p_input;
619 p_input = input_item_New( p_intf, qtu( mrl ), _("Streaming") );
621 /* Add normal Options */
622 for( int j = 0; j < options.size(); j++ )
624 QString qs = colon_unescape( options[j] );
627 input_item_AddOption( p_input, qtu( qs ),
628 VLC_INPUT_OPTION_TRUSTED );
629 msg_Dbg( p_intf, "Adding option: %s", qtu( qs ) );
633 /* Switch between enqueuing and starting the item */
634 /* FIXME: playlist_AddInput() can fail */
635 playlist_AddInput( THEPL, p_input,
636 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END, true, pl_Unlocked );
637 vlc_gc_decref( p_input );
639 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
643 void DialogsProvider::openAndStreamingDialogs()
645 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false, OPEN_AND_STREAM )
646 ->showTab( OPEN_FILE_TAB );
649 void DialogsProvider::openAndTranscodingDialogs()
651 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, false, OPEN_AND_SAVE )
652 ->showTab( OPEN_FILE_TAB );
655 void DialogsProvider::loadSubtitlesFile()
657 input_thread_t *p_input = THEMIM->getInput();
658 if( !p_input ) return;
660 input_item_t *p_item = input_GetItem( p_input );
661 if( !p_item ) return;
663 char *path = input_item_GetURI( p_item );
664 if( !path ) path = strdup( "" );
666 char *sep = strrchr( path, DIR_SEP_CHAR );
667 if( sep ) *sep = '\0';
669 QStringList qsl = showSimpleOpen( qtr( "Open subtitles..." ),
673 foreach( const QString &qsFile, qsl )
675 if( input_AddSubtitle( p_input, qtu( toNativeSeparators( qsFile ) ),
677 msg_Warn( p_intf, "unable to load subtitles from '%s'",
683 /****************************************************************************
685 ****************************************************************************/
687 void DialogsProvider::menuAction( QObject *data )
689 QVLCMenu::DoAction( data );
692 void DialogsProvider::menuUpdateAction( QObject *data )
694 MenuFunc *func = qobject_cast<MenuFunc *>(data);
696 func->doFunc( p_intf );
699 void DialogsProvider::SDMenuAction( const QString& data )
701 char *psz_sd = strdup( qtu( data ) );
702 if( !playlist_IsServicesDiscoveryLoaded( THEPL, psz_sd ) )
703 playlist_ServicesDiscoveryAdd( THEPL, psz_sd );
705 playlist_ServicesDiscoveryRemove( THEPL, psz_sd );
710 * Play the MRL contained in the Recently played menu.
712 void DialogsProvider::playMRL( const QString &mrl )
714 char* psz_uri = make_URI( qtu(mrl) );
715 playlist_Add( THEPL, psz_uri, NULL,
716 PLAYLIST_APPEND | PLAYLIST_GO , PLAYLIST_END, true, false );
719 RecentsMRL::getInstance( p_intf )->addRecent( mrl );