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