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