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_common.h>
30 #include <vlc_intf_strings.h>
33 #include "dialogs_provider.hpp"
34 #include "main_interface.hpp"
36 #include "input_manager.hpp"
37 #include "recents.hpp"
38 #include "util/qvlcapp.hpp"
41 #include "dialogs/playlist.hpp"
42 #include "dialogs/bookmarks.hpp"
43 #include "dialogs/preferences.hpp"
44 #include "dialogs/mediainfo.hpp"
45 #include "dialogs/messages.hpp"
46 #include "dialogs/extended.hpp"
47 #include "dialogs/vlm.hpp"
48 #include "dialogs/sout.hpp"
49 #include "dialogs/open.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 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()
101 /* Stop the playlist */
102 playlist_Stop( THEPL );
104 vlc_object_kill( p_intf->p_libvlc );
105 QApplication::closeAllWindows();
106 QApplication::quit();
109 void DialogsProvider::customEvent( QEvent *event )
111 if( event->type() == (int)DialogEvent_Type )
113 DialogEvent *de = static_cast<DialogEvent*>(event);
114 switch( de->i_dialog )
116 case INTF_DIALOG_FILE_SIMPLE:
117 case INTF_DIALOG_FILE:
119 case INTF_DIALOG_FILE_GENERIC:
120 openFileGenericDialog( de->p_arg ); break;
121 case INTF_DIALOG_DISC:
122 openDiscDialog(); break;
123 case INTF_DIALOG_NET:
124 openNetDialog(); break;
125 case INTF_DIALOG_SAT:
126 case INTF_DIALOG_CAPTURE:
127 openCaptureDialog(); break;
128 case INTF_DIALOG_DIRECTORY:
129 PLAppendDir(); break;
130 case INTF_DIALOG_PLAYLIST:
131 playlistDialog(); break;
132 case INTF_DIALOG_MESSAGES:
133 messagesDialog(); break;
134 case INTF_DIALOG_FILEINFO:
135 mediaInfoDialog(); break;
136 case INTF_DIALOG_PREFS:
137 prefsDialog(); break;
138 case INTF_DIALOG_BOOKMARKS:
139 bookmarksDialog(); break;
140 case INTF_DIALOG_EXTENDED:
141 extendedDialog(); break;
143 case INTF_DIALOG_VLM:
146 case INTF_DIALOG_INTERACTION:
147 doInteraction( de->p_arg ); break;
148 case INTF_DIALOG_POPUPMENU:
149 QVLCMenu::PopupMenu( p_intf, (de->i_arg != 0) ); break;
150 case INTF_DIALOG_AUDIOPOPUPMENU:
151 QVLCMenu::AudioPopupMenu( p_intf ); break;
152 case INTF_DIALOG_VIDEOPOPUPMENU:
153 QVLCMenu::VideoPopupMenu( p_intf ); break;
154 case INTF_DIALOG_MISCPOPUPMENU:
155 QVLCMenu::MiscPopupMenu( p_intf ); break;
156 case INTF_DIALOG_WIZARD:
157 case INTF_DIALOG_STREAMWIZARD:
158 openAndStreamingDialogs(); break;
160 case INTF_DIALOG_UPDATEVLC:
161 updateDialog(); break;
163 case INTF_DIALOG_EXIT:
166 msg_Warn( p_intf, "unimplemented dialog" );
171 /****************************************************************************
172 * Individual simple dialogs
173 ****************************************************************************/
174 void DialogsProvider::playlistDialog()
176 PlaylistDialog::getInstance( p_intf )->toggleVisible();
179 void DialogsProvider::prefsDialog()
181 PrefsDialog::getInstance( p_intf )->toggleVisible();
184 void DialogsProvider::extendedDialog()
186 ExtendedDialog::getInstance( p_intf )->toggleVisible();
189 void DialogsProvider::messagesDialog()
191 MessagesDialog::getInstance( p_intf )->toggleVisible();
194 void DialogsProvider::gotoTimeDialog()
196 GotoTimeDialog::getInstance( p_intf )->toggleVisible();
200 void DialogsProvider::vlmDialog()
202 VLMDialog::getInstance( p_intf )->toggleVisible();
206 void DialogsProvider::helpDialog()
208 HelpDialog::getInstance( p_intf )->toggleVisible();
212 void DialogsProvider::updateDialog()
214 UpdateDialog::getInstance( p_intf )->toggleVisible();
218 void DialogsProvider::aboutDialog()
220 AboutDialog::getInstance( p_intf )->toggleVisible();
223 void DialogsProvider::mediaInfoDialog()
225 MediaInfoDialog::getInstance( p_intf )->toggleVisible();
228 void DialogsProvider::mediaCodecDialog()
230 MediaInfoDialog::getInstance( p_intf )->showTab( 2 );
233 void DialogsProvider::bookmarksDialog()
235 BookmarksDialog::getInstance( p_intf )->toggleVisible();
238 void DialogsProvider::podcastConfigureDialog()
240 PodcastConfigDialog::getInstance( p_intf )->toggleVisible();
243 void DialogsProvider::toolbarDialog()
245 ToolbarEditDialog::getInstance( p_intf )->toggleVisible();
248 void DialogsProvider::pluginDialog()
250 PluginDialog *diag = new PluginDialog( p_intf );
254 /* Generic open file */
255 void DialogsProvider::openFileGenericDialog( intf_dialog_args_t *p_arg )
259 msg_Warn( p_intf, "openFileGenericDialog() called with NULL arg" );
263 /* Replace the extensions to a Qt format */
265 QString extensions = qfu( p_arg->psz_extensions );
266 while ( ( i = extensions.indexOf( "|", i ) ) != -1 )
268 if( ( extensions.count( "|" ) % 2 ) == 0 )
269 extensions.replace( i, 1, ");;" );
271 extensions.replace( i, 1, "(" );
273 extensions.replace(QString(";*"), QString(" *"));
274 extensions.append( ")" );
279 QString file = QFileDialog::getSaveFileName( NULL, p_arg->psz_title,
280 qfu( p_intf->p_sys->psz_filepath ), extensions );
281 if( !file.isEmpty() )
283 p_arg->i_results = 1;
284 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
285 p_arg->psz_results[0] = strdup( qtu( toNativeSepNoSlash( file ) ) );
288 p_arg->i_results = 0;
290 else /* non-save mode */
292 QStringList files = QFileDialog::getOpenFileNames( NULL,
293 p_arg->psz_title, qfu( p_intf->p_sys->psz_filepath ),
295 p_arg->i_results = files.count();
296 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
298 foreach( QString file, files )
299 p_arg->psz_results[i++] = strdup( qtu( toNativeSepNoSlash( file ) ) );
303 if( p_arg->pf_callback )
304 p_arg->pf_callback( p_arg );
306 /* Clean afterwards */
307 if( p_arg->psz_results )
309 for( i = 0; i < p_arg->i_results; i++ )
310 free( p_arg->psz_results[i] );
311 free( p_arg->psz_results );
313 free( p_arg->psz_title );
314 free( p_arg->psz_extensions );
317 /****************************************************************************
318 * All the open/add stuff
319 * Open Dialog first - Simple Open then
320 ****************************************************************************/
322 void DialogsProvider::openDialog( int i_tab )
324 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf )->showTab( i_tab );
326 void DialogsProvider::openDialog()
328 openDialog( OPEN_FILE_TAB );
330 void DialogsProvider::openFileDialog()
332 openDialog( OPEN_FILE_TAB );
334 void DialogsProvider::openDiscDialog()
336 openDialog( OPEN_DISC_TAB );
338 void DialogsProvider::openNetDialog()
340 openDialog( OPEN_NETWORK_TAB );
342 void DialogsProvider::openCaptureDialog()
344 openDialog( OPEN_CAPTURE_TAB );
347 /* Same as the open one, but force the enqueue */
348 void DialogsProvider::PLAppendDialog()
350 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
351 OPEN_AND_ENQUEUE )->showTab( OPEN_FILE_TAB );
354 void DialogsProvider::MLAppendDialog()
356 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
357 OPEN_AND_ENQUEUE, false, false )
358 ->showTab( OPEN_FILE_TAB );
364 QStringList DialogsProvider::showSimpleOpen( QString help,
368 QString fileTypes = "";
369 if( filters & EXT_FILTER_MEDIA ) {
370 ADD_FILTER_MEDIA( fileTypes );
372 if( filters & EXT_FILTER_VIDEO ) {
373 ADD_FILTER_VIDEO( fileTypes );
375 if( filters & EXT_FILTER_AUDIO ) {
376 ADD_FILTER_AUDIO( fileTypes );
378 if( filters & EXT_FILTER_PLAYLIST ) {
379 ADD_FILTER_PLAYLIST( fileTypes );
381 if( filters & EXT_FILTER_SUBTITLE ) {
382 ADD_FILTER_SUBTITLE( fileTypes );
384 ADD_FILTER_ALL( fileTypes );
385 fileTypes.replace(QString(";*"), QString(" *"));
387 return QFileDialog::getOpenFileNames( NULL,
388 help.isEmpty() ? qtr(I_OP_SEL_FILES ) : help,
389 path.isEmpty() ? qfu( p_intf->p_sys->psz_filepath ) : path,
395 * pl helps you to choose from playlist or media library,
396 * go to start or enqueue
398 void DialogsProvider::addFromSimple( bool pl, bool go)
400 QStringList files = DialogsProvider::showSimpleOpen();
402 foreach( QString file, files )
404 playlist_Add( THEPL, qtu( toNativeSeparators( file ) ), NULL,
405 go ? ( PLAYLIST_APPEND | ( i ? 0 : PLAYLIST_GO ) |
406 ( i ? PLAYLIST_PREPARSE : 0 ) )
407 : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
409 pl ? true : false, false );
410 RecentsMRL::getInstance( p_intf )->addRecent(
411 toNativeSeparators( file ) );
416 void DialogsProvider::simpleOpenDialog()
418 addFromSimple( true, true ); /* Playlist and Go */
421 void DialogsProvider::simplePLAppendDialog()
423 addFromSimple( true, false );
426 void DialogsProvider::simpleMLAppendDialog()
428 addFromSimple( false, false );
434 * pl helps you to choose from playlist or media library,
435 * go to start or enqueue
437 static void openDirectory( intf_thread_t *p_intf, bool pl, bool go )
439 QString dir = QFileDialog::getExistingDirectory( NULL, qtr("Open Directory") );
443 QString mrl = dir.endsWith( "VIDEO_TS", Qt::CaseInsensitive ) ?
444 "dvd://" : "directory://" + toNativeSeparators( dir );
445 input_item_t *p_input = input_item_NewExt( THEPL, qtu( mrl ),
448 /* FIXME: playlist_AddInput() can fail */
449 playlist_AddInput( THEPL, p_input,
450 go ? ( PLAYLIST_APPEND | PLAYLIST_GO ) : PLAYLIST_APPEND,
451 PLAYLIST_END, pl, pl_Unlocked );
452 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
454 input_Read( THEPL, p_input, true );
455 vlc_gc_decref( p_input );
459 void DialogsProvider::PLOpenDir()
461 openDirectory( p_intf, true, true );
464 void DialogsProvider::PLAppendDir()
466 openDirectory( p_intf, true, false );
469 void DialogsProvider::MLAppendDir()
471 openDirectory( p_intf, false , false );
477 void DialogsProvider::openAPlaylist()
479 QStringList files = showSimpleOpen( qtr( "Open playlist..." ),
480 EXT_FILTER_PLAYLIST );
481 foreach( QString file, files )
483 playlist_Import( THEPL, qtu( toNativeSeparators( file ) ) );
487 void DialogsProvider::saveAPlaylist()
489 QFileDialog *qfd = new QFileDialog( NULL,
490 qtr( "Save playlist as..." ),
491 qfu( p_intf->p_sys->psz_filepath ),
492 qtr( "XSPF playlist (*.xspf);; " ) +
493 qtr( "M3U playlist (*.m3u);; " ) +
494 qtr( "HTML playlist (*.html);;" ) +
495 qtr( "Any (*.*) " ) );
496 qfd->setFileMode( QFileDialog::AnyFile );
497 qfd->setAcceptMode( QFileDialog::AcceptSave );
498 qfd->setConfirmOverwrite( true );
500 if( qfd->exec() == QDialog::Accepted )
502 if( qfd->selectedFiles().count() > 0 )
504 static const char psz_xspf[] = "export-xspf",
505 psz_m3u[] = "export-m3u",
506 psz_html[] = "export-html";
507 const char *psz_module;
509 QString file = qfd->selectedFiles().first();
510 QString filter = qfd->selectedFilter();
511 const char* filt = filter.toAscii();
513 if( file.contains( ".xsp" ) || filter.contains( "XSPF" ) )
515 psz_module = psz_xspf;
516 if( !file.contains( ".xsp" ) )
517 file.append( ".xspf" );
519 else if( file.contains( ".m3u" ) || filter.contains( "M3U" ) )
521 psz_module = psz_m3u;
522 if( !file.contains( ".m3u" ) )
523 file.append( ".m3u" );
525 else if( file.contains(".html" ) || filter.contains( "HTML" ) )
527 psz_module = psz_html;
528 if( !file.contains( "html" ) )
529 file.append( ".html" );
533 msg_Err( p_intf, "Impossible to recognise the file type" );
538 playlist_Export( THEPL, qtu( toNativeSeparators( file ) ),
539 THEPL->p_local_category, psz_module);
545 /****************************************************************************
547 ****************************************************************************/
549 void DialogsProvider::streamingDialog( QWidget *parent, QString mrl,
550 bool b_transcode_only )
552 SoutDialog *s = SoutDialog::getInstance( parent, p_intf, b_transcode_only );
554 if( s->exec() == QDialog::Accepted )
556 msg_Dbg( p_intf, "Sout mrl %s", qta( s->getMrl() ) );
558 int i_len = strlen( qtu( s->getMrl() ) ) + 10;
559 char *psz_option = (char*)malloc( i_len );
560 snprintf( psz_option, i_len - 1, "%s", qtu( s->getMrl() ) );
562 playlist_AddExt( THEPL, qtu( mrl ), "Streaming",
563 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END,
564 -1, &psz_option, 1, true, pl_Unlocked );
565 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
569 void DialogsProvider::openAndStreamingDialogs()
571 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false, OPEN_AND_STREAM )
572 ->showTab( OPEN_FILE_TAB );
575 void DialogsProvider::openAndTranscodingDialogs()
577 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, false, OPEN_AND_SAVE )
578 ->showTab( OPEN_FILE_TAB );
581 void DialogsProvider::loadSubtitlesFile()
583 input_thread_t *p_input = THEMIM->getInput();
584 if( !p_input ) return;
586 input_item_t *p_item = input_GetItem( p_input );
587 if( !p_item ) return;
589 char *path = input_item_GetURI( p_item );
590 if( !path ) path = strdup( "" );
592 char *sep = strrchr( path, DIR_SEP_CHAR );
593 if( sep ) *sep = '\0';
595 QStringList qsl = showSimpleOpen( qtr( "Open subtitles..." ),
600 foreach( qsFile, qsl )
602 if( input_AddSubtitle( p_input, qtu( toNativeSeparators( qsFile ) ),
604 msg_Warn( p_intf, "unable to load subtitles from '%s'",
610 /****************************************************************************
612 ****************************************************************************/
614 void DialogsProvider::menuAction( QObject *data )
616 QVLCMenu::DoAction( p_intf, data );
619 void DialogsProvider::menuUpdateAction( QObject *data )
621 MenuFunc *func = qobject_cast<MenuFunc *>(data);
623 func->doFunc( p_intf );
626 void DialogsProvider::SDMenuAction( QString data )
628 char *psz_sd = qtu( data );
629 if( !playlist_IsServicesDiscoveryLoaded( THEPL, psz_sd ) )
630 playlist_ServicesDiscoveryAdd( THEPL, psz_sd );
632 playlist_ServicesDiscoveryRemove( THEPL, psz_sd );
636 * Play the MRL contained in the Recently played menu.
638 void DialogsProvider::playMRL( const QString &mrl )
640 playlist_Add( THEPL, qtu( mrl ) , NULL,
641 PLAYLIST_APPEND | PLAYLIST_GO , PLAYLIST_END, true, false );
643 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
646 /*************************************
648 *************************************/
649 void DialogsProvider::doInteraction( intf_dialog_args_t *p_arg )
651 InteractionDialog *qdialog;
652 interaction_dialog_t *p_dialog = p_arg->p_dialog;
653 switch( p_dialog->i_action )
656 qdialog = new InteractionDialog( p_intf, p_dialog );
657 p_dialog->p_private = (void*)qdialog;
658 if( !(p_dialog->i_status == ANSWERED_DIALOG) )
661 case INTERACT_UPDATE:
662 qdialog = (InteractionDialog*)(p_dialog->p_private);
667 /* The INTERACT_NEW message was forgotten
668 so we must create the dialog and update it*/
669 qdialog = new InteractionDialog( p_intf, p_dialog );
670 p_dialog->p_private = (void*)qdialog;
671 if( !(p_dialog->i_status == ANSWERED_DIALOG) )
678 msg_Dbg( p_intf, "Hide the Interaction Dialog" );
679 qdialog = (InteractionDialog*)(p_dialog->p_private);
682 p_dialog->i_status = HIDDEN_DIALOG;
684 case INTERACT_DESTROY:
685 msg_Dbg( p_intf, "Destroy the Interaction Dialog" );
686 qdialog = (InteractionDialog*)(p_dialog->p_private);
687 if( !p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
689 p_dialog->i_status = DESTROYED_DIALOG;