Add a define for the EXTENDED dialog for further use (skins2 ? )
[vlc.git] / modules / gui / qt4 / dialogs_provider.cpp
1 /*****************************************************************************
2  * main_inteface.cpp : Main interface
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include <QEvent>
25 #include <QApplication>
26 #include <QSignalMapper>
27 #include <QFileDialog>
28
29 #include "qt4.hpp"
30 #include "dialogs_provider.hpp"
31 #include "main_interface.hpp"
32 #include "menus.hpp"
33 #include <vlc_intf_strings.h>
34
35 /* The dialogs */
36 #include "dialogs/playlist.hpp"
37 #include "dialogs/preferences.hpp"
38 #include "dialogs/mediainfo.hpp"
39 #include "dialogs/messages.hpp"
40 #include "dialogs/extended.hpp"
41 #include "dialogs/sout.hpp"
42 #include "dialogs/open.hpp"
43 #include "dialogs/help.hpp"
44 #include "dialogs/gototime.hpp"
45
46 DialogsProvider* DialogsProvider::instance = NULL;
47
48 DialogsProvider::DialogsProvider( intf_thread_t *_p_intf ) :
49                                   QObject( NULL ), p_intf( _p_intf )
50 {
51     fixed_timer = new QTimer( this );
52     fixed_timer->start( 150 /* milliseconds */ );
53
54     menusMapper = new QSignalMapper();
55     CONNECT( menusMapper, mapped(QObject *), this, menuAction( QObject *) );
56
57     menusUpdateMapper = new QSignalMapper();
58     CONNECT( menusUpdateMapper, mapped(QObject *),
59              this, menuUpdateAction( QObject *) );
60
61     SDMapper = new QSignalMapper();
62     CONNECT( SDMapper, mapped (QString), this, SDMenuAction( QString ) );
63 }
64
65 DialogsProvider::~DialogsProvider()
66 {
67     PlaylistDialog::killInstance();
68     MediaInfoDialog::killInstance();
69 }
70
71 void DialogsProvider::quit()
72 {
73     p_intf->b_die = VLC_TRUE;
74     QApplication::quit();
75 }
76
77 void DialogsProvider::customEvent( QEvent *event )
78 {
79     if( event->type() == DialogEvent_Type )
80     {
81         DialogEvent *de = static_cast<DialogEvent*>(event);
82         switch( de->i_dialog )
83         {
84             case INTF_DIALOG_FILE:
85                 openDialog(); break;
86             case INTF_DIALOG_DISC:
87                 openDiscDialog(); break;
88             case INTF_DIALOG_NET:
89                 openNetDialog(); break;
90             case INTF_DIALOG_CAPTURE:
91                 openCaptureDialog(); break;
92             case INTF_DIALOG_PLAYLIST:
93                 playlistDialog(); break;
94             case INTF_DIALOG_MESSAGES:
95                 messagesDialog(); break;
96             case INTF_DIALOG_PREFS:
97                prefsDialog(); break;
98             case INTF_DIALOG_POPUPMENU:
99             case INTF_DIALOG_AUDIOPOPUPMENU:
100             case INTF_DIALOG_VIDEOPOPUPMENU:
101             case INTF_DIALOG_MISCPOPUPMENU:
102                popupMenu( de->i_dialog ); break;
103             case INTF_DIALOG_FILEINFO:
104                mediaInfoDialog(); break;
105             case INTF_DIALOG_INTERACTION:
106                doInteraction( de->p_arg ); break;
107             case INTF_DIALOG_BOOKMARKS:
108                bookmarksDialog(); break;
109             case INTF_DIALOG_VLM:
110             case INTF_DIALOG_WIZARD:
111             default:
112                msg_Warn( p_intf, "unimplemented dialog\n" );
113         }
114     }
115 }
116
117 /****************************************************************************
118  * Individual simple dialogs
119  ****************************************************************************/
120 void DialogsProvider::playlistDialog()
121 {
122     PlaylistDialog::getInstance( p_intf )->toggleVisible();
123 }
124
125 void DialogsProvider::prefsDialog()
126 {
127     PrefsDialog::getInstance( p_intf )->toggleVisible();
128 }
129 void DialogsProvider::extendedDialog()
130 {
131     ExtendedDialog::getInstance( p_intf )->toggleVisible();
132 }
133
134 void DialogsProvider::messagesDialog()
135 {
136     MessagesDialog::getInstance( p_intf )->toggleVisible();
137 }
138
139 void DialogsProvider::gotoTimeDialog()
140 {
141     GotoTimeDialog::getInstance( p_intf )->toggleVisible();
142 }
143
144 void DialogsProvider::helpDialog()
145 {
146     HelpDialog::getInstance( p_intf )->toggleVisible();
147 }
148
149 void DialogsProvider::aboutDialog()
150 {
151     AboutDialog::getInstance( p_intf )->toggleVisible();
152 }
153
154 void DialogsProvider::mediaInfoDialog()
155 {
156     MediaInfoDialog::getInstance( p_intf )->toggleVisible();
157 }
158
159 void DialogsProvider::mediaCodecDialog()
160 {
161     MediaInfoDialog::getInstance( p_intf )->showTab( 1 );
162 }
163
164 void DialogsProvider::bookmarksDialog()
165 {
166 }
167
168 /****************************************************************************
169  * All the open/add stuff
170  ****************************************************************************/
171
172 void DialogsProvider::openDialog()
173 {
174     openDialog( 0 );
175 }
176 void DialogsProvider::openFileDialog()
177 {
178     openDialog( 0 );
179 }
180 void DialogsProvider::openDiscDialog()
181 {
182     openDialog( 1 );
183 }
184 void DialogsProvider::openNetDialog()
185 {
186     openDialog( 2 );
187 }
188 void DialogsProvider::openCaptureDialog()
189 {
190     openDialog( 3 );
191 }
192 void DialogsProvider::openDialog( int i_tab )
193 {
194     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf )->showTab( i_tab );
195 }
196
197 void DialogsProvider::PLAppendDialog()
198 {
199 }
200 void DialogsProvider::MLAppendDialog()
201 {
202 }
203
204 /**** Simple open ****/
205 QStringList DialogsProvider::showSimpleOpen( QString help, int filters,
206                                              QString path )
207 {
208     QString fileTypes = "";
209     if( filters & EXT_FILTER_MEDIA ) {
210         ADD_FILTER_MEDIA( fileTypes );
211     }
212     if( filters & EXT_FILTER_VIDEO ) {
213         ADD_FILTER_VIDEO( fileTypes );
214     }
215     if( filters & EXT_FILTER_AUDIO ) {
216         ADD_FILTER_AUDIO( fileTypes );
217     }
218     if( filters & EXT_FILTER_PLAYLIST ) {
219         ADD_FILTER_PLAYLIST( fileTypes );
220     }
221     if( filters & EXT_FILTER_SUBTITLE ) {
222         ADD_FILTER_SUBTITLE( fileTypes );
223     }
224     ADD_FILTER_ALL( fileTypes );
225     fileTypes.replace(QString(";*"), QString(" *"));
226     return QFileDialog::getOpenFileNames( NULL,
227         help.isNull() ? qfu(I_OP_SEL_FILES ) : help,
228         path.isNull() ? qfu( p_intf->p_libvlc->psz_homedir ) : path,
229         fileTypes );
230 }
231
232 void DialogsProvider::addFromSimple( bool pl, bool go)
233 {
234     QStringList files = DialogsProvider::showSimpleOpen();
235     int i = 0;
236     foreach( QString file, files )
237     {
238         const char * psz_utf8 = qtu( file );
239         playlist_Add( THEPL, psz_utf8, NULL,
240                       go ? ( PLAYLIST_APPEND | ( i ? 0 : PLAYLIST_GO ) |
241                                                ( i ? PLAYLIST_PREPARSE : 0 ) )
242                          : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
243                       PLAYLIST_END,
244                       pl ? VLC_TRUE : VLC_FALSE, VLC_FALSE );
245         i++;
246     }
247 }
248
249 void DialogsProvider::simplePLAppendDialog()
250 {
251     addFromSimple( true, false );
252 }
253
254 void DialogsProvider::simpleMLAppendDialog()
255 {
256     addFromSimple( false, false );
257 }
258
259 void DialogsProvider::simpleOpenDialog()
260 {
261     addFromSimple( true, true );
262 }
263
264 void DialogsProvider::openPlaylist()
265 {
266     QStringList files = showSimpleOpen( qtr( "Open playlist file" ),
267                                         EXT_FILTER_PLAYLIST );
268     foreach( QString file, files )
269     {
270         playlist_Import( THEPL, qtu(file) );
271     }
272 }
273
274 void DialogsProvider::savePlaylist()
275 {
276     QFileDialog *qfd = new QFileDialog( NULL,
277                                    qtr("Choose a filename to save playlist"),
278                                    qfu( p_intf->p_libvlc->psz_homedir ),
279                                    qtr("XSPF playlist (*.xspf);; ") +
280                                    qtr("M3U playlist (*.m3u);; Any (*.*) ") );
281     qfd->setFileMode( QFileDialog::AnyFile );
282     qfd->setAcceptMode( QFileDialog::AcceptSave );
283     qfd->setConfirmOverwrite( true );
284
285     if( qfd->exec() == QDialog::Accepted )
286     {
287         if( qfd->selectedFiles().count() > 0 )
288         {
289             char *psz_module, *psz_m3u = "export-m3u",
290                  *psz_xspf = "export-xspf";
291
292             QString file = qfd->selectedFiles().first();
293             QString filter = qfd->selectedFilter();
294
295             if( file.contains(".xsp") ||
296                 ( filter.contains(".xspf") && !file.contains(".m3u") ) )
297             {
298                 psz_module = psz_xspf;
299                 if( !file.contains( ".xsp" ) )
300                     file.append( ".xspf" );
301             }
302             else
303             {
304                 psz_module = psz_m3u;
305                 if( !file.contains( ".m3u" ) )
306                     file.append( ".m3u" );
307             }
308
309             playlist_Export( THEPL, qtu(file), THEPL->p_local_category,
310                              psz_module);
311         }
312     }
313     delete qfd;
314 }
315
316 static void openDirectory( intf_thread_t* p_intf, bool pl, bool go )
317 {
318     QString dir = QFileDialog::getExistingDirectory ( 0,
319                                                      _("Open directory") );
320     input_item_t *p_input = input_ItemNewExt( THEPL, qtu(dir), NULL,
321                                                0, NULL, -1 );
322     playlist_AddInput( THEPL, p_input,
323                        go ? ( PLAYLIST_APPEND | PLAYLIST_GO ) : PLAYLIST_APPEND,
324                        PLAYLIST_END, pl, VLC_FALSE );
325     input_Read( THEPL, p_input, VLC_FALSE );
326 }
327
328 void DialogsProvider::PLAppendDir()
329 {
330     openDirectory( p_intf, true, false );
331 }
332
333 void DialogsProvider::MLAppendDir()
334 {
335     openDirectory( p_intf, false , false );
336 }
337
338
339 /****************************************************************************
340  * Sout emulation
341  ****************************************************************************/
342
343 void DialogsProvider::streamingDialog()
344 {
345     OpenDialog *o = new OpenDialog( p_intf->p_sys->p_mi, p_intf, true );
346     if ( o->exec() == QDialog::Accepted )
347     {
348         SoutDialog *s = new SoutDialog( p_intf->p_sys->p_mi, p_intf );
349         if( s->exec() == QDialog::Accepted )
350         {
351             msg_Err(p_intf, "mrl %s\n", qta(s->mrl));
352             /* Just do it */
353             int i_len = strlen( qtu(s->mrl) ) + 10;
354             char *psz_option = (char*)malloc(i_len);
355             snprintf( psz_option, i_len - 1, ":sout=%s", qtu(s->mrl));
356
357             playlist_AddExt( THEPL, qtu( o->mrl ), "Streaming",
358                              PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END,
359                              -1, &psz_option, 1, VLC_TRUE, VLC_FALSE );
360         }
361         delete s;
362     }
363     delete o;
364 }
365
366 /****************************************************************************
367  * Menus / Interaction
368  ****************************************************************************/
369
370 void DialogsProvider::menuAction( QObject *data )
371 {
372     QVLCMenu::DoAction( p_intf, data );
373 }
374
375 void DialogsProvider::menuUpdateAction( QObject *data )
376 {
377     MenuFunc * f = qobject_cast<MenuFunc *>(data);
378     f->doFunc( p_intf );
379 }
380
381 void DialogsProvider::SDMenuAction( QString data )
382 {
383     char *psz_sd = strdup( qtu( data ) );
384     if( !playlist_IsServicesDiscoveryLoaded( THEPL, psz_sd ) )
385         playlist_ServicesDiscoveryAdd( THEPL, psz_sd );
386     else
387         playlist_ServicesDiscoveryRemove( THEPL, psz_sd );
388
389     free( psz_sd );
390 }
391
392
393 void DialogsProvider::doInteraction( intf_dialog_args_t *p_arg )
394 {
395     InteractionDialog *qdialog;
396     interaction_dialog_t *p_dialog = p_arg->p_dialog;
397     switch( p_dialog->i_action )
398     {
399     case INTERACT_NEW:
400         qdialog = new InteractionDialog( p_intf, p_dialog );
401         p_dialog->p_private = (void*)qdialog;
402         if( !(p_dialog->i_status == ANSWERED_DIALOG) )
403             qdialog->show();
404         break;
405     case INTERACT_UPDATE:
406         qdialog = (InteractionDialog*)(p_dialog->p_private);
407         if( qdialog)
408             qdialog->update();
409         break;
410     case INTERACT_HIDE:
411         qdialog = (InteractionDialog*)(p_dialog->p_private);
412         if( qdialog )
413             qdialog->hide();
414         p_dialog->i_status = HIDDEN_DIALOG;
415         break;
416     case INTERACT_DESTROY:
417         qdialog = (InteractionDialog*)(p_dialog->p_private);
418         if( !p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
419             delete qdialog;
420         p_dialog->i_status = DESTROYED_DIALOG;
421         break;
422     }
423 }
424
425 void DialogsProvider::switchToSkins()
426 {
427     var_SetString( p_intf, "intf-switch", "skins2" );
428 }
429
430 void DialogsProvider::popupMenu( int i_dialog )
431 {
432 }