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>
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();
94 AboutDialog::killInstance();
96 UpdateDialog::killInstance();
98 PluginDialog::killInstance();
99 VLMDialog::killInstance();
100 OpenUrlDialog::killInstance();
101 GotoTimeDialog::killInstance();
102 PodcastConfigDialog::killInstance();
103 EpgDialog::killInstance();
104 ErrorsDialog::killInstance();
107 delete menusUpdateMapper;
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 ); break;
158 case INTF_DIALOG_VIDEOPOPUPMENU:
159 QVLCMenu::VideoPopupMenu( p_intf ); break;
160 case INTF_DIALOG_MISCPOPUPMENU:
161 QVLCMenu::MiscPopupMenu( p_intf ); 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 = OpenUrlDialog::getInstance( 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 );
492 * pl helps you to choose from playlist or media library,
493 * go to start or enqueue
495 static void openDirectory( intf_thread_t *p_intf, bool pl, bool go )
497 QString dir = QFileDialog::getExistingDirectory( NULL, qtr("Open Directory"), p_intf->p_sys->filepath );
501 QString mrl = (dir.endsWith( "VIDEO_TS", Qt::CaseInsensitive ) ?
502 "dvd://" : "directory://")
503 + toNativeSeparators( dir );
504 input_item_t *p_input = input_item_New( THEPL, qtu( mrl ), NULL );
506 /* FIXME: playlist_AddInput() can fail */
507 playlist_AddInput( THEPL, p_input,
508 go ? ( PLAYLIST_APPEND | PLAYLIST_GO ) : PLAYLIST_APPEND,
509 PLAYLIST_END, pl, pl_Unlocked );
510 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
512 input_Read( THEPL, p_input );
513 vlc_gc_decref( p_input );
517 void DialogsProvider::PLOpenDir()
519 openDirectory( p_intf, true, true );
522 void DialogsProvider::PLAppendDir()
524 openDirectory( p_intf, true, false );
527 void DialogsProvider::MLAppendDir()
529 openDirectory( p_intf, false , false );
535 void DialogsProvider::openAPlaylist()
537 QStringList files = showSimpleOpen( qtr( "Open playlist..." ),
538 EXT_FILTER_PLAYLIST );
539 foreach( const QString &file, files )
541 playlist_Import( THEPL, qtu( toNativeSeparators( file ) ) );
545 void DialogsProvider::saveAPlaylist()
552 { N_("XSPF playlist (*.xspf)"), "export-xspf", },
553 { N_("M3U8 playlist (*.m3u)"), "export-m3u8", },
554 { N_("M3U playlist (*.m3u)"), "export-m3u", },
555 { N_("HTML playlist (*.html)"), "export-html", },
557 QString filters, selected;
559 for( size_t i = 0; i < sizeof (types) / sizeof (types[0]); i++ )
561 if( !filters.isEmpty() )
563 filters += qfu( vlc_gettext( types[i].filter ) );
566 QString file = QFileDialog::getSaveFileName( NULL,
567 qtr( "Save playlist as..." ),
568 p_intf->p_sys->filepath, filters, &selected );
572 for( size_t i = 0; i < sizeof (types) / sizeof (types[0]); i++)
573 if( selected == qfu( vlc_gettext( types[i].filter ) ) )
575 playlist_Export( THEPL, qtu( toNativeSeparators( file ) ),
576 THEPL->p_playing, types[i].module );
581 /****************************************************************************
583 ****************************************************************************/
585 void DialogsProvider::streamingDialog( QWidget *parent,
587 bool b_transcode_only,
588 QStringList options )
593 if( !b_transcode_only )
595 SoutDialog *s = new SoutDialog( parent, p_intf, mrl );
596 if( s->exec() == QDialog::Accepted )
598 soutoption = s->getMrl();
607 ConvertDialog *s = new ConvertDialog( parent, p_intf, mrl );
608 if( s->exec() == QDialog::Accepted )
610 soutoption = s->getMrl();
620 if( !soutoption.isEmpty() )
622 options += soutoption.split( " :");
625 input_item_t *p_input;
626 p_input = input_item_New( p_intf, qtu( mrl ), _("Streaming") );
628 /* Add normal Options */
629 for( int j = 0; j < options.size(); j++ )
631 QString qs = colon_unescape( options[j] );
634 input_item_AddOption( p_input, qtu( qs ),
635 VLC_INPUT_OPTION_TRUSTED );
636 msg_Dbg( p_intf, "Adding option: %s", qtu( qs ) );
640 /* Switch between enqueuing and starting the item */
641 /* FIXME: playlist_AddInput() can fail */
642 playlist_AddInput( THEPL, p_input,
643 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END, true, pl_Unlocked );
644 vlc_gc_decref( p_input );
646 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
650 void DialogsProvider::openAndStreamingDialogs()
652 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false, OPEN_AND_STREAM )
653 ->showTab( OPEN_FILE_TAB );
656 void DialogsProvider::openAndTranscodingDialogs()
658 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, false, OPEN_AND_SAVE )
659 ->showTab( OPEN_FILE_TAB );
662 void DialogsProvider::loadSubtitlesFile()
664 input_thread_t *p_input = THEMIM->getInput();
665 if( !p_input ) return;
667 input_item_t *p_item = input_GetItem( p_input );
668 if( !p_item ) return;
670 char *path = input_item_GetURI( p_item );
671 if( !path ) path = strdup( "" );
673 char *sep = strrchr( path, DIR_SEP_CHAR );
674 if( sep ) *sep = '\0';
676 QStringList qsl = showSimpleOpen( qtr( "Open subtitles..." ),
680 foreach( const QString &qsFile, qsl )
682 if( input_AddSubtitle( p_input, qtu( toNativeSeparators( qsFile ) ),
684 msg_Warn( p_intf, "unable to load subtitles from '%s'",
690 /****************************************************************************
692 ****************************************************************************/
694 void DialogsProvider::menuAction( QObject *data )
696 QVLCMenu::DoAction( data );
699 void DialogsProvider::menuUpdateAction( QObject *data )
701 MenuFunc *func = qobject_cast<MenuFunc *>(data);
703 func->doFunc( p_intf );
706 void DialogsProvider::SDMenuAction( const QString& data )
708 char *psz_sd = strdup( qtu( data ) );
709 if( !playlist_IsServicesDiscoveryLoaded( THEPL, psz_sd ) )
710 playlist_ServicesDiscoveryAdd( THEPL, psz_sd );
712 playlist_ServicesDiscoveryRemove( THEPL, psz_sd );
717 * Play the MRL contained in the Recently played menu.
719 void DialogsProvider::playMRL( const QString &mrl )
721 char* psz_uri = make_URI( qtu(mrl) );
722 playlist_Add( THEPL, psz_uri, NULL,
723 PLAYLIST_APPEND | PLAYLIST_GO , PLAYLIST_END, true, false );
726 RecentsMRL::getInstance( p_intf )->addRecent( mrl );