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