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/openurl.hpp"
49 #include "dialogs/help.hpp"
50 #include "dialogs/gototime.hpp"
51 #include "dialogs/podcast_configuration.hpp"
52 #include "dialogs/toolbar.hpp"
53 #include "dialogs/plugins.hpp"
54 #include "dialogs/interaction.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 msg_Dbg( p_intf, "Destroying the Dialog Provider" );
84 PlaylistDialog::killInstance();
85 MediaInfoDialog::killInstance();
86 MessagesDialog::killInstance();
87 ExtendedDialog::killInstance();
88 BookmarksDialog::killInstance();
89 HelpDialog::killInstance();
91 UpdateDialog::killInstance();
95 delete menusUpdateMapper;
99 void DialogsProvider::quit()
102 vlc_object_kill( p_intf->p_libvlc );
105 void DialogsProvider::customEvent( QEvent *event )
107 if( event->type() == (int)DialogEvent_Type )
109 DialogEvent *de = static_cast<DialogEvent*>(event);
110 switch( de->i_dialog )
112 case INTF_DIALOG_FILE_SIMPLE:
113 case INTF_DIALOG_FILE:
115 case INTF_DIALOG_FILE_GENERIC:
116 openFileGenericDialog( de->p_arg ); break;
117 case INTF_DIALOG_DISC:
118 openDiscDialog(); break;
119 case INTF_DIALOG_NET:
120 openNetDialog(); break;
121 case INTF_DIALOG_SAT:
122 case INTF_DIALOG_CAPTURE:
123 openCaptureDialog(); break;
124 case INTF_DIALOG_DIRECTORY:
125 PLAppendDir(); break;
126 case INTF_DIALOG_PLAYLIST:
127 playlistDialog(); break;
128 case INTF_DIALOG_MESSAGES:
129 messagesDialog(); break;
130 case INTF_DIALOG_FILEINFO:
131 mediaInfoDialog(); break;
132 case INTF_DIALOG_PREFS:
133 prefsDialog(); break;
134 case INTF_DIALOG_BOOKMARKS:
135 bookmarksDialog(); break;
136 case INTF_DIALOG_EXTENDED:
137 extendedDialog(); break;
139 case INTF_DIALOG_VLM:
142 case INTF_DIALOG_INTERACTION:
143 doInteraction( de->p_arg ); break;
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::getInstance( p_intf )->toggleVisible();
180 void DialogsProvider::extendedDialog()
182 ExtendedDialog::getInstance( p_intf )->toggleVisible();
185 void DialogsProvider::messagesDialog()
187 MessagesDialog::getInstance( p_intf )->toggleVisible();
190 void DialogsProvider::gotoTimeDialog()
192 GotoTimeDialog::getInstance( p_intf )->toggleVisible();
196 void DialogsProvider::vlmDialog()
198 VLMDialog::getInstance( p_intf )->toggleVisible();
202 void DialogsProvider::helpDialog()
204 HelpDialog::getInstance( p_intf )->toggleVisible();
208 void DialogsProvider::updateDialog()
210 UpdateDialog::getInstance( p_intf )->toggleVisible();
214 void DialogsProvider::aboutDialog()
216 AboutDialog::getInstance( p_intf )->toggleVisible();
219 void DialogsProvider::mediaInfoDialog()
221 MediaInfoDialog::getInstance( p_intf )->toggleVisible();
224 void DialogsProvider::mediaCodecDialog()
226 MediaInfoDialog::getInstance( p_intf )->showTab( 2 );
229 void DialogsProvider::bookmarksDialog()
231 BookmarksDialog::getInstance( p_intf )->toggleVisible();
234 void DialogsProvider::podcastConfigureDialog()
236 PodcastConfigDialog::getInstance( p_intf )->toggleVisible();
239 void DialogsProvider::toolbarDialog()
241 ToolbarEditDialog::getInstance( p_intf )->toggleVisible();
244 void DialogsProvider::pluginDialog()
246 PluginDialog *diag = new PluginDialog( p_intf );
250 /* Generic open file */
251 void DialogsProvider::openFileGenericDialog( intf_dialog_args_t *p_arg )
255 msg_Warn( p_intf, "openFileGenericDialog() called with NULL arg" );
259 /* Replace the extensions to a Qt format */
261 QString extensions = qfu( p_arg->psz_extensions );
262 while ( ( i = extensions.indexOf( "|", i ) ) != -1 )
264 if( ( extensions.count( "|" ) % 2 ) == 0 )
265 extensions.replace( i, 1, ");;" );
267 extensions.replace( i, 1, "(" );
269 extensions.replace(QString(";*"), QString(" *"));
270 extensions.append( ")" );
275 QString file = QFileDialog::getSaveFileName( NULL, p_arg->psz_title,
276 qfu( p_intf->p_sys->psz_filepath ), extensions );
277 if( !file.isEmpty() )
279 p_arg->i_results = 1;
280 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
281 p_arg->psz_results[0] = strdup( qtu( toNativeSepNoSlash( file ) ) );
284 p_arg->i_results = 0;
286 else /* non-save mode */
288 QStringList files = QFileDialog::getOpenFileNames( NULL,
289 p_arg->psz_title, qfu( p_intf->p_sys->psz_filepath ),
291 p_arg->i_results = files.count();
292 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
294 foreach( QString file, files )
295 p_arg->psz_results[i++] = strdup( qtu( toNativeSepNoSlash( file ) ) );
299 if( p_arg->pf_callback )
300 p_arg->pf_callback( p_arg );
302 /* Clean afterwards */
303 if( p_arg->psz_results )
305 for( i = 0; i < p_arg->i_results; i++ )
306 free( p_arg->psz_results[i] );
307 free( p_arg->psz_results );
309 free( p_arg->psz_title );
310 free( p_arg->psz_extensions );
313 /****************************************************************************
314 * All the open/add stuff
315 * Open Dialog first - Simple Open then
316 ****************************************************************************/
318 void DialogsProvider::openDialog( int i_tab )
320 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf )->showTab( i_tab );
322 void DialogsProvider::openDialog()
324 openDialog( OPEN_FILE_TAB );
326 void DialogsProvider::openFileDialog()
328 openDialog( OPEN_FILE_TAB );
330 void DialogsProvider::openDiscDialog()
332 openDialog( OPEN_DISC_TAB );
334 void DialogsProvider::openNetDialog()
336 openDialog( OPEN_NETWORK_TAB );
338 void DialogsProvider::openCaptureDialog()
340 openDialog( OPEN_CAPTURE_TAB );
343 /* Same as the open one, but force the enqueue */
344 void DialogsProvider::PLAppendDialog()
346 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
347 OPEN_AND_ENQUEUE )->showTab( OPEN_FILE_TAB );
350 void DialogsProvider::MLAppendDialog()
352 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
353 OPEN_AND_ENQUEUE, false, false )
354 ->showTab( OPEN_FILE_TAB );
360 QStringList DialogsProvider::showSimpleOpen( QString help,
364 QString fileTypes = "";
365 if( filters & EXT_FILTER_MEDIA ) {
366 ADD_FILTER_MEDIA( fileTypes );
368 if( filters & EXT_FILTER_VIDEO ) {
369 ADD_FILTER_VIDEO( fileTypes );
371 if( filters & EXT_FILTER_AUDIO ) {
372 ADD_FILTER_AUDIO( fileTypes );
374 if( filters & EXT_FILTER_PLAYLIST ) {
375 ADD_FILTER_PLAYLIST( fileTypes );
377 if( filters & EXT_FILTER_SUBTITLE ) {
378 ADD_FILTER_SUBTITLE( fileTypes );
380 ADD_FILTER_ALL( fileTypes );
381 fileTypes.replace(QString(";*"), QString(" *"));
383 return QFileDialog::getOpenFileNames( NULL,
384 help.isEmpty() ? qtr(I_OP_SEL_FILES ) : help,
385 path.isEmpty() ? qfu( p_intf->p_sys->psz_filepath ) : path,
391 * pl helps you to choose from playlist or media library,
392 * go to start or enqueue
394 void DialogsProvider::addFromSimple( bool pl, bool go)
396 QStringList files = DialogsProvider::showSimpleOpen();
398 foreach( QString file, files )
400 playlist_Add( THEPL, qtu( toNativeSeparators( file ) ), NULL,
401 go ? ( PLAYLIST_APPEND | ( i ? 0 : PLAYLIST_GO ) |
402 ( i ? PLAYLIST_PREPARSE : 0 ) )
403 : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
405 pl ? true : false, false );
406 RecentsMRL::getInstance( p_intf )->addRecent(
407 toNativeSeparators( file ) );
412 void DialogsProvider::simpleOpenDialog()
414 addFromSimple( true, true ); /* Playlist and Go */
417 void DialogsProvider::simplePLAppendDialog()
419 addFromSimple( true, false );
422 void DialogsProvider::simpleMLAppendDialog()
424 addFromSimple( false, false );
427 /* Url & Clipboard */
430 * If the clipboard contains URLs, the first is automatically 'preselected'.
432 void DialogsProvider::openUrlDialog()
434 OpenUrlDialog *oud = OpenUrlDialog::getInstance( p_intf->p_sys->p_mi,
436 if( oud->exec() == QDialog::Accepted )
438 QString url = oud->url();
441 playlist_Add( THEPL, qtu( toNativeSeparators( url ) ),
442 NULL, !oud->shouldEnqueue() ?
443 ( PLAYLIST_APPEND | PLAYLIST_GO )
444 : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
445 PLAYLIST_END, true, false );
446 RecentsMRL::getInstance( p_intf )->addRecent(
447 toNativeSeparators( url ) );
455 * pl helps you to choose from playlist or media library,
456 * go to start or enqueue
458 static void openDirectory( intf_thread_t *p_intf, bool pl, bool go )
460 QString dir = QFileDialog::getExistingDirectory( NULL, qtr("Open Directory") );
464 QString mrl = dir.endsWith( "VIDEO_TS", Qt::CaseInsensitive ) ?
465 "dvd://" : "directory://" + toNativeSeparators( dir );
466 input_item_t *p_input = input_item_New( THEPL, qtu( mrl ), NULL );
468 /* FIXME: playlist_AddInput() can fail */
469 playlist_AddInput( THEPL, p_input,
470 go ? ( PLAYLIST_APPEND | PLAYLIST_GO ) : PLAYLIST_APPEND,
471 PLAYLIST_END, pl, pl_Unlocked );
472 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
474 input_Read( THEPL, p_input, true );
475 vlc_gc_decref( p_input );
479 void DialogsProvider::PLOpenDir()
481 openDirectory( p_intf, true, true );
484 void DialogsProvider::PLAppendDir()
486 openDirectory( p_intf, true, false );
489 void DialogsProvider::MLAppendDir()
491 openDirectory( p_intf, false , false );
497 void DialogsProvider::openAPlaylist()
499 QStringList files = showSimpleOpen( qtr( "Open playlist..." ),
500 EXT_FILTER_PLAYLIST );
501 foreach( QString file, files )
503 playlist_Import( THEPL, qtu( toNativeSeparators( file ) ) );
507 void DialogsProvider::saveAPlaylist()
509 QFileDialog *qfd = new QFileDialog( NULL,
510 qtr( "Save playlist as..." ),
511 qfu( p_intf->p_sys->psz_filepath ),
512 qtr( "XSPF playlist (*.xspf);; " ) +
513 qtr( "M3U playlist (*.m3u);; " ) +
514 qtr( "HTML playlist (*.html);;" ) +
515 qtr( "Any (*.*) " ) );
516 qfd->setFileMode( QFileDialog::AnyFile );
517 qfd->setAcceptMode( QFileDialog::AcceptSave );
518 qfd->setConfirmOverwrite( true );
520 if( qfd->exec() == QDialog::Accepted )
522 if( qfd->selectedFiles().count() > 0 )
524 static const char psz_xspf[] = "export-xspf",
525 psz_m3u[] = "export-m3u",
526 psz_html[] = "export-html";
527 const char *psz_module;
529 QString file = qfd->selectedFiles().first();
530 QString filter = qfd->selectedFilter();
532 if( file.contains( ".xsp" ) || filter.contains( "XSPF" ) )
534 psz_module = psz_xspf;
535 if( !file.contains( ".xsp" ) )
536 file.append( ".xspf" );
538 else if( file.contains( ".m3u" ) || filter.contains( "M3U" ) )
540 psz_module = psz_m3u;
541 if( !file.contains( ".m3u" ) )
542 file.append( ".m3u" );
544 else if( file.contains(".html" ) || filter.contains( "HTML" ) )
546 psz_module = psz_html;
547 if( !file.contains( "html" ) )
548 file.append( ".html" );
552 msg_Err( p_intf, "Impossible to recognise the file type" );
557 playlist_Export( THEPL, qtu( toNativeSeparators( file ) ),
558 THEPL->p_local_category, psz_module);
564 /****************************************************************************
566 ****************************************************************************/
568 void DialogsProvider::streamingDialog( QWidget *parent, QString mrl,
569 bool b_transcode_only )
571 SoutDialog *s = SoutDialog::getInstance( parent, p_intf, b_transcode_only );
573 if( s->exec() == QDialog::Accepted )
575 const char *psz_option = qtu( s->getMrl() );
577 msg_Dbg( p_intf, "Sout mrl %s", psz_option );
578 playlist_AddExt( THEPL, qtu( mrl ), "Streaming",
579 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END,
580 -1, 1, &psz_option, VLC_INPUT_OPTION_TRUSTED, true, pl_Unlocked );
581 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
585 void DialogsProvider::openAndStreamingDialogs()
587 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false, OPEN_AND_STREAM )
588 ->showTab( OPEN_FILE_TAB );
591 void DialogsProvider::openAndTranscodingDialogs()
593 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, false, OPEN_AND_SAVE )
594 ->showTab( OPEN_FILE_TAB );
597 void DialogsProvider::loadSubtitlesFile()
599 input_thread_t *p_input = THEMIM->getInput();
600 if( !p_input ) return;
602 input_item_t *p_item = input_GetItem( p_input );
603 if( !p_item ) return;
605 char *path = input_item_GetURI( p_item );
606 if( !path ) path = strdup( "" );
608 char *sep = strrchr( path, DIR_SEP_CHAR );
609 if( sep ) *sep = '\0';
611 QStringList qsl = showSimpleOpen( qtr( "Open subtitles..." ),
616 foreach( qsFile, qsl )
618 if( input_AddSubtitle( p_input, qtu( toNativeSeparators( qsFile ) ),
620 msg_Warn( p_intf, "unable to load subtitles from '%s'",
626 /****************************************************************************
628 ****************************************************************************/
630 void DialogsProvider::menuAction( QObject *data )
632 QVLCMenu::DoAction( data );
635 void DialogsProvider::menuUpdateAction( QObject *data )
637 MenuFunc *func = qobject_cast<MenuFunc *>(data);
639 func->doFunc( p_intf );
642 void DialogsProvider::SDMenuAction( QString data )
644 const char *psz_sd = qtu( data );
645 if( !playlist_IsServicesDiscoveryLoaded( THEPL, psz_sd ) )
646 playlist_ServicesDiscoveryAdd( THEPL, psz_sd );
648 playlist_ServicesDiscoveryRemove( THEPL, psz_sd );
652 * Play the MRL contained in the Recently played menu.
654 void DialogsProvider::playMRL( const QString &mrl )
656 playlist_Add( THEPL, qtu( mrl ) , NULL,
657 PLAYLIST_APPEND | PLAYLIST_GO , PLAYLIST_END, true, false );
659 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
662 /*************************************
664 *************************************/
665 void DialogsProvider::doInteraction( intf_dialog_args_t *p_arg )
667 InteractionDialog *qdialog;
668 interaction_dialog_t *p_dialog = p_arg->p_dialog;
669 switch( p_dialog->i_action )
672 qdialog = new InteractionDialog( p_intf, p_dialog );
673 p_dialog->p_private = (void*)qdialog;
674 if( !(p_dialog->i_status == ANSWERED_DIALOG) )
677 case INTERACT_UPDATE:
678 qdialog = (InteractionDialog*)(p_dialog->p_private);
683 /* The INTERACT_NEW message was forgotten
684 so we must create the dialog and update it*/
685 qdialog = new InteractionDialog( p_intf, p_dialog );
686 p_dialog->p_private = (void*)qdialog;
687 if( !(p_dialog->i_status == ANSWERED_DIALOG) )
694 msg_Dbg( p_intf, "Hide the Interaction Dialog" );
695 qdialog = (InteractionDialog*)(p_dialog->p_private);
698 p_dialog->i_status = HIDDEN_DIALOG;
700 case INTERACT_DESTROY:
701 msg_Dbg( p_intf, "Destroy the Interaction Dialog" );
702 qdialog = (InteractionDialog*)(p_dialog->p_private);
703 if( !p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
705 p_dialog->i_status = DESTROYED_DIALOG;