1 /*****************************************************************************
2 * main_inteface.cpp : Main interface
3 *****************************************************************************
4 * Copyright (C) 2006-2007 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 )
60 fixed_timer = new QTimer( this );
61 fixed_timer->start( 150 /* milliseconds */ );
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();
90 void DialogsProvider::quit()
92 vlc_object_kill( p_intf->p_libvlc );
93 QApplication::closeAllWindows();
97 void DialogsProvider::customEvent( QEvent *event )
99 if( event->type() == DialogEvent_Type )
101 DialogEvent *de = static_cast<DialogEvent*>(event);
102 switch( de->i_dialog )
104 case INTF_DIALOG_FILE_SIMPLE:
105 case INTF_DIALOG_FILE:
107 case INTF_DIALOG_FILE_GENERIC:
108 openFileGenericDialog( de->p_arg ); break;
109 case INTF_DIALOG_DISC:
110 openDiscDialog(); break;
111 case INTF_DIALOG_NET:
112 openNetDialog(); break;
113 case INTF_DIALOG_SAT:
114 case INTF_DIALOG_CAPTURE:
115 openCaptureDialog(); break;
116 case INTF_DIALOG_DIRECTORY:
117 PLAppendDir(); break;
118 case INTF_DIALOG_PLAYLIST:
119 playlistDialog(); break;
120 case INTF_DIALOG_MESSAGES:
121 messagesDialog(); break;
122 case INTF_DIALOG_FILEINFO:
123 mediaInfoDialog(); break;
124 case INTF_DIALOG_PREFS:
125 prefsDialog(); break;
126 case INTF_DIALOG_BOOKMARKS:
127 bookmarksDialog(); break;
128 case INTF_DIALOG_EXTENDED:
129 extendedDialog(); break;
131 case INTF_DIALOG_VLM:
134 case INTF_DIALOG_INTERACTION:
135 doInteraction( de->p_arg ); break;
136 case INTF_DIALOG_POPUPMENU:
137 QVLCMenu::PopupMenu( p_intf, (de->i_arg != 0) ); break;
138 case INTF_DIALOG_AUDIOPOPUPMENU:
139 QVLCMenu::AudioPopupMenu( p_intf ); break;
140 case INTF_DIALOG_VIDEOPOPUPMENU:
141 QVLCMenu::VideoPopupMenu( p_intf ); break;
142 case INTF_DIALOG_MISCPOPUPMENU:
143 QVLCMenu::MiscPopupMenu( p_intf ); break;
144 case INTF_DIALOG_WIZARD:
145 case INTF_DIALOG_STREAMWIZARD:
146 openThenStreamingDialogs(); break;
148 case INTF_DIALOG_UPDATEVLC:
149 updateDialog(); break;
151 case INTF_DIALOG_EXIT:
154 msg_Warn( p_intf, "unimplemented dialog" );
159 /****************************************************************************
160 * Individual simple dialogs
161 ****************************************************************************/
162 void DialogsProvider::playlistDialog()
164 PlaylistDialog::getInstance( p_intf )->toggleVisible();
167 void DialogsProvider::prefsDialog()
169 PrefsDialog::getInstance( p_intf )->toggleVisible();
172 void DialogsProvider::extendedDialog()
174 ExtendedDialog::getInstance( p_intf )->toggleVisible();
177 void DialogsProvider::messagesDialog()
179 MessagesDialog::getInstance( p_intf )->toggleVisible();
182 void DialogsProvider::gotoTimeDialog()
184 GotoTimeDialog::getInstance( p_intf )->toggleVisible();
188 void DialogsProvider::vlmDialog()
190 VLMDialog::getInstance( p_intf )->toggleVisible();
194 void DialogsProvider::helpDialog()
196 HelpDialog::getInstance( p_intf )->toggleVisible();
200 void DialogsProvider::updateDialog()
202 UpdateDialog::getInstance( p_intf )->toggleVisible();
206 void DialogsProvider::aboutDialog()
208 AboutDialog::getInstance( p_intf )->toggleVisible();
211 void DialogsProvider::mediaInfoDialog()
213 MediaInfoDialog::getInstance( p_intf )->toggleVisible();
216 void DialogsProvider::mediaCodecDialog()
218 MediaInfoDialog::getInstance( p_intf )->showTab( 2 );
221 void DialogsProvider::bookmarksDialog()
223 BookmarksDialog::getInstance( p_intf )->toggleVisible();
226 void DialogsProvider::podcastConfigureDialog()
228 PodcastConfigDialog::getInstance( p_intf )->toggleVisible();
232 /****************************************************************************
233 * All the open/add stuff
234 * Open Dialog first - Simple Open then
235 ****************************************************************************/
237 void DialogsProvider::openDialog( int i_tab )
239 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf )->showTab( i_tab );
241 void DialogsProvider::openDialog()
243 openDialog( OPEN_FILE_TAB );
245 void DialogsProvider::openFileGenericDialog( intf_dialog_args_t *p_arg )
249 msg_Warn( p_intf, "openFileGenericDialog() called with NULL arg" );
253 /* Replace the extensions to a Qt format */
255 QString extensions = qfu( p_arg->psz_extensions );
256 while ( ( i = extensions.indexOf( "|", i ) ) != -1 )
258 if( ( extensions.count( "|" ) % 2 ) == 0 )
259 extensions.replace( i, 1, ");;" );
261 extensions.replace( i, 1, "(" );
263 extensions.replace(QString(";*"), QString(" *"));
264 extensions.append( ")" );
269 QString file = QFileDialog::getSaveFileName( NULL, p_arg->psz_title,
270 qfu( p_intf->p_sys->psz_filepath ), extensions );
271 if( !file.isEmpty() )
273 p_arg->i_results = 1;
274 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
275 p_arg->psz_results[0] = strdup( qtu( file ) );
278 p_arg->i_results = 0;
280 else /* non-save mode */
282 QStringList files = QFileDialog::getOpenFileNames( NULL,
283 p_arg->psz_title, qfu( p_intf->p_sys->psz_filepath ),
285 p_arg->i_results = files.count();
286 p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
288 foreach( QString file, files )
289 p_arg->psz_results[i++] = strdup( qtu( file ) );
293 if( p_arg->pf_callback )
294 p_arg->pf_callback( p_arg );
296 /* Clean afterwards */
297 if( p_arg->psz_results )
299 for( i = 0; i < p_arg->i_results; i++ )
300 free( p_arg->psz_results[i] );
301 free( p_arg->psz_results );
303 free( p_arg->psz_title );
304 free( p_arg->psz_extensions );
308 void DialogsProvider::openFileDialog()
310 openDialog( OPEN_FILE_TAB );
312 void DialogsProvider::openDiscDialog()
314 openDialog( OPEN_DISC_TAB );
316 void DialogsProvider::openNetDialog()
318 openDialog( OPEN_NETWORK_TAB );
320 void DialogsProvider::openCaptureDialog()
322 openDialog( OPEN_CAPTURE_TAB );
325 /* Same as the open one, but force the enqueue */
326 void DialogsProvider::PLAppendDialog()
328 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false, OPEN_AND_ENQUEUE)
329 ->showTab( OPEN_FILE_TAB );
332 /* Unimplemmented yet - Usefull ? */
333 void DialogsProvider::MLAppendDialog()
339 QStringList DialogsProvider::showSimpleOpen( QString help,
343 QString fileTypes = "";
344 if( filters & EXT_FILTER_MEDIA ) {
345 ADD_FILTER_MEDIA( fileTypes );
347 if( filters & EXT_FILTER_VIDEO ) {
348 ADD_FILTER_VIDEO( fileTypes );
350 if( filters & EXT_FILTER_AUDIO ) {
351 ADD_FILTER_AUDIO( fileTypes );
353 if( filters & EXT_FILTER_PLAYLIST ) {
354 ADD_FILTER_PLAYLIST( fileTypes );
356 if( filters & EXT_FILTER_SUBTITLE ) {
357 ADD_FILTER_SUBTITLE( fileTypes );
359 ADD_FILTER_ALL( fileTypes );
360 fileTypes.replace(QString(";*"), QString(" *"));
361 return QFileDialog::getOpenFileNames( NULL,
362 help.isNull() ? qfu(I_OP_SEL_FILES ) : help,
363 path.isNull() ? qfu( p_intf->p_sys->psz_filepath ) : path,
369 * pl helps you to choose from playlist or media library,
370 * go to start or enqueue
372 void DialogsProvider::addFromSimple( bool pl, bool go)
374 QStringList files = DialogsProvider::showSimpleOpen();
376 foreach( QString file, files )
378 const char * psz_utf8 = qtu( file );
379 playlist_Add( THEPL, psz_utf8, NULL,
380 go ? ( PLAYLIST_APPEND | ( i ? 0 : PLAYLIST_GO ) |
381 ( i ? PLAYLIST_PREPARSE : 0 ) )
382 : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
384 pl ? true : false, false );
389 void DialogsProvider::simpleOpenDialog()
391 addFromSimple( true, true ); /* Playlist and Go */
394 void DialogsProvider::simplePLAppendDialog()
396 addFromSimple( true, false );
399 void DialogsProvider::simpleMLAppendDialog()
401 addFromSimple( false, false );
407 * pl helps you to choose from playlist or media library,
408 * go to start or enqueue
410 static void openDirectory( intf_thread_t *p_intf, bool pl, bool go )
412 QString dir = QFileDialog::getExistingDirectory( 0, qtr("Open Directory") );
413 if (!dir.isEmpty()) {
414 input_item_t *p_input = input_ItemNewExt( THEPL,
415 qtu( "directory://" + dir ), NULL,
418 /* FIXME: playlist_AddInput() can fail */
419 playlist_AddInput( THEPL, p_input,
420 go ? ( PLAYLIST_APPEND | PLAYLIST_GO ) : PLAYLIST_APPEND,
421 PLAYLIST_END, pl, pl_Unlocked );
422 input_Read( THEPL, p_input, false );
423 vlc_gc_decref( p_input );
427 void DialogsProvider::PLAppendDir()
429 openDirectory( p_intf, true, false );
432 void DialogsProvider::MLAppendDir()
434 openDirectory( p_intf, false , false );
440 void DialogsProvider::openAPlaylist()
442 QStringList files = showSimpleOpen( qtr( "Open playlist file" ),
443 EXT_FILTER_PLAYLIST );
444 foreach( QString file, files )
446 playlist_Import( THEPL, qtu(file) );
450 void DialogsProvider::saveAPlaylist()
452 QFileDialog *qfd = new QFileDialog( NULL,
453 qtr( "Choose a filename to save playlist" ),
454 qfu( p_intf->p_sys->psz_filepath ),
455 qtr( "XSPF playlist (*.xspf);; " ) +
456 qtr( "M3U playlist (*.m3u);; Any (*.*) " ) );
457 qfd->setFileMode( QFileDialog::AnyFile );
458 qfd->setAcceptMode( QFileDialog::AcceptSave );
459 qfd->setConfirmOverwrite( true );
461 if( qfd->exec() == QDialog::Accepted )
463 if( qfd->selectedFiles().count() > 0 )
465 static const char psz_xspf[] = "export-xspf",
466 psz_m3u[] = "export-m3u";
467 const char *psz_module;
469 QString file = qfd->selectedFiles().first();
470 QString filter = qfd->selectedFilter();
472 if( file.contains( ".xsp" ) ||
473 ( filter.contains( ".xspf" ) && !file.contains( ".m3u" ) ) )
475 psz_module = psz_xspf;
476 if( !file.contains( ".xsp" ) )
477 file.append( ".xspf" );
481 psz_module = psz_m3u;
482 if( !file.contains( ".m3u" ) )
483 file.append( ".m3u" );
486 playlist_Export( THEPL, qtu( file ), THEPL->p_local_category,
494 /****************************************************************************
496 ****************************************************************************/
498 void DialogsProvider::streamingDialog( QWidget *parent, QString mrl,
499 bool b_transcode_only )
501 SoutDialog *s = SoutDialog::getInstance( parent, p_intf, b_transcode_only );
503 if( s->exec() == QDialog::Accepted )
505 msg_Dbg( p_intf, "Sout mrl %s", qta( s->getMrl() ) );
507 int i_len = strlen( qtu( s->getMrl() ) ) + 10;
508 char *psz_option = (char*)malloc( i_len );
509 snprintf( psz_option, i_len - 1, "%s", qtu( s->getMrl() ) );
511 playlist_AddExt( THEPL, qtu( mrl ), "Streaming",
512 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END,
513 -1, &psz_option, 1, true, pl_Unlocked );
517 void DialogsProvider::openThenStreamingDialogs()
519 OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false, OPEN_AND_STREAM )
520 ->showTab( OPEN_FILE_TAB );
523 void DialogsProvider::openThenTranscodingDialogs()
525 OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, false, OPEN_AND_SAVE )
526 ->showTab( OPEN_FILE_TAB );
529 /****************************************************************************
530 * Menus / Interaction
531 ****************************************************************************/
533 void DialogsProvider::menuAction( QObject *data )
535 QVLCMenu::DoAction( p_intf, data );
538 void DialogsProvider::menuUpdateAction( QObject *data )
540 MenuFunc * f = qobject_cast<MenuFunc *>(data);
544 void DialogsProvider::SDMenuAction( QString data )
546 char *psz_sd = strdup( qtu( data ) );
547 if( !playlist_IsServicesDiscoveryLoaded( THEPL, psz_sd ) )
548 playlist_ServicesDiscoveryAdd( THEPL, psz_sd );
550 playlist_ServicesDiscoveryRemove( THEPL, psz_sd );
554 void DialogsProvider::doInteraction( intf_dialog_args_t *p_arg )
556 InteractionDialog *qdialog;
557 interaction_dialog_t *p_dialog = p_arg->p_dialog;
558 switch( p_dialog->i_action )
561 qdialog = new InteractionDialog( p_intf, p_dialog );
562 p_dialog->p_private = (void*)qdialog;
563 if( !(p_dialog->i_status == ANSWERED_DIALOG) )
566 case INTERACT_UPDATE:
567 qdialog = (InteractionDialog*)(p_dialog->p_private);
572 /* The INTERACT_NEW message was forgotten
573 so we must create the dialog and update it*/
574 qdialog = new InteractionDialog( p_intf, p_dialog );
575 p_dialog->p_private = (void*)qdialog;
576 if( !(p_dialog->i_status == ANSWERED_DIALOG) )
583 qdialog = (InteractionDialog*)(p_dialog->p_private);
586 p_dialog->i_status = HIDDEN_DIALOG;
588 case INTERACT_DESTROY:
589 qdialog = (InteractionDialog*)(p_dialog->p_private);
590 if( !p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
592 p_dialog->i_status = DESTROYED_DIALOG;
597 void DialogsProvider::loadSubtitlesFile()
599 input_thread_t *p_input = THEMIM->getInput();
602 input_item_t *p_item = input_GetItem( p_input );
605 char *path = input_item_GetURI( p_item );
608 char *sep = strrchr( path, DIR_SEP_CHAR );
611 QStringList qsl = showSimpleOpen( qtr( "Open subtitles file" ),
616 foreach( qsFile, qsl )
618 if( !input_AddSubtitles( p_input, qtu( qsFile ), true ) )
619 msg_Warn( p_intf, "unable to load subtitles from '%s'",