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"
58 #include <QApplication>
59 #include <QSignalMapper>
60 #include <QFileDialog>
63 DialogsProvider* DialogsProvider::instance = NULL;
65 DialogsProvider::DialogsProvider( intf_thread_t *_p_intf ) :
66 QObject( NULL ), p_intf( _p_intf )
70 /* Various signal mappers for the menus */
71 menusMapper = new QSignalMapper();
72 CONNECT( menusMapper, mapped(QObject *), this, menuAction( QObject *) );
74 menusUpdateMapper = new QSignalMapper();
75 CONNECT( menusUpdateMapper, mapped(QObject *),
76 this, menuUpdateAction( QObject *) );
78 SDMapper = new QSignalMapper();
79 CONNECT( SDMapper, mapped (QString), this, SDMenuAction( QString ) );
81 DialogHandler *dialogHandler = new DialogHandler (p_intf, this );
84 DialogsProvider::~DialogsProvider()
86 PlaylistDialog::killInstance();
87 MediaInfoDialog::killInstance();
88 MessagesDialog::killInstance();
89 ExtendedDialog::killInstance();
90 BookmarksDialog::killInstance();
91 HelpDialog::killInstance();
93 UpdateDialog::killInstance();
97 delete menusUpdateMapper;
101 void DialogsProvider::quit()
104 libvlc_Quit( p_intf->p_libvlc );
107 void DialogsProvider::customEvent( QEvent *event )
109 if( event->type() == (int)DialogEvent_Type )
111 DialogEvent *de = static_cast<DialogEvent*>(event);
112 switch( de->i_dialog )
114 case INTF_DIALOG_FILE_SIMPLE:
115 case INTF_DIALOG_FILE:
117 case INTF_DIALOG_FILE_GENERIC:
118 openFileGenericDialog( de->p_arg ); break;
119 case INTF_DIALOG_DISC:
120 openDiscDialog(); break;
121 case INTF_DIALOG_NET:
122 openNetDialog(); break;
123 case INTF_DIALOG_SAT:
124 case INTF_DIALOG_CAPTURE:
125 openCaptureDialog(); break;
126 case INTF_DIALOG_DIRECTORY:
127 PLAppendDir(); break;
128 case INTF_DIALOG_PLAYLIST:
129 playlistDialog(); break;
130 case INTF_DIALOG_MESSAGES:
131 messagesDialog(); break;
132 case INTF_DIALOG_FILEINFO:
133 mediaInfoDialog(); break;
134 case INTF_DIALOG_PREFS:
135 prefsDialog(); break;
136 case INTF_DIALOG_BOOKMARKS:
137 bookmarksDialog(); break;
138 case INTF_DIALOG_EXTENDED:
139 extendedDialog(); break;
141 case INTF_DIALOG_VLM:
144 case INTF_DIALOG_POPUPMENU:
145 QVLCMenu::PopupMenu( p_intf, (de->i_arg != 0) ); break;
146 case INTF_DIALOG_AUDIOPOPUPMENU:
147 QVLCMenu::AudioPopupMenu( p_intf ); break;
148 case INTF_DIALOG_VIDEOPOPUPMENU:
149 QVLCMenu::VideoPopupMenu( p_intf ); break;
150 case INTF_DIALOG_MISCPOPUPMENU:
151 QVLCMenu::MiscPopupMenu( p_intf ); break;
152 case INTF_DIALOG_WIZARD:
153 case INTF_DIALOG_STREAMWIZARD:
154 openAndStreamingDialogs(); break;
156 case INTF_DIALOG_UPDATEVLC:
157 updateDialog(); break;
159 case INTF_DIALOG_EXIT:
162 msg_Warn( p_intf, "unimplemented dialog" );
167 /****************************************************************************
168 * Individual simple dialogs
169 ****************************************************************************/
170 void DialogsProvider::playlistDialog()
172 PlaylistDialog::getInstance( p_intf )->toggleVisible();
175 void DialogsProvider::prefsDialog()
177 PrefsDialog *p = new PrefsDialog( (QWidget *)p_intf->p_sys->p_mi, p_intf );
181 void DialogsProvider::extendedDialog()
183 if( !ExtendedDialog::getInstance( p_intf )->isVisible() || /* Hidden */
184 ExtendedDialog::getInstance( p_intf )->currentTab() != 0 ) /* wrong tab */
185 ExtendedDialog::getInstance( p_intf )->showTab( 0 );
187 ExtendedDialog::getInstance( p_intf )->hide();
190 void DialogsProvider::synchroDialog()
192 if( !ExtendedDialog::getInstance( p_intf )->isVisible() || /* Hidden */
193 ExtendedDialog::getInstance( p_intf )->currentTab() != 2 ) /* wrong tab */
194 ExtendedDialog::getInstance( p_intf )->showTab( 2 );
196 ExtendedDialog::getInstance( p_intf )->hide();
199 void DialogsProvider::messagesDialog()
201 MessagesDialog::getInstance( p_intf )->toggleVisible();
204 void DialogsProvider::gotoTimeDialog()
206 GotoTimeDialog::getInstance( p_intf )->toggleVisible();
210 void DialogsProvider::vlmDialog()
212 VLMDialog::getInstance( p_intf )->toggleVisible();
216 void DialogsProvider::helpDialog()
218 HelpDialog::getInstance( p_intf )->toggleVisible();
222 void DialogsProvider::updateDialog()
224 UpdateDialog::getInstance( p_intf )->toggleVisible();
228 void DialogsProvider::aboutDialog()
230 AboutDialog::getInstance( p_intf )->toggleVisible();
233 void DialogsProvider::mediaInfoDialog()
235 MediaInfoDialog::getInstance( p_intf )->showTab( 0 );
238 void DialogsProvider::mediaCodecDialog()
240 MediaInfoDialog::getInstance( p_intf )->showTab( 2 );
243 void DialogsProvider::bookmarksDialog()
245 BookmarksDialog::getInstance( p_intf )->toggleVisible();
248 void DialogsProvider::podcastConfigureDialog()
250 PodcastConfigDialog::getInstance( p_intf )->toggleVisible();
253 void DialogsProvider::toolbarDialog()
255 ToolbarEditDialog *toolbarEditor = new ToolbarEditDialog( (QWidget *)p_intf->p_sys->p_mi, p_intf );
256 if( toolbarEditor->exec() == QDialog::Accepted )
257 emit toolBarConfUpdated();
260 void DialogsProvider::pluginDialog()
262 PluginDialog::getInstance( p_intf )->toggleVisible();
265 /* Generic open file */
266 void DialogsProvider::openFileGenericDialog( intf_dialog_args_t *p_arg )
270 msg_Warn( p_intf, "openFileGenericDialog() called with NULL arg" );
274 /* Replace the extensions to a Qt format */
276 QString extensions = qfu( p_arg->psz_extensions );
277 while ( ( i = extensions.indexOf( "|", i ) ) != -1 )
279 if( ( extensions.count( "|" ) % 2 ) == 0 )
280 extensions.replace( i, 1, ");;" );
282 extensions.replace( i, 1, "(" );
284 extensions.replace( ";*", " *" );
285 extensions.append( ")" );
290 QString file = QFileDialog::getSaveFileName( NULL, p_arg->psz_title,
291 p_intf->p_sys->filepath, extensions );
292 if( !file.isEmpty() )
294 p_arg->i_results = 1;
295 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
296 p_arg->psz_results[0] = strdup( qtu( toNativeSepNoSlash( file ) ) );
299 p_arg->i_results = 0;
301 else /* non-save mode */
303 QStringList files = QFileDialog::getOpenFileNames( NULL,
304 p_arg->psz_title, p_intf->p_sys->filepath,
306 p_arg->i_results = files.count();
307 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
309 foreach( const QString &file, files )
310 p_arg->psz_results[i++] = strdup( qtu( toNativeSepNoSlash( file ) ) );
311 p_intf->p_sys->filepath = qfu( p_arg->psz_results[i] );
315 if( p_arg->pf_callback )
316 p_arg->pf_callback( p_arg );
318 /* Clean afterwards */
319 if( p_arg->psz_results )
321 for( i = 0; i < p_arg->i_results; i++ )
322 free( p_arg->psz_results[i] );
323 free( p_arg->psz_results );
325 free( p_arg->psz_title );
326 free( p_arg->psz_extensions );
329 /****************************************************************************
330 * All the open/add stuff
331 * Open Dialog first - Simple Open then
332 ****************************************************************************/
334 void DialogsProvider::openDialog( int i_tab )
336 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf )->showTab( i_tab );
338 void DialogsProvider::openDialog()
340 openDialog( OPEN_FILE_TAB );
342 void DialogsProvider::openFileDialog()
344 openDialog( OPEN_FILE_TAB );
346 void DialogsProvider::openDiscDialog()
348 openDialog( OPEN_DISC_TAB );
350 void DialogsProvider::openNetDialog()
352 openDialog( OPEN_NETWORK_TAB );
354 void DialogsProvider::openCaptureDialog()
356 openDialog( OPEN_CAPTURE_TAB );
359 /* Same as the open one, but force the enqueue */
360 void DialogsProvider::PLAppendDialog( int tab )
362 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
363 OPEN_AND_ENQUEUE )->showTab( tab );
366 void DialogsProvider::MLAppendDialog( int tab )
368 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
369 OPEN_AND_ENQUEUE, false, false )
376 QStringList DialogsProvider::showSimpleOpen( const QString& help,
378 const QString& path )
380 QString fileTypes = "";
381 if( filters & EXT_FILTER_MEDIA ) {
382 ADD_FILTER_MEDIA( fileTypes );
384 if( filters & EXT_FILTER_VIDEO ) {
385 ADD_FILTER_VIDEO( fileTypes );
387 if( filters & EXT_FILTER_AUDIO ) {
388 ADD_FILTER_AUDIO( fileTypes );
390 if( filters & EXT_FILTER_PLAYLIST ) {
391 ADD_FILTER_PLAYLIST( fileTypes );
393 if( filters & EXT_FILTER_SUBTITLE ) {
394 ADD_FILTER_SUBTITLE( fileTypes );
396 ADD_FILTER_ALL( fileTypes );
397 fileTypes.replace( ";*", " *");
399 QStringList files = QFileDialog::getOpenFileNames( NULL,
400 help.isEmpty() ? qtr(I_OP_SEL_FILES ) : help,
401 path.isEmpty() ? p_intf->p_sys->filepath : path,
404 if( !files.isEmpty() ) savedirpathFromFile( files.last() );
411 * pl helps you to choose from playlist or media library,
412 * go to start or enqueue
414 void DialogsProvider::addFromSimple( bool pl, bool go)
416 QStringList files = DialogsProvider::showSimpleOpen();
419 foreach( const QString &file, files )
421 playlist_Add( THEPL, qtu( toNativeSeparators( file ) ), NULL,
422 go ? ( PLAYLIST_APPEND | ( i ? PLAYLIST_PREPARSE : PLAYLIST_GO ) )
423 : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
424 PLAYLIST_END, pl, pl_Unlocked );
425 RecentsMRL::getInstance( p_intf )->addRecent(
426 toNativeSeparators( file ) );
431 void DialogsProvider::simpleOpenDialog()
433 addFromSimple( true, true ); /* Playlist and Go */
436 void DialogsProvider::simplePLAppendDialog()
438 addFromSimple( true, false );
441 void DialogsProvider::simpleMLAppendDialog()
443 addFromSimple( false, false );
446 /* Url & Clipboard */
449 * If the clipboard contains URLs, the first is automatically 'preselected'.
451 void DialogsProvider::openUrlDialog()
453 OpenUrlDialog *oud = OpenUrlDialog::getInstance( p_intf->p_sys->p_mi,
455 if( oud->exec() == QDialog::Accepted )
457 QString url = oud->url();
460 playlist_Add( THEPL, qtu( url ),
461 NULL, !oud->shouldEnqueue() ?
462 ( PLAYLIST_APPEND | PLAYLIST_GO )
463 : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
464 PLAYLIST_END, true, false );
465 RecentsMRL::getInstance( p_intf )->addRecent( url );
473 * pl helps you to choose from playlist or media library,
474 * go to start or enqueue
476 static void openDirectory( intf_thread_t *p_intf, bool pl, bool go )
478 QString dir = QFileDialog::getExistingDirectory( NULL, qtr("Open Directory"), p_intf->p_sys->filepath );
482 QString mrl = (dir.endsWith( "VIDEO_TS", Qt::CaseInsensitive ) ?
483 "dvd://" : "directory://")
484 + toNativeSeparators( dir );
485 input_item_t *p_input = input_item_New( THEPL, qtu( mrl ), NULL );
487 /* FIXME: playlist_AddInput() can fail */
488 playlist_AddInput( THEPL, p_input,
489 go ? ( PLAYLIST_APPEND | PLAYLIST_GO ) : PLAYLIST_APPEND,
490 PLAYLIST_END, pl, pl_Unlocked );
491 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
493 input_Read( THEPL, p_input, true );
494 vlc_gc_decref( p_input );
498 void DialogsProvider::PLOpenDir()
500 openDirectory( p_intf, true, true );
503 void DialogsProvider::PLAppendDir()
505 openDirectory( p_intf, true, false );
508 void DialogsProvider::MLAppendDir()
510 openDirectory( p_intf, false , false );
516 void DialogsProvider::openAPlaylist()
518 QStringList files = showSimpleOpen( qtr( "Open playlist..." ),
519 EXT_FILTER_PLAYLIST );
520 foreach( const QString &file, files )
522 playlist_Import( THEPL, qtu( toNativeSeparators( file ) ) );
526 void DialogsProvider::saveAPlaylist()
533 { N_("XSPF playlist (*.xspf)"), "export-xspf", },
534 { N_("M3U playlist (*.m3u)"), "export-m3u", },
535 { N_("HTML playlist (*.html)"), "export-html", },
537 QString filters, selected;
539 for( size_t i = 0; i < sizeof (types) / sizeof (types[0]); i++ )
541 if( !filters.isEmpty() )
543 filters += qfu( vlc_gettext( types[i].filter ) );
546 QString file = QFileDialog::getSaveFileName( NULL,
547 qtr( "Save playlist as..." ),
548 p_intf->p_sys->filepath, filters, &selected );
552 for( size_t i = 0; i < sizeof (types) / sizeof (types[0]); i++)
553 if( selected == qfu( vlc_gettext( types[i].filter ) ) )
555 playlist_Export( THEPL, qtu( toNativeSeparators( file ) ),
556 THEPL->p_local_category, types[i].module );
561 /****************************************************************************
563 ****************************************************************************/
565 void DialogsProvider::streamingDialog( QWidget *parent,
567 bool b_transcode_only,
568 QStringList options )
570 char *psz_soutoption;
573 if( !b_transcode_only )
575 SoutDialog *s = new SoutDialog( parent, p_intf, mrl );
576 if( s->exec() == QDialog::Accepted )
578 psz_soutoption = strdup( qtu( s->getMrl() ) );
587 ConvertDialog *s = new ConvertDialog( parent, p_intf, mrl );
588 if( s->exec() == QDialog::Accepted )
590 psz_soutoption = strdup( qtu( s->getMrl() ) );
600 if( !EMPTY_STR( psz_soutoption ) )
602 options += QString( psz_soutoption ).split( " :");
605 input_item_t *p_input;
606 p_input = input_item_New( p_intf, qtu( mrl ), _("Streaming") );
608 /* Add normal Options */
609 for( int j = 0; j < options.size(); j++ )
611 QString qs = colon_unescape( options[j] );
614 input_item_AddOption( p_input, qtu( qs ),
615 VLC_INPUT_OPTION_TRUSTED );
616 msg_Dbg( p_intf, "Adding option: %s", qtu( qs ) );
620 /* Switch between enqueuing and starting the item */
621 /* FIXME: playlist_AddInput() can fail */
622 playlist_AddInput( THEPL, p_input,
623 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END, true, pl_Unlocked );
624 vlc_gc_decref( p_input );
626 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
628 free( psz_soutoption );
631 void DialogsProvider::openAndStreamingDialogs()
633 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false, OPEN_AND_STREAM )
634 ->showTab( OPEN_FILE_TAB );
637 void DialogsProvider::openAndTranscodingDialogs()
639 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, false, OPEN_AND_SAVE )
640 ->showTab( OPEN_FILE_TAB );
643 void DialogsProvider::loadSubtitlesFile()
645 input_thread_t *p_input = THEMIM->getInput();
646 if( !p_input ) return;
648 input_item_t *p_item = input_GetItem( p_input );
649 if( !p_item ) return;
651 char *path = input_item_GetURI( p_item );
652 if( !path ) path = strdup( "" );
654 char *sep = strrchr( path, DIR_SEP_CHAR );
655 if( sep ) *sep = '\0';
657 QStringList qsl = showSimpleOpen( qtr( "Open subtitles..." ),
661 foreach( const QString &qsFile, qsl )
663 if( input_AddSubtitle( p_input, qtu( toNativeSeparators( qsFile ) ),
665 msg_Warn( p_intf, "unable to load subtitles from '%s'",
671 /****************************************************************************
673 ****************************************************************************/
675 void DialogsProvider::menuAction( QObject *data )
677 QVLCMenu::DoAction( data );
680 void DialogsProvider::menuUpdateAction( QObject *data )
682 MenuFunc *func = qobject_cast<MenuFunc *>(data);
684 func->doFunc( p_intf );
687 void DialogsProvider::SDMenuAction( const QString& data )
689 char *psz_sd = strdup( qtu( data ) );
690 if( !playlist_IsServicesDiscoveryLoaded( THEPL, psz_sd ) )
691 playlist_ServicesDiscoveryAdd( THEPL, psz_sd );
693 playlist_ServicesDiscoveryRemove( THEPL, psz_sd );
698 * Play the MRL contained in the Recently played menu.
700 void DialogsProvider::playMRL( const QString &mrl )
702 playlist_Add( THEPL, qtu( mrl ) , NULL,
703 PLAYLIST_APPEND | PLAYLIST_GO , PLAYLIST_END, true, false );
705 RecentsMRL::getInstance( p_intf )->addRecent( mrl );