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