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;
105 QVLCMenu::PopupMenu( p_intf, false );
106 QVLCMenu::AudioPopupMenu( p_intf, false );
107 QVLCMenu::VideoPopupMenu( p_intf, false );
108 QVLCMenu::MiscPopupMenu( p_intf, false );
111 void DialogsProvider::quit()
114 libvlc_Quit( p_intf->p_libvlc );
117 void DialogsProvider::customEvent( QEvent *event )
119 if( event->type() == (int)DialogEvent_Type )
121 DialogEvent *de = static_cast<DialogEvent*>(event);
122 switch( de->i_dialog )
124 case INTF_DIALOG_FILE_SIMPLE:
125 case INTF_DIALOG_FILE:
127 case INTF_DIALOG_FILE_GENERIC:
128 openFileGenericDialog( de->p_arg ); break;
129 case INTF_DIALOG_DISC:
130 openDiscDialog(); break;
131 case INTF_DIALOG_NET:
132 openNetDialog(); break;
133 case INTF_DIALOG_SAT:
134 case INTF_DIALOG_CAPTURE:
135 openCaptureDialog(); break;
136 case INTF_DIALOG_DIRECTORY:
137 PLAppendDir(); break;
138 case INTF_DIALOG_PLAYLIST:
139 playlistDialog(); break;
140 case INTF_DIALOG_MESSAGES:
141 messagesDialog(); break;
142 case INTF_DIALOG_FILEINFO:
143 mediaInfoDialog(); break;
144 case INTF_DIALOG_PREFS:
145 prefsDialog(); break;
146 case INTF_DIALOG_BOOKMARKS:
147 bookmarksDialog(); break;
148 case INTF_DIALOG_EXTENDED:
149 extendedDialog(); break;
151 case INTF_DIALOG_VLM:
154 case INTF_DIALOG_POPUPMENU:
155 QVLCMenu::PopupMenu( p_intf, (de->i_arg != 0) ); break;
156 case INTF_DIALOG_AUDIOPOPUPMENU:
157 QVLCMenu::AudioPopupMenu( p_intf, (de->i_arg != 0) ); break;
158 case INTF_DIALOG_VIDEOPOPUPMENU:
159 QVLCMenu::VideoPopupMenu( p_intf, (de->i_arg != 0) ); break;
160 case INTF_DIALOG_MISCPOPUPMENU:
161 QVLCMenu::MiscPopupMenu( p_intf, (de->i_arg != 0) ); break;
162 case INTF_DIALOG_WIZARD:
163 case INTF_DIALOG_STREAMWIZARD:
164 openAndStreamingDialogs(); break;
166 case INTF_DIALOG_UPDATEVLC:
167 updateDialog(); break;
169 case INTF_DIALOG_EXIT:
172 msg_Warn( p_intf, "unimplemented dialog" );
177 /****************************************************************************
178 * Individual simple dialogs
179 ****************************************************************************/
180 void DialogsProvider::playlistDialog()
182 PlaylistDialog::getInstance( p_intf )->toggleVisible();
185 void DialogsProvider::prefsDialog()
187 PrefsDialog *p = new PrefsDialog( (QWidget *)p_intf->p_sys->p_mi, p_intf );
191 void DialogsProvider::extendedDialog()
193 if( !ExtendedDialog::getInstance( p_intf )->isVisible() || /* Hidden */
194 ExtendedDialog::getInstance( p_intf )->currentTab() != 0 ) /* wrong tab */
195 ExtendedDialog::getInstance( p_intf )->showTab( 0 );
197 ExtendedDialog::getInstance( p_intf )->hide();
200 void DialogsProvider::synchroDialog()
202 if( !ExtendedDialog::getInstance( p_intf )->isVisible() || /* Hidden */
203 ExtendedDialog::getInstance( p_intf )->currentTab() != 2 ) /* wrong tab */
204 ExtendedDialog::getInstance( p_intf )->showTab( 2 );
206 ExtendedDialog::getInstance( p_intf )->hide();
209 void DialogsProvider::messagesDialog()
211 MessagesDialog::getInstance( p_intf )->toggleVisible();
214 void DialogsProvider::gotoTimeDialog()
216 GotoTimeDialog::getInstance( p_intf )->toggleVisible();
220 void DialogsProvider::vlmDialog()
222 VLMDialog::getInstance( p_intf )->toggleVisible();
226 void DialogsProvider::helpDialog()
228 HelpDialog::getInstance( p_intf )->toggleVisible();
232 void DialogsProvider::updateDialog()
234 UpdateDialog::getInstance( p_intf )->toggleVisible();
238 void DialogsProvider::aboutDialog()
240 AboutDialog::getInstance( p_intf )->toggleVisible();
243 void DialogsProvider::mediaInfoDialog()
245 MediaInfoDialog::getInstance( p_intf )->showTab( 0 );
248 void DialogsProvider::mediaCodecDialog()
250 MediaInfoDialog::getInstance( p_intf )->showTab( 2 );
253 void DialogsProvider::bookmarksDialog()
255 BookmarksDialog::getInstance( p_intf )->toggleVisible();
258 void DialogsProvider::podcastConfigureDialog()
260 PodcastConfigDialog::getInstance( p_intf )->toggleVisible();
263 void DialogsProvider::toolbarDialog()
265 ToolbarEditDialog *toolbarEditor = new ToolbarEditDialog( (QWidget *)p_intf->p_sys->p_mi, p_intf );
266 if( toolbarEditor->exec() == QDialog::Accepted )
267 emit toolBarConfUpdated();
270 void DialogsProvider::pluginDialog()
272 PluginDialog::getInstance( p_intf )->toggleVisible();
275 void DialogsProvider::epgDialog()
277 EpgDialog::getInstance( p_intf )->toggleVisible();
280 /* Generic open file */
281 void DialogsProvider::openFileGenericDialog( intf_dialog_args_t *p_arg )
285 msg_Warn( p_intf, "openFileGenericDialog() called with NULL arg" );
289 /* Replace the extensions to a Qt format */
291 QString extensions = qfu( p_arg->psz_extensions );
292 while ( ( i = extensions.indexOf( "|", i ) ) != -1 )
294 if( ( extensions.count( "|" ) % 2 ) == 0 )
295 extensions.replace( i, 1, ");;" );
297 extensions.replace( i, 1, "(" );
299 extensions.replace( ";*", " *" );
300 extensions.append( ")" );
305 QString file = QFileDialog::getSaveFileName( NULL, p_arg->psz_title,
306 p_intf->p_sys->filepath, extensions );
307 if( !file.isEmpty() )
309 p_arg->i_results = 1;
310 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
311 p_arg->psz_results[0] = strdup( qtu( toNativeSepNoSlash( file ) ) );
314 p_arg->i_results = 0;
316 else /* non-save mode */
318 QStringList files = QFileDialog::getOpenFileNames( NULL,
319 p_arg->psz_title, p_intf->p_sys->filepath,
321 p_arg->i_results = files.count();
322 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
324 foreach( const QString &file, files )
325 p_arg->psz_results[i++] = strdup( qtu( toNativeSepNoSlash( file ) ) );
327 p_intf->p_sys->filepath = QString::fromAscii("");
329 p_intf->p_sys->filepath = qfu( p_arg->psz_results[i-1] );
333 if( p_arg->pf_callback )
334 p_arg->pf_callback( p_arg );
336 /* Clean afterwards */
337 if( p_arg->psz_results )
339 for( i = 0; i < p_arg->i_results; i++ )
340 free( p_arg->psz_results[i] );
341 free( p_arg->psz_results );
343 free( p_arg->psz_title );
344 free( p_arg->psz_extensions );
347 /****************************************************************************
348 * All the open/add stuff
349 * Open Dialog first - Simple Open then
350 ****************************************************************************/
352 void DialogsProvider::openDialog( int i_tab )
354 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf )->showTab( i_tab );
356 void DialogsProvider::openDialog()
358 openDialog( OPEN_FILE_TAB );
360 void DialogsProvider::openFileDialog()
362 openDialog( OPEN_FILE_TAB );
364 void DialogsProvider::openDiscDialog()
366 openDialog( OPEN_DISC_TAB );
368 void DialogsProvider::openNetDialog()
370 openDialog( OPEN_NETWORK_TAB );
372 void DialogsProvider::openCaptureDialog()
374 openDialog( OPEN_CAPTURE_TAB );
377 /* Same as the open one, but force the enqueue */
378 void DialogsProvider::PLAppendDialog( int tab )
380 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
381 OPEN_AND_ENQUEUE )->showTab( tab );
384 void DialogsProvider::MLAppendDialog( int tab )
386 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
387 OPEN_AND_ENQUEUE, false, false )
394 QStringList DialogsProvider::showSimpleOpen( const QString& help,
396 const QString& path )
398 QString fileTypes = "";
399 if( filters & EXT_FILTER_MEDIA ) {
400 ADD_FILTER_MEDIA( fileTypes );
402 if( filters & EXT_FILTER_VIDEO ) {
403 ADD_FILTER_VIDEO( fileTypes );
405 if( filters & EXT_FILTER_AUDIO ) {
406 ADD_FILTER_AUDIO( fileTypes );
408 if( filters & EXT_FILTER_PLAYLIST ) {
409 ADD_FILTER_PLAYLIST( fileTypes );
411 if( filters & EXT_FILTER_SUBTITLE ) {
412 ADD_FILTER_SUBTITLE( fileTypes );
414 ADD_FILTER_ALL( fileTypes );
415 fileTypes.replace( ";*", " *");
417 QStringList files = QFileDialog::getOpenFileNames( NULL,
418 help.isEmpty() ? qtr(I_OP_SEL_FILES ) : help,
419 path.isEmpty() ? p_intf->p_sys->filepath : path,
422 if( !files.isEmpty() ) savedirpathFromFile( files.last() );
429 * pl helps you to choose from playlist or media library,
430 * go to start or enqueue
432 void DialogsProvider::addFromSimple( bool pl, bool go)
434 QStringList files = DialogsProvider::showSimpleOpen();
437 foreach( const QString &file, files )
439 char* psz_uri = make_URI( qtu( toNativeSeparators(file) ) );
440 playlist_Add( THEPL, psz_uri, NULL,
441 go ? ( PLAYLIST_APPEND | ( i ? PLAYLIST_PREPARSE : PLAYLIST_GO ) )
442 : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
443 PLAYLIST_END, pl, pl_Unlocked );
445 RecentsMRL::getInstance( p_intf )->addRecent(
446 toNativeSeparators( file ) );
451 void DialogsProvider::simpleOpenDialog()
453 addFromSimple( true, true ); /* Playlist and Go */
456 void DialogsProvider::simplePLAppendDialog()
458 addFromSimple( true, false );
461 void DialogsProvider::simpleMLAppendDialog()
463 addFromSimple( false, false );
466 /* Url & Clipboard */
469 * If the clipboard contains URLs, the first is automatically 'preselected'.
471 void DialogsProvider::openUrlDialog()
473 OpenUrlDialog *oud = new OpenUrlDialog( p_intf );
474 if( oud->exec() == QDialog::Accepted )
476 QString url = oud->url();
479 playlist_Add( THEPL, qtu( url ),
480 NULL, !oud->shouldEnqueue() ?
481 ( PLAYLIST_APPEND | PLAYLIST_GO )
482 : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
483 PLAYLIST_END, true, false );
484 RecentsMRL::getInstance( p_intf )->addRecent( url );
493 * pl helps you to choose from playlist or media library,
494 * go to start or enqueue
496 static void openDirectory( intf_thread_t *p_intf, bool pl, bool go )
498 QString dir = QFileDialog::getExistingDirectory( NULL, qtr( I_OP_DIR_WINTITLE ), p_intf->p_sys->filepath );
502 QString mrl = (dir.endsWith( "VIDEO_TS", Qt::CaseInsensitive ) ?
503 "dvd://" : "directory://")
504 + toNativeSeparators( dir );
505 input_item_t *p_input = input_item_New( THEPL, qtu( mrl ), NULL );
507 /* FIXME: playlist_AddInput() can fail */
508 playlist_AddInput( THEPL, p_input,
509 go ? ( PLAYLIST_APPEND | PLAYLIST_GO ) : PLAYLIST_APPEND,
510 PLAYLIST_END, pl, pl_Unlocked );
511 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
513 input_Read( THEPL, p_input );
514 vlc_gc_decref( p_input );
518 void DialogsProvider::PLOpenDir()
520 openDirectory( p_intf, true, true );
523 void DialogsProvider::PLAppendDir()
525 openDirectory( p_intf, true, false );
528 void DialogsProvider::MLAppendDir()
530 openDirectory( p_intf, false , false );
536 void DialogsProvider::openAPlaylist()
538 QStringList files = showSimpleOpen( qtr( "Open playlist..." ),
539 EXT_FILTER_PLAYLIST );
540 foreach( const QString &file, files )
542 playlist_Import( THEPL, qtu( toNativeSeparators( file ) ) );
546 void DialogsProvider::saveAPlaylist()
553 { N_("XSPF playlist (*.xspf)"), "export-xspf", },
554 { N_("M3U8 playlist (*.m3u)"), "export-m3u8", },
555 { N_("M3U playlist (*.m3u)"), "export-m3u", },
556 { N_("HTML playlist (*.html)"), "export-html", },
558 QString filters, selected;
560 for( size_t i = 0; i < sizeof (types) / sizeof (types[0]); i++ )
562 if( !filters.isEmpty() )
564 filters += qfu( vlc_gettext( types[i].filter ) );
567 QString file = QFileDialog::getSaveFileName( NULL,
568 qtr( "Save playlist as..." ),
569 p_intf->p_sys->filepath, filters, &selected );
573 for( size_t i = 0; i < sizeof (types) / sizeof (types[0]); i++)
574 if( selected == qfu( vlc_gettext( types[i].filter ) ) )
576 playlist_Export( THEPL, qtu( toNativeSeparators( file ) ),
577 THEPL->p_playing, types[i].module );
582 /****************************************************************************
584 ****************************************************************************/
586 void DialogsProvider::streamingDialog( QWidget *parent,
588 bool b_transcode_only,
589 QStringList options )
594 if( !b_transcode_only )
596 SoutDialog *s = new SoutDialog( parent, p_intf, mrl );
597 if( s->exec() == QDialog::Accepted )
599 soutoption = s->getMrl();
608 ConvertDialog *s = new ConvertDialog( parent, p_intf, mrl );
609 if( s->exec() == QDialog::Accepted )
611 soutoption = s->getMrl();
621 if( !soutoption.isEmpty() )
623 options += soutoption.split( " :");
626 input_item_t *p_input;
627 p_input = input_item_New( p_intf, qtu( mrl ), _("Streaming") );
629 /* Add normal Options */
630 for( int j = 0; j < options.size(); j++ )
632 QString qs = colon_unescape( options[j] );
635 input_item_AddOption( p_input, qtu( qs ),
636 VLC_INPUT_OPTION_TRUSTED );
637 msg_Dbg( p_intf, "Adding option: %s", qtu( qs ) );
641 /* Switch between enqueuing and starting the item */
642 /* FIXME: playlist_AddInput() can fail */
643 playlist_AddInput( THEPL, p_input,
644 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END, true, pl_Unlocked );
645 vlc_gc_decref( p_input );
647 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
651 void DialogsProvider::openAndStreamingDialogs()
653 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false, OPEN_AND_STREAM )
654 ->showTab( OPEN_FILE_TAB );
657 void DialogsProvider::openAndTranscodingDialogs()
659 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, false, OPEN_AND_SAVE )
660 ->showTab( OPEN_FILE_TAB );
663 void DialogsProvider::loadSubtitlesFile()
665 input_thread_t *p_input = THEMIM->getInput();
666 if( !p_input ) return;
668 input_item_t *p_item = input_GetItem( p_input );
669 if( !p_item ) return;
671 char *path = input_item_GetURI( p_item );
672 if( !path ) path = strdup( "" );
674 char *sep = strrchr( path, DIR_SEP_CHAR );
675 if( sep ) *sep = '\0';
677 QStringList qsl = showSimpleOpen( qtr( "Open subtitles..." ),
681 foreach( const QString &qsFile, qsl )
683 if( input_AddSubtitle( p_input, qtu( toNativeSeparators( qsFile ) ),
685 msg_Warn( p_intf, "unable to load subtitles from '%s'",
691 /****************************************************************************
693 ****************************************************************************/
695 void DialogsProvider::menuAction( QObject *data )
697 QVLCMenu::DoAction( data );
700 void DialogsProvider::menuUpdateAction( QObject *data )
702 MenuFunc *func = qobject_cast<MenuFunc *>(data);
704 func->doFunc( p_intf );
707 void DialogsProvider::SDMenuAction( const QString& data )
709 char *psz_sd = strdup( qtu( data ) );
710 if( !playlist_IsServicesDiscoveryLoaded( THEPL, psz_sd ) )
711 playlist_ServicesDiscoveryAdd( THEPL, psz_sd );
713 playlist_ServicesDiscoveryRemove( THEPL, psz_sd );
718 * Play the MRL contained in the Recently played menu.
720 void DialogsProvider::playMRL( const QString &mrl )
722 char* psz_uri = make_URI( qtu(mrl) );
723 playlist_Add( THEPL, psz_uri, NULL,
724 PLAYLIST_APPEND | PLAYLIST_GO , PLAYLIST_END, true, false );
727 RecentsMRL::getInstance( p_intf )->addRecent( mrl );