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