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