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