revert [23722]
[vlc.git] / 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 #ifdef UPDATE_CHECK
131         case INTF_DIALOG_UPDATEVLC:
132             updateDialog(); break;
133 #endif
134         case INTF_DIALOG_EXIT:
135         default:
136            msg_Warn( p_intf, "unimplemented dialog" );
137         }
138     }
139 }
140
141 /****************************************************************************
142  * Individual simple dialogs
143  ****************************************************************************/
144 void DialogsProvider::playlistDialog()
145 {
146     PlaylistDialog::getInstance( p_intf )->toggleVisible();
147 }
148
149 void DialogsProvider::prefsDialog()
150 {
151     PrefsDialog::getInstance( p_intf )->toggleVisible();
152 }
153
154 void DialogsProvider::extendedDialog()
155 {
156     ExtendedDialog::getInstance( p_intf )->toggleVisible();
157 }
158
159 void DialogsProvider::messagesDialog()
160 {
161     MessagesDialog::getInstance( p_intf )->toggleVisible();
162 }
163
164 void DialogsProvider::gotoTimeDialog()
165 {
166     GotoTimeDialog::getInstance( p_intf )->toggleVisible();
167 }
168
169 void DialogsProvider::vlmDialog()
170 {
171     VLMDialog::getInstance( p_intf )->toggleVisible();
172 }
173
174 void DialogsProvider::helpDialog()
175 {
176     HelpDialog::getInstance( p_intf )->toggleVisible();
177 }
178
179 #ifdef UPDATE_CHECK
180 void DialogsProvider::updateDialog()
181 {
182     UpdateDialog::getInstance( p_intf )->toggleVisible();
183 }
184 #endif
185
186 void DialogsProvider::aboutDialog()
187 {
188     AboutDialog::getInstance( p_intf )->toggleVisible();
189 }
190
191 void DialogsProvider::mediaInfoDialog()
192 {
193     MediaInfoDialog::getInstance( p_intf )->toggleVisible();
194 }
195
196 void DialogsProvider::mediaCodecDialog()
197 {
198     MediaInfoDialog::getInstance( p_intf )->showTab( 2 );
199 }
200
201 void DialogsProvider::bookmarksDialog()
202 {
203     /* TODO - Implement me */
204     /*  BookmarkDialog::getInstance( p_intf )->toggleVisible(); */
205 }
206
207 void DialogsProvider::podcastConfigureDialog()
208 {
209     PodcastConfigDialog::getInstance( p_intf )->toggleVisible();
210 }
211
212
213 /****************************************************************************
214  * All the open/add stuff
215  * Open Dialog first - Simple Open then
216  ****************************************************************************/
217
218 void DialogsProvider::openDialog( int i_tab )
219 {
220     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf )->showTab( i_tab );
221 }
222 void DialogsProvider::openDialog()
223 {
224     openDialog( OPEN_FILE_TAB );
225 }
226 void DialogsProvider::openFileDialog()
227 {
228     openDialog( OPEN_FILE_TAB );
229 }
230 void DialogsProvider::openDiscDialog()
231 {
232     openDialog( OPEN_DISC_TAB );
233 }
234 void DialogsProvider::openNetDialog()
235 {
236     openDialog( OPEN_NETWORK_TAB );
237 }
238 void DialogsProvider::openCaptureDialog()
239 {
240     openDialog( OPEN_CAPTURE_TAB );
241 }
242
243 /* Same as the open one, but force the enqueue */
244 void DialogsProvider::PLAppendDialog()
245 {
246     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, OPEN_AND_ENQUEUE)
247                             ->showTab( OPEN_FILE_TAB );
248 }
249
250 /* Unimplemmented yet - Usefull ? */
251 void DialogsProvider::MLAppendDialog()
252 {
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 //FIXME !!
418 void DialogsProvider::streamingDialog( QString mrl, bool b_transcode_only )
419 {
420     SoutDialog *s = new SoutDialog( p_intf->p_sys->p_mi, p_intf,
421                                                     b_transcode_only );
422     if( s->exec() == QDialog::Accepted )
423     {
424         msg_Err( p_intf, "mrl %s", qta( s->getMrl() ) );
425         /* Just do it */
426         int i_len = strlen( qtu( s->getMrl() ) ) + 10;
427         char *psz_option = (char*)malloc( i_len );
428         snprintf( psz_option, i_len - 1, "%s", qtu( s->getMrl() ) );
429
430         playlist_AddExt( THEPL, qtu( mrl ), "Streaming",
431                          PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END,
432                         -1, &psz_option, 1, VLC_TRUE, VLC_FALSE );
433     }
434     delete s;
435 }
436
437 void DialogsProvider::openThenStreamingDialogs()
438 {
439     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, OPEN_AND_STREAM )
440                                 ->showTab( 0 );
441 }
442
443 void DialogsProvider::openThenTranscodingDialogs()
444 {
445     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, OPEN_AND_SAVE )
446                                 ->showTab( 0 );
447 }
448 /*
449 void DialogsProvider::streamingDialog()
450 {
451     OpenDialog *o = new OpenDialog( p_intf->p_sys->p_mi, p_intf, true );
452     if ( o->exec() == QDialog::Accepted )
453     {
454         SoutDialog *s = new SoutDialog( p_intf->p_sys->p_mi, p_intf );
455         if( s->exec() == QDialog::Accepted )
456         {
457             msg_Err(p_intf, "mrl %s\n", qta(s->mrl));
458             /* Just do it
459             int i_len = strlen( qtu(s->mrl) ) + 10;
460             char *psz_option = (char*)malloc(i_len);
461             snprintf( psz_option, i_len - 1, ":sout=%s", qtu(s->mrl));
462
463             playlist_AddExt( THEPL, qtu( o->mrl ), "Streaming",
464                              PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END,
465                              -1, &psz_option, 1, VLC_TRUE, VLC_FALSE );
466         }
467         delete s;
468     }
469     delete o;
470 }*/
471
472
473 /****************************************************************************
474  * Menus / Interaction
475  ****************************************************************************/
476
477 void DialogsProvider::menuAction( QObject *data )
478 {
479     QVLCMenu::DoAction( p_intf, data );
480 }
481
482 void DialogsProvider::menuUpdateAction( QObject *data )
483 {
484     MenuFunc * f = qobject_cast<MenuFunc *>(data);
485     f->doFunc( p_intf );
486 }
487
488 void DialogsProvider::SDMenuAction( QString data )
489 {
490     char *psz_sd = strdup( qtu( data ) );
491     if( !playlist_IsServicesDiscoveryLoaded( THEPL, psz_sd ) )
492         playlist_ServicesDiscoveryAdd( THEPL, psz_sd );
493     else
494         playlist_ServicesDiscoveryRemove( THEPL, psz_sd );
495     free( psz_sd );
496 }
497
498 void DialogsProvider::doInteraction( intf_dialog_args_t *p_arg )
499 {
500     InteractionDialog *qdialog;
501     interaction_dialog_t *p_dialog = p_arg->p_dialog;
502     switch( p_dialog->i_action )
503     {
504     case INTERACT_NEW:
505         qdialog = new InteractionDialog( p_intf, p_dialog );
506         p_dialog->p_private = (void*)qdialog;
507         if( !(p_dialog->i_status == ANSWERED_DIALOG) )
508             qdialog->show();
509         break;
510     case INTERACT_UPDATE:
511         qdialog = (InteractionDialog*)(p_dialog->p_private);
512         if( qdialog)
513             qdialog->update();
514         break;
515     case INTERACT_HIDE:
516         qdialog = (InteractionDialog*)(p_dialog->p_private);
517         if( qdialog )
518             qdialog->hide();
519         p_dialog->i_status = HIDDEN_DIALOG;
520         break;
521     case INTERACT_DESTROY:
522         qdialog = (InteractionDialog*)(p_dialog->p_private);
523         if( !p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
524             delete qdialog;
525         p_dialog->i_status = DESTROYED_DIALOG;
526         break;
527     }
528 }
529
530 void DialogsProvider::switchToSkins()
531 {
532     var_SetString( p_intf, "intf-switch", "skins2" );
533 }