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 <QApplication>
30 #include <QSignalMapper>
31 #include <QFileDialog>
33 #include <vlc_common.h>
35 #include "dialogs_provider.hpp"
36 #include "main_interface.hpp"
38 #include <vlc_intf_strings.h>
39 #include "input_manager.hpp"
40 #include "recents.hpp"
43 #include "dialogs/playlist.hpp"
44 #include "dialogs/bookmarks.hpp"
45 #include "dialogs/preferences.hpp"
46 #include "dialogs/mediainfo.hpp"
47 #include "dialogs/messages.hpp"
48 #include "dialogs/extended.hpp"
49 #include "dialogs/vlm.hpp"
50 #include "dialogs/sout.hpp"
51 #include "dialogs/open.hpp"
52 #include "dialogs/help.hpp"
53 #include "dialogs/gototime.hpp"
54 #include "dialogs/podcast_configuration.hpp"
56 DialogsProvider* DialogsProvider::instance = NULL;
58 DialogsProvider::DialogsProvider( intf_thread_t *_p_intf ) :
59 QObject( NULL ), p_intf( _p_intf )
63 menusMapper = new QSignalMapper();
64 CONNECT( menusMapper, mapped(QObject *), this, menuAction( QObject *) );
66 menusUpdateMapper = new QSignalMapper();
67 CONNECT( menusUpdateMapper, mapped(QObject *),
68 this, menuUpdateAction( QObject *) );
70 SDMapper = new QSignalMapper();
71 CONNECT( SDMapper, mapped (QString), this, SDMenuAction( QString ) );
74 DialogsProvider::~DialogsProvider()
76 msg_Dbg( p_intf, "Destroying the Dialog Provider" );
77 PlaylistDialog::killInstance();
78 MediaInfoDialog::killInstance();
79 MessagesDialog::killInstance();
80 ExtendedDialog::killInstance();
81 BookmarksDialog::killInstance();
82 HelpDialog::killInstance();
84 UpdateDialog::killInstance();
88 delete menusUpdateMapper;
92 void DialogsProvider::quit()
94 /* Stop the playlist */
95 playlist_Stop( THEPL );
97 vlc_object_kill( p_intf->p_libvlc );
98 QApplication::closeAllWindows();
102 void DialogsProvider::customEvent( QEvent *event )
104 if( event->type() == DialogEvent_Type )
106 DialogEvent *de = static_cast<DialogEvent*>(event);
107 switch( de->i_dialog )
109 case INTF_DIALOG_FILE_SIMPLE:
110 case INTF_DIALOG_FILE:
112 case INTF_DIALOG_FILE_GENERIC:
113 openFileGenericDialog( de->p_arg ); break;
114 case INTF_DIALOG_DISC:
115 openDiscDialog(); break;
116 case INTF_DIALOG_NET:
117 openNetDialog(); break;
118 case INTF_DIALOG_SAT:
119 case INTF_DIALOG_CAPTURE:
120 openCaptureDialog(); break;
121 case INTF_DIALOG_DIRECTORY:
122 PLAppendDir(); break;
123 case INTF_DIALOG_PLAYLIST:
124 playlistDialog(); break;
125 case INTF_DIALOG_MESSAGES:
126 messagesDialog(); break;
127 case INTF_DIALOG_FILEINFO:
128 mediaInfoDialog(); break;
129 case INTF_DIALOG_PREFS:
130 prefsDialog(); break;
131 case INTF_DIALOG_BOOKMARKS:
132 bookmarksDialog(); break;
133 case INTF_DIALOG_EXTENDED:
134 extendedDialog(); break;
136 case INTF_DIALOG_VLM:
139 case INTF_DIALOG_INTERACTION:
140 doInteraction( de->p_arg ); break;
141 case INTF_DIALOG_POPUPMENU:
142 QVLCMenu::PopupMenu( p_intf, (de->i_arg != 0) ); break;
143 case INTF_DIALOG_AUDIOPOPUPMENU:
144 QVLCMenu::AudioPopupMenu( p_intf ); break;
145 case INTF_DIALOG_VIDEOPOPUPMENU:
146 QVLCMenu::VideoPopupMenu( p_intf ); break;
147 case INTF_DIALOG_MISCPOPUPMENU:
148 QVLCMenu::MiscPopupMenu( p_intf ); break;
149 case INTF_DIALOG_WIZARD:
150 case INTF_DIALOG_STREAMWIZARD:
151 openThenStreamingDialogs(); break;
153 case INTF_DIALOG_UPDATEVLC:
154 updateDialog(); break;
156 case INTF_DIALOG_EXIT:
159 msg_Warn( p_intf, "unimplemented dialog" );
164 /****************************************************************************
165 * Individual simple dialogs
166 ****************************************************************************/
167 void DialogsProvider::playlistDialog()
169 PlaylistDialog::getInstance( p_intf )->toggleVisible();
172 void DialogsProvider::prefsDialog()
174 PrefsDialog::getInstance( p_intf )->toggleVisible();
177 void DialogsProvider::extendedDialog()
179 ExtendedDialog::getInstance( p_intf )->toggleVisible();
182 void DialogsProvider::messagesDialog()
184 MessagesDialog::getInstance( p_intf )->toggleVisible();
187 void DialogsProvider::gotoTimeDialog()
189 GotoTimeDialog::getInstance( p_intf )->toggleVisible();
193 void DialogsProvider::vlmDialog()
195 VLMDialog::getInstance( p_intf )->toggleVisible();
199 void DialogsProvider::helpDialog()
201 HelpDialog::getInstance( p_intf )->toggleVisible();
205 void DialogsProvider::updateDialog()
207 UpdateDialog::getInstance( p_intf )->toggleVisible();
211 void DialogsProvider::aboutDialog()
213 AboutDialog::getInstance( p_intf )->toggleVisible();
216 void DialogsProvider::mediaInfoDialog()
218 MediaInfoDialog::getInstance( p_intf )->toggleVisible();
221 void DialogsProvider::mediaCodecDialog()
223 MediaInfoDialog::getInstance( p_intf )->showTab( 2 );
226 void DialogsProvider::bookmarksDialog()
228 BookmarksDialog::getInstance( p_intf )->toggleVisible();
231 void DialogsProvider::podcastConfigureDialog()
233 PodcastConfigDialog::getInstance( p_intf )->toggleVisible();
237 /****************************************************************************
238 * All the open/add stuff
239 * Open Dialog first - Simple Open then
240 ****************************************************************************/
242 void DialogsProvider::openDialog( int i_tab )
244 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf )->showTab( i_tab );
246 void DialogsProvider::openDialog()
248 openDialog( OPEN_FILE_TAB );
250 void DialogsProvider::openFileGenericDialog( intf_dialog_args_t *p_arg )
254 msg_Warn( p_intf, "openFileGenericDialog() called with NULL arg" );
258 /* Replace the extensions to a Qt format */
260 QString extensions = qfu( p_arg->psz_extensions );
261 while ( ( i = extensions.indexOf( "|", i ) ) != -1 )
263 if( ( extensions.count( "|" ) % 2 ) == 0 )
264 extensions.replace( i, 1, ");;" );
266 extensions.replace( i, 1, "(" );
268 extensions.replace(QString(";*"), QString(" *"));
269 extensions.append( ")" );
274 QString file = QFileDialog::getSaveFileName( NULL, p_arg->psz_title,
275 qfu( p_intf->p_sys->psz_filepath ), extensions );
276 if( !file.isEmpty() )
278 p_arg->i_results = 1;
279 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
280 p_arg->psz_results[0] = strdup( qtu( toNativeSepNoSlash( file ) ) );
283 p_arg->i_results = 0;
285 else /* non-save mode */
287 QStringList files = QFileDialog::getOpenFileNames( NULL,
288 p_arg->psz_title, qfu( p_intf->p_sys->psz_filepath ),
290 p_arg->i_results = files.count();
291 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
293 foreach( QString file, files )
294 p_arg->psz_results[i++] = strdup( qtu( toNativeSepNoSlash( file ) ) );
298 if( p_arg->pf_callback )
299 p_arg->pf_callback( p_arg );
301 /* Clean afterwards */
302 if( p_arg->psz_results )
304 for( i = 0; i < p_arg->i_results; i++ )
305 free( p_arg->psz_results[i] );
306 free( p_arg->psz_results );
308 free( p_arg->psz_title );
309 free( p_arg->psz_extensions );
313 void DialogsProvider::openFileDialog()
315 openDialog( OPEN_FILE_TAB );
317 void DialogsProvider::openDiscDialog()
319 openDialog( OPEN_DISC_TAB );
321 void DialogsProvider::openNetDialog()
323 openDialog( OPEN_NETWORK_TAB );
325 void DialogsProvider::openCaptureDialog()
327 openDialog( OPEN_CAPTURE_TAB );
330 /* Same as the open one, but force the enqueue */
331 void DialogsProvider::PLAppendDialog()
333 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false, OPEN_AND_ENQUEUE)
334 ->showTab( OPEN_FILE_TAB );
337 void DialogsProvider::MLAppendDialog()
339 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
340 OPEN_AND_ENQUEUE, false, false )
341 ->showTab( OPEN_FILE_TAB );
347 QStringList DialogsProvider::showSimpleOpen( QString help,
351 QString fileTypes = "";
352 if( filters & EXT_FILTER_MEDIA ) {
353 ADD_FILTER_MEDIA( fileTypes );
355 if( filters & EXT_FILTER_VIDEO ) {
356 ADD_FILTER_VIDEO( fileTypes );
358 if( filters & EXT_FILTER_AUDIO ) {
359 ADD_FILTER_AUDIO( fileTypes );
361 if( filters & EXT_FILTER_PLAYLIST ) {
362 ADD_FILTER_PLAYLIST( fileTypes );
364 if( filters & EXT_FILTER_SUBTITLE ) {
365 ADD_FILTER_SUBTITLE( fileTypes );
367 ADD_FILTER_ALL( fileTypes );
368 fileTypes.replace(QString(";*"), QString(" *"));
370 return QFileDialog::getOpenFileNames( NULL,
371 help.isEmpty() ? qfu(I_OP_SEL_FILES ) : help,
372 path.isEmpty() ? qfu( p_intf->p_sys->psz_filepath ) : path,
378 * pl helps you to choose from playlist or media library,
379 * go to start or enqueue
381 void DialogsProvider::addFromSimple( bool pl, bool go)
383 QStringList files = DialogsProvider::showSimpleOpen();
385 foreach( QString file, files )
387 playlist_Add( THEPL, qtu( toNativeSeparators( file ) ), NULL,
388 go ? ( PLAYLIST_APPEND | ( i ? 0 : PLAYLIST_GO ) |
389 ( i ? PLAYLIST_PREPARSE : 0 ) )
390 : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
392 pl ? true : false, false );
393 RecentsMRL::getInstance( p_intf )->addRecent(
394 toNativeSeparators( file ) );
399 void DialogsProvider::simpleOpenDialog()
401 addFromSimple( true, true ); /* Playlist and Go */
404 void DialogsProvider::simplePLAppendDialog()
406 addFromSimple( true, false );
409 void DialogsProvider::simpleMLAppendDialog()
411 addFromSimple( false, false );
417 * pl helps you to choose from playlist or media library,
418 * go to start or enqueue
420 static void openDirectory( intf_thread_t *p_intf, bool pl, bool go )
422 QString dir = QFileDialog::getExistingDirectory( NULL, qtr("Open Directory") );
426 QString mrl = dir.endsWith( "VIDEO_TS", Qt::CaseInsensitive )
427 ? "dvd://" : "directory://" + toNativeSeparators( dir );
428 msg_Dbg( p_intf, "Directory opening: %s", qtu( dir ) );
429 input_item_t *p_input = input_item_NewExt( THEPL, qtu( mrl ),
432 /* FIXME: playlist_AddInput() can fail */
433 playlist_AddInput( THEPL, p_input,
434 go ? ( PLAYLIST_APPEND | PLAYLIST_GO ) : PLAYLIST_APPEND,
435 PLAYLIST_END, pl, pl_Unlocked );
436 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
438 input_Read( THEPL, p_input, true );
439 vlc_gc_decref( p_input );
443 void DialogsProvider::PLOpenDir()
445 openDirectory( p_intf, true, true );
448 void DialogsProvider::PLAppendDir()
450 openDirectory( p_intf, true, false );
453 void DialogsProvider::MLAppendDir()
455 openDirectory( p_intf, false , false );
461 void DialogsProvider::openAPlaylist()
463 QStringList files = showSimpleOpen( qtr( "Open playlist..." ),
464 EXT_FILTER_PLAYLIST );
465 foreach( QString file, files )
467 playlist_Import( THEPL, qtu( toNativeSeparators( file ) ) );
471 void DialogsProvider::saveAPlaylist()
473 QFileDialog *qfd = new QFileDialog( NULL,
474 qtr( "Save playlist as..." ),
475 qfu( p_intf->p_sys->psz_filepath ),
476 qtr( "XSPF playlist (*.xspf);; " ) +
477 qtr( "M3U playlist (*.m3u);; Any (*.*) " ) );
478 qfd->setFileMode( QFileDialog::AnyFile );
479 qfd->setAcceptMode( QFileDialog::AcceptSave );
480 qfd->setConfirmOverwrite( true );
482 if( qfd->exec() == QDialog::Accepted )
484 if( qfd->selectedFiles().count() > 0 )
486 static const char psz_xspf[] = "export-xspf",
487 psz_m3u[] = "export-m3u";
488 const char *psz_module;
490 QString file = qfd->selectedFiles().first();
491 QString filter = qfd->selectedFilter();
493 if( file.contains( ".xsp" ) ||
494 ( filter.contains( ".xspf" ) && !file.contains( ".m3u" ) ) )
496 psz_module = psz_xspf;
497 if( !file.contains( ".xsp" ) )
498 file.append( ".xspf" );
502 psz_module = psz_m3u;
503 if( !file.contains( ".m3u" ) )
504 file.append( ".m3u" );
507 playlist_Export( THEPL, qtu( toNativeSeparators( file ) ),
508 THEPL->p_local_category, psz_module);
515 /****************************************************************************
517 ****************************************************************************/
519 void DialogsProvider::streamingDialog( QWidget *parent, QString mrl,
520 bool b_transcode_only )
522 SoutDialog *s = SoutDialog::getInstance( parent, p_intf, b_transcode_only );
524 if( s->exec() == QDialog::Accepted )
526 msg_Dbg( p_intf, "Sout mrl %s", qta( s->getMrl() ) );
528 int i_len = strlen( qtu( s->getMrl() ) ) + 10;
529 char *psz_option = (char*)malloc( i_len );
530 snprintf( psz_option, i_len - 1, "%s", qtu( s->getMrl() ) );
532 playlist_AddExt( THEPL, qtu( mrl ), "Streaming",
533 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END,
534 -1, &psz_option, 1, true, pl_Unlocked );
535 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
539 void DialogsProvider::openThenStreamingDialogs()
541 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false, OPEN_AND_STREAM )
542 ->showTab( OPEN_FILE_TAB );
545 void DialogsProvider::openThenTranscodingDialogs()
547 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, false, OPEN_AND_SAVE )
548 ->showTab( OPEN_FILE_TAB );
551 /****************************************************************************
552 * Menus / Interaction
553 ****************************************************************************/
555 void DialogsProvider::menuAction( QObject *data )
557 QVLCMenu::DoAction( p_intf, data );
560 void DialogsProvider::menuUpdateAction( QObject *data )
562 MenuFunc * f = qobject_cast<MenuFunc *>(data);
566 void DialogsProvider::SDMenuAction( QString data )
568 char *psz_sd = strdup( qtu( data ) );
569 if( !playlist_IsServicesDiscoveryLoaded( THEPL, psz_sd ) )
570 playlist_ServicesDiscoveryAdd( THEPL, psz_sd );
572 playlist_ServicesDiscoveryRemove( THEPL, psz_sd );
576 void DialogsProvider::doInteraction( intf_dialog_args_t *p_arg )
578 InteractionDialog *qdialog;
579 interaction_dialog_t *p_dialog = p_arg->p_dialog;
580 switch( p_dialog->i_action )
583 qdialog = new InteractionDialog( p_intf, p_dialog );
584 p_dialog->p_private = (void*)qdialog;
585 if( !(p_dialog->i_status == ANSWERED_DIALOG) )
588 case INTERACT_UPDATE:
589 qdialog = (InteractionDialog*)(p_dialog->p_private);
594 /* The INTERACT_NEW message was forgotten
595 so we must create the dialog and update it*/
596 qdialog = new InteractionDialog( p_intf, p_dialog );
597 p_dialog->p_private = (void*)qdialog;
598 if( !(p_dialog->i_status == ANSWERED_DIALOG) )
605 msg_Dbg( p_intf, "Hide the Interaction Dialog" );
606 qdialog = (InteractionDialog*)(p_dialog->p_private);
609 p_dialog->i_status = HIDDEN_DIALOG;
611 case INTERACT_DESTROY:
612 msg_Dbg( p_intf, "Destroy the Interaction Dialog" );
613 qdialog = (InteractionDialog*)(p_dialog->p_private);
614 if( !p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
616 p_dialog->i_status = DESTROYED_DIALOG;
621 void DialogsProvider::loadSubtitlesFile()
623 input_thread_t *p_input = THEMIM->getInput();
626 input_item_t *p_item = input_GetItem( p_input );
629 char *path = input_item_GetURI( p_item );
632 char *sep = strrchr( path, DIR_SEP_CHAR );
635 QStringList qsl = showSimpleOpen( qtr( "Open subtitles..." ),
640 foreach( qsFile, qsl )
642 if( !input_AddSubtitles( p_input, qtu( toNativeSeparators( qsFile ) ),
644 msg_Warn( p_intf, "unable to load subtitles from '%s'",
650 * Play the MRL contained in the Recently played menu.
652 void DialogsProvider::playMRL( const QString &mrl )
654 input_item_t *p_input = input_item_New( p_intf,
656 playlist_AddInput( THEPL, p_input, PLAYLIST_GO,
657 PLAYLIST_END, true, pl_Unlocked );
658 vlc_gc_decref( p_input );
660 RecentsMRL::getInstance( p_intf )->addRecent( mrl );