]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/simple_preferences.cpp
Qt4 - SPrefs, save the access-filter.
[vlc] / modules / gui / qt4 / components / simple_preferences.cpp
1 /*****************************************************************************
2  * simple_preferences.cpp : "Simple preferences"
3  ****************************************************************************
4  * Copyright (C) 2006-2007 the VideoLAN team
5  * $Id: preferences.cpp 16348 2006-08-25 21:10:10Z zorglub $
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Antoine Cellerier <dionoea@videolan.org>
9  *          Jean-Baptiste Kempf <jb@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #include "components/simple_preferences.hpp"
27 #include "components/preferences_widgets.hpp"
28
29 #include "ui/sprefs_input.h"
30 #include "ui/sprefs_audio.h"
31 #include "ui/sprefs_video.h"
32 #include "ui/sprefs_subtitles.h"
33 #include "ui/sprefs_hotkeys.h"
34 #include "ui/sprefs_interface.h"
35
36 #include <vlc_config_cat.h>
37 #include <vlc_configuration.h>
38
39 #include <QString>
40 #include <QFont>
41 #include <QToolButton>
42 #include <QButtonGroup>
43 #include <QUrl>
44 #include <QVBoxLayout>
45
46 #define ICON_HEIGHT 64
47 #define BUTTON_HEIGHT 74
48
49 /*********************************************************************
50  * The List of categories
51  *********************************************************************/
52 SPrefsCatList::SPrefsCatList( intf_thread_t *_p_intf, QWidget *_parent ) :
53                                   QWidget( _parent ), p_intf( _p_intf )
54 {
55     QVBoxLayout *layout = new QVBoxLayout();
56
57     QButtonGroup *buttonGroup = new QButtonGroup( this );
58     buttonGroup->setExclusive ( true );
59     CONNECT( buttonGroup, buttonClicked ( int ),
60             this, switchPanel( int ) );
61
62 #define ADD_CATEGORY( button, label, icon, numb )                           \
63     QToolButton * button = new QToolButton( this );                         \
64     button->setIcon( QIcon( ":/pixmaps/" #icon ) );                         \
65     button->setIconSize( QSize( ICON_HEIGHT , ICON_HEIGHT ) );              \
66     button->setText( label );                                               \
67     button->setToolButtonStyle( Qt::ToolButtonTextUnderIcon );              \
68     button->resize( BUTTON_HEIGHT , BUTTON_HEIGHT);                         \
69     button->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding) ;  \
70     button->setAutoRaise( true );                                           \
71     button->setCheckable( true );                                           \
72     buttonGroup->addButton( button, numb );                                 \
73     layout->addWidget( button );
74
75     ADD_CATEGORY( SPrefsInterface, qtr("Interface"),
76                   spref_cone_Interface_64.png, 0 );
77     ADD_CATEGORY( SPrefsAudio, qtr("Audio"), spref_cone_Audio_64.png, 1 );
78     ADD_CATEGORY( SPrefsVideo, qtr("Video"), spref_cone_Video_64.png, 2 );
79     ADD_CATEGORY( SPrefsSubtitles, qtr("Subtitles"),
80                   spref_cone_Subtitles_64.png, 3 );
81     ADD_CATEGORY( SPrefsInputAndCodecs, qtr("Input and Codecs"),
82                   spref_cone_Input_64.png, 4 );
83     ADD_CATEGORY( SPrefsHotkeys, qtr("Hotkeys"), spref_cone_Hotkeys_64.png, 5 );
84
85     SPrefsInterface->setChecked( true );
86     layout->setMargin( 0 );
87     layout->setSpacing( 1 );
88
89     this->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
90     setLayout( layout );
91
92 }
93
94 void SPrefsCatList::switchPanel( int i )
95 {
96     emit currentItemChanged( i );
97 }
98
99 /*********************************************************************
100  * The Panels
101  *********************************************************************/
102 SPrefsPanel::SPrefsPanel( intf_thread_t *_p_intf, QWidget *_parent,
103                           int _number ) : QWidget( _parent ), p_intf( _p_intf )
104 {
105     module_config_t *p_config;
106     ConfigControl *control;
107     number = _number;
108
109 #define CONFIG_GENERIC( option, type, label, qcontrol )                   \
110             p_config =  config_FindConfig( VLC_OBJECT(p_intf), option );  \
111             if( p_config )                                                \
112             {                                                             \
113                 control =  new type ## ConfigControl( VLC_OBJECT(p_intf), \
114                            p_config, label, ui.qcontrol, false );         \
115                 controls.append( control );                               \
116             }
117
118 #define CONFIG_GENERIC_NO_BOOL( option, type, label, qcontrol )           \
119             p_config =  config_FindConfig( VLC_OBJECT(p_intf), option );  \
120             if( p_config )                                                \
121             {                                                             \
122                 control =  new type ## ConfigControl( VLC_OBJECT(p_intf), \
123                            p_config, label, ui.qcontrol );                \
124                 controls.append( control );                               \
125             }
126
127 #define CONFIG_GENERIC_FILE( option, type, label, qcontrol, qbutton )         \
128                 p_config =  config_FindConfig( VLC_OBJECT(p_intf), option );  \
129                 if( p_config )                                                \
130                 {                                                             \
131                     control =  new type ## ConfigControl( VLC_OBJECT(p_intf), \
132                                p_config, label, ui.qcontrol, ui.qbutton,      \
133                             false );                                          \
134                     controls.append( control );                               \
135                 }
136
137 #define START_SPREFS_CAT( name , label )    \
138         case SPrefs ## name:                \
139         {                                   \
140             Ui::SPrefs ## name ui;          \
141             ui.setupUi( panel );            \
142             panel_label->setText( label );
143
144 #define END_SPREFS_CAT      \
145             break;          \
146         }
147
148     QVBoxLayout *panel_layout = new QVBoxLayout();
149     QWidget *panel = new QWidget();
150     panel_layout->setMargin( 3 );
151
152     // Title Label
153     QLabel *panel_label = new QLabel;
154     QFont labelFont = QApplication::font( static_cast<QWidget*>(0) );
155     labelFont.setPointSize( labelFont.pointSize() + 6 );
156     labelFont.setFamily( "Verdana" );
157     panel_label->setFont( labelFont );
158
159     // Title <hr>
160     QFrame *title_line = new QFrame;
161     title_line->setFrameShape(QFrame::HLine);
162     title_line->setFrameShadow(QFrame::Sunken);
163
164     QFont italicFont = QApplication::font( static_cast<QWidget*>(0) );
165     italicFont.setItalic( true );
166
167     switch( number )
168     {
169         /******************************
170          * VIDEO Panel Implementation *
171          ******************************/
172         START_SPREFS_CAT( Video , qtr("General video settings") );
173             CONFIG_GENERIC( "video", Bool, NULL, enableVideo );
174
175             CONFIG_GENERIC( "fullscreen", Bool, NULL, fullscreen );
176             CONFIG_GENERIC( "overlay", Bool, NULL, overlay );
177             CONFIG_GENERIC( "video-on-top", Bool, NULL, alwaysOnTop );
178             CONFIG_GENERIC( "video-deco", Bool, NULL, windowDecorations );
179             CONFIG_GENERIC( "skip-frames" , Bool, NULL, skipFrames );
180             CONFIG_GENERIC( "overlay", Bool, NULL, overlay );
181             CONFIG_GENERIC( "vout", Module, NULL, outputModule );
182
183 #ifdef WIN32
184             CONFIG_GENERIC( "directx-wallpaper" , Bool , NULL, wallpaperMode );
185             CONFIG_GENERIC( "directx-device", StringList, NULL,
186                     dXdisplayDevice );
187 #else
188             ui.directXBox->setVisible( false );
189 #endif
190
191             CONFIG_GENERIC_FILE( "snapshot-path", Directory, NULL,
192                     snapshotsDirectory, snapshotsDirectoryBrowse );
193             CONFIG_GENERIC( "snapshot-prefix", String, NULL, snapshotsPrefix );
194             CONFIG_GENERIC( "snapshot-sequential", Bool, NULL,
195                             snapshotsSequentialNumbering );
196             CONFIG_GENERIC( "snapshot-format", StringList, NULL,
197                             snapshotsFormat );
198          END_SPREFS_CAT;
199
200         /******************************
201          * AUDIO Panel Implementation *
202          ******************************/
203         START_SPREFS_CAT( Audio, qtr("General audio settings") );
204
205             CONFIG_GENERIC( "audio", Bool, NULL, enableAudio );
206
207             /* General Audio Options */
208             CONFIG_GENERIC_NO_BOOL( "volume" , IntegerRangeSlider, NULL,
209                                      defaultVolume );
210             CONFIG_GENERIC( "audio-language" , String , NULL,
211                             preferredAudioLanguage );
212
213             CONFIG_GENERIC( "spdif", Bool, NULL, spdifBox );
214             CONFIG_GENERIC( "force-dolby-surround" , IntegerList , NULL,
215                             detectionDolby );
216
217             CONFIG_GENERIC( "headphone-dolby" , Bool , NULL, headphoneEffect );
218
219             CONFIG_GENERIC_NO_BOOL( "norm-max-level" , Float , NULL,
220                                     volNormSpin );
221             CONFIG_GENERIC( "audio-visual" , Module , NULL, visualisation);
222
223             /* Audio Output Specifics */
224             CONFIG_GENERIC( "aout", Module, NULL, outputModule );
225
226             CONNECT( ui.outputModule, currentIndexChanged( int ), this,
227                              updateAudioOptions( int ) );
228             audioOutput = ui.outputModule;
229
230         //TODO: use modules_Exists
231 #ifndef WIN32
232             CONFIG_GENERIC( "alsadev" , StringList , ui.alsaLabel, alsaDevice );
233             CONFIG_GENERIC_FILE( "dspdev" , File , ui.OSSLabel, OSSDevice,
234                                  OSSBrowse );
235 #else
236             CONFIG_GENERIC( "directx-audio-device", IntegerList,
237                     ui.DirectXLabel, DirectXDevice );
238 #endif
239         // File exists everywhere
240             CONFIG_GENERIC_FILE( "audiofile-file" , File , ui.fileLabel,
241                                  fileName, fileBrowseButton );
242             alsa_options = ui.alsaControl;
243             oss_options = ui.OSSControl;
244             directx_options = ui.DirectXControl;
245             file_options = ui.fileControl;
246
247         /* and hide if necessary */
248 #ifdef WIN32
249             oss_options->hide();
250             alsa_options->hide();
251 #else
252             directx_options->hide();
253 #endif
254
255             updateAudioOptions( audioOutput->currentIndex() );
256
257             /* LastFM */
258             CONFIG_GENERIC( "lastfm-username", String, ui.lastfm_user_label,
259                          lastfm_user_edit );
260             CONFIG_GENERIC( "lastfm-password", String, ui.lastfm_pass_label,
261                          lastfm_pass_edit );
262             ui.lastfm_user_edit->hide();
263             ui.lastfm_user_label->hide();
264             ui.lastfm_pass_edit->hide();
265             ui.lastfm_pass_label->hide();
266
267             if( config_ExistIntf( VLC_OBJECT( p_intf ), "audioscrobbler" ) )
268                 ui.lastfm->setCheckState( Qt::Checked );
269             else
270                 ui.lastfm->setCheckState( Qt::Unchecked );
271             CONNECT( ui.lastfm, stateChanged( int ), this ,
272                     lastfm_Changed( int ) );
273
274             /* Normalizer */
275             normalizerBox = ui.volNormBox;
276             CONNECT( ui.volNormBox, toggled( bool ), ui.volNormSpin, setEnabled( bool ) );
277             qs_filter = qfu( config_GetPsz( p_intf, "audio-filter" ) );
278             bool b_normalizer = ( qs_filter.contains( "volnorm" ) );
279             {
280                 ui.volNormBox->setChecked( b_normalizer );
281                 ui.volNormSpin->setEnabled( b_normalizer );
282             }
283
284         END_SPREFS_CAT;
285
286         /* Input and Codecs Panel Implementation */
287         START_SPREFS_CAT( InputAndCodecs, qtr("Input & Codecs settings") );
288             inputDevice = ui.DVDDevice;
289           /* Disk Devices */
290             {
291                 ui.DVDDevice->setToolTip(
292                     qtr( "If this propriety is blank, then you have\n"
293                          "values for DVD, VCD, and CDDA.\n"
294                          "You can define a unique one or set that in"
295                          "the advanced preferences" ) );
296                 char *psz_dvddiscpath = config_GetPsz( p_intf, "dvd" );
297                 char *psz_vcddiscpath = config_GetPsz( p_intf, "vcd" );
298                 char *psz_cddadiscpath = config_GetPsz( p_intf, "cd-audio" );
299                 if( ( *psz_cddadiscpath == *psz_dvddiscpath )
300                    && ( *psz_dvddiscpath == *psz_vcddiscpath ) )
301                 {
302                     ui.DVDDevice->setText( qfu( psz_dvddiscpath ) );
303                 }
304                 delete psz_cddadiscpath; delete psz_dvddiscpath;
305                 delete psz_vcddiscpath;
306             }
307
308             CONFIG_GENERIC_NO_BOOL( "server-port", Integer, NULL, UDPPort );
309             CONFIG_GENERIC( "http-proxy", String , NULL, proxy );
310         
311           /* Caching */
312 /*          CONFIG_GENERIC( );*/ //FIXME
313
314             CONFIG_GENERIC_NO_BOOL( "ffmpeg-pp-q", Integer, NULL, PostProcLevel );
315             CONFIG_GENERIC( "avi-index", IntegerList, NULL, AviRepair );
316             CONFIG_GENERIC( "rtsp-tcp", Bool, NULL, RTSP_TCPBox );
317 #ifdef WIN32
318             CONFIG_GENERIC( "prefer-system-codecs", Bool, NULL, systemCodecBox );
319 #else
320             ui.systemCodecBox->hide();
321 #endif  
322             /* Access Filters */
323             qs_filter = qfu( config_GetPsz( p_intf, "access-filter" ) );
324             ui.timeshiftBox->setChecked( qs_filter.contains( "timeshift" ) );
325             ui.dumpBox->setChecked( qs_filter.contains( "dump" ) );
326             ui.recordBox->setChecked( qs_filter.contains( "record" ) );
327             ui.bandwidthBox->setChecked( qs_filter.contains( "bandwidth" ) );
328             timeshiftBox = ui.timeshiftBox;
329             recordBox = ui.recordBox;
330             dumpBox = ui.dumpBox;
331             bandwidthBox = ui.bandwidthBox;
332         END_SPREFS_CAT;
333
334         /* Interface Panel */
335         START_SPREFS_CAT( Interface, qtr("Interface settings") );
336             ui.defaultLabel->setFont( italicFont );
337             ui.skinsLabel->setFont( italicFont );
338
339 #if defined( WIN32 ) || defined (__APPLE__)
340             CONFIG_GENERIC( "language", StringList, NULL, language );
341 #else
342             ui.language->hide();
343             ui.languageLabel->hide();
344 #endif
345
346            /* interface */
347             char *psz_intf = config_GetPsz( p_intf, "intf" );
348             if( psz_intf )
349             {
350                 msg_Dbg( p_intf, "Interface in config file: %s", psz_intf );
351                 if( strstr( psz_intf, "skin" ) )
352                     ui.skins->setChecked( true );
353                 else if( strstr( psz_intf, "qt" ) )
354                     ui.qt4->setChecked( true );
355             }
356             skinInterfaceButton = ui.skins;
357             qtInterfaceButton = ui.qt4;
358             delete psz_intf;
359
360             CONFIG_GENERIC( "qt-always-video", Bool, NULL, qtAlwaysVideo );
361             CONFIG_GENERIC_FILE( "skins2-last", File, NULL, fileSkin,
362                     skinBrowse );
363 #if defined( WIN32 ) || defined(HAVE_DBUS_3)
364             CONFIG_GENERIC( "one-instance", Bool, NULL, OneInterfaceMode );
365             CONFIG_GENERIC( "playlist-enqueue", Bool, NULL,
366                     EnqueueOneInterfaceMode );
367 #else
368             ui.OneInterfaceBox->hide();
369 #endif
370         END_SPREFS_CAT;
371
372         START_SPREFS_CAT( Subtitles, qtr("Subtitles & OSD settings") );
373             CONFIG_GENERIC( "osd", Bool, NULL, OSDBox);
374
375             CONFIG_GENERIC( "subsdec-encoding", StringList, NULL, encoding );
376             CONFIG_GENERIC( "sub-language", String, NULL, preferredLanguage );
377             CONFIG_GENERIC_FILE( "freetype-font", File, NULL, font,
378                             fontBrowse );
379             CONFIG_GENERIC( "freetype-color", IntegerList, NULL, fontColor );
380             CONFIG_GENERIC( "freetype-rel-fontsize", IntegerList, NULL,
381                             fontSize );
382             CONFIG_GENERIC( "freetype-effect", IntegerList, NULL, effect );
383
384         END_SPREFS_CAT;
385
386         START_SPREFS_CAT( Hotkeys, "Configure Hotkeys" );
387         //FIMXE
388         END_SPREFS_CAT;
389         }
390
391     panel_layout->addWidget(panel_label);
392     panel_layout->addWidget(title_line);
393     panel_layout->addWidget( panel );
394     panel_layout->addStretch( 2 );
395
396     this->setLayout(panel_layout);
397 }
398
399 void SPrefsPanel::updateAudioOptions( int number)
400 {
401     QString value = audioOutput->itemData( number ).toString();
402
403 #ifndef WIN32
404     oss_options->hide();
405     alsa_options->hide();
406 #else
407     directx_options->hide();
408 #endif
409     file_options->hide();
410
411    if( value == "aout_file" )
412          file_options->show();
413 #ifndef WIN32
414     else if( value == "alsa" )
415         alsa_options->show();
416     else if( value == "oss" )
417         oss_options->show();
418 #else
419     else if( value == "directx" )
420         directx_options->show();
421 #endif
422 }
423
424 void SPrefsPanel::apply()
425 {
426     msg_Dbg( p_intf, "Trying to save the %i simple panel", number );
427
428     /* Generic save for ever panel */
429     QList<ConfigControl *>::Iterator i;
430     for( i = controls.begin() ; i != controls.end() ; i++ )
431     {
432         ConfigControl *c = qobject_cast<ConfigControl *>(*i);
433         c->doApply( p_intf );
434     }
435
436     /* Devices */
437     if( number == SPrefsInputAndCodecs )
438     {
439         char *psz_devicepath = qtu( inputDevice->text() );
440         if( !EMPTY_STR( psz_devicepath ) )
441         {
442             config_PutPsz( p_intf, "dvd", psz_devicepath );
443             config_PutPsz( p_intf, "vcd", psz_devicepath );
444             config_PutPsz( p_intf, "cd-audio", psz_devicepath );
445         }
446
447         bool b_first = true;
448 #define saveBox( name, box ) {\
449         if( box->isChecked() ) { \
450             if( b_first ) { \
451                 qs_filter.append( name ); \
452                 b_first = false; \
453             } \
454             else qs_filter.append( ":" ).append( name ); \
455         } } 
456     
457         saveBox( "record", recordBox );
458         saveBox( "dump", dumpBox );
459         saveBox( "timeshift", timeshiftBox );
460         saveBox( "bandwidth", bandwidthBox );
461         config_PutPsz( p_intf, "access-filter", qtu( qs_filter ) );
462     }
463
464     /* Interfaces */
465     if( number == SPrefsInterface )
466     {
467         if( skinInterfaceButton->isChecked() )
468             config_PutPsz( p_intf, "intf", "skins2" );
469         if( qtInterfaceButton->isChecked() )
470             config_PutPsz( p_intf, "intf", "qt4" );
471     }
472
473     if( number == SPrefsAudio )
474     {
475         bool b_normChecked = normalizerBox->isChecked();
476         if( qs_filter.isEmpty() )
477         {
478             /* the psz_filter is already empty, so we just append it needed */
479             if( b_normChecked ) qs_filter = "volnorm";
480         }
481         else /* Not Empty */
482         {
483             if( qs_filter.contains( "volnorm" ) )
484             {
485                 /* The qs_filter not empty and contains "volnorm" 
486                    that we have to remove */
487                 if( !b_normChecked )
488                 {
489                     /* Ugly :D */
490                     qs_filter.remove( "volnorm:" );
491                     qs_filter.remove( ":volnorm" );
492                     qs_filter.remove( "volnorm" );
493                 }
494             }
495             else /* qs_filter not empty, but doesn't have volnorm inside */
496                 if( b_normChecked ) qs_filter.append( ":volnorm" );
497         }
498         config_PutPsz( p_intf, "audio-filter", qtu( qs_filter ) );
499     }
500 }
501
502 void SPrefsPanel::clean()
503 {}
504
505 void SPrefsPanel::lastfm_Changed( int i_state )
506 {
507     if( i_state == Qt::Checked )
508         config_AddIntf( VLC_OBJECT( p_intf ), "audioscrobbler" );
509     else if( i_state == Qt::Unchecked )
510         config_RemoveIntf( VLC_OBJECT( p_intf ), "audioscrobbler" );
511 }