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"
42 #include "dialogs/playlist.hpp"
43 #include "dialogs/bookmarks.hpp"
44 #include "dialogs/preferences.hpp"
45 #include "dialogs/mediainfo.hpp"
46 #include "dialogs/messages.hpp"
47 #include "dialogs/extended.hpp"
48 #include "dialogs/vlm.hpp"
49 #include "dialogs/sout.hpp"
50 #include "dialogs/open.hpp"
51 #include "dialogs/help.hpp"
52 #include "dialogs/gototime.hpp"
53 #include "dialogs/podcast_configuration.hpp"
55 DialogsProvider* DialogsProvider::instance = NULL;
57 DialogsProvider::DialogsProvider( intf_thread_t *_p_intf ) :
58 QObject( NULL ), p_intf( _p_intf )
61 fixed_timer = new QTimer( this );
62 fixed_timer->start( 150 /* milliseconds */ );
64 menusMapper = new QSignalMapper();
65 CONNECT( menusMapper, mapped(QObject *), this, menuAction( QObject *) );
67 menusUpdateMapper = new QSignalMapper();
68 CONNECT( menusUpdateMapper, mapped(QObject *),
69 this, menuUpdateAction( QObject *) );
71 SDMapper = new QSignalMapper();
72 CONNECT( SDMapper, mapped (QString), this, SDMenuAction( QString ) );
75 DialogsProvider::~DialogsProvider()
77 msg_Dbg( p_intf, "Destroying the Dialog Provider" );
78 PlaylistDialog::killInstance();
79 MediaInfoDialog::killInstance();
80 MessagesDialog::killInstance();
81 ExtendedDialog::killInstance();
82 BookmarksDialog::killInstance();
83 HelpDialog::killInstance();
85 UpdateDialog::killInstance();
90 delete menusUpdateMapper;
94 void DialogsProvider::quit()
96 /* Stop the playlist */
97 playlist_Stop( THEPL );
99 vlc_object_kill( p_intf->p_libvlc );
100 QApplication::closeAllWindows();
101 QApplication::quit();
104 void DialogsProvider::customEvent( QEvent *event )
106 if( event->type() == DialogEvent_Type )
108 DialogEvent *de = static_cast<DialogEvent*>(event);
109 switch( de->i_dialog )
111 case INTF_DIALOG_FILE_SIMPLE:
112 case INTF_DIALOG_FILE:
114 case INTF_DIALOG_FILE_GENERIC:
115 openFileGenericDialog( de->p_arg ); break;
116 case INTF_DIALOG_DISC:
117 openDiscDialog(); break;
118 case INTF_DIALOG_NET:
119 openNetDialog(); break;
120 case INTF_DIALOG_SAT:
121 case INTF_DIALOG_CAPTURE:
122 openCaptureDialog(); break;
123 case INTF_DIALOG_DIRECTORY:
124 PLAppendDir(); break;
125 case INTF_DIALOG_PLAYLIST:
126 playlistDialog(); break;
127 case INTF_DIALOG_MESSAGES:
128 messagesDialog(); break;
129 case INTF_DIALOG_FILEINFO:
130 mediaInfoDialog(); break;
131 case INTF_DIALOG_PREFS:
132 prefsDialog(); break;
133 case INTF_DIALOG_BOOKMARKS:
134 bookmarksDialog(); break;
135 case INTF_DIALOG_EXTENDED:
136 extendedDialog(); break;
138 case INTF_DIALOG_VLM:
141 case INTF_DIALOG_INTERACTION:
142 doInteraction( de->p_arg ); break;
143 case INTF_DIALOG_POPUPMENU:
144 QVLCMenu::PopupMenu( p_intf, (de->i_arg != 0) ); break;
145 case INTF_DIALOG_AUDIOPOPUPMENU:
146 QVLCMenu::AudioPopupMenu( p_intf ); break;
147 case INTF_DIALOG_VIDEOPOPUPMENU:
148 QVLCMenu::VideoPopupMenu( p_intf ); break;
149 case INTF_DIALOG_MISCPOPUPMENU:
150 QVLCMenu::MiscPopupMenu( p_intf ); break;
151 case INTF_DIALOG_WIZARD:
152 case INTF_DIALOG_STREAMWIZARD:
153 openThenStreamingDialogs(); break;
155 case INTF_DIALOG_UPDATEVLC:
156 updateDialog(); break;
158 case INTF_DIALOG_EXIT:
161 msg_Warn( p_intf, "unimplemented dialog" );
166 /****************************************************************************
167 * Individual simple dialogs
168 ****************************************************************************/
169 void DialogsProvider::playlistDialog()
171 PlaylistDialog::getInstance( p_intf )->toggleVisible();
174 void DialogsProvider::prefsDialog()
176 PrefsDialog::getInstance( p_intf )->toggleVisible();
179 void DialogsProvider::extendedDialog()
181 ExtendedDialog::getInstance( p_intf )->toggleVisible();
184 void DialogsProvider::messagesDialog()
186 MessagesDialog::getInstance( p_intf )->toggleVisible();
189 void DialogsProvider::gotoTimeDialog()
191 GotoTimeDialog::getInstance( p_intf )->toggleVisible();
195 void DialogsProvider::vlmDialog()
197 VLMDialog::getInstance( p_intf )->toggleVisible();
201 void DialogsProvider::helpDialog()
203 HelpDialog::getInstance( p_intf )->toggleVisible();
207 void DialogsProvider::updateDialog()
209 UpdateDialog::getInstance( p_intf )->toggleVisible();
213 void DialogsProvider::aboutDialog()
215 AboutDialog::getInstance( p_intf )->toggleVisible();
218 void DialogsProvider::mediaInfoDialog()
220 MediaInfoDialog::getInstance( p_intf )->toggleVisible();
223 void DialogsProvider::mediaCodecDialog()
225 MediaInfoDialog::getInstance( p_intf )->showTab( 2 );
228 void DialogsProvider::bookmarksDialog()
230 BookmarksDialog::getInstance( p_intf )->toggleVisible();
233 void DialogsProvider::podcastConfigureDialog()
235 PodcastConfigDialog::getInstance( p_intf )->toggleVisible();
239 /****************************************************************************
240 * All the open/add stuff
241 * Open Dialog first - Simple Open then
242 ****************************************************************************/
244 void DialogsProvider::openDialog( int i_tab )
246 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf )->showTab( i_tab );
248 void DialogsProvider::openDialog()
250 openDialog( OPEN_FILE_TAB );
252 void DialogsProvider::openFileGenericDialog( intf_dialog_args_t *p_arg )
256 msg_Warn( p_intf, "openFileGenericDialog() called with NULL arg" );
260 /* Replace the extensions to a Qt format */
262 QString extensions = qfu( p_arg->psz_extensions );
263 while ( ( i = extensions.indexOf( "|", i ) ) != -1 )
265 if( ( extensions.count( "|" ) % 2 ) == 0 )
266 extensions.replace( i, 1, ");;" );
268 extensions.replace( i, 1, "(" );
270 extensions.replace(QString(";*"), QString(" *"));
271 extensions.append( ")" );
276 QString file = QFileDialog::getSaveFileName( NULL, p_arg->psz_title,
277 qfu( p_intf->p_sys->psz_filepath ), extensions );
278 if( !file.isEmpty() )
280 p_arg->i_results = 1;
281 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
282 p_arg->psz_results[0] = strdup( qtu( toNativeSepNoSlash( file ) ) );
285 p_arg->i_results = 0;
287 else /* non-save mode */
289 QStringList files = QFileDialog::getOpenFileNames( NULL,
290 p_arg->psz_title, qfu( p_intf->p_sys->psz_filepath ),
292 p_arg->i_results = files.count();
293 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
295 foreach( QString file, files )
296 p_arg->psz_results[i++] = strdup( qtu( toNativeSepNoSlash( file ) ) );
300 if( p_arg->pf_callback )
301 p_arg->pf_callback( p_arg );
303 /* Clean afterwards */
304 if( p_arg->psz_results )
306 for( i = 0; i < p_arg->i_results; i++ )
307 free( p_arg->psz_results[i] );
308 free( p_arg->psz_results );
310 free( p_arg->psz_title );
311 free( p_arg->psz_extensions );
315 void DialogsProvider::openFileDialog()
317 openDialog( OPEN_FILE_TAB );
319 void DialogsProvider::openDiscDialog()
321 openDialog( OPEN_DISC_TAB );
323 void DialogsProvider::openNetDialog()
325 openDialog( OPEN_NETWORK_TAB );
327 void DialogsProvider::openCaptureDialog()
329 openDialog( OPEN_CAPTURE_TAB );
332 /* Same as the open one, but force the enqueue */
333 void DialogsProvider::PLAppendDialog()
335 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false, OPEN_AND_ENQUEUE)
336 ->showTab( OPEN_FILE_TAB );
339 void DialogsProvider::MLAppendDialog()
341 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false,
342 OPEN_AND_ENQUEUE, false, false )
343 ->showTab( OPEN_FILE_TAB );
349 QStringList DialogsProvider::showSimpleOpen( QString help,
353 QString fileTypes = "";
354 if( filters & EXT_FILTER_MEDIA ) {
355 ADD_FILTER_MEDIA( fileTypes );
357 if( filters & EXT_FILTER_VIDEO ) {
358 ADD_FILTER_VIDEO( fileTypes );
360 if( filters & EXT_FILTER_AUDIO ) {
361 ADD_FILTER_AUDIO( fileTypes );
363 if( filters & EXT_FILTER_PLAYLIST ) {
364 ADD_FILTER_PLAYLIST( fileTypes );
366 if( filters & EXT_FILTER_SUBTITLE ) {
367 ADD_FILTER_SUBTITLE( fileTypes );
369 ADD_FILTER_ALL( fileTypes );
370 fileTypes.replace(QString(";*"), QString(" *"));
372 return QFileDialog::getOpenFileNames( NULL,
373 help.isEmpty() ? qfu(I_OP_SEL_FILES ) : help,
374 path.isEmpty() ? qfu( p_intf->p_sys->psz_filepath ) : path,
380 * pl helps you to choose from playlist or media library,
381 * go to start or enqueue
383 void DialogsProvider::addFromSimple( bool pl, bool go)
385 QStringList files = DialogsProvider::showSimpleOpen();
387 foreach( QString file, files )
389 const char * psz_utf8 = qtu( toNativeSeparators( file ) );
390 playlist_Add( THEPL, psz_utf8, NULL,
391 go ? ( PLAYLIST_APPEND | ( i ? 0 : PLAYLIST_GO ) |
392 ( i ? PLAYLIST_PREPARSE : 0 ) )
393 : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
395 pl ? true : false, false );
400 void DialogsProvider::simpleOpenDialog()
402 addFromSimple( true, true ); /* Playlist and Go */
405 void DialogsProvider::simplePLAppendDialog()
407 addFromSimple( true, false );
410 void DialogsProvider::simpleMLAppendDialog()
412 addFromSimple( false, false );
418 * pl helps you to choose from playlist or media library,
419 * go to start or enqueue
421 static void openDirectory( intf_thread_t *p_intf, bool pl, bool go )
423 QString dir = QFileDialog::getExistingDirectory( NULL, qtr("Open Directory") );
427 input_item_t *p_input = input_item_NewExt( THEPL,
428 qtu( "directory://" + toNativeSeparators(dir) ),
431 /* FIXME: playlist_AddInput() can fail */
432 playlist_AddInput( THEPL, p_input,
433 go ? ( PLAYLIST_APPEND | PLAYLIST_GO ) : PLAYLIST_APPEND,
434 PLAYLIST_END, pl, pl_Unlocked );
436 input_Read( THEPL, p_input, true );
437 vlc_gc_decref( p_input );
441 void DialogsProvider::PLOpenDir()
443 openDirectory( p_intf, true, true );
446 void DialogsProvider::PLAppendDir()
448 openDirectory( p_intf, true, false );
451 void DialogsProvider::MLAppendDir()
453 openDirectory( p_intf, false , false );
459 void DialogsProvider::openAPlaylist()
461 QStringList files = showSimpleOpen( qtr( "Open playlist..." ),
462 EXT_FILTER_PLAYLIST );
463 foreach( QString file, files )
465 playlist_Import( THEPL, qtu( toNativeSeparators( file ) ) );
469 void DialogsProvider::saveAPlaylist()
471 QFileDialog *qfd = new QFileDialog( NULL,
472 qtr( "Save playlist as..." ),
473 qfu( p_intf->p_sys->psz_filepath ),
474 qtr( "XSPF playlist (*.xspf);; " ) +
475 qtr( "M3U playlist (*.m3u);; Any (*.*) " ) );
476 qfd->setFileMode( QFileDialog::AnyFile );
477 qfd->setAcceptMode( QFileDialog::AcceptSave );
478 qfd->setConfirmOverwrite( true );
480 if( qfd->exec() == QDialog::Accepted )
482 if( qfd->selectedFiles().count() > 0 )
484 static const char psz_xspf[] = "export-xspf",
485 psz_m3u[] = "export-m3u";
486 const char *psz_module;
488 QString file = qfd->selectedFiles().first();
489 QString filter = qfd->selectedFilter();
491 if( file.contains( ".xsp" ) ||
492 ( filter.contains( ".xspf" ) && !file.contains( ".m3u" ) ) )
494 psz_module = psz_xspf;
495 if( !file.contains( ".xsp" ) )
496 file.append( ".xspf" );
500 psz_module = psz_m3u;
501 if( !file.contains( ".m3u" ) )
502 file.append( ".m3u" );
505 playlist_Export( THEPL, qtu( toNativeSeparators( file ) ),
506 THEPL->p_local_category, psz_module);
513 /****************************************************************************
515 ****************************************************************************/
517 void DialogsProvider::streamingDialog( QWidget *parent, QString mrl,
518 bool b_transcode_only )
520 SoutDialog *s = SoutDialog::getInstance( parent, p_intf, b_transcode_only );
522 if( s->exec() == QDialog::Accepted )
524 msg_Dbg( p_intf, "Sout mrl %s", qta( s->getMrl() ) );
526 int i_len = strlen( qtu( s->getMrl() ) ) + 10;
527 char *psz_option = (char*)malloc( i_len );
528 snprintf( psz_option, i_len - 1, "%s", qtu( s->getMrl() ) );
530 playlist_AddExt( THEPL, qtu( mrl ), "Streaming",
531 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END,
532 -1, &psz_option, 1, true, pl_Unlocked );
536 void DialogsProvider::openThenStreamingDialogs()
538 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false, OPEN_AND_STREAM )
539 ->showTab( OPEN_FILE_TAB );
542 void DialogsProvider::openThenTranscodingDialogs()
544 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, false, OPEN_AND_SAVE )
545 ->showTab( OPEN_FILE_TAB );
548 /****************************************************************************
549 * Menus / Interaction
550 ****************************************************************************/
552 void DialogsProvider::menuAction( QObject *data )
554 QVLCMenu::DoAction( p_intf, data );
557 void DialogsProvider::menuUpdateAction( QObject *data )
559 MenuFunc * f = qobject_cast<MenuFunc *>(data);
563 void DialogsProvider::SDMenuAction( QString data )
565 char *psz_sd = strdup( qtu( data ) );
566 if( !playlist_IsServicesDiscoveryLoaded( THEPL, psz_sd ) )
567 playlist_ServicesDiscoveryAdd( THEPL, psz_sd );
569 playlist_ServicesDiscoveryRemove( THEPL, psz_sd );
573 void DialogsProvider::doInteraction( intf_dialog_args_t *p_arg )
575 InteractionDialog *qdialog;
576 interaction_dialog_t *p_dialog = p_arg->p_dialog;
577 switch( p_dialog->i_action )
580 qdialog = new InteractionDialog( p_intf, p_dialog );
581 p_dialog->p_private = (void*)qdialog;
582 if( !(p_dialog->i_status == ANSWERED_DIALOG) )
585 case INTERACT_UPDATE:
586 qdialog = (InteractionDialog*)(p_dialog->p_private);
591 /* The INTERACT_NEW message was forgotten
592 so we must create the dialog and update it*/
593 qdialog = new InteractionDialog( p_intf, p_dialog );
594 p_dialog->p_private = (void*)qdialog;
595 if( !(p_dialog->i_status == ANSWERED_DIALOG) )
602 msg_Dbg( p_intf, "Hide the Interaction Dialog" );
603 qdialog = (InteractionDialog*)(p_dialog->p_private);
606 p_dialog->i_status = HIDDEN_DIALOG;
608 case INTERACT_DESTROY:
609 msg_Dbg( p_intf, "Destroy the Interaction Dialog" );
610 qdialog = (InteractionDialog*)(p_dialog->p_private);
611 if( !p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
613 p_dialog->i_status = DESTROYED_DIALOG;
618 void DialogsProvider::loadSubtitlesFile()
620 input_thread_t *p_input = THEMIM->getInput();
623 input_item_t *p_item = input_GetItem( p_input );
626 char *path = input_item_GetURI( p_item );
629 char *sep = strrchr( path, DIR_SEP_CHAR );
632 QStringList qsl = showSimpleOpen( qtr( "Open subtitles..." ),
637 foreach( qsFile, qsl )
639 if( !input_AddSubtitles( p_input, qtu( toNativeSeparators( qsFile ) ),
641 msg_Warn( p_intf, "unable to load subtitles from '%s'",