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