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 ExtendedDialog::getInstance( p_intf )->showTab( 0 );
186 void DialogsProvider::synchroDialog()
188 ExtendedDialog::getInstance( p_intf )->showTab( 2 );
191 void DialogsProvider::messagesDialog()
193 MessagesDialog::getInstance( p_intf )->toggleVisible();
196 void DialogsProvider::gotoTimeDialog()
198 GotoTimeDialog::getInstance( p_intf )->toggleVisible();
202 void DialogsProvider::vlmDialog()
204 VLMDialog::getInstance( p_intf )->toggleVisible();
208 void DialogsProvider::helpDialog()
210 HelpDialog::getInstance( p_intf )->toggleVisible();
214 void DialogsProvider::updateDialog()
216 UpdateDialog::getInstance( p_intf )->toggleVisible();
220 void DialogsProvider::aboutDialog()
222 AboutDialog::getInstance( p_intf )->toggleVisible();
225 void DialogsProvider::mediaInfoDialog()
227 MediaInfoDialog::getInstance( p_intf )->showTab( 0 );
230 void DialogsProvider::mediaCodecDialog()
232 MediaInfoDialog::getInstance( p_intf )->showTab( 2 );
235 void DialogsProvider::bookmarksDialog()
237 BookmarksDialog::getInstance( p_intf )->toggleVisible();
240 void DialogsProvider::podcastConfigureDialog()
242 PodcastConfigDialog::getInstance( p_intf )->toggleVisible();
245 void DialogsProvider::toolbarDialog()
247 ToolbarEditDialog *toolbarEditor = new ToolbarEditDialog( (QWidget *)p_intf->p_sys->p_mi, p_intf );
248 if( toolbarEditor->exec() == QDialog::Accepted )
249 emit toolBarConfUpdated();
252 void DialogsProvider::pluginDialog()
254 PluginDialog *diag = new PluginDialog( p_intf );
258 /* Generic open file */
259 void DialogsProvider::openFileGenericDialog( intf_dialog_args_t *p_arg )
263 msg_Warn( p_intf, "openFileGenericDialog() called with NULL arg" );
267 /* Replace the extensions to a Qt format */
269 QString extensions = qfu( p_arg->psz_extensions );
270 while ( ( i = extensions.indexOf( "|", i ) ) != -1 )
272 if( ( extensions.count( "|" ) % 2 ) == 0 )
273 extensions.replace( i, 1, ");;" );
275 extensions.replace( i, 1, "(" );
277 extensions.replace( ";*", " *" );
278 extensions.append( ")" );
283 QString file = QFileDialog::getSaveFileName( NULL, p_arg->psz_title,
284 p_intf->p_sys->filepath, extensions );
285 if( !file.isEmpty() )
287 p_arg->i_results = 1;
288 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
289 p_arg->psz_results[0] = strdup( qtu( toNativeSepNoSlash( file ) ) );
292 p_arg->i_results = 0;
294 else /* non-save mode */
296 QStringList files = QFileDialog::getOpenFileNames( NULL,
297 p_arg->psz_title, p_intf->p_sys->filepath,
299 p_arg->i_results = files.count();
300 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
302 foreach( const QString &file, files )
303 p_arg->psz_results[i++] = strdup( qtu( toNativeSepNoSlash( file ) ) );
304 p_intf->p_sys->filepath = qfu( p_arg->psz_results[i] );
308 if( p_arg->pf_callback )
309 p_arg->pf_callback( p_arg );
311 /* Clean afterwards */
312 if( p_arg->psz_results )
314 for( i = 0; i < p_arg->i_results; i++ )
315 free( p_arg->psz_results[i] );
316 free( p_arg->psz_results );
318 free( p_arg->psz_title );
319 free( p_arg->psz_extensions );
322 /****************************************************************************
323 * All the open/add stuff
324 * Open Dialog first - Simple Open then
325 ****************************************************************************/
327 void DialogsProvider::openDialog( int i_tab )
329 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf )->showTab( i_tab );
331 void DialogsProvider::openDialog()
333 openDialog( OPEN_FILE_TAB );
335 void DialogsProvider::openFileDialog()
337 openDialog( OPEN_FILE_TAB );
339 void DialogsProvider::openDiscDialog()
341 openDialog( OPEN_DISC_TAB );
343 void DialogsProvider::openNetDialog()
345 openDialog( OPEN_NETWORK_TAB );
347 void DialogsProvider::openCaptureDialog()
349 openDialog( OPEN_CAPTURE_TAB );
352 /* Same as the open one, but force the enqueue */
353 void DialogsProvider::PLAppendDialog( int tab )
355 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
356 OPEN_AND_ENQUEUE )->showTab( tab );
359 void DialogsProvider::MLAppendDialog( int tab )
361 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
362 OPEN_AND_ENQUEUE, false, false )
369 QStringList DialogsProvider::showSimpleOpen( QString help,
373 QString fileTypes = "";
374 if( filters & EXT_FILTER_MEDIA ) {
375 ADD_FILTER_MEDIA( fileTypes );
377 if( filters & EXT_FILTER_VIDEO ) {
378 ADD_FILTER_VIDEO( fileTypes );
380 if( filters & EXT_FILTER_AUDIO ) {
381 ADD_FILTER_AUDIO( fileTypes );
383 if( filters & EXT_FILTER_PLAYLIST ) {
384 ADD_FILTER_PLAYLIST( fileTypes );
386 if( filters & EXT_FILTER_SUBTITLE ) {
387 ADD_FILTER_SUBTITLE( fileTypes );
389 ADD_FILTER_ALL( fileTypes );
390 fileTypes.replace( ";*", " *");
392 return QFileDialog::getOpenFileNames( NULL,
393 help.isEmpty() ? qtr(I_OP_SEL_FILES ) : help,
394 path.isEmpty() ? p_intf->p_sys->filepath : path,
400 * pl helps you to choose from playlist or media library,
401 * go to start or enqueue
403 void DialogsProvider::addFromSimple( bool pl, bool go)
405 QStringList files = DialogsProvider::showSimpleOpen();
408 foreach( const QString &file, files )
410 playlist_Add( THEPL, qtu( toNativeSeparators( file ) ), NULL,
411 go ? ( PLAYLIST_APPEND | ( i ? 0 : PLAYLIST_GO ) |
412 ( i ? PLAYLIST_PREPARSE : 0 ) )
413 : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
415 pl ? true : false, false );
416 RecentsMRL::getInstance( p_intf )->addRecent(
417 toNativeSeparators( file ) );
422 void DialogsProvider::simpleOpenDialog()
424 addFromSimple( true, true ); /* Playlist and Go */
427 void DialogsProvider::simplePLAppendDialog()
429 addFromSimple( true, false );
432 void DialogsProvider::simpleMLAppendDialog()
434 addFromSimple( false, false );
437 /* Url & Clipboard */
440 * If the clipboard contains URLs, the first is automatically 'preselected'.
442 void DialogsProvider::openUrlDialog()
444 OpenUrlDialog *oud = OpenUrlDialog::getInstance( p_intf->p_sys->p_mi,
446 if( oud->exec() == QDialog::Accepted )
448 QString url = oud->url();
451 playlist_Add( THEPL, qtu( url ),
452 NULL, !oud->shouldEnqueue() ?
453 ( PLAYLIST_APPEND | PLAYLIST_GO )
454 : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
455 PLAYLIST_END, true, false );
456 RecentsMRL::getInstance( p_intf )->addRecent( url );
464 * pl helps you to choose from playlist or media library,
465 * go to start or enqueue
467 static void openDirectory( intf_thread_t *p_intf, bool pl, bool go )
469 QString dir = QFileDialog::getExistingDirectory( NULL, qtr("Open Directory") );
473 QString mrl = dir.endsWith( "VIDEO_TS", Qt::CaseInsensitive ) ?
474 "dvd://" : "directory://" + toNativeSeparators( dir );
475 input_item_t *p_input = input_item_New( THEPL, qtu( mrl ), NULL );
477 /* FIXME: playlist_AddInput() can fail */
478 playlist_AddInput( THEPL, p_input,
479 go ? ( PLAYLIST_APPEND | PLAYLIST_GO ) : PLAYLIST_APPEND,
480 PLAYLIST_END, pl, pl_Unlocked );
481 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
483 input_Read( THEPL, p_input, true );
484 vlc_gc_decref( p_input );
488 void DialogsProvider::PLOpenDir()
490 openDirectory( p_intf, true, true );
493 void DialogsProvider::PLAppendDir()
495 openDirectory( p_intf, true, false );
498 void DialogsProvider::MLAppendDir()
500 openDirectory( p_intf, false , false );
506 void DialogsProvider::openAPlaylist()
508 QStringList files = showSimpleOpen( qtr( "Open playlist..." ),
509 EXT_FILTER_PLAYLIST );
510 foreach( const QString &file, files )
512 playlist_Import( THEPL, qtu( toNativeSeparators( file ) ) );
516 void DialogsProvider::saveAPlaylist()
518 QFileDialog *qfd = new QFileDialog( NULL,
519 qtr( "Save playlist as..." ),
520 p_intf->p_sys->filepath,
521 qtr( "XSPF playlist (*.xspf);; " ) +
522 qtr( "M3U playlist (*.m3u);; " ) +
523 qtr( "HTML playlist (*.html)" ) );
524 qfd->setFileMode( QFileDialog::AnyFile );
525 qfd->setAcceptMode( QFileDialog::AcceptSave );
526 qfd->setConfirmOverwrite( true );
528 if( qfd->exec() == QDialog::Accepted )
530 if( qfd->selectedFiles().count() > 0 )
532 static const char psz_xspf[] = "export-xspf",
533 psz_m3u[] = "export-m3u",
534 psz_html[] = "export-html";
535 const char *psz_module;
537 QString file = qfd->selectedFiles().first();
538 QString filter = qfd->selectedFilter();
540 if( file.contains( ".xsp" ) || filter.contains( "XSPF" ) )
542 psz_module = psz_xspf;
543 if( !file.contains( ".xsp" ) )
544 file.append( ".xspf" );
546 else if( file.contains( ".m3u" ) || filter.contains( "M3U" ) )
548 psz_module = psz_m3u;
549 if( !file.contains( ".m3u" ) )
550 file.append( ".m3u" );
552 else if( file.contains(".html" ) || filter.contains( "HTML" ) )
554 psz_module = psz_html;
555 if( !file.contains( "html" ) )
556 file.append( ".html" );
560 msg_Err( p_intf, "Impossible to recognise the file type" );
565 playlist_Export( THEPL, qtu( toNativeSeparators( file ) ),
566 THEPL->p_local_category, psz_module);
572 /****************************************************************************
574 ****************************************************************************/
576 void DialogsProvider::streamingDialog( QWidget *parent,
578 bool b_transcode_only,
579 QStringList options )
581 char *psz_soutoption;
584 if( !b_transcode_only )
586 SoutDialog *s = new SoutDialog( parent, p_intf, mrl );
587 if( s->exec() == QDialog::Accepted )
589 psz_soutoption = strdup( qtu( s->getMrl() ) );
598 ConvertDialog *s = new ConvertDialog( parent, p_intf, mrl );
599 if( s->exec() == QDialog::Accepted )
601 psz_soutoption = strdup( qtu( s->getMrl() ) );
611 if( !EMPTY_STR( psz_soutoption ) )
613 options += QString( psz_soutoption ).split( " :");
616 input_item_t *p_input;
617 p_input = input_item_New( p_intf, qtu( mrl ), _("Streaming") );
619 /* Add normal Options */
620 for( int j = 0; j < options.size(); j++ )
622 QString qs = colon_unescape( options[j] );
625 input_item_AddOption( p_input, qtu( qs ),
626 VLC_INPUT_OPTION_TRUSTED );
627 msg_Dbg( p_intf, "Adding option: %s", qtu( qs ) );
631 /* Switch between enqueuing and starting the item */
632 /* FIXME: playlist_AddInput() can fail */
633 playlist_AddInput( THEPL, p_input,
634 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END, true, pl_Unlocked );
635 vlc_gc_decref( p_input );
637 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
639 free( psz_soutoption );
642 void DialogsProvider::openAndStreamingDialogs()
644 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false, OPEN_AND_STREAM )
645 ->showTab( OPEN_FILE_TAB );
648 void DialogsProvider::openAndTranscodingDialogs()
650 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, false, OPEN_AND_SAVE )
651 ->showTab( OPEN_FILE_TAB );
654 void DialogsProvider::loadSubtitlesFile()
656 input_thread_t *p_input = THEMIM->getInput();
657 if( !p_input ) return;
659 input_item_t *p_item = input_GetItem( p_input );
660 if( !p_item ) return;
662 char *path = input_item_GetURI( p_item );
663 if( !path ) path = strdup( "" );
665 char *sep = strrchr( path, DIR_SEP_CHAR );
666 if( sep ) *sep = '\0';
668 QStringList qsl = showSimpleOpen( qtr( "Open subtitles..." ),
672 foreach( const QString &qsFile, qsl )
674 if( input_AddSubtitle( p_input, qtu( toNativeSeparators( qsFile ) ),
676 msg_Warn( p_intf, "unable to load subtitles from '%s'",
682 /****************************************************************************
684 ****************************************************************************/
686 void DialogsProvider::menuAction( QObject *data )
688 QVLCMenu::DoAction( data );
691 void DialogsProvider::menuUpdateAction( QObject *data )
693 MenuFunc *func = qobject_cast<MenuFunc *>(data);
695 func->doFunc( p_intf );
698 void DialogsProvider::SDMenuAction( QString data )
700 char *psz_sd = strdup( qtu( data ) );
701 if( !playlist_IsServicesDiscoveryLoaded( THEPL, psz_sd ) )
702 playlist_ServicesDiscoveryAdd( THEPL, psz_sd );
704 playlist_ServicesDiscoveryRemove( THEPL, psz_sd );
709 * Play the MRL contained in the Recently played menu.
711 void DialogsProvider::playMRL( const QString &mrl )
713 playlist_Add( THEPL, qtu( mrl ) , NULL,
714 PLAYLIST_APPEND | PLAYLIST_GO , PLAYLIST_END, true, false );
716 RecentsMRL::getInstance( p_intf )->addRecent( mrl );