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/open.hpp"
48 #include "dialogs/help.hpp"
49 #include "dialogs/gototime.hpp"
50 #include "dialogs/podcast_configuration.hpp"
51 #include "dialogs/toolbar.hpp"
52 #include "dialogs/plugins.hpp"
53 #include "dialogs/interaction.hpp"
56 #include <QApplication>
57 #include <QSignalMapper>
58 #include <QFileDialog>
61 DialogsProvider* DialogsProvider::instance = NULL;
63 DialogsProvider::DialogsProvider( intf_thread_t *_p_intf ) :
64 QObject( NULL ), p_intf( _p_intf )
68 /* Various signal mappers for the menus */
69 menusMapper = new QSignalMapper();
70 CONNECT( menusMapper, mapped(QObject *), this, menuAction( QObject *) );
72 menusUpdateMapper = new QSignalMapper();
73 CONNECT( menusUpdateMapper, mapped(QObject *),
74 this, menuUpdateAction( QObject *) );
76 SDMapper = new QSignalMapper();
77 CONNECT( SDMapper, mapped (QString), this, SDMenuAction( QString ) );
80 DialogsProvider::~DialogsProvider()
82 msg_Dbg( p_intf, "Destroying the Dialog Provider" );
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 vlc_object_kill( 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_INTERACTION:
142 doInteraction( de->p_arg ); break;
143 case INTF_DIALOG_POPUPMENU:
144 QVLCMenu::PopupMenu( p_intf, (de->i_arg != 0) ); break;
145 case INTF_DIALOG_AUDIOPOPUPMENU:
146 QVLCMenu::AudioPopupMenu( p_intf ); break;
147 case INTF_DIALOG_VIDEOPOPUPMENU:
148 QVLCMenu::VideoPopupMenu( p_intf ); break;
149 case INTF_DIALOG_MISCPOPUPMENU:
150 QVLCMenu::MiscPopupMenu( p_intf ); break;
151 case INTF_DIALOG_WIZARD:
152 case INTF_DIALOG_STREAMWIZARD:
153 openAndStreamingDialogs(); break;
155 case INTF_DIALOG_UPDATEVLC:
156 updateDialog(); break;
158 case INTF_DIALOG_EXIT:
161 msg_Warn( p_intf, "unimplemented dialog" );
166 /****************************************************************************
167 * Individual simple dialogs
168 ****************************************************************************/
169 void DialogsProvider::playlistDialog()
171 PlaylistDialog::getInstance( p_intf )->toggleVisible();
174 void DialogsProvider::prefsDialog()
176 PrefsDialog::getInstance( p_intf )->toggleVisible();
179 void DialogsProvider::extendedDialog()
181 ExtendedDialog::getInstance( p_intf )->toggleVisible();
184 void DialogsProvider::messagesDialog()
186 MessagesDialog::getInstance( p_intf )->toggleVisible();
189 void DialogsProvider::gotoTimeDialog()
191 GotoTimeDialog::getInstance( p_intf )->toggleVisible();
195 void DialogsProvider::vlmDialog()
197 VLMDialog::getInstance( p_intf )->toggleVisible();
201 void DialogsProvider::helpDialog()
203 HelpDialog::getInstance( p_intf )->toggleVisible();
207 void DialogsProvider::updateDialog()
209 UpdateDialog::getInstance( p_intf )->toggleVisible();
213 void DialogsProvider::aboutDialog()
215 AboutDialog::getInstance( p_intf )->toggleVisible();
218 void DialogsProvider::mediaInfoDialog()
220 MediaInfoDialog::getInstance( p_intf )->toggleVisible();
223 void DialogsProvider::mediaCodecDialog()
225 MediaInfoDialog::getInstance( p_intf )->showTab( 2 );
228 void DialogsProvider::bookmarksDialog()
230 BookmarksDialog::getInstance( p_intf )->toggleVisible();
233 void DialogsProvider::podcastConfigureDialog()
235 PodcastConfigDialog::getInstance( p_intf )->toggleVisible();
238 void DialogsProvider::toolbarDialog()
240 ToolbarEditDialog::getInstance( p_intf )->toggleVisible();
243 void DialogsProvider::pluginDialog()
245 PluginDialog *diag = new PluginDialog( p_intf );
249 /* Generic open file */
250 void DialogsProvider::openFileGenericDialog( intf_dialog_args_t *p_arg )
254 msg_Warn( p_intf, "openFileGenericDialog() called with NULL arg" );
258 /* Replace the extensions to a Qt format */
260 QString extensions = qfu( p_arg->psz_extensions );
261 while ( ( i = extensions.indexOf( "|", i ) ) != -1 )
263 if( ( extensions.count( "|" ) % 2 ) == 0 )
264 extensions.replace( i, 1, ");;" );
266 extensions.replace( i, 1, "(" );
268 extensions.replace(QString(";*"), QString(" *"));
269 extensions.append( ")" );
274 QString file = QFileDialog::getSaveFileName( NULL, p_arg->psz_title,
275 qfu( p_intf->p_sys->psz_filepath ), extensions );
276 if( !file.isEmpty() )
278 p_arg->i_results = 1;
279 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
280 p_arg->psz_results[0] = strdup( qtu( toNativeSepNoSlash( file ) ) );
283 p_arg->i_results = 0;
285 else /* non-save mode */
287 QStringList files = QFileDialog::getOpenFileNames( NULL,
288 p_arg->psz_title, qfu( p_intf->p_sys->psz_filepath ),
290 p_arg->i_results = files.count();
291 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
293 foreach( QString file, files )
294 p_arg->psz_results[i++] = strdup( qtu( toNativeSepNoSlash( file ) ) );
298 if( p_arg->pf_callback )
299 p_arg->pf_callback( p_arg );
301 /* Clean afterwards */
302 if( p_arg->psz_results )
304 for( i = 0; i < p_arg->i_results; i++ )
305 free( p_arg->psz_results[i] );
306 free( p_arg->psz_results );
308 free( p_arg->psz_title );
309 free( p_arg->psz_extensions );
312 /****************************************************************************
313 * All the open/add stuff
314 * Open Dialog first - Simple Open then
315 ****************************************************************************/
317 void DialogsProvider::openDialog( int i_tab )
319 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf )->showTab( i_tab );
321 void DialogsProvider::openDialog()
323 openDialog( OPEN_FILE_TAB );
325 void DialogsProvider::openFileDialog()
327 openDialog( OPEN_FILE_TAB );
329 void DialogsProvider::openDiscDialog()
331 openDialog( OPEN_DISC_TAB );
333 void DialogsProvider::openNetDialog()
335 openDialog( OPEN_NETWORK_TAB );
337 void DialogsProvider::openCaptureDialog()
339 openDialog( OPEN_CAPTURE_TAB );
342 /* Same as the open one, but force the enqueue */
343 void DialogsProvider::PLAppendDialog()
345 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
346 OPEN_AND_ENQUEUE )->showTab( OPEN_FILE_TAB );
349 void DialogsProvider::MLAppendDialog()
351 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
352 OPEN_AND_ENQUEUE, false, false )
353 ->showTab( OPEN_FILE_TAB );
359 QStringList DialogsProvider::showSimpleOpen( QString help,
363 QString fileTypes = "";
364 if( filters & EXT_FILTER_MEDIA ) {
365 ADD_FILTER_MEDIA( fileTypes );
367 if( filters & EXT_FILTER_VIDEO ) {
368 ADD_FILTER_VIDEO( fileTypes );
370 if( filters & EXT_FILTER_AUDIO ) {
371 ADD_FILTER_AUDIO( fileTypes );
373 if( filters & EXT_FILTER_PLAYLIST ) {
374 ADD_FILTER_PLAYLIST( fileTypes );
376 if( filters & EXT_FILTER_SUBTITLE ) {
377 ADD_FILTER_SUBTITLE( fileTypes );
379 ADD_FILTER_ALL( fileTypes );
380 fileTypes.replace(QString(";*"), QString(" *"));
382 return QFileDialog::getOpenFileNames( NULL,
383 help.isEmpty() ? qtr(I_OP_SEL_FILES ) : help,
384 path.isEmpty() ? qfu( p_intf->p_sys->psz_filepath ) : path,
390 * pl helps you to choose from playlist or media library,
391 * go to start or enqueue
393 void DialogsProvider::addFromSimple( bool pl, bool go)
395 QStringList files = DialogsProvider::showSimpleOpen();
397 foreach( QString file, files )
399 playlist_Add( THEPL, qtu( toNativeSeparators( file ) ), NULL,
400 go ? ( PLAYLIST_APPEND | ( i ? 0 : PLAYLIST_GO ) |
401 ( i ? PLAYLIST_PREPARSE : 0 ) )
402 : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
404 pl ? true : false, false );
405 RecentsMRL::getInstance( p_intf )->addRecent(
406 toNativeSeparators( file ) );
411 void DialogsProvider::simpleOpenDialog()
413 addFromSimple( true, true ); /* Playlist and Go */
416 void DialogsProvider::simplePLAppendDialog()
418 addFromSimple( true, false );
421 void DialogsProvider::simpleMLAppendDialog()
423 addFromSimple( false, false );
429 * pl helps you to choose from playlist or media library,
430 * go to start or enqueue
432 static void openDirectory( intf_thread_t *p_intf, bool pl, bool go )
434 QString dir = QFileDialog::getExistingDirectory( NULL, qtr("Open Directory") );
438 QString mrl = dir.endsWith( "VIDEO_TS", Qt::CaseInsensitive ) ?
439 "dvd://" : "directory://" + toNativeSeparators( dir );
440 input_item_t *p_input = input_item_NewExt( THEPL, qtu( mrl ),
443 /* FIXME: playlist_AddInput() can fail */
444 playlist_AddInput( THEPL, p_input,
445 go ? ( PLAYLIST_APPEND | PLAYLIST_GO ) : PLAYLIST_APPEND,
446 PLAYLIST_END, pl, pl_Unlocked );
447 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
449 input_Read( THEPL, p_input, true );
450 vlc_gc_decref( p_input );
454 void DialogsProvider::PLOpenDir()
456 openDirectory( p_intf, true, true );
459 void DialogsProvider::PLAppendDir()
461 openDirectory( p_intf, true, false );
464 void DialogsProvider::MLAppendDir()
466 openDirectory( p_intf, false , false );
472 void DialogsProvider::openAPlaylist()
474 QStringList files = showSimpleOpen( qtr( "Open playlist..." ),
475 EXT_FILTER_PLAYLIST );
476 foreach( QString file, files )
478 playlist_Import( THEPL, qtu( toNativeSeparators( file ) ) );
482 void DialogsProvider::saveAPlaylist()
484 QFileDialog *qfd = new QFileDialog( NULL,
485 qtr( "Save playlist as..." ),
486 qfu( p_intf->p_sys->psz_filepath ),
487 qtr( "XSPF playlist (*.xspf);; " ) +
488 qtr( "M3U playlist (*.m3u);; " ) +
489 qtr( "HTML playlist (*.html);;" ) +
490 qtr( "Any (*.*) " ) );
491 qfd->setFileMode( QFileDialog::AnyFile );
492 qfd->setAcceptMode( QFileDialog::AcceptSave );
493 qfd->setConfirmOverwrite( true );
495 if( qfd->exec() == QDialog::Accepted )
497 if( qfd->selectedFiles().count() > 0 )
499 static const char psz_xspf[] = "export-xspf",
500 psz_m3u[] = "export-m3u",
501 psz_html[] = "export-html";
502 const char *psz_module;
504 QString file = qfd->selectedFiles().first();
505 QString filter = qfd->selectedFilter();
507 if( file.contains( ".xsp" ) || filter.contains( "XSPF" ) )
509 psz_module = psz_xspf;
510 if( !file.contains( ".xsp" ) )
511 file.append( ".xspf" );
513 else if( file.contains( ".m3u" ) || filter.contains( "M3U" ) )
515 psz_module = psz_m3u;
516 if( !file.contains( ".m3u" ) )
517 file.append( ".m3u" );
519 else if( file.contains(".html" ) || filter.contains( "HTML" ) )
521 psz_module = psz_html;
522 if( !file.contains( "html" ) )
523 file.append( ".html" );
527 msg_Err( p_intf, "Impossible to recognise the file type" );
532 playlist_Export( THEPL, qtu( toNativeSeparators( file ) ),
533 THEPL->p_local_category, psz_module);
539 /****************************************************************************
541 ****************************************************************************/
543 void DialogsProvider::streamingDialog( QWidget *parent, QString mrl,
544 bool b_transcode_only )
546 SoutDialog *s = SoutDialog::getInstance( parent, p_intf, b_transcode_only );
548 if( s->exec() == QDialog::Accepted )
550 const char *psz_option = qtu( s->getMrl() );
552 msg_Dbg( p_intf, "Sout mrl %s", psz_option );
553 playlist_AddExt( THEPL, qtu( mrl ), "Streaming",
554 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END,
555 -1, &psz_option, 1, true, pl_Unlocked );
556 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
560 void DialogsProvider::openAndStreamingDialogs()
562 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false, OPEN_AND_STREAM )
563 ->showTab( OPEN_FILE_TAB );
566 void DialogsProvider::openAndTranscodingDialogs()
568 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, false, OPEN_AND_SAVE )
569 ->showTab( OPEN_FILE_TAB );
572 void DialogsProvider::loadSubtitlesFile()
574 input_thread_t *p_input = THEMIM->getInput();
575 if( !p_input ) return;
577 input_item_t *p_item = input_GetItem( p_input );
578 if( !p_item ) return;
580 char *path = input_item_GetURI( p_item );
581 if( !path ) path = strdup( "" );
583 char *sep = strrchr( path, DIR_SEP_CHAR );
584 if( sep ) *sep = '\0';
586 QStringList qsl = showSimpleOpen( qtr( "Open subtitles..." ),
591 foreach( qsFile, qsl )
593 if( input_AddSubtitle( p_input, qtu( toNativeSeparators( qsFile ) ),
595 msg_Warn( p_intf, "unable to load subtitles from '%s'",
601 /****************************************************************************
603 ****************************************************************************/
605 void DialogsProvider::menuAction( QObject *data )
607 QVLCMenu::DoAction( data );
610 void DialogsProvider::menuUpdateAction( QObject *data )
612 MenuFunc *func = qobject_cast<MenuFunc *>(data);
614 func->doFunc( p_intf );
617 void DialogsProvider::SDMenuAction( QString data )
619 const char *psz_sd = qtu( data );
620 if( !playlist_IsServicesDiscoveryLoaded( THEPL, psz_sd ) )
621 playlist_ServicesDiscoveryAdd( THEPL, psz_sd );
623 playlist_ServicesDiscoveryRemove( THEPL, psz_sd );
627 * Play the MRL contained in the Recently played menu.
629 void DialogsProvider::playMRL( const QString &mrl )
631 playlist_Add( THEPL, qtu( mrl ) , NULL,
632 PLAYLIST_APPEND | PLAYLIST_GO , PLAYLIST_END, true, false );
634 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
637 /*************************************
639 *************************************/
640 void DialogsProvider::doInteraction( intf_dialog_args_t *p_arg )
642 InteractionDialog *qdialog;
643 interaction_dialog_t *p_dialog = p_arg->p_dialog;
644 switch( p_dialog->i_action )
647 qdialog = new InteractionDialog( p_intf, p_dialog );
648 p_dialog->p_private = (void*)qdialog;
649 if( !(p_dialog->i_status == ANSWERED_DIALOG) )
652 case INTERACT_UPDATE:
653 qdialog = (InteractionDialog*)(p_dialog->p_private);
658 /* The INTERACT_NEW message was forgotten
659 so we must create the dialog and update it*/
660 qdialog = new InteractionDialog( p_intf, p_dialog );
661 p_dialog->p_private = (void*)qdialog;
662 if( !(p_dialog->i_status == ANSWERED_DIALOG) )
669 msg_Dbg( p_intf, "Hide the Interaction Dialog" );
670 qdialog = (InteractionDialog*)(p_dialog->p_private);
673 p_dialog->i_status = HIDDEN_DIALOG;
675 case INTERACT_DESTROY:
676 msg_Dbg( p_intf, "Destroy the Interaction Dialog" );
677 qdialog = (InteractionDialog*)(p_dialog->p_private);
678 if( !p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
680 p_dialog->i_status = DESTROYED_DIALOG;