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"
40 #include "dialogs/playlist.hpp"
41 #include "dialogs/bookmarks.hpp"
42 #include "dialogs/preferences.hpp"
43 #include "dialogs/mediainfo.hpp"
44 #include "dialogs/messages.hpp"
45 #include "dialogs/extended.hpp"
46 #include "dialogs/vlm.hpp"
47 #include "dialogs/sout.hpp"
48 #include "dialogs/open.hpp"
49 #include "dialogs/help.hpp"
50 #include "dialogs/gototime.hpp"
51 #include "dialogs/podcast_configuration.hpp"
54 #include <QApplication>
55 #include <QSignalMapper>
56 #include <QFileDialog>
59 DialogsProvider* DialogsProvider::instance = NULL;
61 DialogsProvider::DialogsProvider( intf_thread_t *_p_intf ) :
62 QObject( NULL ), p_intf( _p_intf )
66 /* Various signal mappers for the menus */
67 menusMapper = new QSignalMapper();
68 CONNECT( menusMapper, mapped(QObject *), this, menuAction( QObject *) );
70 menusUpdateMapper = new QSignalMapper();
71 CONNECT( menusUpdateMapper, mapped(QObject *),
72 this, menuUpdateAction( QObject *) );
74 SDMapper = new QSignalMapper();
75 CONNECT( SDMapper, mapped (QString), this, SDMenuAction( QString ) );
78 DialogsProvider::~DialogsProvider()
80 msg_Dbg( p_intf, "Destroying the Dialog Provider" );
81 PlaylistDialog::killInstance();
82 MediaInfoDialog::killInstance();
83 MessagesDialog::killInstance();
84 ExtendedDialog::killInstance();
85 BookmarksDialog::killInstance();
86 HelpDialog::killInstance();
88 UpdateDialog::killInstance();
92 delete menusUpdateMapper;
96 void DialogsProvider::quit()
98 /* Stop the playlist */
99 playlist_Stop( THEPL );
101 vlc_object_kill( p_intf->p_libvlc );
102 QApplication::closeAllWindows();
103 QApplication::quit();
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::messagesDialog()
188 MessagesDialog::getInstance( p_intf )->toggleVisible();
191 void DialogsProvider::gotoTimeDialog()
193 GotoTimeDialog::getInstance( p_intf )->toggleVisible();
197 void DialogsProvider::vlmDialog()
199 VLMDialog::getInstance( p_intf )->toggleVisible();
203 void DialogsProvider::helpDialog()
205 HelpDialog::getInstance( p_intf )->toggleVisible();
209 void DialogsProvider::updateDialog()
211 UpdateDialog::getInstance( p_intf )->toggleVisible();
215 void DialogsProvider::aboutDialog()
217 AboutDialog::getInstance( p_intf )->toggleVisible();
220 void DialogsProvider::mediaInfoDialog()
222 MediaInfoDialog::getInstance( p_intf )->toggleVisible();
225 void DialogsProvider::mediaCodecDialog()
227 MediaInfoDialog::getInstance( p_intf )->showTab( 2 );
230 void DialogsProvider::bookmarksDialog()
232 BookmarksDialog::getInstance( p_intf )->toggleVisible();
235 void DialogsProvider::podcastConfigureDialog()
237 PodcastConfigDialog::getInstance( p_intf )->toggleVisible();
240 /* Generic open file */
241 void DialogsProvider::openFileGenericDialog( intf_dialog_args_t *p_arg )
245 msg_Warn( p_intf, "openFileGenericDialog() called with NULL arg" );
249 /* Replace the extensions to a Qt format */
251 QString extensions = qfu( p_arg->psz_extensions );
252 while ( ( i = extensions.indexOf( "|", i ) ) != -1 )
254 if( ( extensions.count( "|" ) % 2 ) == 0 )
255 extensions.replace( i, 1, ");;" );
257 extensions.replace( i, 1, "(" );
259 extensions.replace(QString(";*"), QString(" *"));
260 extensions.append( ")" );
265 QString file = QFileDialog::getSaveFileName( NULL, p_arg->psz_title,
266 qfu( p_intf->p_sys->psz_filepath ), extensions );
267 if( !file.isEmpty() )
269 p_arg->i_results = 1;
270 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
271 p_arg->psz_results[0] = strdup( qtu( toNativeSepNoSlash( file ) ) );
274 p_arg->i_results = 0;
276 else /* non-save mode */
278 QStringList files = QFileDialog::getOpenFileNames( NULL,
279 p_arg->psz_title, qfu( p_intf->p_sys->psz_filepath ),
281 p_arg->i_results = files.count();
282 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
284 foreach( QString file, files )
285 p_arg->psz_results[i++] = strdup( qtu( toNativeSepNoSlash( file ) ) );
289 if( p_arg->pf_callback )
290 p_arg->pf_callback( p_arg );
292 /* Clean afterwards */
293 if( p_arg->psz_results )
295 for( i = 0; i < p_arg->i_results; i++ )
296 free( p_arg->psz_results[i] );
297 free( p_arg->psz_results );
299 free( p_arg->psz_title );
300 free( p_arg->psz_extensions );
303 /****************************************************************************
304 * All the open/add stuff
305 * Open Dialog first - Simple Open then
306 ****************************************************************************/
308 void DialogsProvider::openDialog( int i_tab )
310 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf )->showTab( i_tab );
312 void DialogsProvider::openDialog()
314 openDialog( OPEN_FILE_TAB );
316 void DialogsProvider::openFileDialog()
318 openDialog( OPEN_FILE_TAB );
320 void DialogsProvider::openDiscDialog()
322 openDialog( OPEN_DISC_TAB );
324 void DialogsProvider::openNetDialog()
326 openDialog( OPEN_NETWORK_TAB );
328 void DialogsProvider::openCaptureDialog()
330 openDialog( OPEN_CAPTURE_TAB );
333 /* Same as the open one, but force the enqueue */
334 void DialogsProvider::PLAppendDialog()
336 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
337 OPEN_AND_ENQUEUE )->showTab( OPEN_FILE_TAB );
340 void DialogsProvider::MLAppendDialog()
342 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
343 OPEN_AND_ENQUEUE, false, false )
344 ->showTab( OPEN_FILE_TAB );
350 QStringList DialogsProvider::showSimpleOpen( QString help,
354 QString fileTypes = "";
355 if( filters & EXT_FILTER_MEDIA ) {
356 ADD_FILTER_MEDIA( fileTypes );
358 if( filters & EXT_FILTER_VIDEO ) {
359 ADD_FILTER_VIDEO( fileTypes );
361 if( filters & EXT_FILTER_AUDIO ) {
362 ADD_FILTER_AUDIO( fileTypes );
364 if( filters & EXT_FILTER_PLAYLIST ) {
365 ADD_FILTER_PLAYLIST( fileTypes );
367 if( filters & EXT_FILTER_SUBTITLE ) {
368 ADD_FILTER_SUBTITLE( fileTypes );
370 ADD_FILTER_ALL( fileTypes );
371 fileTypes.replace(QString(";*"), QString(" *"));
373 return QFileDialog::getOpenFileNames( NULL,
374 help.isEmpty() ? qfu(I_OP_SEL_FILES ) : help,
375 path.isEmpty() ? qfu( p_intf->p_sys->psz_filepath ) : path,
381 * pl helps you to choose from playlist or media library,
382 * go to start or enqueue
384 void DialogsProvider::addFromSimple( bool pl, bool go)
386 QStringList files = DialogsProvider::showSimpleOpen();
388 foreach( QString file, files )
390 playlist_Add( THEPL, qtu( toNativeSeparators( file ) ), NULL,
391 go ? ( PLAYLIST_APPEND | ( i ? 0 : PLAYLIST_GO ) |
392 ( i ? PLAYLIST_PREPARSE : 0 ) )
393 : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
395 pl ? true : false, false );
396 RecentsMRL::getInstance( p_intf )->addRecent(
397 toNativeSeparators( file ) );
402 void DialogsProvider::simpleOpenDialog()
404 addFromSimple( true, true ); /* Playlist and Go */
407 void DialogsProvider::simplePLAppendDialog()
409 addFromSimple( true, false );
412 void DialogsProvider::simpleMLAppendDialog()
414 addFromSimple( false, false );
420 * pl helps you to choose from playlist or media library,
421 * go to start or enqueue
423 static void openDirectory( intf_thread_t *p_intf, bool pl, bool go )
425 QString dir = QFileDialog::getExistingDirectory( NULL, qtr("Open Directory") );
429 QString mrl = dir.endsWith( "VIDEO_TS", Qt::CaseInsensitive )
430 ? "dvd://" : "directory://" + toNativeSeparators( dir );
431 msg_Dbg( p_intf, "Directory opening: %s", qtu( dir ) );
432 input_item_t *p_input = input_item_NewExt( THEPL, qtu( mrl ),
435 /* FIXME: playlist_AddInput() can fail */
436 playlist_AddInput( THEPL, p_input,
437 go ? ( PLAYLIST_APPEND | PLAYLIST_GO ) : PLAYLIST_APPEND,
438 PLAYLIST_END, pl, pl_Unlocked );
439 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
441 input_Read( THEPL, p_input, true );
442 vlc_gc_decref( p_input );
446 void DialogsProvider::PLOpenDir()
448 openDirectory( p_intf, true, true );
451 void DialogsProvider::PLAppendDir()
453 openDirectory( p_intf, true, false );
456 void DialogsProvider::MLAppendDir()
458 openDirectory( p_intf, false , false );
464 void DialogsProvider::openAPlaylist()
466 QStringList files = showSimpleOpen( qtr( "Open playlist..." ),
467 EXT_FILTER_PLAYLIST );
468 foreach( QString file, files )
470 playlist_Import( THEPL, qtu( toNativeSeparators( file ) ) );
474 void DialogsProvider::saveAPlaylist()
476 QFileDialog *qfd = new QFileDialog( NULL,
477 qtr( "Save playlist as..." ),
478 qfu( p_intf->p_sys->psz_filepath ),
479 qtr( "XSPF playlist (*.xspf);; " ) +
480 qtr( "M3U playlist (*.m3u);; Any (*.*) " ) );
481 qfd->setFileMode( QFileDialog::AnyFile );
482 qfd->setAcceptMode( QFileDialog::AcceptSave );
483 qfd->setConfirmOverwrite( true );
485 if( qfd->exec() == QDialog::Accepted )
487 if( qfd->selectedFiles().count() > 0 )
489 static const char psz_xspf[] = "export-xspf",
490 psz_m3u[] = "export-m3u";
491 const char *psz_module;
493 QString file = qfd->selectedFiles().first();
494 QString filter = qfd->selectedFilter();
496 if( file.contains( ".xsp" ) ||
497 ( filter.contains( ".xspf" ) && !file.contains( ".m3u" ) ) )
499 psz_module = psz_xspf;
500 if( !file.contains( ".xsp" ) )
501 file.append( ".xspf" );
505 psz_module = psz_m3u;
506 if( !file.contains( ".m3u" ) )
507 file.append( ".m3u" );
510 playlist_Export( THEPL, qtu( toNativeSeparators( file ) ),
511 THEPL->p_local_category, psz_module);
517 /****************************************************************************
519 ****************************************************************************/
521 void DialogsProvider::streamingDialog( QWidget *parent, QString mrl,
522 bool b_transcode_only )
524 SoutDialog *s = SoutDialog::getInstance( parent, p_intf, b_transcode_only );
526 if( s->exec() == QDialog::Accepted )
528 msg_Dbg( p_intf, "Sout mrl %s", qta( s->getMrl() ) );
530 int i_len = strlen( qtu( s->getMrl() ) ) + 10;
531 char *psz_option = (char*)malloc( i_len );
532 snprintf( psz_option, i_len - 1, "%s", qtu( s->getMrl() ) );
534 playlist_AddExt( THEPL, qtu( mrl ), "Streaming",
535 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END,
536 -1, &psz_option, 1, true, pl_Unlocked );
537 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
541 void DialogsProvider::openAndStreamingDialogs()
543 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false, OPEN_AND_STREAM )
544 ->showTab( OPEN_FILE_TAB );
547 void DialogsProvider::openAndTranscodingDialogs()
549 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, false, OPEN_AND_SAVE )
550 ->showTab( OPEN_FILE_TAB );
553 void DialogsProvider::loadSubtitlesFile()
555 input_thread_t *p_input = THEMIM->getInput();
556 if( !p_input ) return;
558 input_item_t *p_item = input_GetItem( p_input );
559 if( !p_item ) return;
561 char *path = input_item_GetURI( p_item );
562 if( !path ) path = strdup( "" );
564 char *sep = strrchr( path, DIR_SEP_CHAR );
565 if( sep ) *sep = '\0';
567 QStringList qsl = showSimpleOpen( qtr( "Open subtitles..." ),
572 foreach( qsFile, qsl )
574 if( !input_AddSubtitles( p_input, qtu( toNativeSeparators( qsFile ) ),
576 msg_Warn( p_intf, "unable to load subtitles from '%s'",
582 /****************************************************************************
584 ****************************************************************************/
586 void DialogsProvider::menuAction( QObject *data )
588 QVLCMenu::DoAction( p_intf, data );
591 void DialogsProvider::menuUpdateAction( QObject *data )
593 MenuFunc *func = qobject_cast<MenuFunc *>(data);
595 func->doFunc( p_intf );
598 void DialogsProvider::SDMenuAction( QString data )
600 char *psz_sd = qtu( data );
601 if( !playlist_IsServicesDiscoveryLoaded( THEPL, psz_sd ) )
602 playlist_ServicesDiscoveryAdd( THEPL, psz_sd );
604 playlist_ServicesDiscoveryRemove( THEPL, psz_sd );
608 * Play the MRL contained in the Recently played menu.
610 void DialogsProvider::playMRL( const QString &mrl )
612 input_item_t *p_input = input_item_New( p_intf,
614 playlist_AddInput( THEPL, p_input, PLAYLIST_GO,
615 PLAYLIST_END, true, pl_Unlocked );
616 vlc_gc_decref( p_input );
618 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
621 /*************************************
623 *************************************/
624 void DialogsProvider::doInteraction( intf_dialog_args_t *p_arg )
626 InteractionDialog *qdialog;
627 interaction_dialog_t *p_dialog = p_arg->p_dialog;
628 switch( p_dialog->i_action )
631 qdialog = new InteractionDialog( p_intf, p_dialog );
632 p_dialog->p_private = (void*)qdialog;
633 if( !(p_dialog->i_status == ANSWERED_DIALOG) )
636 case INTERACT_UPDATE:
637 qdialog = (InteractionDialog*)(p_dialog->p_private);
642 /* The INTERACT_NEW message was forgotten
643 so we must create the dialog and update it*/
644 qdialog = new InteractionDialog( p_intf, p_dialog );
645 p_dialog->p_private = (void*)qdialog;
646 if( !(p_dialog->i_status == ANSWERED_DIALOG) )
653 msg_Dbg( p_intf, "Hide the Interaction Dialog" );
654 qdialog = (InteractionDialog*)(p_dialog->p_private);
657 p_dialog->i_status = HIDDEN_DIALOG;
659 case INTERACT_DESTROY:
660 msg_Dbg( p_intf, "Destroy the Interaction Dialog" );
661 qdialog = (InteractionDialog*)(p_dialog->p_private);
662 if( !p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
664 p_dialog->i_status = DESTROYED_DIALOG;