1443b4e09dc0cf33060f78d5a317b618c61fa00d
[vlc.git] / modules / gui / qt4 / dialogs_provider.cpp
1 /*****************************************************************************
2  * dialogs_provider.cpp : Dialog Provider
3  *****************************************************************************
4  * Copyright (C) 2006-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <QEvent>
29 #include <QApplication>
30 #include <QSignalMapper>
31 #include <QFileDialog>
32
33 #include <vlc_common.h>
34 #include "qt4.hpp"
35 #include "dialogs_provider.hpp"
36 #include "main_interface.hpp"
37 #include "menus.hpp"
38 #include <vlc_intf_strings.h>
39 #include "input_manager.hpp"
40
41 /* The dialogs */
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"
54
55 DialogsProvider* DialogsProvider::instance = NULL;
56
57 DialogsProvider::DialogsProvider( intf_thread_t *_p_intf ) :
58                                   QObject( NULL ), p_intf( _p_intf )
59 {
60     b_isDying = false;
61     fixed_timer = new QTimer( this );
62     fixed_timer->start( 150 /* milliseconds */ );
63
64     menusMapper = new QSignalMapper();
65     CONNECT( menusMapper, mapped(QObject *), this, menuAction( QObject *) );
66
67     menusUpdateMapper = new QSignalMapper();
68     CONNECT( menusUpdateMapper, mapped(QObject *),
69              this, menuUpdateAction( QObject *) );
70
71     SDMapper = new QSignalMapper();
72     CONNECT( SDMapper, mapped (QString), this, SDMenuAction( QString ) );
73 }
74
75 DialogsProvider::~DialogsProvider()
76 {
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();
84 #ifdef UPDATE_CHECK
85     UpdateDialog::killInstance();
86 #endif
87
88     fixed_timer->stop();
89     delete menusMapper;
90     delete menusUpdateMapper;
91     delete SDMapper;
92 }
93
94 void DialogsProvider::quit()
95 {
96     /* Stop the playlist */
97     playlist_Stop( THEPL );
98     b_isDying = true;
99     vlc_object_kill( p_intf->p_libvlc );
100     QApplication::closeAllWindows();
101     QApplication::quit();
102 }
103
104 void DialogsProvider::customEvent( QEvent *event )
105 {
106     if( event->type() == DialogEvent_Type )
107     {
108         DialogEvent *de = static_cast<DialogEvent*>(event);
109         switch( de->i_dialog )
110         {
111         case INTF_DIALOG_FILE_SIMPLE:
112         case INTF_DIALOG_FILE:
113             openDialog(); break;
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;
137 #ifdef ENABLE_VLM
138         case INTF_DIALOG_VLM:
139            vlmDialog(); break;
140 #endif
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;
154 #ifdef UPDATE_CHECK
155         case INTF_DIALOG_UPDATEVLC:
156             updateDialog(); break;
157 #endif
158         case INTF_DIALOG_EXIT:
159             quit(); break;
160         default:
161            msg_Warn( p_intf, "unimplemented dialog" );
162         }
163     }
164 }
165
166 /****************************************************************************
167  * Individual simple dialogs
168  ****************************************************************************/
169 void DialogsProvider::playlistDialog()
170 {
171     PlaylistDialog::getInstance( p_intf )->toggleVisible();
172 }
173
174 void DialogsProvider::prefsDialog()
175 {
176     PrefsDialog::getInstance( p_intf )->toggleVisible();
177 }
178
179 void DialogsProvider::extendedDialog()
180 {
181     ExtendedDialog::getInstance( p_intf )->toggleVisible();
182 }
183
184 void DialogsProvider::messagesDialog()
185 {
186     MessagesDialog::getInstance( p_intf )->toggleVisible();
187 }
188
189 void DialogsProvider::gotoTimeDialog()
190 {
191     GotoTimeDialog::getInstance( p_intf )->toggleVisible();
192 }
193
194 #ifdef ENABLE_VLM
195 void DialogsProvider::vlmDialog()
196 {
197     VLMDialog::getInstance( p_intf )->toggleVisible();
198 }
199 #endif
200
201 void DialogsProvider::helpDialog()
202 {
203     HelpDialog::getInstance( p_intf )->toggleVisible();
204 }
205
206 #ifdef UPDATE_CHECK
207 void DialogsProvider::updateDialog()
208 {
209     UpdateDialog::getInstance( p_intf )->toggleVisible();
210 }
211 #endif
212
213 void DialogsProvider::aboutDialog()
214 {
215     AboutDialog::getInstance( p_intf )->toggleVisible();
216 }
217
218 void DialogsProvider::mediaInfoDialog()
219 {
220     MediaInfoDialog::getInstance( p_intf )->toggleVisible();
221 }
222
223 void DialogsProvider::mediaCodecDialog()
224 {
225     MediaInfoDialog::getInstance( p_intf )->showTab( 2 );
226 }
227
228 void DialogsProvider::bookmarksDialog()
229 {
230     BookmarksDialog::getInstance( p_intf )->toggleVisible();
231 }
232
233 void DialogsProvider::podcastConfigureDialog()
234 {
235     PodcastConfigDialog::getInstance( p_intf )->toggleVisible();
236 }
237
238
239 /****************************************************************************
240  * All the open/add stuff
241  * Open Dialog first - Simple Open then
242  ****************************************************************************/
243
244 void DialogsProvider::openDialog( int i_tab )
245 {
246     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf )->showTab( i_tab );
247 }
248 void DialogsProvider::openDialog()
249 {
250     openDialog( OPEN_FILE_TAB );
251 }
252 void DialogsProvider::openFileGenericDialog( intf_dialog_args_t *p_arg )
253 {
254     if( p_arg == NULL )
255     {
256         msg_Warn( p_intf, "openFileGenericDialog() called with NULL arg" );
257         return;
258     }
259
260     /* Replace the extensions to a Qt format */
261     int i = 0;
262     QString extensions = qfu( p_arg->psz_extensions );
263     while ( ( i = extensions.indexOf( "|", i ) ) != -1 )
264     {
265         if( ( extensions.count( "|" ) % 2 ) == 0 )
266             extensions.replace( i, 1, ");;" );
267         else
268             extensions.replace( i, 1, "(" );
269     }
270     extensions.replace(QString(";*"), QString(" *"));
271     extensions.append( ")" );
272
273     /* Save */
274     if( p_arg->b_save )
275     {
276         QString file = QFileDialog::getSaveFileName( NULL, p_arg->psz_title,
277                             qfu( p_intf->p_sys->psz_filepath ), extensions );
278         if( !file.isEmpty() )
279         {
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( file ) );
283         }
284         else
285             p_arg->i_results = 0;
286     }
287     else /* non-save mode */
288     {
289         QStringList files = QFileDialog::getOpenFileNames( NULL,
290                 p_arg->psz_title, qfu( p_intf->p_sys->psz_filepath ),
291                 extensions );
292         p_arg->i_results = files.count();
293         p_arg->psz_results = (char **)malloc( p_arg->i_results * sizeof( char * ) );
294         i = 0;
295         foreach( QString file, files )
296             p_arg->psz_results[i++] = strdup( qtu( file ) );
297     }
298
299     /* Callback */
300     if( p_arg->pf_callback )
301         p_arg->pf_callback( p_arg );
302
303     /* Clean afterwards */
304     if( p_arg->psz_results )
305     {
306         for( i = 0; i < p_arg->i_results; i++ )
307             free( p_arg->psz_results[i] );
308         free( p_arg->psz_results );
309     }
310     free( p_arg->psz_title );
311     free( p_arg->psz_extensions );
312     free( p_arg );
313 }
314
315 void DialogsProvider::openFileDialog()
316 {
317     openDialog( OPEN_FILE_TAB );
318 }
319 void DialogsProvider::openDiscDialog()
320 {
321     openDialog( OPEN_DISC_TAB );
322 }
323 void DialogsProvider::openNetDialog()
324 {
325     openDialog( OPEN_NETWORK_TAB );
326 }
327 void DialogsProvider::openCaptureDialog()
328 {
329     openDialog( OPEN_CAPTURE_TAB );
330 }
331
332 /* Same as the open one, but force the enqueue */
333 void DialogsProvider::PLAppendDialog()
334 {
335     OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false, OPEN_AND_ENQUEUE)
336                             ->showTab( OPEN_FILE_TAB );
337 }
338
339 /* Unimplemmented yet - Usefull ? */
340 void DialogsProvider::MLAppendDialog()
341 {}
342
343 /**
344  * Simple open
345  ***/
346 QStringList DialogsProvider::showSimpleOpen( QString help,
347                                              int filters,
348                                              QString path )
349 {
350     QString fileTypes = "";
351     if( filters & EXT_FILTER_MEDIA ) {
352         ADD_FILTER_MEDIA( fileTypes );
353     }
354     if( filters & EXT_FILTER_VIDEO ) {
355         ADD_FILTER_VIDEO( fileTypes );
356     }
357     if( filters & EXT_FILTER_AUDIO ) {
358         ADD_FILTER_AUDIO( fileTypes );
359     }
360     if( filters & EXT_FILTER_PLAYLIST ) {
361         ADD_FILTER_PLAYLIST( fileTypes );
362     }
363     if( filters & EXT_FILTER_SUBTITLE ) {
364         ADD_FILTER_SUBTITLE( fileTypes );
365     }
366     ADD_FILTER_ALL( fileTypes );
367     fileTypes.replace(QString(";*"), QString(" *"));
368     return QFileDialog::getOpenFileNames( NULL,
369         help.isNull() ? qfu(I_OP_SEL_FILES ) : help,
370         path.isNull() ? qfu( p_intf->p_sys->psz_filepath ) : path,
371         fileTypes );
372 }
373
374 /**
375  * Open a file,
376  * pl helps you to choose from playlist or media library,
377  * go to start or enqueue
378  **/
379 void DialogsProvider::addFromSimple( bool pl, bool go)
380 {
381     QStringList files = DialogsProvider::showSimpleOpen();
382     int i = 0;
383     foreach( QString file, files )
384     {
385         const char * psz_utf8 = qtu( file );
386         playlist_Add( THEPL, psz_utf8, NULL,
387                       go ? ( PLAYLIST_APPEND | ( i ? 0 : PLAYLIST_GO ) |
388                                                ( i ? PLAYLIST_PREPARSE : 0 ) )
389                          : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
390                       PLAYLIST_END,
391                       pl ? true : false, false );
392         i++;
393     }
394 }
395
396 void DialogsProvider::simpleOpenDialog()
397 {
398     addFromSimple( true, true ); /* Playlist and Go */
399 }
400
401 void DialogsProvider::simplePLAppendDialog()
402 {
403     addFromSimple( true, false );
404 }
405
406 void DialogsProvider::simpleMLAppendDialog()
407 {
408     addFromSimple( false, false );
409 }
410
411 /* Directory */
412 /**
413  * Open a directory,
414  * pl helps you to choose from playlist or media library,
415  * go to start or enqueue
416  **/
417 static void openDirectory( intf_thread_t *p_intf, bool pl, bool go )
418 {
419     QString dir = QFileDialog::getExistingDirectory( 0, qtr("Open Directory") );
420     if (!dir.isEmpty()) {
421         input_item_t *p_input = input_item_NewExt( THEPL,
422                                         qtu( "directory://" + dir ), NULL,
423                                         0, NULL, -1 );
424
425         /* FIXME: playlist_AddInput() can fail */
426         playlist_AddInput( THEPL, p_input,
427                        go ? ( PLAYLIST_APPEND | PLAYLIST_GO ) : PLAYLIST_APPEND,
428                        PLAYLIST_END, pl, pl_Unlocked );
429         if( !go )
430             input_Read( THEPL, p_input, false );
431         vlc_gc_decref( p_input );
432     }
433 }
434
435 void DialogsProvider::PLOpenDir()
436 {
437     openDirectory( p_intf, true, true );
438 }
439
440 void DialogsProvider::PLAppendDir()
441 {
442     openDirectory( p_intf, true, false );
443 }
444
445 void DialogsProvider::MLAppendDir()
446 {
447     openDirectory( p_intf, false , false );
448 }
449
450 /****************
451  * Playlist     *
452  ****************/
453 void DialogsProvider::openAPlaylist()
454 {
455     QStringList files = showSimpleOpen( qtr( "Open playlist file" ),
456                                         EXT_FILTER_PLAYLIST );
457     foreach( QString file, files )
458     {
459         playlist_Import( THEPL, qtu(file) );
460     }
461 }
462
463 void DialogsProvider::saveAPlaylist()
464 {
465     QFileDialog *qfd = new QFileDialog( NULL,
466                                    qtr( "Choose a filename to save playlist" ),
467                                    qfu( p_intf->p_sys->psz_filepath ),
468                                    qtr( "XSPF playlist (*.xspf);; " ) +
469                                    qtr( "M3U playlist (*.m3u);; Any (*.*) " ) );
470     qfd->setFileMode( QFileDialog::AnyFile );
471     qfd->setAcceptMode( QFileDialog::AcceptSave );
472     qfd->setConfirmOverwrite( true );
473
474     if( qfd->exec() == QDialog::Accepted )
475     {
476         if( qfd->selectedFiles().count() > 0 )
477         {
478             static const char psz_xspf[] = "export-xspf",
479                               psz_m3u[] = "export-m3u";
480             const char *psz_module;
481
482             QString file = qfd->selectedFiles().first();
483             QString filter = qfd->selectedFilter();
484
485             if( file.contains( ".xsp" ) ||
486                 ( filter.contains( ".xspf" ) && !file.contains( ".m3u" ) ) )
487             {
488                 psz_module = psz_xspf;
489                 if( !file.contains( ".xsp" ) )
490                     file.append( ".xspf" );
491             }
492             else
493             {
494                 psz_module = psz_m3u;
495                 if( !file.contains( ".m3u" ) )
496                     file.append( ".m3u" );
497             }
498
499             playlist_Export( THEPL, qtu( file ), THEPL->p_local_category,
500                              psz_module);
501         }
502     }
503     delete qfd;
504 }
505
506
507 /****************************************************************************
508  * Sout emulation
509  ****************************************************************************/
510
511 void DialogsProvider::streamingDialog( QWidget *parent, QString mrl,
512                                        bool b_transcode_only )
513 {
514     SoutDialog *s = SoutDialog::getInstance( parent, p_intf, b_transcode_only );
515
516     if( s->exec() == QDialog::Accepted )
517     {
518         msg_Dbg( p_intf, "Sout mrl %s", qta( s->getMrl() ) );
519         /* Just do it */
520         int i_len = strlen( qtu( s->getMrl() ) ) + 10;
521         char *psz_option = (char*)malloc( i_len );
522         snprintf( psz_option, i_len - 1, "%s", qtu( s->getMrl() ) );
523
524         playlist_AddExt( THEPL, qtu( mrl ), "Streaming",
525                          PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END,
526                         -1, &psz_option, 1, true, pl_Unlocked );
527     }
528 }
529
530 void DialogsProvider::openThenStreamingDialogs()
531 {
532     OpenDialog::getInstance( p_intf->p_sys->p_mi, p_intf, false, OPEN_AND_STREAM )
533                                 ->showTab( OPEN_FILE_TAB );
534 }
535
536 void DialogsProvider::openThenTranscodingDialogs()
537 {
538     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, false, OPEN_AND_SAVE )
539                                 ->showTab( OPEN_FILE_TAB );
540 }
541
542 /****************************************************************************
543  * Menus / Interaction
544  ****************************************************************************/
545
546 void DialogsProvider::menuAction( QObject *data )
547 {
548     QVLCMenu::DoAction( p_intf, data );
549 }
550
551 void DialogsProvider::menuUpdateAction( QObject *data )
552 {
553     MenuFunc * f = qobject_cast<MenuFunc *>(data);
554     f->doFunc( p_intf );
555 }
556
557 void DialogsProvider::SDMenuAction( QString data )
558 {
559     char *psz_sd = strdup( qtu( data ) );
560     if( !playlist_IsServicesDiscoveryLoaded( THEPL, psz_sd ) )
561         playlist_ServicesDiscoveryAdd( THEPL, psz_sd );
562     else
563         playlist_ServicesDiscoveryRemove( THEPL, psz_sd );
564     free( psz_sd );
565 }
566
567 void DialogsProvider::doInteraction( intf_dialog_args_t *p_arg )
568 {
569     InteractionDialog *qdialog;
570     interaction_dialog_t *p_dialog = p_arg->p_dialog;
571     switch( p_dialog->i_action )
572     {
573     case INTERACT_NEW:
574         qdialog = new InteractionDialog( p_intf, p_dialog );
575         p_dialog->p_private = (void*)qdialog;
576         if( !(p_dialog->i_status == ANSWERED_DIALOG) )
577             qdialog->show();
578         break;
579     case INTERACT_UPDATE:
580         qdialog = (InteractionDialog*)(p_dialog->p_private);
581         if( qdialog )
582             qdialog->update();
583         else
584         {
585             /* The INTERACT_NEW message was forgotten
586                so we must create the dialog and update it*/
587             qdialog = new InteractionDialog( p_intf, p_dialog );
588             p_dialog->p_private = (void*)qdialog;
589             if( !(p_dialog->i_status == ANSWERED_DIALOG) )
590                 qdialog->show();
591             if( qdialog )
592                 qdialog->update();
593         }
594         break;
595     case INTERACT_HIDE:
596         msg_Dbg( p_intf, "Hide the Interaction Dialog" );
597         qdialog = (InteractionDialog*)(p_dialog->p_private);
598         if( qdialog )
599             qdialog->hide();
600         p_dialog->i_status = HIDDEN_DIALOG;
601         break;
602     case INTERACT_DESTROY:
603         msg_Dbg( p_intf, "Destroy the Interaction Dialog" );
604         qdialog = (InteractionDialog*)(p_dialog->p_private);
605         if( !p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
606             delete qdialog;
607         p_dialog->i_status = DESTROYED_DIALOG;
608         break;
609     }
610 }
611
612 void DialogsProvider::loadSubtitlesFile()
613 {
614     input_thread_t *p_input = THEMIM->getInput();
615     if( !p_input )
616         return;
617     input_item_t *p_item = input_GetItem( p_input );
618     if( !p_item )
619         return;
620     char *path = input_item_GetURI( p_item );
621     if( !path )
622         path = strdup( "" );
623     char *sep = strrchr( path, DIR_SEP_CHAR );
624     if( sep )
625         *sep = '\0';
626     QStringList qsl = showSimpleOpen( qtr( "Open subtitles file" ),
627                                       EXT_FILTER_SUBTITLE,
628                                       path );
629     free( path );
630     QString qsFile;
631     foreach( qsFile, qsl )
632     {
633         if( !input_AddSubtitles( p_input, qtu( qsFile ), true ) )
634             msg_Warn( p_intf, "unable to load subtitles from '%s'",
635                       qtu( qsFile ) );
636     }
637 }