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