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