1 /*****************************************************************************
2 * dialogs_provider.cpp : Dialog Provider
3 *****************************************************************************
4 * Copyright (C) 2006-2008 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/interaction.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 ) );
82 DialogsProvider::~DialogsProvider()
84 PlaylistDialog::killInstance();
85 MediaInfoDialog::killInstance();
86 MessagesDialog::killInstance();
87 ExtendedDialog::killInstance();
88 BookmarksDialog::killInstance();
89 HelpDialog::killInstance();
91 UpdateDialog::killInstance();
93 ToolbarEditDialog::killInstance();
96 delete menusUpdateMapper;
100 void DialogsProvider::quit()
103 libvlc_Quit( p_intf->p_libvlc );
106 void DialogsProvider::customEvent( QEvent *event )
108 if( event->type() == (int)DialogEvent_Type )
110 DialogEvent *de = static_cast<DialogEvent*>(event);
111 switch( de->i_dialog )
113 case INTF_DIALOG_FILE_SIMPLE:
114 case INTF_DIALOG_FILE:
116 case INTF_DIALOG_FILE_GENERIC:
117 openFileGenericDialog( de->p_arg ); break;
118 case INTF_DIALOG_DISC:
119 openDiscDialog(); break;
120 case INTF_DIALOG_NET:
121 openNetDialog(); break;
122 case INTF_DIALOG_SAT:
123 case INTF_DIALOG_CAPTURE:
124 openCaptureDialog(); break;
125 case INTF_DIALOG_DIRECTORY:
126 PLAppendDir(); break;
127 case INTF_DIALOG_PLAYLIST:
128 playlistDialog(); break;
129 case INTF_DIALOG_MESSAGES:
130 messagesDialog(); break;
131 case INTF_DIALOG_FILEINFO:
132 mediaInfoDialog(); break;
133 case INTF_DIALOG_PREFS:
134 prefsDialog(); break;
135 case INTF_DIALOG_BOOKMARKS:
136 bookmarksDialog(); break;
137 case INTF_DIALOG_EXTENDED:
138 extendedDialog(); break;
140 case INTF_DIALOG_VLM:
143 case INTF_DIALOG_INTERACTION:
144 doInteraction( de->p_arg ); break;
145 case INTF_DIALOG_POPUPMENU:
146 QVLCMenu::PopupMenu( p_intf, (de->i_arg != 0) ); break;
147 case INTF_DIALOG_AUDIOPOPUPMENU:
148 QVLCMenu::AudioPopupMenu( p_intf ); break;
149 case INTF_DIALOG_VIDEOPOPUPMENU:
150 QVLCMenu::VideoPopupMenu( p_intf ); break;
151 case INTF_DIALOG_MISCPOPUPMENU:
152 QVLCMenu::MiscPopupMenu( p_intf ); break;
153 case INTF_DIALOG_WIZARD:
154 case INTF_DIALOG_STREAMWIZARD:
155 openAndStreamingDialogs(); break;
157 case INTF_DIALOG_UPDATEVLC:
158 updateDialog(); break;
160 case INTF_DIALOG_EXIT:
163 msg_Warn( p_intf, "unimplemented dialog" );
168 /****************************************************************************
169 * Individual simple dialogs
170 ****************************************************************************/
171 void DialogsProvider::playlistDialog()
173 PlaylistDialog::getInstance( p_intf )->toggleVisible();
176 void DialogsProvider::prefsDialog()
178 PrefsDialog::getInstance( p_intf )->toggleVisible();
181 void DialogsProvider::extendedDialog()
183 ExtendedDialog::getInstance( p_intf )->toggleVisible();
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 )->toggleVisible();
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::getInstance( p_intf )->toggleVisible();
250 void DialogsProvider::pluginDialog()
252 PluginDialog *diag = new PluginDialog( p_intf );
256 /* Generic open file */
257 void DialogsProvider::openFileGenericDialog( intf_dialog_args_t *p_arg )
261 msg_Warn( p_intf, "openFileGenericDialog() called with NULL arg" );
265 /* Replace the extensions to a Qt format */
267 QString extensions = qfu( p_arg->psz_extensions );
268 while ( ( i = extensions.indexOf( "|", i ) ) != -1 )
270 if( ( extensions.count( "|" ) % 2 ) == 0 )
271 extensions.replace( i, 1, ");;" );
273 extensions.replace( i, 1, "(" );
275 extensions.replace(QString(";*"), QString(" *"));
276 extensions.append( ")" );
281 QString file = QFileDialog::getSaveFileName( NULL, p_arg->psz_title,
282 qfu( p_intf->p_sys->psz_filepath ), extensions );
283 if( !file.isEmpty() )
285 p_arg->i_results = 1;
286 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
287 p_arg->psz_results[0] = strdup( qtu( toNativeSepNoSlash( file ) ) );
290 p_arg->i_results = 0;
292 else /* non-save mode */
294 QStringList files = QFileDialog::getOpenFileNames( NULL,
295 p_arg->psz_title, qfu( p_intf->p_sys->psz_filepath ),
297 p_arg->i_results = files.count();
298 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
300 foreach( const QString &file, files )
301 p_arg->psz_results[i++] = strdup( qtu( toNativeSepNoSlash( file ) ) );
305 if( p_arg->pf_callback )
306 p_arg->pf_callback( p_arg );
308 /* Clean afterwards */
309 if( p_arg->psz_results )
311 for( i = 0; i < p_arg->i_results; i++ )
312 free( p_arg->psz_results[i] );
313 free( p_arg->psz_results );
315 free( p_arg->psz_title );
316 free( p_arg->psz_extensions );
319 /****************************************************************************
320 * All the open/add stuff
321 * Open Dialog first - Simple Open then
322 ****************************************************************************/
324 void DialogsProvider::openDialog( int i_tab )
326 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf )->showTab( i_tab );
328 void DialogsProvider::openDialog()
330 openDialog( OPEN_FILE_TAB );
332 void DialogsProvider::openFileDialog()
334 openDialog( OPEN_FILE_TAB );
336 void DialogsProvider::openDiscDialog()
338 openDialog( OPEN_DISC_TAB );
340 void DialogsProvider::openNetDialog()
342 openDialog( OPEN_NETWORK_TAB );
344 void DialogsProvider::openCaptureDialog()
346 openDialog( OPEN_CAPTURE_TAB );
349 /* Same as the open one, but force the enqueue */
350 void DialogsProvider::PLAppendDialog()
352 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
353 OPEN_AND_ENQUEUE )->showTab( OPEN_FILE_TAB );
356 void DialogsProvider::MLAppendDialog()
358 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
359 OPEN_AND_ENQUEUE, false, false )
360 ->showTab( OPEN_FILE_TAB );
366 QStringList DialogsProvider::showSimpleOpen( QString help,
370 QString fileTypes = "";
371 if( filters & EXT_FILTER_MEDIA ) {
372 ADD_FILTER_MEDIA( fileTypes );
374 if( filters & EXT_FILTER_VIDEO ) {
375 ADD_FILTER_VIDEO( fileTypes );
377 if( filters & EXT_FILTER_AUDIO ) {
378 ADD_FILTER_AUDIO( fileTypes );
380 if( filters & EXT_FILTER_PLAYLIST ) {
381 ADD_FILTER_PLAYLIST( fileTypes );
383 if( filters & EXT_FILTER_SUBTITLE ) {
384 ADD_FILTER_SUBTITLE( fileTypes );
386 ADD_FILTER_ALL( fileTypes );
387 fileTypes.replace(QString(";*"), QString(" *"));
389 return QFileDialog::getOpenFileNames( NULL,
390 help.isEmpty() ? qtr(I_OP_SEL_FILES ) : help,
391 path.isEmpty() ? qfu( p_intf->p_sys->psz_filepath ) : path,
397 * pl helps you to choose from playlist or media library,
398 * go to start or enqueue
400 void DialogsProvider::addFromSimple( bool pl, bool go)
402 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 qfu( p_intf->p_sys->psz_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, QString mrl,
573 bool b_transcode_only )
575 const char *psz_option;
576 if( !b_transcode_only )
578 SoutDialog *s = SoutDialog::getInstance( parent, p_intf, mrl );
579 if( s->exec() == QDialog::Accepted )
580 psz_option = qtu( s->getMrl() );
582 ConvertDialog *s = new ConvertDialog( parent, p_intf, mrl );
583 if( s->exec() == QDialog::Accepted )
584 psz_option = qtu( s->getMrl() );
588 if( !EMPTY_STR( psz_option ) )
591 msg_Dbg( p_intf, "Sout mrl %s", psz_option );
592 playlist_AddExt( THEPL, qtu( mrl ), _("Streaming"),
593 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END,
594 -1, 1, &psz_option, VLC_INPUT_OPTION_TRUSTED, true, pl_Unlocked );
595 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
599 void DialogsProvider::openAndStreamingDialogs()
601 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false, OPEN_AND_STREAM )
602 ->showTab( OPEN_FILE_TAB );
605 void DialogsProvider::openAndTranscodingDialogs()
607 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, false, OPEN_AND_SAVE )
608 ->showTab( OPEN_FILE_TAB );
611 void DialogsProvider::loadSubtitlesFile()
613 input_thread_t *p_input = THEMIM->getInput();
614 if( !p_input ) return;
616 input_item_t *p_item = input_GetItem( p_input );
617 if( !p_item ) return;
619 char *path = input_item_GetURI( p_item );
620 if( !path ) path = strdup( "" );
622 char *sep = strrchr( path, DIR_SEP_CHAR );
623 if( sep ) *sep = '\0';
625 QStringList qsl = showSimpleOpen( qtr( "Open subtitles..." ),
629 foreach( const QString &qsFile, qsl )
631 if( input_AddSubtitle( p_input, qtu( toNativeSeparators( qsFile ) ),
633 msg_Warn( p_intf, "unable to load subtitles from '%s'",
639 /****************************************************************************
641 ****************************************************************************/
643 void DialogsProvider::menuAction( QObject *data )
645 QVLCMenu::DoAction( data );
648 void DialogsProvider::menuUpdateAction( QObject *data )
650 MenuFunc *func = qobject_cast<MenuFunc *>(data);
652 func->doFunc( p_intf );
655 void DialogsProvider::SDMenuAction( QString data )
657 char *psz_sd = strdup( qtu( data ) );
658 if( !playlist_IsServicesDiscoveryLoaded( THEPL, psz_sd ) )
659 playlist_ServicesDiscoveryAdd( THEPL, psz_sd );
661 playlist_ServicesDiscoveryRemove( THEPL, psz_sd );
666 * Play the MRL contained in the Recently played menu.
668 void DialogsProvider::playMRL( const QString &mrl )
670 playlist_Add( THEPL, qtu( mrl ) , NULL,
671 PLAYLIST_APPEND | PLAYLIST_GO , PLAYLIST_END, true, false );
673 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
676 /*************************************
678 *************************************/
679 void DialogsProvider::doInteraction( intf_dialog_args_t *p_arg )
681 InteractionDialog *qdialog;
682 interaction_dialog_t *p_dialog = p_arg->p_dialog;
683 switch( p_dialog->i_action )
686 qdialog = new InteractionDialog( p_intf, p_dialog );
687 p_dialog->p_private = (void*)qdialog;
688 if( !(p_dialog->i_status == ANSWERED_DIALOG) )
691 case INTERACT_UPDATE:
692 qdialog = (InteractionDialog*)(p_dialog->p_private);
697 /* The INTERACT_NEW message was forgotten
698 so we must create the dialog and update it*/
699 qdialog = new InteractionDialog( p_intf, p_dialog );
700 p_dialog->p_private = (void*)qdialog;
701 if( !(p_dialog->i_status == ANSWERED_DIALOG) )
708 msg_Dbg( p_intf, "Hide the Interaction Dialog" );
709 qdialog = (InteractionDialog*)(p_dialog->p_private);
712 p_dialog->i_status = HIDDEN_DIALOG;
714 case INTERACT_DESTROY:
715 msg_Dbg( p_intf, "Destroy the Interaction Dialog" );
716 qdialog = (InteractionDialog*)(p_dialog->p_private);
717 if( !p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
719 p_dialog->i_status = DESTROYED_DIALOG;