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