]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs_provider.cpp
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         /* Why this ?
360          * input_Read( THEPL, p_input, false ); */
361         vlc_gc_decref( p_input );
362     }
363 }
364
365 void DialogsProvider::PLAppendDir()
366 {
367     openDirectory( p_intf, true, true );
368 }
369
370 void DialogsProvider::MLAppendDir()
371 {
372     openDirectory( p_intf, false , false );
373 }
374
375 /****************
376  * Playlist     *
377  ****************/
378 void DialogsProvider::openAPlaylist()
379 {
380     QStringList files = showSimpleOpen( qtr( "Open playlist file" ),
381                                         EXT_FILTER_PLAYLIST );
382     foreach( QString file, files )
383     {
384         playlist_Import( THEPL, qtu(file) );
385     }
386 }
387
388 void DialogsProvider::saveAPlaylist()
389 {
390     QFileDialog *qfd = new QFileDialog( NULL,
391                                    qtr( "Choose a filename to save playlist" ),
392                                    qfu( p_intf->p_sys->psz_filepath ),
393                                    qtr( "XSPF playlist (*.xspf);; " ) +
394                                    qtr( "M3U playlist (*.m3u);; Any (*.*) " ) );
395     qfd->setFileMode( QFileDialog::AnyFile );
396     qfd->setAcceptMode( QFileDialog::AcceptSave );
397     qfd->setConfirmOverwrite( true );
398
399     if( qfd->exec() == QDialog::Accepted )
400     {
401         if( qfd->selectedFiles().count() > 0 )
402         {
403             static const char psz_xspf[] = "export-xspf",
404                               psz_m3u[] = "export-m3u";
405             const char *psz_module;
406
407             QString file = qfd->selectedFiles().first();
408             QString filter = qfd->selectedFilter();
409
410             if( file.contains( ".xsp" ) ||
411                 ( filter.contains( ".xspf" ) && !file.contains( ".m3u" ) ) )
412             {
413                 psz_module = psz_xspf;
414                 if( !file.contains( ".xsp" ) )
415                     file.append( ".xspf" );
416             }
417             else
418             {
419                 psz_module = psz_m3u;
420                 if( !file.contains( ".m3u" ) )
421                     file.append( ".m3u" );
422             }
423
424             playlist_Export( THEPL, qtu( file ), THEPL->p_local_category,
425                              psz_module);
426         }
427     }
428     delete qfd;
429 }
430
431
432 /****************************************************************************
433  * Sout emulation
434  ****************************************************************************/
435
436 void DialogsProvider::streamingDialog( QWidget *parent, QString mrl,
437                                        bool b_transcode_only )
438 {
439     SoutDialog *s = SoutDialog::getInstance( parent, p_intf, b_transcode_only );
440
441     if( s->exec() == QDialog::Accepted )
442     {
443         msg_Dbg( p_intf, "Sout mrl %s", qta( s->getMrl() ) );
444         /* Just do it */
445         int i_len = strlen( qtu( s->getMrl() ) ) + 10;
446         char *psz_option = (char*)malloc( i_len );
447         snprintf( psz_option, i_len - 1, "%s", qtu( s->getMrl() ) );
448
449         playlist_AddExt( THEPL, qtu( mrl ), "Streaming",
450                          PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END,
451                         -1, &psz_option, 1, true, pl_Unlocked );
452     }
453 }
454
455 void DialogsProvider::openThenStreamingDialogs()
456 {
457     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, OPEN_AND_STREAM )
458                                 ->showTab( 0 );
459 }
460
461 void DialogsProvider::openThenTranscodingDialogs()
462 {
463     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, OPEN_AND_SAVE )
464                                 ->showTab( 0 );
465 }
466
467 /****************************************************************************
468  * Menus / Interaction
469  ****************************************************************************/
470
471 void DialogsProvider::menuAction( QObject *data )
472 {
473     QVLCMenu::DoAction( p_intf, data );
474 }
475
476 void DialogsProvider::menuUpdateAction( QObject *data )
477 {
478     MenuFunc * f = qobject_cast<MenuFunc *>(data);
479     f->doFunc( p_intf );
480 }
481
482 void DialogsProvider::SDMenuAction( QString data )
483 {
484     char *psz_sd = strdup( qtu( data ) );
485     if( !playlist_IsServicesDiscoveryLoaded( THEPL, psz_sd ) )
486         playlist_ServicesDiscoveryAdd( THEPL, psz_sd );
487     else
488         playlist_ServicesDiscoveryRemove( THEPL, psz_sd );
489     free( psz_sd );
490 }
491
492 void DialogsProvider::doInteraction( intf_dialog_args_t *p_arg )
493 {
494     InteractionDialog *qdialog;
495     interaction_dialog_t *p_dialog = p_arg->p_dialog;
496     switch( p_dialog->i_action )
497     {
498     case INTERACT_NEW:
499         qdialog = new InteractionDialog( p_intf, p_dialog );
500         p_dialog->p_private = (void*)qdialog;
501         if( !(p_dialog->i_status == ANSWERED_DIALOG) )
502             qdialog->show();
503         break;
504     case INTERACT_UPDATE:
505         qdialog = (InteractionDialog*)(p_dialog->p_private);
506         if( qdialog )
507             qdialog->update();
508         else
509         {
510             /* The INTERACT_NEW message was forgotten
511                so we must create the dialog and update it*/
512             qdialog = new InteractionDialog( p_intf, p_dialog );
513             p_dialog->p_private = (void*)qdialog;
514             if( !(p_dialog->i_status == ANSWERED_DIALOG) )
515                 qdialog->show();
516             if( qdialog )
517                 qdialog->update();
518         }
519         break;
520     case INTERACT_HIDE:
521         qdialog = (InteractionDialog*)(p_dialog->p_private);
522         if( qdialog )
523             qdialog->hide();
524         p_dialog->i_status = HIDDEN_DIALOG;
525         break;
526     case INTERACT_DESTROY:
527         qdialog = (InteractionDialog*)(p_dialog->p_private);
528         if( !p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
529             delete qdialog;
530         p_dialog->i_status = DESTROYED_DIALOG;
531         break;
532     }
533 }
534
535 void DialogsProvider::loadSubtitlesFile()
536 {
537     input_thread_t *p_input = THEMIM->getInput();
538     if( !p_input )
539         return;
540     QString qsFile = QFileDialog::getOpenFileName(
541              NULL,
542              qtr( "Choose subtitles file" ),
543              "",
544              qtr( "Subtitles files (*.cdg *.idx *.srt *.sub *.utf);;"
545                   "All files (*)" ) );
546     if( !input_AddSubtitles( p_input, qtu( qsFile ), true ) )
547         msg_Warn( p_intf, "unable to load subtitles file..." );
548 }