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