]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs_provider.cpp
Revert "Fix #1761: open directory plays."
[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 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <QEvent>
29 #include <QApplication>
30 #include <QSignalMapper>
31 #include <QFileDialog>
32
33 #include <vlc_common.h>
34 #include "qt4.hpp"
35 #include "dialogs_provider.hpp"
36 #include "main_interface.hpp"
37 #include "menus.hpp"
38 #include <vlc_intf_strings.h>
39 #include "input_manager.hpp"
40
41 /* The dialogs */
42 #include "dialogs/playlist.hpp"
43 #include "dialogs/bookmarks.hpp"
44 #include "dialogs/preferences.hpp"
45 #include "dialogs/mediainfo.hpp"
46 #include "dialogs/messages.hpp"
47 #include "dialogs/extended.hpp"
48 #include "dialogs/vlm.hpp"
49 #include "dialogs/sout.hpp"
50 #include "dialogs/open.hpp"
51 #include "dialogs/help.hpp"
52 #include "dialogs/gototime.hpp"
53 #include "dialogs/podcast_configuration.hpp"
54
55 DialogsProvider* DialogsProvider::instance = NULL;
56
57 DialogsProvider::DialogsProvider( intf_thread_t *_p_intf ) :
58                                   QObject( NULL ), p_intf( _p_intf )
59 {
60     fixed_timer = new QTimer( this );
61     fixed_timer->start( 150 /* milliseconds */ );
62
63     menusMapper = new QSignalMapper();
64     CONNECT( menusMapper, mapped(QObject *), this, menuAction( QObject *) );
65
66     menusUpdateMapper = new QSignalMapper();
67     CONNECT( menusUpdateMapper, mapped(QObject *),
68              this, menuUpdateAction( QObject *) );
69
70     SDMapper = new QSignalMapper();
71     CONNECT( SDMapper, mapped (QString), this, SDMenuAction( QString ) );
72 }
73
74 DialogsProvider::~DialogsProvider()
75 {
76     msg_Dbg( p_intf, "Destroying the Dialog Provider" );
77     PlaylistDialog::killInstance();
78     MediaInfoDialog::killInstance();
79     MessagesDialog::killInstance();
80     ExtendedDialog::killInstance();
81     BookmarksDialog::killInstance();
82     HelpDialog::killInstance();
83 #ifdef UPDATE_CHECK
84     UpdateDialog::killInstance();
85 #endif
86
87     fixed_timer->stop();
88 }
89
90 void DialogsProvider::quit()
91 {
92     vlc_object_kill( p_intf->p_libvlc );
93     QApplication::closeAllWindows();
94     QApplication::quit();
95 }
96
97 void DialogsProvider::customEvent( QEvent *event )
98 {
99     if( event->type() == DialogEvent_Type )
100     {
101         DialogEvent *de = static_cast<DialogEvent*>(event);
102         switch( de->i_dialog )
103         {
104         case INTF_DIALOG_FILE_SIMPLE:
105         case INTF_DIALOG_FILE:
106             openDialog(); break;
107         case INTF_DIALOG_DISC:
108             openDiscDialog(); break;
109         case INTF_DIALOG_NET:
110             openNetDialog(); break;
111         case INTF_DIALOG_SAT:
112         case INTF_DIALOG_CAPTURE:
113             openCaptureDialog(); break;
114         case INTF_DIALOG_DIRECTORY:
115             PLAppendDir(); break;
116         case INTF_DIALOG_PLAYLIST:
117             playlistDialog(); break;
118         case INTF_DIALOG_MESSAGES:
119             messagesDialog(); break;
120         case INTF_DIALOG_FILEINFO:
121            mediaInfoDialog(); break;
122         case INTF_DIALOG_PREFS:
123            prefsDialog(); break;
124         case INTF_DIALOG_BOOKMARKS:
125            bookmarksDialog(); break;
126         case INTF_DIALOG_EXTENDED:
127            extendedDialog(); break;
128 #ifdef ENABLE_VLM
129         case INTF_DIALOG_VLM:
130            vlmDialog(); break;
131 #endif
132         case INTF_DIALOG_INTERACTION:
133            doInteraction( de->p_arg ); break;
134         case INTF_DIALOG_POPUPMENU:
135            QVLCMenu::PopupMenu( p_intf, (de->i_arg != 0) ); break;
136         case INTF_DIALOG_AUDIOPOPUPMENU:
137            QVLCMenu::AudioPopupMenu( p_intf ); break;
138         case INTF_DIALOG_VIDEOPOPUPMENU:
139            QVLCMenu::VideoPopupMenu( p_intf ); break;
140         case INTF_DIALOG_MISCPOPUPMENU:
141            QVLCMenu::MiscPopupMenu( p_intf ); break;
142         case INTF_DIALOG_WIZARD:
143         case INTF_DIALOG_STREAMWIZARD:
144             openThenStreamingDialogs(); break;
145 #ifdef UPDATE_CHECK
146         case INTF_DIALOG_UPDATEVLC:
147             updateDialog(); break;
148 #endif
149         case INTF_DIALOG_EXIT:
150             quit(); break;
151         default:
152            msg_Warn( p_intf, "unimplemented dialog" );
153         }
154     }
155 }
156
157 /****************************************************************************
158  * Individual simple dialogs
159  ****************************************************************************/
160 void DialogsProvider::playlistDialog()
161 {
162     PlaylistDialog::getInstance( p_intf )->toggleVisible();
163 }
164
165 void DialogsProvider::prefsDialog()
166 {
167     PrefsDialog::getInstance( p_intf )->toggleVisible();
168 }
169
170 void DialogsProvider::extendedDialog()
171 {
172     ExtendedDialog::getInstance( p_intf )->toggleVisible();
173 }
174
175 void DialogsProvider::messagesDialog()
176 {
177     MessagesDialog::getInstance( p_intf )->toggleVisible();
178 }
179
180 void DialogsProvider::gotoTimeDialog()
181 {
182     GotoTimeDialog::getInstance( p_intf )->toggleVisible();
183 }
184
185 #ifdef ENABLE_VLM
186 void DialogsProvider::vlmDialog()
187 {
188     VLMDialog::getInstance( p_intf )->toggleVisible();
189 }
190 #endif
191
192 void DialogsProvider::helpDialog()
193 {
194     HelpDialog::getInstance( p_intf )->toggleVisible();
195 }
196
197 #ifdef UPDATE_CHECK
198 void DialogsProvider::updateDialog()
199 {
200     UpdateDialog::getInstance( p_intf )->toggleVisible();
201 }
202 #endif
203
204 void DialogsProvider::aboutDialog()
205 {
206     AboutDialog::getInstance( p_intf )->toggleVisible();
207 }
208
209 void DialogsProvider::mediaInfoDialog()
210 {
211     MediaInfoDialog::getInstance( p_intf )->toggleVisible();
212 }
213
214 void DialogsProvider::mediaCodecDialog()
215 {
216     MediaInfoDialog::getInstance( p_intf )->showTab( 2 );
217 }
218
219 void DialogsProvider::bookmarksDialog()
220 {
221     BookmarksDialog::getInstance( p_intf )->toggleVisible();
222 }
223
224 void DialogsProvider::podcastConfigureDialog()
225 {
226     PodcastConfigDialog::getInstance( p_intf )->toggleVisible();
227 }
228
229
230 /****************************************************************************
231  * All the open/add stuff
232  * Open Dialog first - Simple Open then
233  ****************************************************************************/
234
235 void DialogsProvider::openDialog( int i_tab )
236 {
237     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf )->showTab( i_tab );
238 }
239 void DialogsProvider::openDialog()
240 {
241     openDialog( OPEN_FILE_TAB );
242 }
243 void DialogsProvider::openFileDialog()
244 {
245     openDialog( OPEN_FILE_TAB );
246 }
247 void DialogsProvider::openDiscDialog()
248 {
249     openDialog( OPEN_DISC_TAB );
250 }
251 void DialogsProvider::openNetDialog()
252 {
253     openDialog( OPEN_NETWORK_TAB );
254 }
255 void DialogsProvider::openCaptureDialog()
256 {
257     openDialog( OPEN_CAPTURE_TAB );
258 }
259
260 /* Same as the open one, but force the enqueue */
261 void DialogsProvider::PLAppendDialog()
262 {
263     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, OPEN_AND_ENQUEUE)
264                             ->showTab( OPEN_FILE_TAB );
265 }
266
267 /* Unimplemmented yet - Usefull ? */
268 void DialogsProvider::MLAppendDialog()
269 {}
270
271 /**
272  * Simple open
273  * Not used anymore. Let the code until we are sure we don't want it
274  * Two opens make it confusing for the user.
275  ***/
276 QStringList DialogsProvider::showSimpleOpen( QString help,
277                                              int filters,
278                                              QString path )
279 {
280     QString fileTypes = "";
281     if( filters & EXT_FILTER_MEDIA ) {
282         ADD_FILTER_MEDIA( fileTypes );
283     }
284     if( filters & EXT_FILTER_VIDEO ) {
285         ADD_FILTER_VIDEO( fileTypes );
286     }
287     if( filters & EXT_FILTER_AUDIO ) {
288         ADD_FILTER_AUDIO( fileTypes );
289     }
290     if( filters & EXT_FILTER_PLAYLIST ) {
291         ADD_FILTER_PLAYLIST( fileTypes );
292     }
293     if( filters & EXT_FILTER_SUBTITLE ) {
294         ADD_FILTER_SUBTITLE( fileTypes );
295     }
296     ADD_FILTER_ALL( fileTypes );
297     fileTypes.replace(QString(";*"), QString(" *"));
298     return QFileDialog::getOpenFileNames( NULL,
299         help.isNull() ? qfu(I_OP_SEL_FILES ) : help,
300         path.isNull() ? qfu( p_intf->p_sys->psz_filepath ) : path,
301         fileTypes );
302 }
303
304 /**
305  * Open a file,
306  * pl helps you to choose from playlist or media library,
307  * go to start or enqueue
308  **/
309 void DialogsProvider::addFromSimple( bool pl, bool go)
310 {
311     QStringList files = DialogsProvider::showSimpleOpen();
312     int i = 0;
313     foreach( QString file, files )
314     {
315         const char * psz_utf8 = qtu( file );
316         playlist_Add( THEPL, psz_utf8, NULL,
317                       go ? ( PLAYLIST_APPEND | ( i ? 0 : PLAYLIST_GO ) |
318                                                ( i ? PLAYLIST_PREPARSE : 0 ) )
319                          : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
320                       PLAYLIST_END,
321                       pl ? true : false, false );
322         i++;
323     }
324 }
325
326 void DialogsProvider::simpleOpenDialog()
327 {
328     addFromSimple( true, true ); /* Playlist and Go */
329 }
330
331 void DialogsProvider::simplePLAppendDialog()
332 {
333     addFromSimple( true, false );
334 }
335
336 void DialogsProvider::simpleMLAppendDialog()
337 {
338     addFromSimple( false, false );
339 }
340
341 /* Directory */
342 /**
343  * Open a directory,
344  * pl helps you to choose from playlist or media library,
345  * go to start or enqueue
346  **/
347 static void openDirectory( intf_thread_t *p_intf, bool pl, bool go )
348 {
349     QString dir = QFileDialog::getExistingDirectory( 0, qtr("Open Directory") );
350     if (!dir.isEmpty()) {
351         input_item_t *p_input = input_ItemNewExt( THEPL,
352                                         qtu( "directory://" + dir ), NULL,
353                                         0, NULL, -1 );
354
355         /* FIXME: playlist_AddInput() can fail */
356         playlist_AddInput( THEPL, p_input,
357                        go ? ( PLAYLIST_APPEND | PLAYLIST_GO ) : PLAYLIST_APPEND,
358                        PLAYLIST_END, pl, pl_Unlocked );
359         input_Read( THEPL, p_input, false );
360         vlc_gc_decref( p_input );
361     }
362 }
363
364 void DialogsProvider::PLAppendDir()
365 {
366     openDirectory( p_intf, true, false );
367 }
368
369 void DialogsProvider::MLAppendDir()
370 {
371     openDirectory( p_intf, false , false );
372 }
373
374 /****************
375  * Playlist     *
376  ****************/
377 void DialogsProvider::openAPlaylist()
378 {
379     QStringList files = showSimpleOpen( qtr( "Open playlist file" ),
380                                         EXT_FILTER_PLAYLIST );
381     foreach( QString file, files )
382     {
383         playlist_Import( THEPL, qtu(file) );
384     }
385 }
386
387 void DialogsProvider::saveAPlaylist()
388 {
389     QFileDialog *qfd = new QFileDialog( NULL,
390                                    qtr( "Choose a filename to save playlist" ),
391                                    qfu( p_intf->p_sys->psz_filepath ),
392                                    qtr( "XSPF playlist (*.xspf);; " ) +
393                                    qtr( "M3U playlist (*.m3u);; Any (*.*) " ) );
394     qfd->setFileMode( QFileDialog::AnyFile );
395     qfd->setAcceptMode( QFileDialog::AcceptSave );
396     qfd->setConfirmOverwrite( true );
397
398     if( qfd->exec() == QDialog::Accepted )
399     {
400         if( qfd->selectedFiles().count() > 0 )
401         {
402             static const char psz_xspf[] = "export-xspf",
403                               psz_m3u[] = "export-m3u";
404             const char *psz_module;
405
406             QString file = qfd->selectedFiles().first();
407             QString filter = qfd->selectedFilter();
408
409             if( file.contains( ".xsp" ) ||
410                 ( filter.contains( ".xspf" ) && !file.contains( ".m3u" ) ) )
411             {
412                 psz_module = psz_xspf;
413                 if( !file.contains( ".xsp" ) )
414                     file.append( ".xspf" );
415             }
416             else
417             {
418                 psz_module = psz_m3u;
419                 if( !file.contains( ".m3u" ) )
420                     file.append( ".m3u" );
421             }
422
423             playlist_Export( THEPL, qtu( file ), THEPL->p_local_category,
424                              psz_module);
425         }
426     }
427     delete qfd;
428 }
429
430
431 /****************************************************************************
432  * Sout emulation
433  ****************************************************************************/
434
435 void DialogsProvider::streamingDialog( QWidget *parent, QString mrl,
436                                        bool b_transcode_only )
437 {
438     SoutDialog *s = SoutDialog::getInstance( parent, p_intf, b_transcode_only );
439
440     if( s->exec() == QDialog::Accepted )
441     {
442         msg_Dbg( p_intf, "Sout mrl %s", qta( s->getMrl() ) );
443         /* Just do it */
444         int i_len = strlen( qtu( s->getMrl() ) ) + 10;
445         char *psz_option = (char*)malloc( i_len );
446         snprintf( psz_option, i_len - 1, "%s", qtu( s->getMrl() ) );
447
448         playlist_AddExt( THEPL, qtu( mrl ), "Streaming",
449                          PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END,
450                         -1, &psz_option, 1, true, pl_Unlocked );
451     }
452 }
453
454 void DialogsProvider::openThenStreamingDialogs()
455 {
456     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, OPEN_AND_STREAM )
457                                 ->showTab( 0 );
458 }
459
460 void DialogsProvider::openThenTranscodingDialogs()
461 {
462     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, OPEN_AND_SAVE )
463                                 ->showTab( 0 );
464 }
465
466 /****************************************************************************
467  * Menus / Interaction
468  ****************************************************************************/
469
470 void DialogsProvider::menuAction( QObject *data )
471 {
472     QVLCMenu::DoAction( p_intf, data );
473 }
474
475 void DialogsProvider::menuUpdateAction( QObject *data )
476 {
477     MenuFunc * f = qobject_cast<MenuFunc *>(data);
478     f->doFunc( p_intf );
479 }
480
481 void DialogsProvider::SDMenuAction( QString data )
482 {
483     char *psz_sd = strdup( qtu( data ) );
484     if( !playlist_IsServicesDiscoveryLoaded( THEPL, psz_sd ) )
485         playlist_ServicesDiscoveryAdd( THEPL, psz_sd );
486     else
487         playlist_ServicesDiscoveryRemove( THEPL, psz_sd );
488     free( psz_sd );
489 }
490
491 void DialogsProvider::doInteraction( intf_dialog_args_t *p_arg )
492 {
493     InteractionDialog *qdialog;
494     interaction_dialog_t *p_dialog = p_arg->p_dialog;
495     switch( p_dialog->i_action )
496     {
497     case INTERACT_NEW:
498         qdialog = new InteractionDialog( p_intf, p_dialog );
499         p_dialog->p_private = (void*)qdialog;
500         if( !(p_dialog->i_status == ANSWERED_DIALOG) )
501             qdialog->show();
502         break;
503     case INTERACT_UPDATE:
504         qdialog = (InteractionDialog*)(p_dialog->p_private);
505         if( qdialog )
506             qdialog->update();
507         else
508         {
509             /* The INTERACT_NEW message was forgotten
510                so we must create the dialog and update it*/
511             qdialog = new InteractionDialog( p_intf, p_dialog );
512             p_dialog->p_private = (void*)qdialog;
513             if( !(p_dialog->i_status == ANSWERED_DIALOG) )
514                 qdialog->show();
515             if( qdialog )
516                 qdialog->update();
517         }
518         break;
519     case INTERACT_HIDE:
520         qdialog = (InteractionDialog*)(p_dialog->p_private);
521         if( qdialog )
522             qdialog->hide();
523         p_dialog->i_status = HIDDEN_DIALOG;
524         break;
525     case INTERACT_DESTROY:
526         qdialog = (InteractionDialog*)(p_dialog->p_private);
527         if( !p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
528             delete qdialog;
529         p_dialog->i_status = DESTROYED_DIALOG;
530         break;
531     }
532 }
533
534 void DialogsProvider::loadSubtitlesFile()
535 {
536     input_thread_t *p_input = THEMIM->getInput();
537     if( !p_input )
538         return;
539     QString qsFile = QFileDialog::getOpenFileName(
540              NULL,
541              qtr( "Choose subtitles file" ),
542              "",
543              qtr( "Subtitles files (*.cdg *.idx *.srt *.sub *.utf);;"
544                   "All files (*)" ) );
545     if( !input_AddSubtitles( p_input, qtu( qsFile ), true ) )
546         msg_Warn( p_intf, "unable to load subtitles file..." );
547 }