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"
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()
100 /* Stop the playlist */
101 playlist_Stop( THEPL );
103 vlc_object_kill( p_intf->p_libvlc );
104 QApplication::closeAllWindows();
105 QApplication::quit();
108 void DialogsProvider::customEvent( QEvent *event )
110 if( event->type() == (int)DialogEvent_Type )
112 DialogEvent *de = static_cast<DialogEvent*>(event);
113 switch( de->i_dialog )
115 case INTF_DIALOG_FILE_SIMPLE:
116 case INTF_DIALOG_FILE:
118 case INTF_DIALOG_FILE_GENERIC:
119 openFileGenericDialog( de->p_arg ); break;
120 case INTF_DIALOG_DISC:
121 openDiscDialog(); break;
122 case INTF_DIALOG_NET:
123 openNetDialog(); break;
124 case INTF_DIALOG_SAT:
125 case INTF_DIALOG_CAPTURE:
126 openCaptureDialog(); break;
127 case INTF_DIALOG_DIRECTORY:
128 PLAppendDir(); break;
129 case INTF_DIALOG_PLAYLIST:
130 playlistDialog(); break;
131 case INTF_DIALOG_MESSAGES:
132 messagesDialog(); break;
133 case INTF_DIALOG_FILEINFO:
134 mediaInfoDialog(); break;
135 case INTF_DIALOG_PREFS:
136 prefsDialog(); break;
137 case INTF_DIALOG_BOOKMARKS:
138 bookmarksDialog(); break;
139 case INTF_DIALOG_EXTENDED:
140 extendedDialog(); break;
142 case INTF_DIALOG_VLM:
145 case INTF_DIALOG_INTERACTION:
146 doInteraction( de->p_arg ); break;
147 case INTF_DIALOG_POPUPMENU:
148 QVLCMenu::PopupMenu( p_intf, (de->i_arg != 0) ); break;
149 case INTF_DIALOG_AUDIOPOPUPMENU:
150 QVLCMenu::AudioPopupMenu( p_intf ); break;
151 case INTF_DIALOG_VIDEOPOPUPMENU:
152 QVLCMenu::VideoPopupMenu( p_intf ); break;
153 case INTF_DIALOG_MISCPOPUPMENU:
154 QVLCMenu::MiscPopupMenu( p_intf ); break;
155 case INTF_DIALOG_WIZARD:
156 case INTF_DIALOG_STREAMWIZARD:
157 openAndStreamingDialogs(); break;
159 case INTF_DIALOG_UPDATEVLC:
160 updateDialog(); break;
162 case INTF_DIALOG_EXIT:
165 msg_Warn( p_intf, "unimplemented dialog" );
170 /****************************************************************************
171 * Individual simple dialogs
172 ****************************************************************************/
173 void DialogsProvider::playlistDialog()
175 PlaylistDialog::getInstance( p_intf )->toggleVisible();
178 void DialogsProvider::prefsDialog()
180 PrefsDialog::getInstance( p_intf )->toggleVisible();
183 void DialogsProvider::extendedDialog()
185 ExtendedDialog::getInstance( p_intf )->toggleVisible();
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 )->toggleVisible();
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::getInstance( p_intf )->toggleVisible();
247 /* Generic open file */
248 void DialogsProvider::openFileGenericDialog( intf_dialog_args_t *p_arg )
252 msg_Warn( p_intf, "openFileGenericDialog() called with NULL arg" );
256 /* Replace the extensions to a Qt format */
258 QString extensions = qfu( p_arg->psz_extensions );
259 while ( ( i = extensions.indexOf( "|", i ) ) != -1 )
261 if( ( extensions.count( "|" ) % 2 ) == 0 )
262 extensions.replace( i, 1, ");;" );
264 extensions.replace( i, 1, "(" );
266 extensions.replace(QString(";*"), QString(" *"));
267 extensions.append( ")" );
272 QString file = QFileDialog::getSaveFileName( NULL, p_arg->psz_title,
273 qfu( p_intf->p_sys->psz_filepath ), extensions );
274 if( !file.isEmpty() )
276 p_arg->i_results = 1;
277 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
278 p_arg->psz_results[0] = strdup( qtu( toNativeSepNoSlash( file ) ) );
281 p_arg->i_results = 0;
283 else /* non-save mode */
285 QStringList files = QFileDialog::getOpenFileNames( NULL,
286 p_arg->psz_title, qfu( p_intf->p_sys->psz_filepath ),
288 p_arg->i_results = files.count();
289 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
291 foreach( QString file, files )
292 p_arg->psz_results[i++] = strdup( qtu( toNativeSepNoSlash( file ) ) );
296 if( p_arg->pf_callback )
297 p_arg->pf_callback( p_arg );
299 /* Clean afterwards */
300 if( p_arg->psz_results )
302 for( i = 0; i < p_arg->i_results; i++ )
303 free( p_arg->psz_results[i] );
304 free( p_arg->psz_results );
306 free( p_arg->psz_title );
307 free( p_arg->psz_extensions );
310 /****************************************************************************
311 * All the open/add stuff
312 * Open Dialog first - Simple Open then
313 ****************************************************************************/
315 void DialogsProvider::openDialog( int i_tab )
317 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf )->showTab( i_tab );
319 void DialogsProvider::openDialog()
321 openDialog( OPEN_FILE_TAB );
323 void DialogsProvider::openFileDialog()
325 openDialog( OPEN_FILE_TAB );
327 void DialogsProvider::openDiscDialog()
329 openDialog( OPEN_DISC_TAB );
331 void DialogsProvider::openNetDialog()
333 openDialog( OPEN_NETWORK_TAB );
335 void DialogsProvider::openCaptureDialog()
337 openDialog( OPEN_CAPTURE_TAB );
340 /* Same as the open one, but force the enqueue */
341 void DialogsProvider::PLAppendDialog()
343 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
344 OPEN_AND_ENQUEUE )->showTab( OPEN_FILE_TAB );
347 void DialogsProvider::MLAppendDialog()
349 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
350 OPEN_AND_ENQUEUE, false, false )
351 ->showTab( OPEN_FILE_TAB );
357 QStringList DialogsProvider::showSimpleOpen( QString help,
361 QString fileTypes = "";
362 if( filters & EXT_FILTER_MEDIA ) {
363 ADD_FILTER_MEDIA( fileTypes );
365 if( filters & EXT_FILTER_VIDEO ) {
366 ADD_FILTER_VIDEO( fileTypes );
368 if( filters & EXT_FILTER_AUDIO ) {
369 ADD_FILTER_AUDIO( fileTypes );
371 if( filters & EXT_FILTER_PLAYLIST ) {
372 ADD_FILTER_PLAYLIST( fileTypes );
374 if( filters & EXT_FILTER_SUBTITLE ) {
375 ADD_FILTER_SUBTITLE( fileTypes );
377 ADD_FILTER_ALL( fileTypes );
378 fileTypes.replace(QString(";*"), QString(" *"));
380 return QFileDialog::getOpenFileNames( NULL,
381 help.isEmpty() ? qtr(I_OP_SEL_FILES ) : help,
382 path.isEmpty() ? qfu( p_intf->p_sys->psz_filepath ) : path,
388 * pl helps you to choose from playlist or media library,
389 * go to start or enqueue
391 void DialogsProvider::addFromSimple( bool pl, bool go)
393 QStringList files = DialogsProvider::showSimpleOpen();
395 foreach( QString file, files )
397 playlist_Add( THEPL, qtu( toNativeSeparators( file ) ), NULL,
398 go ? ( PLAYLIST_APPEND | ( i ? 0 : PLAYLIST_GO ) |
399 ( i ? PLAYLIST_PREPARSE : 0 ) )
400 : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
402 pl ? true : false, false );
403 RecentsMRL::getInstance( p_intf )->addRecent(
404 toNativeSeparators( file ) );
409 void DialogsProvider::simpleOpenDialog()
411 addFromSimple( true, true ); /* Playlist and Go */
414 void DialogsProvider::simplePLAppendDialog()
416 addFromSimple( true, false );
419 void DialogsProvider::simpleMLAppendDialog()
421 addFromSimple( false, false );
427 * pl helps you to choose from playlist or media library,
428 * go to start or enqueue
430 static void openDirectory( intf_thread_t *p_intf, bool pl, bool go )
432 QString dir = QFileDialog::getExistingDirectory( NULL, qtr("Open Directory") );
436 QString mrl = dir.endsWith( "VIDEO_TS", Qt::CaseInsensitive ) ?
437 "dvd://" : "directory://" + toNativeSeparators( dir );
438 input_item_t *p_input = input_item_NewExt( THEPL, qtu( mrl ),
441 /* FIXME: playlist_AddInput() can fail */
442 playlist_AddInput( THEPL, p_input,
443 go ? ( PLAYLIST_APPEND | PLAYLIST_GO ) : PLAYLIST_APPEND,
444 PLAYLIST_END, pl, pl_Unlocked );
445 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
447 input_Read( THEPL, p_input, true );
448 vlc_gc_decref( p_input );
452 void DialogsProvider::PLOpenDir()
454 openDirectory( p_intf, true, true );
457 void DialogsProvider::PLAppendDir()
459 openDirectory( p_intf, true, false );
462 void DialogsProvider::MLAppendDir()
464 openDirectory( p_intf, false , false );
470 void DialogsProvider::openAPlaylist()
472 QStringList files = showSimpleOpen( qtr( "Open playlist..." ),
473 EXT_FILTER_PLAYLIST );
474 foreach( QString file, files )
476 playlist_Import( THEPL, qtu( toNativeSeparators( file ) ) );
480 void DialogsProvider::saveAPlaylist()
482 QFileDialog *qfd = new QFileDialog( NULL,
483 qtr( "Save playlist as..." ),
484 qfu( p_intf->p_sys->psz_filepath ),
485 qtr( "XSPF playlist (*.xspf);; " ) +
486 qtr( "M3U playlist (*.m3u);; " ) +
487 qtr( "HTML playlist (*.html);;" ) +
488 qtr( "Any (*.*) " ) );
489 qfd->setFileMode( QFileDialog::AnyFile );
490 qfd->setAcceptMode( QFileDialog::AcceptSave );
491 qfd->setConfirmOverwrite( true );
493 if( qfd->exec() == QDialog::Accepted )
495 if( qfd->selectedFiles().count() > 0 )
497 static const char psz_xspf[] = "export-xspf",
498 psz_m3u[] = "export-m3u",
499 psz_html[] = "export-html";
500 const char *psz_module;
502 QString file = qfd->selectedFiles().first();
503 QString filter = qfd->selectedFilter();
504 const char* filt = filter.toAscii();
506 if( file.contains( ".xsp" ) || filter.contains( "XSPF" ) )
508 psz_module = psz_xspf;
509 if( !file.contains( ".xsp" ) )
510 file.append( ".xspf" );
512 else if( file.contains( ".m3u" ) || filter.contains( "M3U" ) )
514 psz_module = psz_m3u;
515 if( !file.contains( ".m3u" ) )
516 file.append( ".m3u" );
518 else if( file.contains(".html" ) || filter.contains( "HTML" ) )
520 psz_module = psz_html;
521 if( !file.contains( "html" ) )
522 file.append( ".html" );
526 msg_Err( p_intf, "Impossible to recognise the file type" );
531 playlist_Export( THEPL, qtu( toNativeSeparators( file ) ),
532 THEPL->p_local_category, psz_module);
538 /****************************************************************************
540 ****************************************************************************/
542 void DialogsProvider::streamingDialog( QWidget *parent, QString mrl,
543 bool b_transcode_only )
545 SoutDialog *s = SoutDialog::getInstance( parent, p_intf, b_transcode_only );
547 if( s->exec() == QDialog::Accepted )
549 msg_Dbg( p_intf, "Sout mrl %s", qta( s->getMrl() ) );
551 int i_len = strlen( qtu( s->getMrl() ) ) + 10;
552 char *psz_option = (char*)malloc( i_len );
553 snprintf( psz_option, i_len - 1, "%s", qtu( s->getMrl() ) );
555 playlist_AddExt( THEPL, qtu( mrl ), "Streaming",
556 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END,
557 -1, &psz_option, 1, true, pl_Unlocked );
558 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
562 void DialogsProvider::openAndStreamingDialogs()
564 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false, OPEN_AND_STREAM )
565 ->showTab( OPEN_FILE_TAB );
568 void DialogsProvider::openAndTranscodingDialogs()
570 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, false, OPEN_AND_SAVE )
571 ->showTab( OPEN_FILE_TAB );
574 void DialogsProvider::loadSubtitlesFile()
576 input_thread_t *p_input = THEMIM->getInput();
577 if( !p_input ) return;
579 input_item_t *p_item = input_GetItem( p_input );
580 if( !p_item ) return;
582 char *path = input_item_GetURI( p_item );
583 if( !path ) path = strdup( "" );
585 char *sep = strrchr( path, DIR_SEP_CHAR );
586 if( sep ) *sep = '\0';
588 QStringList qsl = showSimpleOpen( qtr( "Open subtitles..." ),
593 foreach( qsFile, qsl )
595 if( input_AddSubtitle( p_input, qtu( toNativeSeparators( qsFile ) ),
597 msg_Warn( p_intf, "unable to load subtitles from '%s'",
603 /****************************************************************************
605 ****************************************************************************/
607 void DialogsProvider::menuAction( QObject *data )
609 QVLCMenu::DoAction( p_intf, data );
612 void DialogsProvider::menuUpdateAction( QObject *data )
614 MenuFunc *func = qobject_cast<MenuFunc *>(data);
616 func->doFunc( p_intf );
619 void DialogsProvider::SDMenuAction( QString data )
621 char *psz_sd = qtu( data );
622 if( !playlist_IsServicesDiscoveryLoaded( THEPL, psz_sd ) )
623 playlist_ServicesDiscoveryAdd( THEPL, psz_sd );
625 playlist_ServicesDiscoveryRemove( THEPL, psz_sd );
629 * Play the MRL contained in the Recently played menu.
631 void DialogsProvider::playMRL( const QString &mrl )
633 playlist_Add( THEPL, qtu( mrl ) , NULL,
634 PLAYLIST_APPEND | PLAYLIST_GO , PLAYLIST_END, true, false );
636 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
639 /*************************************
641 *************************************/
642 void DialogsProvider::doInteraction( intf_dialog_args_t *p_arg )
644 InteractionDialog *qdialog;
645 interaction_dialog_t *p_dialog = p_arg->p_dialog;
646 switch( p_dialog->i_action )
649 qdialog = new InteractionDialog( p_intf, p_dialog );
650 p_dialog->p_private = (void*)qdialog;
651 if( !(p_dialog->i_status == ANSWERED_DIALOG) )
654 case INTERACT_UPDATE:
655 qdialog = (InteractionDialog*)(p_dialog->p_private);
660 /* The INTERACT_NEW message was forgotten
661 so we must create the dialog and update it*/
662 qdialog = new InteractionDialog( p_intf, p_dialog );
663 p_dialog->p_private = (void*)qdialog;
664 if( !(p_dialog->i_status == ANSWERED_DIALOG) )
671 msg_Dbg( p_intf, "Hide the Interaction Dialog" );
672 qdialog = (InteractionDialog*)(p_dialog->p_private);
675 p_dialog->i_status = HIDDEN_DIALOG;
677 case INTERACT_DESTROY:
678 msg_Dbg( p_intf, "Destroy the Interaction Dialog" );
679 qdialog = (InteractionDialog*)(p_dialog->p_private);
680 if( !p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
682 p_dialog->i_status = DESTROYED_DIALOG;