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