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/convert.hpp"
48 #include "dialogs/open.hpp"
49 #include "dialogs/openurl.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 PlaylistDialog::killInstance();
84 MediaInfoDialog::killInstance();
85 MessagesDialog::killInstance();
86 ExtendedDialog::killInstance();
87 BookmarksDialog::killInstance();
88 HelpDialog::killInstance();
90 UpdateDialog::killInstance();
92 ToolbarEditDialog::killInstance();
95 delete menusUpdateMapper;
99 void DialogsProvider::quit()
102 libvlc_Quit( 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_POPUPMENU:
143 QVLCMenu::PopupMenu( p_intf, (de->i_arg != 0) ); break;
144 case INTF_DIALOG_AUDIOPOPUPMENU:
145 QVLCMenu::AudioPopupMenu( p_intf ); break;
146 case INTF_DIALOG_VIDEOPOPUPMENU:
147 QVLCMenu::VideoPopupMenu( p_intf ); break;
148 case INTF_DIALOG_MISCPOPUPMENU:
149 QVLCMenu::MiscPopupMenu( p_intf ); break;
150 case INTF_DIALOG_WIZARD:
151 case INTF_DIALOG_STREAMWIZARD:
152 openAndStreamingDialogs(); break;
154 case INTF_DIALOG_UPDATEVLC:
155 updateDialog(); break;
157 case INTF_DIALOG_EXIT:
160 msg_Warn( p_intf, "unimplemented dialog" );
165 /****************************************************************************
166 * Individual simple dialogs
167 ****************************************************************************/
168 void DialogsProvider::playlistDialog()
170 PlaylistDialog::getInstance( p_intf )->toggleVisible();
173 void DialogsProvider::prefsDialog()
175 PrefsDialog *p = new PrefsDialog( (QWidget *)p_intf->p_sys->p_mi, p_intf );
179 void DialogsProvider::extendedDialog()
181 ExtendedDialog::getInstance( p_intf )->toggleVisible();
184 void DialogsProvider::synchroDialog()
186 ExtendedDialog::getInstance( p_intf )->showTab( 2 );
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( const 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( const 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 );
431 /* Url & Clipboard */
434 * If the clipboard contains URLs, the first is automatically 'preselected'.
436 void DialogsProvider::openUrlDialog()
438 OpenUrlDialog *oud = OpenUrlDialog::getInstance( p_intf->p_sys->p_mi,
440 if( oud->exec() == QDialog::Accepted )
442 QString url = oud->url();
445 playlist_Add( THEPL, qtu( url ),
446 NULL, !oud->shouldEnqueue() ?
447 ( PLAYLIST_APPEND | PLAYLIST_GO )
448 : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
449 PLAYLIST_END, true, false );
450 RecentsMRL::getInstance( p_intf )->addRecent( url );
458 * pl helps you to choose from playlist or media library,
459 * go to start or enqueue
461 static void openDirectory( intf_thread_t *p_intf, bool pl, bool go )
463 QString dir = QFileDialog::getExistingDirectory( NULL, qtr("Open Directory") );
467 QString mrl = dir.endsWith( "VIDEO_TS", Qt::CaseInsensitive ) ?
468 "dvd://" : "directory://" + toNativeSeparators( dir );
469 input_item_t *p_input = input_item_New( THEPL, qtu( mrl ), NULL );
471 /* FIXME: playlist_AddInput() can fail */
472 playlist_AddInput( THEPL, p_input,
473 go ? ( PLAYLIST_APPEND | PLAYLIST_GO ) : PLAYLIST_APPEND,
474 PLAYLIST_END, pl, pl_Unlocked );
475 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
477 input_Read( THEPL, p_input, true );
478 vlc_gc_decref( p_input );
482 void DialogsProvider::PLOpenDir()
484 openDirectory( p_intf, true, true );
487 void DialogsProvider::PLAppendDir()
489 openDirectory( p_intf, true, false );
492 void DialogsProvider::MLAppendDir()
494 openDirectory( p_intf, false , false );
500 void DialogsProvider::openAPlaylist()
502 QStringList files = showSimpleOpen( qtr( "Open playlist..." ),
503 EXT_FILTER_PLAYLIST );
504 foreach( const QString &file, files )
506 playlist_Import( THEPL, qtu( toNativeSeparators( file ) ) );
510 void DialogsProvider::saveAPlaylist()
512 QFileDialog *qfd = new QFileDialog( NULL,
513 qtr( "Save playlist as..." ),
514 qfu( p_intf->p_sys->psz_filepath ),
515 qtr( "XSPF playlist (*.xspf);; " ) +
516 qtr( "M3U playlist (*.m3u);; " ) +
517 qtr( "HTML playlist (*.html)" ) );
518 qfd->setFileMode( QFileDialog::AnyFile );
519 qfd->setAcceptMode( QFileDialog::AcceptSave );
520 qfd->setConfirmOverwrite( true );
522 if( qfd->exec() == QDialog::Accepted )
524 if( qfd->selectedFiles().count() > 0 )
526 static const char psz_xspf[] = "export-xspf",
527 psz_m3u[] = "export-m3u",
528 psz_html[] = "export-html";
529 const char *psz_module;
531 QString file = qfd->selectedFiles().first();
532 QString filter = qfd->selectedFilter();
534 if( file.contains( ".xsp" ) || filter.contains( "XSPF" ) )
536 psz_module = psz_xspf;
537 if( !file.contains( ".xsp" ) )
538 file.append( ".xspf" );
540 else if( file.contains( ".m3u" ) || filter.contains( "M3U" ) )
542 psz_module = psz_m3u;
543 if( !file.contains( ".m3u" ) )
544 file.append( ".m3u" );
546 else if( file.contains(".html" ) || filter.contains( "HTML" ) )
548 psz_module = psz_html;
549 if( !file.contains( "html" ) )
550 file.append( ".html" );
554 msg_Err( p_intf, "Impossible to recognise the file type" );
559 playlist_Export( THEPL, qtu( toNativeSeparators( file ) ),
560 THEPL->p_local_category, psz_module);
566 /****************************************************************************
568 ****************************************************************************/
570 void DialogsProvider::streamingDialog( QWidget *parent,
572 bool b_transcode_only,
573 QStringList options )
575 char *psz_soutoption;
578 if( !b_transcode_only )
580 SoutDialog *s = new SoutDialog( parent, p_intf, mrl );
581 if( s->exec() == QDialog::Accepted )
583 psz_soutoption = strdup( qtu( s->getMrl() ) );
592 ConvertDialog *s = new ConvertDialog( parent, p_intf, mrl );
593 if( s->exec() == QDialog::Accepted )
595 psz_soutoption = strdup( qtu( s->getMrl() ) );
605 if( !EMPTY_STR( psz_soutoption ) )
608 input_item_t *p_input;
609 p_input = input_item_New( p_intf, qtu( mrl ), _("Streaming") );
611 /* Add normal Options */
612 for( int j = 0; j < options.size(); j++ )
614 QString qs = colon_unescape( options[j] );
617 input_item_AddOption( p_input, qtu( qs ),
618 VLC_INPUT_OPTION_TRUSTED );
623 msg_Dbg( p_intf, "Streaming MRL is: %s", psz_soutoption );
624 input_item_AddOption( p_input, psz_soutoption, VLC_INPUT_OPTION_TRUSTED );
626 /* Switch between enqueuing and starting the item */
627 /* FIXME: playlist_AddInput() can fail */
628 playlist_AddInput( THEPL, p_input,
629 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END, true, pl_Unlocked );
630 vlc_gc_decref( p_input );
632 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
636 void DialogsProvider::openAndStreamingDialogs()
638 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false, OPEN_AND_STREAM )
639 ->showTab( OPEN_FILE_TAB );
642 void DialogsProvider::openAndTranscodingDialogs()
644 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, false, OPEN_AND_SAVE )
645 ->showTab( OPEN_FILE_TAB );
648 void DialogsProvider::loadSubtitlesFile()
650 input_thread_t *p_input = THEMIM->getInput();
651 if( !p_input ) return;
653 input_item_t *p_item = input_GetItem( p_input );
654 if( !p_item ) return;
656 char *path = input_item_GetURI( p_item );
657 if( !path ) path = strdup( "" );
659 char *sep = strrchr( path, DIR_SEP_CHAR );
660 if( sep ) *sep = '\0';
662 QStringList qsl = showSimpleOpen( qtr( "Open subtitles..." ),
666 foreach( const QString &qsFile, qsl )
668 if( input_AddSubtitle( p_input, qtu( toNativeSeparators( qsFile ) ),
670 msg_Warn( p_intf, "unable to load subtitles from '%s'",
676 /****************************************************************************
678 ****************************************************************************/
680 void DialogsProvider::menuAction( QObject *data )
682 QVLCMenu::DoAction( data );
685 void DialogsProvider::menuUpdateAction( QObject *data )
687 MenuFunc *func = qobject_cast<MenuFunc *>(data);
689 func->doFunc( p_intf );
692 void DialogsProvider::SDMenuAction( QString data )
694 char *psz_sd = strdup( qtu( data ) );
695 if( !playlist_IsServicesDiscoveryLoaded( THEPL, psz_sd ) )
696 playlist_ServicesDiscoveryAdd( THEPL, psz_sd );
698 playlist_ServicesDiscoveryRemove( THEPL, psz_sd );
703 * Play the MRL contained in the Recently played menu.
705 void DialogsProvider::playMRL( const QString &mrl )
707 playlist_Add( THEPL, qtu( mrl ) , NULL,
708 PLAYLIST_APPEND | PLAYLIST_GO , PLAYLIST_END, true, false );
710 RecentsMRL::getInstance( p_intf )->addRecent( mrl );