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