]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs_provider.cpp
Don't include config.h from the headers - refs #297.
[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/vlc.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
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 DialogsProvider* DialogsProvider::instance = NULL;
55
56 DialogsProvider::DialogsProvider( intf_thread_t *_p_intf ) :
57                                   QObject( NULL ), p_intf( _p_intf )
58 {
59     fixed_timer = new QTimer( this );
60     fixed_timer->start( 150 /* milliseconds */ );
61
62     menusMapper = new QSignalMapper();
63     CONNECT( menusMapper, mapped(QObject *), this, menuAction( QObject *) );
64
65     menusUpdateMapper = new QSignalMapper();
66     CONNECT( menusUpdateMapper, mapped(QObject *),
67              this, menuUpdateAction( QObject *) );
68
69     SDMapper = new QSignalMapper();
70     CONNECT( SDMapper, mapped (QString), this, SDMenuAction( QString ) );
71 }
72
73 DialogsProvider::~DialogsProvider()
74 {
75     PlaylistDialog::killInstance();
76     MediaInfoDialog::killInstance();
77     fixed_timer->stop();
78 }
79
80 void DialogsProvider::quit()
81 {
82     vlc_object_kill( p_intf );
83     QApplication::closeAllWindows();
84     QApplication::quit();
85 }
86
87 void DialogsProvider::customEvent( QEvent *event )
88 {
89     if( event->type() == DialogEvent_Type )
90     {
91         DialogEvent *de = static_cast<DialogEvent*>(event);
92         switch( de->i_dialog )
93         {
94         case INTF_DIALOG_FILE_SIMPLE:
95         case INTF_DIALOG_FILE:
96             openDialog(); break;
97         case INTF_DIALOG_DISC:
98             openDiscDialog(); break;
99         case INTF_DIALOG_NET:
100             openNetDialog(); break;
101         case INTF_DIALOG_SAT:
102         case INTF_DIALOG_CAPTURE:
103             openCaptureDialog(); break;
104         case INTF_DIALOG_DIRECTORY:
105             PLAppendDir(); break;
106         case INTF_DIALOG_PLAYLIST:
107             playlistDialog(); break;
108         case INTF_DIALOG_MESSAGES:
109             messagesDialog(); break;
110         case INTF_DIALOG_FILEINFO:
111            mediaInfoDialog(); break;
112         case INTF_DIALOG_PREFS:
113            prefsDialog(); break;
114         case INTF_DIALOG_BOOKMARKS:
115            bookmarksDialog(); break;
116         case INTF_DIALOG_EXTENDED:
117            extendedDialog(); break;
118 #ifdef ENABLE_VLM
119         case INTF_DIALOG_VLM:
120            vlmDialog(); break;
121 #endif
122         case INTF_DIALOG_INTERACTION:
123            doInteraction( de->p_arg ); break;
124         case INTF_DIALOG_POPUPMENU:
125            QVLCMenu::PopupMenu( p_intf, (de->i_arg != 0) ); break;
126         case INTF_DIALOG_AUDIOPOPUPMENU:
127            QVLCMenu::AudioPopupMenu( p_intf ); break;
128         case INTF_DIALOG_VIDEOPOPUPMENU:
129            QVLCMenu::VideoPopupMenu( p_intf ); break;
130         case INTF_DIALOG_MISCPOPUPMENU:
131            QVLCMenu::MiscPopupMenu( p_intf ); break;
132         case INTF_DIALOG_WIZARD:
133         case INTF_DIALOG_STREAMWIZARD:
134             openThenStreamingDialogs(); break;
135 #ifdef UPDATE_CHECK
136         case INTF_DIALOG_UPDATEVLC:
137             updateDialog(); break;
138 #endif
139         case INTF_DIALOG_EXIT:
140             quit(); break;
141         default:
142            msg_Warn( p_intf, "unimplemented dialog" );
143         }
144     }
145 }
146
147 /****************************************************************************
148  * Individual simple dialogs
149  ****************************************************************************/
150 void DialogsProvider::playlistDialog()
151 {
152     PlaylistDialog::getInstance( p_intf )->toggleVisible();
153 }
154
155 void DialogsProvider::prefsDialog()
156 {
157     PrefsDialog::getInstance( p_intf )->toggleVisible();
158 }
159
160 void DialogsProvider::extendedDialog()
161 {
162     ExtendedDialog::getInstance( p_intf )->toggleVisible();
163 }
164
165 void DialogsProvider::messagesDialog()
166 {
167     MessagesDialog::getInstance( p_intf )->toggleVisible();
168 }
169
170 void DialogsProvider::gotoTimeDialog()
171 {
172     GotoTimeDialog::getInstance( p_intf )->toggleVisible();
173 }
174
175 #ifdef ENABLE_VLM
176 void DialogsProvider::vlmDialog()
177 {
178     VLMDialog::getInstance( p_intf )->toggleVisible();
179 }
180 #endif
181
182 void DialogsProvider::helpDialog()
183 {
184     HelpDialog::getInstance( p_intf )->toggleVisible();
185 }
186
187 #ifdef UPDATE_CHECK
188 void DialogsProvider::updateDialog()
189 {
190     UpdateDialog::getInstance( p_intf )->toggleVisible();
191 }
192 #endif
193
194 void DialogsProvider::aboutDialog()
195 {
196     AboutDialog::getInstance( p_intf )->toggleVisible();
197 }
198
199 void DialogsProvider::mediaInfoDialog()
200 {
201     MediaInfoDialog::getInstance( p_intf )->toggleVisible();
202 }
203
204 void DialogsProvider::mediaCodecDialog()
205 {
206     MediaInfoDialog::getInstance( p_intf )->showTab( 2 );
207 }
208
209 void DialogsProvider::bookmarksDialog()
210 {
211     BookmarksDialog::getInstance( p_intf )->toggleVisible();
212 }
213
214 void DialogsProvider::podcastConfigureDialog()
215 {
216     PodcastConfigDialog::getInstance( p_intf )->toggleVisible();
217 }
218
219
220 /****************************************************************************
221  * All the open/add stuff
222  * Open Dialog first - Simple Open then
223  ****************************************************************************/
224
225 void DialogsProvider::openDialog( int i_tab )
226 {
227     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf )->showTab( i_tab );
228 }
229 void DialogsProvider::openDialog()
230 {
231     openDialog( OPEN_FILE_TAB );
232 }
233 void DialogsProvider::openFileDialog()
234 {
235     openDialog( OPEN_FILE_TAB );
236 }
237 void DialogsProvider::openDiscDialog()
238 {
239     openDialog( OPEN_DISC_TAB );
240 }
241 void DialogsProvider::openNetDialog()
242 {
243     openDialog( OPEN_NETWORK_TAB );
244 }
245 void DialogsProvider::openCaptureDialog()
246 {
247     openDialog( OPEN_CAPTURE_TAB );
248 }
249
250 /* Same as the open one, but force the enqueue */
251 void DialogsProvider::PLAppendDialog()
252 {
253     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, OPEN_AND_ENQUEUE)
254                             ->showTab( OPEN_FILE_TAB );
255 }
256
257 /* Unimplemmented yet - Usefull ? */
258 void DialogsProvider::MLAppendDialog()
259 {}
260
261 /**
262  * Simple open
263  * Not used anymore. Let the code until we are sure we don't want it
264  * Two opens make it confusing for the user.
265  ***/
266 QStringList DialogsProvider::showSimpleOpen( QString help,
267                                              int filters,
268                                              QString path )
269 {
270     QString fileTypes = "";
271     if( filters & EXT_FILTER_MEDIA ) {
272         ADD_FILTER_MEDIA( fileTypes );
273     }
274     if( filters & EXT_FILTER_VIDEO ) {
275         ADD_FILTER_VIDEO( fileTypes );
276     }
277     if( filters & EXT_FILTER_AUDIO ) {
278         ADD_FILTER_AUDIO( fileTypes );
279     }
280     if( filters & EXT_FILTER_PLAYLIST ) {
281         ADD_FILTER_PLAYLIST( fileTypes );
282     }
283     if( filters & EXT_FILTER_SUBTITLE ) {
284         ADD_FILTER_SUBTITLE( fileTypes );
285     }
286     ADD_FILTER_ALL( fileTypes );
287     fileTypes.replace(QString(";*"), QString(" *"));
288     return QFileDialog::getOpenFileNames( NULL,
289         help.isNull() ? qfu(I_OP_SEL_FILES ) : help,
290         path.isNull() ? qfu( p_intf->p_sys->psz_filepath ) : path,
291         fileTypes );
292 }
293
294 /**
295  * Open a file,
296  * pl helps you to choose from playlist or media library,
297  * go to start or enqueue
298  **/
299 void DialogsProvider::addFromSimple( bool pl, bool go)
300 {
301     QStringList files = DialogsProvider::showSimpleOpen();
302     int i = 0;
303     foreach( QString file, files )
304     {
305         const char * psz_utf8 = qtu( file );
306         playlist_Add( THEPL, psz_utf8, NULL,
307                       go ? ( PLAYLIST_APPEND | ( i ? 0 : PLAYLIST_GO ) |
308                                                ( i ? PLAYLIST_PREPARSE : 0 ) )
309                          : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
310                       PLAYLIST_END,
311                       pl ? VLC_TRUE : VLC_FALSE, VLC_FALSE );
312         i++;
313     }
314 }
315
316 void DialogsProvider::simpleOpenDialog()
317 {
318     addFromSimple( true, true ); /* Playlist and Go */
319 }
320
321 void DialogsProvider::simplePLAppendDialog()
322 {
323     addFromSimple( true, false );
324 }
325
326 void DialogsProvider::simpleMLAppendDialog()
327 {
328     addFromSimple( false, false );
329 }
330
331 /* Directory */
332 /**
333  * Open a directory,
334  * pl helps you to choose from playlist or media library,
335  * go to start or enqueue
336  **/
337 static void openDirectory( intf_thread_t *p_intf, bool pl, bool go )
338 {
339     QString dir = QFileDialog::getExistingDirectory( 0, qtr(I_OP_OPDIR) );
340     if (!dir.isEmpty()) {
341         input_item_t *p_input = input_ItemNewExt( THEPL,
342                                         qtu( "directory://" + dir ), NULL,
343                                         0, NULL, -1 );
344
345         playlist_AddInput( THEPL, p_input,
346                        go ? ( PLAYLIST_APPEND | PLAYLIST_GO ) : PLAYLIST_APPEND,
347                        PLAYLIST_END, pl, VLC_FALSE );
348         input_Read( THEPL, p_input, VLC_FALSE );
349     }
350 }
351
352 void DialogsProvider::PLAppendDir()
353 {
354     openDirectory( p_intf, true, false );
355 }
356
357 void DialogsProvider::MLAppendDir()
358 {
359     openDirectory( p_intf, false , false );
360 }
361
362 /****************
363  * Playlist     *
364  ****************/
365 void DialogsProvider::openAPlaylist()
366 {
367     QStringList files = showSimpleOpen( qtr( "Open playlist file" ),
368                                         EXT_FILTER_PLAYLIST );
369     foreach( QString file, files )
370     {
371         playlist_Import( THEPL, qtu(file) );
372     }
373 }
374
375 void DialogsProvider::saveAPlaylist()
376 {
377     QFileDialog *qfd = new QFileDialog( NULL,
378                                    qtr( "Choose a filename to save playlist" ),
379                                    qfu( p_intf->p_sys->psz_filepath ),
380                                    qtr( "XSPF playlist (*.xspf);; " ) +
381                                    qtr( "M3U playlist (*.m3u);; Any (*.*) " ) );
382     qfd->setFileMode( QFileDialog::AnyFile );
383     qfd->setAcceptMode( QFileDialog::AcceptSave );
384     qfd->setConfirmOverwrite( true );
385
386     if( qfd->exec() == QDialog::Accepted )
387     {
388         if( qfd->selectedFiles().count() > 0 )
389         {
390             static const char psz_xspf[] = "export-xspf",
391                               psz_m3u[] = "export-m3u";
392             const char *psz_module;
393
394             QString file = qfd->selectedFiles().first();
395             QString filter = qfd->selectedFilter();
396
397             if( file.contains( ".xsp" ) ||
398                 ( filter.contains( ".xspf" ) && !file.contains( ".m3u" ) ) )
399             {
400                 psz_module = psz_xspf;
401                 if( !file.contains( ".xsp" ) )
402                     file.append( ".xspf" );
403             }
404             else
405             {
406                 psz_module = psz_m3u;
407                 if( !file.contains( ".m3u" ) )
408                     file.append( ".m3u" );
409             }
410
411             playlist_Export( THEPL, qtu( file ), THEPL->p_local_category,
412                              psz_module);
413         }
414     }
415     delete qfd;
416 }
417
418
419 /****************************************************************************
420  * Sout emulation
421  ****************************************************************************/
422
423 void DialogsProvider::streamingDialog( QWidget *parent, QString mrl,
424                                        bool b_transcode_only )
425 {
426     SoutDialog *s = SoutDialog::getInstance( parent, p_intf, b_transcode_only );
427
428     if( s->exec() == QDialog::Accepted )
429     {
430         msg_Err( p_intf, "Sout mrl %s", qta( s->getMrl() ) );
431         /* Just do it */
432         int i_len = strlen( qtu( s->getMrl() ) ) + 10;
433         char *psz_option = (char*)malloc( i_len );
434         snprintf( psz_option, i_len - 1, "%s", qtu( s->getMrl() ) );
435
436         playlist_AddExt( THEPL, qtu( mrl ), "Streaming",
437                          PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END,
438                         -1, &psz_option, 1, VLC_TRUE, VLC_FALSE );
439     }
440 }
441
442 void DialogsProvider::openThenStreamingDialogs()
443 {
444     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, OPEN_AND_STREAM )
445                                 ->showTab( 0 );
446 }
447
448 void DialogsProvider::openThenTranscodingDialogs()
449 {
450     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, OPEN_AND_SAVE )
451                                 ->showTab( 0 );
452 }
453
454 /****************************************************************************
455  * Menus / Interaction
456  ****************************************************************************/
457
458 void DialogsProvider::menuAction( QObject *data )
459 {
460     QVLCMenu::DoAction( p_intf, data );
461 }
462
463 void DialogsProvider::menuUpdateAction( QObject *data )
464 {
465     MenuFunc * f = qobject_cast<MenuFunc *>(data);
466     f->doFunc( p_intf );
467 }
468
469 void DialogsProvider::SDMenuAction( QString data )
470 {
471     char *psz_sd = strdup( qtu( data ) );
472     if( !playlist_IsServicesDiscoveryLoaded( THEPL, psz_sd ) )
473         playlist_ServicesDiscoveryAdd( THEPL, psz_sd );
474     else
475         playlist_ServicesDiscoveryRemove( THEPL, psz_sd );
476     free( psz_sd );
477 }
478
479 void DialogsProvider::doInteraction( intf_dialog_args_t *p_arg )
480 {
481     InteractionDialog *qdialog;
482     interaction_dialog_t *p_dialog = p_arg->p_dialog;
483     switch( p_dialog->i_action )
484     {
485     case INTERACT_NEW:
486         qdialog = new InteractionDialog( p_intf, p_dialog );
487         p_dialog->p_private = (void*)qdialog;
488         if( !(p_dialog->i_status == ANSWERED_DIALOG) )
489             qdialog->show();
490         break;
491     case INTERACT_UPDATE:
492         qdialog = (InteractionDialog*)(p_dialog->p_private);
493         if( qdialog )
494             qdialog->update();
495         break;
496     case INTERACT_HIDE:
497         qdialog = (InteractionDialog*)(p_dialog->p_private);
498         if( qdialog )
499             qdialog->hide();
500         p_dialog->i_status = HIDDEN_DIALOG;
501         break;
502     case INTERACT_DESTROY:
503         qdialog = (InteractionDialog*)(p_dialog->p_private);
504         if( !p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
505             delete qdialog;
506         p_dialog->i_status = DESTROYED_DIALOG;
507         break;
508     }
509 }
510
511 void DialogsProvider::switchToSkins()
512 {
513     var_SetString( p_intf, "intf-switch", "skins2" );
514 }
515
516 void DialogsProvider::switchToWx()
517 {
518     var_SetString( p_intf, "intf-switch", "wxwidgets" );
519 }