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