]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs_provider.cpp
Qt4: podcast configuration, don't use a modal window, don't use a Qdailog but a QVLCF...
[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
25 #include <QEvent>
26 #include <QApplication>
27 #include <QSignalMapper>
28 #include <QFileDialog>
29
30 #include "qt4.hpp"
31 #include "dialogs_provider.hpp"
32 #include "main_interface.hpp"
33 #include "menus.hpp"
34 #include <vlc_intf_strings.h>
35
36 /* The dialogs */
37 #include "dialogs/playlist.hpp"
38 #include "dialogs/preferences.hpp"
39 #include "dialogs/mediainfo.hpp"
40 #include "dialogs/messages.hpp"
41 #include "dialogs/extended.hpp"
42 #include "dialogs/sout.hpp"
43 #include "dialogs/open.hpp"
44 #include "dialogs/vlm.hpp"
45 #include "dialogs/help.hpp"
46 #include "dialogs/gototime.hpp"
47 #include "dialogs/podcast_configuration.hpp"
48 #include "dialogs/vlm.hpp"
49
50
51 DialogsProvider* DialogsProvider::instance = NULL;
52
53 DialogsProvider::DialogsProvider( intf_thread_t *_p_intf ) :
54                                   QObject( NULL ), p_intf( _p_intf )
55 {
56     fixed_timer = new QTimer( this );
57     fixed_timer->start( 150 /* milliseconds */ );
58
59     menusMapper = new QSignalMapper();
60     CONNECT( menusMapper, mapped(QObject *), this, menuAction( QObject *) );
61
62     menusUpdateMapper = new QSignalMapper();
63     CONNECT( menusUpdateMapper, mapped(QObject *),
64              this, menuUpdateAction( QObject *) );
65
66     SDMapper = new QSignalMapper();
67     CONNECT( SDMapper, mapped (QString), this, SDMenuAction( QString ) );
68 }
69
70 DialogsProvider::~DialogsProvider()
71 {
72     PlaylistDialog::killInstance();
73     MediaInfoDialog::killInstance();
74 }
75
76 void DialogsProvider::quit()
77 {
78     vlc_object_kill( p_intf );
79     QApplication::quit();
80 }
81
82 void DialogsProvider::customEvent( QEvent *event )
83 {
84     if( event->type() == DialogEvent_Type )
85     {
86         DialogEvent *de = static_cast<DialogEvent*>(event);
87         switch( de->i_dialog )
88         {
89             case INTF_DIALOG_FILE_SIMPLE:
90             case INTF_DIALOG_FILE:
91                 openDialog(); break;
92             case INTF_DIALOG_DISC:
93                 openDiscDialog(); break;
94             case INTF_DIALOG_NET:
95                 openNetDialog(); break;
96             case INTF_DIALOG_SAT:
97             case INTF_DIALOG_CAPTURE:
98                 openCaptureDialog(); break;
99             case INTF_DIALOG_DIRECTORY:
100                 PLAppendDir(); break;
101             case INTF_DIALOG_PLAYLIST:
102                 playlistDialog(); break;
103             case INTF_DIALOG_MESSAGES:
104                 messagesDialog(); break;
105             case INTF_DIALOG_FILEINFO:
106                mediaInfoDialog(); break;
107             case INTF_DIALOG_PREFS:
108                prefsDialog(); break;
109             case INTF_DIALOG_BOOKMARKS:
110                bookmarksDialog(); break;
111             case INTF_DIALOG_EXTENDED:
112                extendedDialog(); break;
113             case INTF_DIALOG_VLM:
114                vlmDialog(); break;
115             case INTF_DIALOG_INTERACTION:
116                doInteraction( de->p_arg ); break;
117             case INTF_DIALOG_POPUPMENU:
118                QVLCMenu::PopupMenu( p_intf, (de->i_arg != 0) ); break;
119             case INTF_DIALOG_AUDIOPOPUPMENU:
120                QVLCMenu::AudioPopupMenu( p_intf ); break;
121             case INTF_DIALOG_VIDEOPOPUPMENU:
122                QVLCMenu::VideoPopupMenu( p_intf ); break;
123             case INTF_DIALOG_MISCPOPUPMENU:
124                QVLCMenu::MiscPopupMenu( p_intf ); break;
125             case INTF_DIALOG_WIZARD:
126             case INTF_DIALOG_STREAMWIZARD:
127             case INTF_DIALOG_UPDATEVLC:
128             case INTF_DIALOG_EXIT:
129             default:
130                msg_Warn( p_intf, "unimplemented dialog\n" );
131         }
132     }
133 }
134
135 /****************************************************************************
136  * Individual simple dialogs
137  ****************************************************************************/
138 void DialogsProvider::playlistDialog()
139 {
140     PlaylistDialog::getInstance( p_intf )->toggleVisible();
141 }
142
143 void DialogsProvider::prefsDialog()
144 {
145     PrefsDialog::getInstance( p_intf )->toggleVisible();
146 }
147 void DialogsProvider::extendedDialog()
148 {
149     ExtendedDialog::getInstance( p_intf )->toggleVisible();
150 }
151
152 void DialogsProvider::messagesDialog()
153 {
154     MessagesDialog::getInstance( p_intf )->toggleVisible();
155 }
156
157 void DialogsProvider::gotoTimeDialog()
158 {
159     GotoTimeDialog::getInstance( p_intf )->toggleVisible();
160 }
161
162 void DialogsProvider::vlmDialog()
163 {
164     VLMDialog::getInstance( p_intf )->toggleVisible();
165 }
166
167 void DialogsProvider::helpDialog()
168 {
169     HelpDialog::getInstance( p_intf )->toggleVisible();
170 }
171
172 void DialogsProvider::aboutDialog()
173 {
174     AboutDialog::getInstance( p_intf )->toggleVisible();
175 }
176
177 void DialogsProvider::mediaInfoDialog()
178 {
179     MediaInfoDialog::getInstance( p_intf )->toggleVisible();
180 }
181
182 void DialogsProvider::mediaCodecDialog()
183 {
184     MediaInfoDialog::getInstance( p_intf )->showTab( 2 );
185 }
186
187 void DialogsProvider::bookmarksDialog()
188 {
189     /* FIXME - Implement me */
190     /*  BookmarkDialog::getInstance( p_intf )->toggleVisible(); */
191 }
192
193 void DialogsProvider::podcastConfigureDialog()
194 {
195     PodcastConfigDialog::getInstance( p_intf )->toggleVisible();
196 }
197
198
199 /****************************************************************************
200  * All the open/add stuff
201  * Open Dialog first - Simple Open then
202  ****************************************************************************/
203
204 void DialogsProvider::openDialog( int i_tab )
205 {
206     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf )->showTab( i_tab );
207 }
208 void DialogsProvider::openDialog()
209 {
210     openDialog( OPEN_FILE_TAB );
211 }
212 void DialogsProvider::openFileDialog()
213 {
214     openDialog( OPEN_FILE_TAB );
215 }
216 void DialogsProvider::openDiscDialog()
217 {
218     openDialog( OPEN_DISC_TAB );
219 }
220 void DialogsProvider::openNetDialog()
221 {
222     openDialog( OPEN_NETWORK_TAB );
223 }
224 void DialogsProvider::openCaptureDialog()
225 {
226     openDialog( OPEN_CAPTURE_TAB );
227 }
228
229 /* Same as the open one, but force the enqueue */
230 void DialogsProvider::PLAppendDialog()
231 {
232     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, ENQUEUE)->showTab(0);
233 }
234
235 /* Unimplemmented yet - Usefull ? */
236 void DialogsProvider::MLAppendDialog()
237 {
238 }
239
240 /**
241  * Simple open
242  * Not used anymore. Let the code until we are sure we don't want it
243  * Two opens make it confusing for the user.
244  ***/
245 QStringList DialogsProvider::showSimpleOpen( QString help,
246                                              int filters,
247                                              QString path )
248 {
249     QString fileTypes = "";
250     if( filters & EXT_FILTER_MEDIA ) {
251         ADD_FILTER_MEDIA( fileTypes );
252     }
253     if( filters & EXT_FILTER_VIDEO ) {
254         ADD_FILTER_VIDEO( fileTypes );
255     }
256     if( filters & EXT_FILTER_AUDIO ) {
257         ADD_FILTER_AUDIO( fileTypes );
258     }
259     if( filters & EXT_FILTER_PLAYLIST ) {
260         ADD_FILTER_PLAYLIST( fileTypes );
261     }
262     if( filters & EXT_FILTER_SUBTITLE ) {
263         ADD_FILTER_SUBTITLE( fileTypes );
264     }
265     ADD_FILTER_ALL( fileTypes );
266     fileTypes.replace(QString(";*"), QString(" *"));
267     return QFileDialog::getOpenFileNames( NULL,
268         help.isNull() ? qfu(I_OP_SEL_FILES ) : help,
269         path.isNull() ? qfu( p_intf->p_libvlc->psz_homedir ) : path,
270         fileTypes );
271 }
272
273 void DialogsProvider::addFromSimple( bool pl, bool go)
274 {
275     QStringList files = DialogsProvider::showSimpleOpen();
276     int i = 0;
277     foreach( QString file, files )
278     {
279         const char * psz_utf8 = qtu( file );
280         playlist_Add( THEPL, psz_utf8, NULL,
281                       go ? ( PLAYLIST_APPEND | ( i ? 0 : PLAYLIST_GO ) |
282                                                ( i ? PLAYLIST_PREPARSE : 0 ) )
283                          : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
284                       PLAYLIST_END,
285                       pl ? VLC_TRUE : VLC_FALSE, VLC_FALSE );
286         i++;
287     }
288 }
289
290 void DialogsProvider::simplePLAppendDialog()
291 {
292     addFromSimple( true, false );
293 }
294
295 void DialogsProvider::simpleMLAppendDialog()
296 {
297     addFromSimple( false, false );
298 }
299
300 void DialogsProvider::simpleOpenDialog()
301 {
302     addFromSimple( true, true );
303 }
304
305 /* Directory */
306
307 /**
308  * Open a directory,
309  * pl helps you to choose from playlist or media library,
310  * go to start or enqueue
311  **/
312 static void openDirectory( intf_thread_t *p_intf, bool pl, bool go )
313 {
314     QString dir = QFileDialog::getExistingDirectory ( 0, qtr("Open directory") );
315     if (!dir.isEmpty()) {
316         input_item_t *p_input = input_ItemNewExt( THEPL, qtu(dir), NULL,
317                                                0, NULL, -1 );
318
319         playlist_AddInput( THEPL, p_input,
320                        go ? ( PLAYLIST_APPEND | PLAYLIST_GO ) : PLAYLIST_APPEND,
321                        PLAYLIST_END, pl, VLC_FALSE );
322         input_Read( THEPL, p_input, VLC_FALSE );
323     }
324 }
325
326 void DialogsProvider::PLAppendDir()
327 {
328     openDirectory( p_intf, true, false );
329 }
330
331 void DialogsProvider::MLAppendDir()
332 {
333     openDirectory( p_intf, false , false );
334 }
335
336 /****************
337  * Playlist     *
338  ****************/
339 void DialogsProvider::openPlaylist()
340 {
341     QStringList files = showSimpleOpen( qtr( "Open playlist file" ),
342                                         EXT_FILTER_PLAYLIST );
343     foreach( QString file, files )
344     {
345         playlist_Import( THEPL, qtu(file) );
346     }
347 }
348
349 void DialogsProvider::savePlaylist()
350 {
351     QFileDialog *qfd = new QFileDialog( NULL,
352                                    qtr("Choose a filename to save playlist"),
353                                    qfu( p_intf->p_libvlc->psz_homedir ),
354                                    qtr("XSPF playlist (*.xspf);; ") +
355                                    qtr("M3U playlist (*.m3u);; Any (*.*) ") );
356     qfd->setFileMode( QFileDialog::AnyFile );
357     qfd->setAcceptMode( QFileDialog::AcceptSave );
358     qfd->setConfirmOverwrite( true );
359
360     if( qfd->exec() == QDialog::Accepted )
361     {
362         if( qfd->selectedFiles().count() > 0 )
363         {
364             static const char psz_xspf[] = "export-xspf",
365                               psz_m3u[] = "esport-m3u";
366             const char *psz_module;
367
368             QString file = qfd->selectedFiles().first();
369             QString filter = qfd->selectedFilter();
370
371             if( file.contains(".xsp") ||
372                 ( filter.contains(".xspf") && !file.contains(".m3u") ) )
373             {
374                 psz_module = psz_xspf;
375                 if( !file.contains( ".xsp" ) )
376                     file.append( ".xspf" );
377             }
378             else
379             {
380                 psz_module = psz_m3u;
381                 if( !file.contains( ".m3u" ) )
382                     file.append( ".m3u" );
383             }
384
385             playlist_Export( THEPL, qtu(file), THEPL->p_local_category,
386                              psz_module);
387         }
388     }
389     delete qfd;
390 }
391
392
393 /****************************************************************************
394  * Sout emulation
395  ****************************************************************************/
396
397 //FIXME !!
398 void DialogsProvider::streamingDialog( QString mrl, bool b_transcode_only )
399 {
400     SoutDialog *s = new SoutDialog( p_intf->p_sys->p_mi, p_intf,
401                                                     b_transcode_only );
402     if( s->exec() == QDialog::Accepted )
403     {
404         msg_Err( p_intf, "mrl %s\n", qta( s->getMrl() ) );
405         /* Just do it */
406         int i_len = strlen( qtu( s->getMrl() ) ) + 10;
407         char *psz_option = (char*)malloc(i_len);
408         snprintf( psz_option, i_len - 1, "%s", qtu( s->getMrl() ) );
409
410         playlist_AddExt( THEPL, qtu( mrl ), "Streaming",
411                          PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END,
412                         -1, &psz_option, 1, VLC_TRUE, VLC_FALSE );
413     }
414     delete s;
415 }
416
417 void DialogsProvider::openThenStreamingDialogs()
418 {
419     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, OPEN_AND_STREAM )
420                                 ->showTab( 0 );
421 }
422
423 void DialogsProvider::openThenTranscodingDialogs()
424 {
425     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, OPEN_AND_SAVE )
426                                 ->showTab( 0 );
427 }
428 /*
429 void DialogsProvider::streamingDialog()
430 {
431     OpenDialog *o = new OpenDialog( p_intf->p_sys->p_mi, p_intf, true );
432     if ( o->exec() == QDialog::Accepted )
433     {
434         SoutDialog *s = new SoutDialog( p_intf->p_sys->p_mi, p_intf );
435         if( s->exec() == QDialog::Accepted )
436         {
437             msg_Err(p_intf, "mrl %s\n", qta(s->mrl));
438             /* Just do it
439             int i_len = strlen( qtu(s->mrl) ) + 10;
440             char *psz_option = (char*)malloc(i_len);
441             snprintf( psz_option, i_len - 1, ":sout=%s", qtu(s->mrl));
442
443             playlist_AddExt( THEPL, qtu( o->mrl ), "Streaming",
444                              PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END,
445                              -1, &psz_option, 1, VLC_TRUE, VLC_FALSE );
446         }
447         delete s;
448     }
449     delete o;
450 }*/
451
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 }