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