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"
57 #include <QApplication>
58 #include <QSignalMapper>
59 #include <QFileDialog>
62 DialogsProvider* DialogsProvider::instance = NULL;
64 DialogsProvider::DialogsProvider( intf_thread_t *_p_intf ) :
65 QObject( NULL ), p_intf( _p_intf )
69 /* Various signal mappers for the menus */
70 menusMapper = new QSignalMapper();
71 CONNECT( menusMapper, mapped(QObject *), this, menuAction( QObject *) );
73 menusUpdateMapper = new QSignalMapper();
74 CONNECT( menusUpdateMapper, mapped(QObject *),
75 this, menuUpdateAction( QObject *) );
77 SDMapper = new QSignalMapper();
78 CONNECT( SDMapper, mapped (QString), this, SDMenuAction( QString ) );
81 DialogsProvider::~DialogsProvider()
83 PlaylistDialog::killInstance();
84 MediaInfoDialog::killInstance();
85 MessagesDialog::killInstance();
86 ExtendedDialog::killInstance();
87 BookmarksDialog::killInstance();
88 HelpDialog::killInstance();
90 UpdateDialog::killInstance();
94 delete menusUpdateMapper;
98 void DialogsProvider::quit()
101 libvlc_Quit( p_intf->p_libvlc );
104 void DialogsProvider::customEvent( QEvent *event )
106 if( event->type() == (int)DialogEvent_Type )
108 DialogEvent *de = static_cast<DialogEvent*>(event);
109 switch( de->i_dialog )
111 case INTF_DIALOG_FILE_SIMPLE:
112 case INTF_DIALOG_FILE:
114 case INTF_DIALOG_FILE_GENERIC:
115 openFileGenericDialog( de->p_arg ); break;
116 case INTF_DIALOG_DISC:
117 openDiscDialog(); break;
118 case INTF_DIALOG_NET:
119 openNetDialog(); break;
120 case INTF_DIALOG_SAT:
121 case INTF_DIALOG_CAPTURE:
122 openCaptureDialog(); break;
123 case INTF_DIALOG_DIRECTORY:
124 PLAppendDir(); break;
125 case INTF_DIALOG_PLAYLIST:
126 playlistDialog(); break;
127 case INTF_DIALOG_MESSAGES:
128 messagesDialog(); break;
129 case INTF_DIALOG_FILEINFO:
130 mediaInfoDialog(); break;
131 case INTF_DIALOG_PREFS:
132 prefsDialog(); break;
133 case INTF_DIALOG_BOOKMARKS:
134 bookmarksDialog(); break;
135 case INTF_DIALOG_EXTENDED:
136 extendedDialog(); break;
138 case INTF_DIALOG_VLM:
141 case INTF_DIALOG_POPUPMENU:
142 QVLCMenu::PopupMenu( p_intf, (de->i_arg != 0) ); break;
143 case INTF_DIALOG_AUDIOPOPUPMENU:
144 QVLCMenu::AudioPopupMenu( p_intf ); break;
145 case INTF_DIALOG_VIDEOPOPUPMENU:
146 QVLCMenu::VideoPopupMenu( p_intf ); break;
147 case INTF_DIALOG_MISCPOPUPMENU:
148 QVLCMenu::MiscPopupMenu( p_intf ); break;
149 case INTF_DIALOG_WIZARD:
150 case INTF_DIALOG_STREAMWIZARD:
151 openAndStreamingDialogs(); break;
153 case INTF_DIALOG_UPDATEVLC:
154 updateDialog(); break;
156 case INTF_DIALOG_EXIT:
159 msg_Warn( p_intf, "unimplemented dialog" );
164 /****************************************************************************
165 * Individual simple dialogs
166 ****************************************************************************/
167 void DialogsProvider::playlistDialog()
169 PlaylistDialog::getInstance( p_intf )->toggleVisible();
172 void DialogsProvider::prefsDialog()
174 PrefsDialog *p = new PrefsDialog( (QWidget *)p_intf->p_sys->p_mi, p_intf );
178 void DialogsProvider::extendedDialog()
180 ExtendedDialog::getInstance( p_intf )->showTab( 0 );
183 void DialogsProvider::synchroDialog()
185 ExtendedDialog::getInstance( p_intf )->showTab( 2 );
188 void DialogsProvider::messagesDialog()
190 MessagesDialog::getInstance( p_intf )->toggleVisible();
193 void DialogsProvider::gotoTimeDialog()
195 GotoTimeDialog::getInstance( p_intf )->toggleVisible();
199 void DialogsProvider::vlmDialog()
201 VLMDialog::getInstance( p_intf )->toggleVisible();
205 void DialogsProvider::helpDialog()
207 HelpDialog::getInstance( p_intf )->toggleVisible();
211 void DialogsProvider::updateDialog()
213 UpdateDialog::getInstance( p_intf )->toggleVisible();
217 void DialogsProvider::aboutDialog()
219 AboutDialog::getInstance( p_intf )->toggleVisible();
222 void DialogsProvider::mediaInfoDialog()
224 MediaInfoDialog::getInstance( p_intf )->showTab( 0 );
227 void DialogsProvider::mediaCodecDialog()
229 MediaInfoDialog::getInstance( p_intf )->showTab( 2 );
232 void DialogsProvider::bookmarksDialog()
234 BookmarksDialog::getInstance( p_intf )->toggleVisible();
237 void DialogsProvider::podcastConfigureDialog()
239 PodcastConfigDialog::getInstance( p_intf )->toggleVisible();
242 void DialogsProvider::toolbarDialog()
244 ToolbarEditDialog *toolbarEditor = new ToolbarEditDialog( (QWidget *)p_intf->p_sys->p_mi, p_intf );
245 if( toolbarEditor->exec() == QDialog::Accepted )
246 emit toolBarConfUpdated();
249 void DialogsProvider::pluginDialog()
251 PluginDialog *diag = new PluginDialog( p_intf );
255 /* Generic open file */
256 void DialogsProvider::openFileGenericDialog( intf_dialog_args_t *p_arg )
260 msg_Warn( p_intf, "openFileGenericDialog() called with NULL arg" );
264 /* Replace the extensions to a Qt format */
266 QString extensions = qfu( p_arg->psz_extensions );
267 while ( ( i = extensions.indexOf( "|", i ) ) != -1 )
269 if( ( extensions.count( "|" ) % 2 ) == 0 )
270 extensions.replace( i, 1, ");;" );
272 extensions.replace( i, 1, "(" );
274 extensions.replace(QString(";*"), QString(" *"));
275 extensions.append( ")" );
280 QString file = QFileDialog::getSaveFileName( NULL, p_arg->psz_title,
281 p_intf->p_sys->filepath, extensions );
282 if( !file.isEmpty() )
284 p_arg->i_results = 1;
285 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
286 p_arg->psz_results[0] = strdup( qtu( toNativeSepNoSlash( file ) ) );
289 p_arg->i_results = 0;
291 else /* non-save mode */
293 QStringList files = QFileDialog::getOpenFileNames( NULL,
294 p_arg->psz_title, p_intf->p_sys->filepath,
296 p_arg->i_results = files.count();
297 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
299 foreach( const QString &file, files )
300 p_arg->psz_results[i++] = strdup( qtu( toNativeSepNoSlash( file ) ) );
304 if( p_arg->pf_callback )
305 p_arg->pf_callback( p_arg );
307 /* Clean afterwards */
308 if( p_arg->psz_results )
310 for( i = 0; i < p_arg->i_results; i++ )
311 free( p_arg->psz_results[i] );
312 free( p_arg->psz_results );
314 free( p_arg->psz_title );
315 free( p_arg->psz_extensions );
318 /****************************************************************************
319 * All the open/add stuff
320 * Open Dialog first - Simple Open then
321 ****************************************************************************/
323 void DialogsProvider::openDialog( int i_tab )
325 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf )->showTab( i_tab );
327 void DialogsProvider::openDialog()
329 openDialog( OPEN_FILE_TAB );
331 void DialogsProvider::openFileDialog()
333 openDialog( OPEN_FILE_TAB );
335 void DialogsProvider::openDiscDialog()
337 openDialog( OPEN_DISC_TAB );
339 void DialogsProvider::openNetDialog()
341 openDialog( OPEN_NETWORK_TAB );
343 void DialogsProvider::openCaptureDialog()
345 openDialog( OPEN_CAPTURE_TAB );
348 /* Same as the open one, but force the enqueue */
349 void DialogsProvider::PLAppendDialog()
351 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
352 OPEN_AND_ENQUEUE )->showTab( OPEN_FILE_TAB );
355 void DialogsProvider::MLAppendDialog()
357 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
358 OPEN_AND_ENQUEUE, false, false )
359 ->showTab( OPEN_FILE_TAB );
365 QStringList DialogsProvider::showSimpleOpen( QString help,
369 QString fileTypes = "";
370 if( filters & EXT_FILTER_MEDIA ) {
371 ADD_FILTER_MEDIA( fileTypes );
373 if( filters & EXT_FILTER_VIDEO ) {
374 ADD_FILTER_VIDEO( fileTypes );
376 if( filters & EXT_FILTER_AUDIO ) {
377 ADD_FILTER_AUDIO( fileTypes );
379 if( filters & EXT_FILTER_PLAYLIST ) {
380 ADD_FILTER_PLAYLIST( fileTypes );
382 if( filters & EXT_FILTER_SUBTITLE ) {
383 ADD_FILTER_SUBTITLE( fileTypes );
385 ADD_FILTER_ALL( fileTypes );
386 fileTypes.replace(QString(";*"), QString(" *"));
388 return QFileDialog::getOpenFileNames( NULL,
389 help.isEmpty() ? qtr(I_OP_SEL_FILES ) : help,
390 path.isEmpty() ? p_intf->p_sys->filepath : path,
396 * pl helps you to choose from playlist or media library,
397 * go to start or enqueue
399 void DialogsProvider::addFromSimple( bool pl, bool go)
401 QStringList files = DialogsProvider::showSimpleOpen();
404 foreach( const QString &file, files )
406 playlist_Add( THEPL, qtu( toNativeSeparators( file ) ), NULL,
407 go ? ( PLAYLIST_APPEND | ( i ? 0 : PLAYLIST_GO ) |
408 ( i ? PLAYLIST_PREPARSE : 0 ) )
409 : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
411 pl ? true : false, false );
412 RecentsMRL::getInstance( p_intf )->addRecent(
413 toNativeSeparators( file ) );
418 void DialogsProvider::simpleOpenDialog()
420 addFromSimple( true, true ); /* Playlist and Go */
423 void DialogsProvider::simplePLAppendDialog()
425 addFromSimple( true, false );
428 void DialogsProvider::simpleMLAppendDialog()
430 addFromSimple( false, false );
433 /* Url & Clipboard */
436 * If the clipboard contains URLs, the first is automatically 'preselected'.
438 void DialogsProvider::openUrlDialog()
440 OpenUrlDialog *oud = OpenUrlDialog::getInstance( p_intf->p_sys->p_mi,
442 if( oud->exec() == QDialog::Accepted )
444 QString url = oud->url();
447 playlist_Add( THEPL, qtu( url ),
448 NULL, !oud->shouldEnqueue() ?
449 ( PLAYLIST_APPEND | PLAYLIST_GO )
450 : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
451 PLAYLIST_END, true, false );
452 RecentsMRL::getInstance( p_intf )->addRecent( url );
460 * pl helps you to choose from playlist or media library,
461 * go to start or enqueue
463 static void openDirectory( intf_thread_t *p_intf, bool pl, bool go )
465 QString dir = QFileDialog::getExistingDirectory( NULL, qtr("Open Directory") );
469 QString mrl = dir.endsWith( "VIDEO_TS", Qt::CaseInsensitive ) ?
470 "dvd://" : "directory://" + toNativeSeparators( dir );
471 input_item_t *p_input = input_item_New( THEPL, qtu( mrl ), NULL );
473 /* FIXME: playlist_AddInput() can fail */
474 playlist_AddInput( THEPL, p_input,
475 go ? ( PLAYLIST_APPEND | PLAYLIST_GO ) : PLAYLIST_APPEND,
476 PLAYLIST_END, pl, pl_Unlocked );
477 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
479 input_Read( THEPL, p_input, true );
480 vlc_gc_decref( p_input );
484 void DialogsProvider::PLOpenDir()
486 openDirectory( p_intf, true, true );
489 void DialogsProvider::PLAppendDir()
491 openDirectory( p_intf, true, false );
494 void DialogsProvider::MLAppendDir()
496 openDirectory( p_intf, false , false );
502 void DialogsProvider::openAPlaylist()
504 QStringList files = showSimpleOpen( qtr( "Open playlist..." ),
505 EXT_FILTER_PLAYLIST );
506 foreach( const QString &file, files )
508 playlist_Import( THEPL, qtu( toNativeSeparators( file ) ) );
512 void DialogsProvider::saveAPlaylist()
514 QFileDialog *qfd = new QFileDialog( NULL,
515 qtr( "Save playlist as..." ),
516 p_intf->p_sys->filepath,
517 qtr( "XSPF playlist (*.xspf);; " ) +
518 qtr( "M3U playlist (*.m3u);; " ) +
519 qtr( "HTML playlist (*.html)" ) );
520 qfd->setFileMode( QFileDialog::AnyFile );
521 qfd->setAcceptMode( QFileDialog::AcceptSave );
522 qfd->setConfirmOverwrite( true );
524 if( qfd->exec() == QDialog::Accepted )
526 if( qfd->selectedFiles().count() > 0 )
528 static const char psz_xspf[] = "export-xspf",
529 psz_m3u[] = "export-m3u",
530 psz_html[] = "export-html";
531 const char *psz_module;
533 QString file = qfd->selectedFiles().first();
534 QString filter = qfd->selectedFilter();
536 if( file.contains( ".xsp" ) || filter.contains( "XSPF" ) )
538 psz_module = psz_xspf;
539 if( !file.contains( ".xsp" ) )
540 file.append( ".xspf" );
542 else if( file.contains( ".m3u" ) || filter.contains( "M3U" ) )
544 psz_module = psz_m3u;
545 if( !file.contains( ".m3u" ) )
546 file.append( ".m3u" );
548 else if( file.contains(".html" ) || filter.contains( "HTML" ) )
550 psz_module = psz_html;
551 if( !file.contains( "html" ) )
552 file.append( ".html" );
556 msg_Err( p_intf, "Impossible to recognise the file type" );
561 playlist_Export( THEPL, qtu( toNativeSeparators( file ) ),
562 THEPL->p_local_category, psz_module);
568 /****************************************************************************
570 ****************************************************************************/
572 void DialogsProvider::streamingDialog( QWidget *parent,
574 bool b_transcode_only,
575 QStringList options )
577 char *psz_soutoption;
580 if( !b_transcode_only )
582 SoutDialog *s = new SoutDialog( parent, p_intf, mrl );
583 if( s->exec() == QDialog::Accepted )
585 psz_soutoption = strdup( qtu( s->getMrl() ) );
594 ConvertDialog *s = new ConvertDialog( parent, p_intf, mrl );
595 if( s->exec() == QDialog::Accepted )
597 psz_soutoption = strdup( qtu( s->getMrl() ) );
607 if( !EMPTY_STR( psz_soutoption ) )
609 options += QString( psz_soutoption ).split( " :");
612 input_item_t *p_input;
613 p_input = input_item_New( p_intf, qtu( mrl ), _("Streaming") );
615 /* Add normal Options */
616 for( int j = 0; j < options.size(); j++ )
618 QString qs = colon_unescape( options[j] );
621 input_item_AddOption( p_input, qtu( qs ),
622 VLC_INPUT_OPTION_TRUSTED );
623 msg_Dbg( p_intf, "Adding option: %s", qtu( qs ) );
627 /* Switch between enqueuing and starting the item */
628 /* FIXME: playlist_AddInput() can fail */
629 playlist_AddInput( THEPL, p_input,
630 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END, true, pl_Unlocked );
631 vlc_gc_decref( p_input );
633 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
635 free( psz_soutoption );
638 void DialogsProvider::openAndStreamingDialogs()
640 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false, OPEN_AND_STREAM )
641 ->showTab( OPEN_FILE_TAB );
644 void DialogsProvider::openAndTranscodingDialogs()
646 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, false, OPEN_AND_SAVE )
647 ->showTab( OPEN_FILE_TAB );
650 void DialogsProvider::loadSubtitlesFile()
652 input_thread_t *p_input = THEMIM->getInput();
653 if( !p_input ) return;
655 input_item_t *p_item = input_GetItem( p_input );
656 if( !p_item ) return;
658 char *path = input_item_GetURI( p_item );
659 if( !path ) path = strdup( "" );
661 char *sep = strrchr( path, DIR_SEP_CHAR );
662 if( sep ) *sep = '\0';
664 QStringList qsl = showSimpleOpen( qtr( "Open subtitles..." ),
668 foreach( const QString &qsFile, qsl )
670 if( input_AddSubtitle( p_input, qtu( toNativeSeparators( qsFile ) ),
672 msg_Warn( p_intf, "unable to load subtitles from '%s'",
678 /****************************************************************************
680 ****************************************************************************/
682 void DialogsProvider::menuAction( QObject *data )
684 QVLCMenu::DoAction( data );
687 void DialogsProvider::menuUpdateAction( QObject *data )
689 MenuFunc *func = qobject_cast<MenuFunc *>(data);
691 func->doFunc( p_intf );
694 void DialogsProvider::SDMenuAction( QString data )
696 char *psz_sd = strdup( qtu( data ) );
697 if( !playlist_IsServicesDiscoveryLoaded( THEPL, psz_sd ) )
698 playlist_ServicesDiscoveryAdd( THEPL, psz_sd );
700 playlist_ServicesDiscoveryRemove( THEPL, psz_sd );
705 * Play the MRL contained in the Recently played menu.
707 void DialogsProvider::playMRL( const QString &mrl )
709 playlist_Add( THEPL, qtu( mrl ) , NULL,
710 PLAYLIST_APPEND | PLAYLIST_GO , PLAYLIST_END, true, false );
712 RecentsMRL::getInstance( p_intf )->addRecent( mrl );