]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/simple_preferences.cpp
Less memleaks in Qt interface.
[vlc] / modules / gui / qt4 / components / simple_preferences.cpp
1 /*****************************************************************************
2  * simple_preferences.cpp : "Simple preferences"
3  ****************************************************************************
4  * Copyright (C) 2006-2008 the VideoLAN team
5  * $Id$
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 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "components/simple_preferences.hpp"
31 #include "components/preferences_widgets.hpp"
32
33 #include <vlc_config_cat.h>
34 #include <vlc_configuration.h>
35
36 #include <QString>
37 #include <QFont>
38 #include <QToolButton>
39 #include <QButtonGroup>
40 #include <QUrl>
41 #include <QVBoxLayout>
42
43 #include <string>
44
45 #define ICON_HEIGHT 64
46 #define BUTTON_HEIGHT 74
47
48 /*********************************************************************
49  * The List of categories
50  *********************************************************************/
51 SPrefsCatList::SPrefsCatList( intf_thread_t *_p_intf, QWidget *_parent ) :
52                                   QWidget( _parent ), p_intf( _p_intf )
53 {
54     QVBoxLayout *layout = new QVBoxLayout();
55
56     QButtonGroup *buttonGroup = new QButtonGroup( this );
57     buttonGroup->setExclusive ( true );
58     CONNECT( buttonGroup, buttonClicked ( int ),
59             this, switchPanel( int ) );
60
61 #define ADD_CATEGORY( button, label, icon, numb )                           \
62     QToolButton * button = new QToolButton( this );                         \
63     button->setIcon( QIcon( ":/pixmaps/prefs/" #icon ) );                   \
64     button->setIconSize( QSize( ICON_HEIGHT , ICON_HEIGHT ) );              \
65     button->setText( label );                                               \
66     button->setToolButtonStyle( Qt::ToolButtonTextUnderIcon );              \
67     button->resize( BUTTON_HEIGHT , BUTTON_HEIGHT);                         \
68     button->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding) ;  \
69     button->setAutoRaise( true );                                           \
70     button->setCheckable( true );                                           \
71     buttonGroup->addButton( button, numb );                                 \
72     layout->addWidget( button );
73
74     ADD_CATEGORY( SPrefsInterface, qtr("Interface"),
75                   spref_cone_Interface_64.png, 0 );
76     ADD_CATEGORY( SPrefsAudio, qtr("Audio"), spref_cone_Audio_64.png, 1 );
77     ADD_CATEGORY( SPrefsVideo, qtr("Video"), spref_cone_Video_64.png, 2 );
78     ADD_CATEGORY( SPrefsSubtitles, qtr("Subtitles && OSD"),
79                   spref_cone_Subtitles_64.png, 3 );
80     ADD_CATEGORY( SPrefsInputAndCodecs, qtr("Input && Codecs"),
81                   spref_cone_Input_64.png, 4 );
82     ADD_CATEGORY( SPrefsHotkeys, qtr("Hotkeys"), spref_cone_Hotkeys_64.png, 5 );
83
84     SPrefsInterface->setChecked( true );
85     layout->setMargin( 0 );
86     layout->setSpacing( 1 );
87
88     setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
89     setLayout( layout );
90
91 }
92
93 void SPrefsCatList::switchPanel( int i )
94 {
95     emit currentItemChanged( i );
96 }
97
98 /*********************************************************************
99  * The Panels
100  *********************************************************************/
101 SPrefsPanel::SPrefsPanel( intf_thread_t *_p_intf, QWidget *_parent,
102                           int _number ) : QWidget( _parent ), p_intf( _p_intf )
103 {
104 printf( "SPrefsPanel::SPrefsPanel\n" );
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_GENERIC2( 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, qcontrol, false );         \
124                 controls.append( control );                               \
125             }
126
127
128 #define CONFIG_GENERIC_NO_BOOL( option, type, label, qcontrol )           \
129             p_config =  config_FindConfig( VLC_OBJECT(p_intf), option );  \
130             if( p_config )                                                \
131             {                                                             \
132                 control =  new type ## ConfigControl( VLC_OBJECT(p_intf), \
133                            p_config, label, ui.qcontrol );                \
134                 controls.append( control );                               \
135             }
136
137 #define CONFIG_GENERIC_FILE( option, type, label, qcontrol, qbutton )         \
138                 p_config =  config_FindConfig( VLC_OBJECT(p_intf), option );  \
139                 if( p_config )                                                \
140                 {                                                             \
141                     control =  new type ## ConfigControl( VLC_OBJECT(p_intf), \
142                                p_config, label, qcontrol, qbutton,      \
143                             false );                                          \
144                     controls.append( control );                               \
145                 }
146
147 #define START_SPREFS_CAT( name , label )    \
148         case SPrefs ## name:                \
149         {                                   \
150             Ui::SPrefs ## name ui;      \
151             ui.setupUi( panel );            \
152             panel_label->setText( label );
153
154 #define END_SPREFS_CAT      \
155             break;          \
156         }
157
158     QVBoxLayout *panel_layout = new QVBoxLayout();
159     QWidget *panel = new QWidget();
160     panel_layout->setMargin( 3 );
161
162     // Title Label
163     QLabel *panel_label = new QLabel;
164     QFont labelFont = QApplication::font( static_cast<QWidget*>(0) );
165     labelFont.setPointSize( labelFont.pointSize() + 6 );
166     labelFont.setFamily( "Verdana" );
167     panel_label->setFont( labelFont );
168
169     // Title <hr>
170     QFrame *title_line = new QFrame;
171     title_line->setFrameShape(QFrame::HLine);
172     title_line->setFrameShadow(QFrame::Sunken);
173
174     QFont italicFont = QApplication::font( static_cast<QWidget*>(0) );
175     italicFont.setItalic( true );
176
177     switch( number )
178     {
179         /******************************
180          * VIDEO Panel Implementation *
181          ******************************/
182         START_SPREFS_CAT( Video , qtr("General Video Settings") );
183             CONFIG_GENERIC( "video", Bool, NULL, enableVideo );
184
185             CONFIG_GENERIC( "fullscreen", Bool, NULL, fullscreen );
186             CONFIG_GENERIC( "overlay", Bool, NULL, overlay );
187             CONFIG_GENERIC( "video-on-top", Bool, NULL, alwaysOnTop );
188             CONFIG_GENERIC( "video-deco", Bool, NULL, windowDecorations );
189             CONFIG_GENERIC( "skip-frames" , Bool, NULL, skipFrames );
190             CONFIG_GENERIC( "vout", Module, NULL, outputModule );
191
192 #ifdef WIN32
193             CONFIG_GENERIC( "directx-wallpaper" , Bool , NULL, wallpaperMode );
194             CONFIG_GENERIC( "directx-device", StringList, NULL,
195                             dXdisplayDevice );
196 #else
197             ui.directXBox->setVisible( false );
198 #endif
199
200             CONFIG_GENERIC_FILE( "snapshot-path", Directory, NULL,
201                                  ui.snapshotsDirectory, ui.snapshotsDirectoryBrowse );
202             CONFIG_GENERIC( "snapshot-prefix", String, NULL, snapshotsPrefix );
203             CONFIG_GENERIC( "snapshot-sequential", Bool, NULL,
204                             snapshotsSequentialNumbering );
205             CONFIG_GENERIC( "snapshot-format", StringList, NULL,
206                             snapshotsFormat );
207          END_SPREFS_CAT;
208
209         /******************************
210          * AUDIO Panel Implementation *
211          ******************************/
212         START_SPREFS_CAT( Audio, qtr("General Audio Settings") );
213
214             CONFIG_GENERIC( "audio", Bool, NULL, enableAudio );
215
216 #define audioCommon( name ) \
217             QWidget * name ## Control = new QWidget( ui.outputAudioBox ); \
218             QHBoxLayout * name ## Layout = new QHBoxLayout( name ## Control); \
219             name ## Layout->setMargin( 0 ); \
220             name ## Layout->setSpacing( 0 ); \
221             QLabel * name ## Label = new QLabel( qtr( "Device:" ), name ## Control ); \
222             name ## Label->setMinimumSize(QSize(100, 0)); \
223             name ## Layout->addWidget( name ## Label ); \
224
225 #define audioControl( name) \
226             audioCommon( name ) \
227             QComboBox * name ## Device = new QComboBox( name ## Control ); \
228             name ## Layout->addWidget( name ## Device ); \
229             name ## Label->setBuddy( name ## Device ); \
230             outputAudioLayout->addWidget( name ## Control, outputAudioLayout->rowCount(), 0, 1, -1 );
231
232 #define audioControl2( name) \
233             audioCommon( name ) \
234             QLineEdit * name ## Device = new QLineEdit( name ## Control ); \
235             name ## Layout->addWidget( name ## Device ); \
236             name ## Label->setBuddy( name ## Device ); \
237             QPushButton * name ## Browse = new QPushButton( qtr( "Browse..." ), name ## Control); \
238             name ## Layout->addWidget( name ## Browse ); \
239             outputAudioLayout->addWidget( name ## Control, outputAudioLayout->rowCount(), 0, 1, -1 );
240
241             /* hide if necessary */
242             ui.lastfm_user_edit->hide();
243             ui.lastfm_user_label->hide();
244             ui.lastfm_pass_edit->hide();
245             ui.lastfm_pass_label->hide();
246
247             /* Build if necessary */
248             QGridLayout * outputAudioLayout = qobject_cast<QGridLayout *>(ui.outputAudioBox->layout());
249 #ifdef WIN32
250             audioControl( DirectX );
251             optionWidgets.append( DirectXControl );
252             CONFIG_GENERIC2( "directx-audio-device", IntegerList,
253                     DirectXLabel, DirectXDevice );
254 #else
255             if( module_Exists( p_intf, "alsa" ) )
256             {
257                 audioControl( alsa );
258                 optionWidgets.append( alsaControl );
259
260                 CONFIG_GENERIC2( "alsadev" , StringList , alsaLabel,
261                                 alsaDevice );
262             }
263             else
264                 optionWidgets.append( NULL );
265             if( module_Exists( p_intf, "oss" ) )
266             {
267                 audioControl2( OSS );
268                 optionWidgets.append( OSSControl );
269                 CONFIG_GENERIC_FILE( "dspdev" , File , OSSLabel, OSSDevice,
270                                  OSSBrowse );
271             }
272             else
273                 optionWidgets.append( NULL );
274 #endif
275
276             /* General Audio Options */
277             CONFIG_GENERIC_NO_BOOL( "volume" , IntegerRangeSlider, NULL,
278                                      defaultVolume );
279             CONNECT( ui.defaultVolume, valueChanged( int ),
280                     this, updateAudioVolume( int ) );
281
282             CONFIG_GENERIC( "audio-language" , String , NULL,
283                             preferredAudioLanguage );
284
285             CONFIG_GENERIC( "spdif", Bool, NULL, spdifBox );
286             CONFIG_GENERIC( "qt-autosave-volume", Bool, NULL, saveVolBox );
287             CONFIG_GENERIC( "force-dolby-surround" , IntegerList , NULL,
288                             detectionDolby );
289
290             CONFIG_GENERIC( "headphone-dolby" , Bool , NULL, headphoneEffect );
291
292             CONFIG_GENERIC_NO_BOOL( "norm-max-level" , Float , NULL,
293                                     volNormSpin );
294             CONFIG_GENERIC( "audio-visual" , Module , NULL, visualisation);
295
296             /* Audio Output Specifics */
297             CONFIG_GENERIC( "aout", Module, NULL, outputModule );
298
299             CONNECT( ui.outputModule, currentIndexChanged( int ),
300                      this, updateAudioOptions( int ) );
301
302             /* File output exists on all platforms */
303             CONFIG_GENERIC_FILE( "audiofile-file" , File , ui.fileLabel,
304                                  ui.fileName, ui.fileBrowseButton );
305
306             optionWidgets.append( ui.fileControl );
307             optionWidgets.append( ui.outputModule );
308             optionWidgets.append( ui.volNormBox );
309             /*Little mofification of ui.volumeValue to compile with Qt < 4.3 */
310 #if HAS_QT43
311             ui.volumeValue->setButtonSymbols(QAbstractSpinBox::NoButtons);
312 #endif
313             optionWidgets.append( ui.volumeValue );
314             updateAudioOptions( ui.outputModule->currentIndex() );
315
316             /* LastFM */
317             if( module_Exists( p_intf, "audioscrobbler" ) )
318             {
319                 CONFIG_GENERIC( "lastfm-username", String, ui.lastfm_user_label,
320                         lastfm_user_edit );
321                 CONFIG_GENERIC( "lastfm-password", String, ui.lastfm_pass_label,
322                         lastfm_pass_edit );
323
324                 if( config_ExistIntf( VLC_OBJECT( p_intf ), "audioscrobbler" ) )
325                     ui.lastfm->setChecked( true );
326                 else
327                     ui.lastfm->setChecked( false );
328                 CONNECT( ui.lastfm, stateChanged( int ), this ,
329                         lastfm_Changed( int ) );
330             }
331             else
332                 ui.lastfm->hide();
333
334             /* Normalizer */
335
336             CONNECT( ui.volNormBox, toggled( bool ), ui.volNormSpin,
337                      setEnabled( bool ) );
338             char* psz = config_GetPsz( p_intf, "audio-filter" );
339             qs_filter = qfu( psz );
340             free( psz );
341             bool b_normalizer = ( qs_filter.contains( "volnorm" ) );
342             {
343                 ui.volNormBox->setChecked( b_normalizer );
344                 ui.volNormSpin->setEnabled( b_normalizer );
345             }
346
347             /* Volume Label */
348             updateAudioVolume( ui.defaultVolume->value() ); // First time init
349
350         END_SPREFS_CAT;
351
352         /* Input and Codecs Panel Implementation */
353         START_SPREFS_CAT( InputAndCodecs, qtr("Input & Codecs Settings") );
354
355             /* Disk Devices */
356             {
357                 ui.DVDDevice->setToolTip(
358                     qtr( "If this property is blank, different values\n"
359                          "for DVD, VCD, and CDDA are set.\n"
360                          "You can define a unique one or configure them \n"
361                          "individually in the advanced preferences." ) );
362                 char *psz_dvddiscpath = config_GetPsz( p_intf, "dvd" );
363                 char *psz_vcddiscpath = config_GetPsz( p_intf, "vcd" );
364                 char *psz_cddadiscpath = config_GetPsz( p_intf, "cd-audio" );
365                 if( psz_dvddiscpath && psz_vcddiscpath && psz_cddadiscpath )
366                 if( !strcmp( psz_cddadiscpath, psz_dvddiscpath ) &&
367                     !strcmp( psz_dvddiscpath, psz_vcddiscpath ) )
368                 {
369                     ui.DVDDevice->setText( qfu( psz_dvddiscpath ) );
370                 }
371                 free( psz_cddadiscpath );
372                 free( psz_dvddiscpath );
373                 free( psz_vcddiscpath );
374             }
375
376             CONFIG_GENERIC_NO_BOOL( "server-port", Integer, NULL, UDPPort );
377             CONFIG_GENERIC( "http-proxy", String , NULL, proxy );
378             CONFIG_GENERIC_NO_BOOL( "ffmpeg-pp-q", Integer, NULL, PostProcLevel );
379             CONFIG_GENERIC( "avi-index", IntegerList, NULL, AviRepair );
380             CONFIG_GENERIC( "rtsp-tcp", Bool, NULL, RTSP_TCPBox );
381 #ifdef WIN32
382             CONFIG_GENERIC( "prefer-system-codecs", Bool, NULL, systemCodecBox );
383 #else
384             ui.systemCodecBox->hide();
385 #endif
386             /* Access Filters */
387             char* psz = config_GetPsz( p_intf, "access-filter" );
388             qs_filter = qfu( psz );
389             free( psz );
390             ui.timeshiftBox->setChecked( qs_filter.contains( "timeshift" ) );
391             ui.dumpBox->setChecked( qs_filter.contains( "dump" ) );
392             ui.recordBox->setChecked( qs_filter.contains( "record" ) );
393             ui.bandwidthBox->setChecked( qs_filter.contains( "bandwidth" ) );
394
395             optionWidgets.append( ui.recordBox );
396             optionWidgets.append( ui.dumpBox );
397             optionWidgets.append( ui.bandwidthBox );
398             optionWidgets.append( ui.timeshiftBox );
399             optionWidgets.append( ui.DVDDevice );
400             optionWidgets.append( ui.cachingCombo );
401
402             /* Caching */
403             /* Add the things to the ComboBox */
404             #define addToCachingBox( str, cachingNumber ) \
405                 ui.cachingCombo->addItem( str, QVariant( cachingNumber ) );
406             addToCachingBox( "Custom", CachingCustom );
407             addToCachingBox( "Lowest latency", CachingLowest );
408             addToCachingBox( "Low latency", CachingLow );
409             addToCachingBox( "Normal", CachingNormal );
410             addToCachingBox( "High latency", CachingHigh );
411             addToCachingBox( "Higher latency", CachingHigher );
412
413 #define TestCaC( name ) \
414     b_cache_equal =  b_cache_equal && \
415      ( i_cache == config_GetInt( p_intf, name ) )
416
417 #define TestCaCi( name, int ) \
418     b_cache_equal = b_cache_equal &&  \
419     ( ( i_cache * int ) == config_GetInt( p_intf, name ) )
420             /* Select the accurate value of the ComboBox */
421             bool b_cache_equal = true;
422             int i_cache = config_GetInt( p_intf, "file-caching");
423
424             TestCaC( "udp-caching" );
425             if (module_Exists (p_intf, "dvdread"))
426                 TestCaC( "dvdread-caching" );
427             if (module_Exists (p_intf, "dvdnav"))
428                 TestCaC( "dvdnav-caching" );
429             TestCaC( "tcp-caching" );
430             TestCaC( "fake-caching" ); TestCaC( "cdda-caching" );
431             TestCaC( "screen-caching" ); TestCaC( "vcd-caching" );
432             #ifdef WIN32
433             TestCaC( "dshow-caching" );
434             #else
435             if (module_Exists (p_intf, "v4l"))
436                 TestCaC( "v4l-caching" );
437             if (module_Exists (p_intf, "access_jack"))
438                 TestCaC( "jack-input-caching" );
439             if (module_Exists (p_intf, "v4l2"))
440                 TestCaC( "v4l2-caching" );
441             if (module_Exists (p_intf, "pvr"))
442                 TestCaC( "pvr-caching" );
443             #endif
444             TestCaCi( "rtsp-caching", 4 ); TestCaCi( "ftp-caching", 2 );
445             TestCaCi( "http-caching", 4 );
446             if (module_Exists (p_intf, "access_realrtsp"))
447                 TestCaCi( "realrtsp-caching", 10 );
448             TestCaCi( "mms-caching", 19 );
449             if( b_cache_equal ) ui.cachingCombo->setCurrentIndex(
450                 ui.cachingCombo->findData( QVariant( i_cache ) ) );
451
452         END_SPREFS_CAT;
453         /*******************
454          * Interface Panel *
455          *******************/
456         START_SPREFS_CAT( Interface, qtr("Interface Settings") );
457             ui.defaultLabel->setFont( italicFont );
458             ui.skinsLabel->setFont( italicFont );
459
460 #if defined( WIN32 )
461             CONFIG_GENERIC( "language", StringList, NULL, language );
462             BUTTONACT( ui.assoButton, assoDialog() );
463 #else
464             ui.language->hide();
465             ui.languageLabel->hide();
466             ui.assoName->hide();
467             ui.assoButton->hide();
468 #endif
469
470             /* interface */
471             char *psz_intf = config_GetPsz( p_intf, "intf" );
472             if( psz_intf )
473             {
474                 if( strstr( psz_intf, "skin" ) )
475                     ui.skins->setChecked( true );
476                 else if( strstr( psz_intf, "qt" ) )
477                     ui.qt4->setChecked( true );
478             }
479             free( psz_intf );
480
481             optionWidgets.append( ui.skins );
482             optionWidgets.append( ui.qt4 );
483
484             CONFIG_GENERIC( "qt-display-mode", IntegerList, NULL,
485                             displayModeBox );
486             CONFIG_GENERIC( "embedded-video", Bool, NULL, embedVideo );
487             CONFIG_GENERIC( "qt-fs-controller", Bool, NULL, fsController );
488             CONFIG_GENERIC_FILE( "skins2-last", File, NULL, ui.fileSkin,
489                     ui.skinBrowse );
490
491             CONFIG_GENERIC( "album-art", IntegerList, ui.artFetchLabel,
492                                                       artFetcher );
493
494             /* UPDATE options */
495 #ifdef UPDATE_CHECK
496             CONFIG_GENERIC( "qt-updates-notif", Bool, NULL, updatesBox );
497             CONFIG_GENERIC_NO_BOOL( "qt-updates-days", Integer, NULL,
498                     updatesDays );
499             CONNECT( ui.updatesBox, toggled( bool ),
500                      ui.updatesDays, setEnabled( bool ) );
501 #else
502             ui.updatesBox->hide();
503             ui.updatesDays->hide();
504 #endif
505             /* ONE INSTANCE options */
506 #if defined( WIN32 ) || defined( HAVE_DBUS ) || defined(__APPLE__)
507             CONFIG_GENERIC( "one-instance", Bool, NULL, OneInterfaceMode );
508             CONFIG_GENERIC( "playlist-enqueue", Bool, NULL,
509                     EnqueueOneInterfaceMode );
510 #else
511             ui.OneInterfaceBox->hide();
512 #endif
513         END_SPREFS_CAT;
514
515         START_SPREFS_CAT( Subtitles, qtr("Subtitles & On Screen Display Settings") );
516             CONFIG_GENERIC( "osd", Bool, NULL, OSDBox);
517
518             CONFIG_GENERIC( "subsdec-encoding", StringList, NULL, encoding );
519             CONFIG_GENERIC( "sub-language", String, NULL, preferredLanguage );
520             CONFIG_GENERIC_FILE( "freetype-font", File, NULL, ui.font,
521                             ui.fontBrowse );
522             CONFIG_GENERIC( "freetype-color", IntegerList, NULL, fontColor );
523             CONFIG_GENERIC( "freetype-rel-fontsize", IntegerList, NULL,
524                             fontSize );
525             CONFIG_GENERIC( "freetype-effect", IntegerList, NULL, effect );
526
527         END_SPREFS_CAT;
528
529         case SPrefsHotkeys:
530         {
531             p_config = config_FindConfig( VLC_OBJECT(p_intf), "key-fullscreen" );
532
533             QGridLayout *gLayout = new QGridLayout;
534             panel->setLayout( gLayout );
535             int line = 0;
536
537             control = new KeySelectorControl( VLC_OBJECT(p_intf), p_config ,
538                                                 this, gLayout, line );
539
540             panel_label->setText( qtr( "Configure Hotkeys" ) );
541             controls.append( control );
542
543             break;
544         }
545     }
546
547     panel_layout->addWidget( panel_label );
548     panel_layout->addWidget( title_line );
549     panel_layout->addWidget( panel );
550     if( number != SPrefsHotkeys ) panel_layout->addStretch( 2 );
551
552     setLayout( panel_layout );
553 }
554
555 void SPrefsPanel::updateAudioOptions( int number)
556 {
557     QString value = qobject_cast<QComboBox *>(optionWidgets[audioOutCoB])
558                                             ->itemData( number ).toString();
559 #ifdef WIN32
560     optionWidgets[directxW]->setVisible( ( value == "directx" ) );
561 #else
562     /* optionWidgets[ossW] can be NULL */
563     if( optionWidgets[ossW] )
564         optionWidgets[ossW]->setVisible( ( value == "oss" ) );
565     /* optionWidgets[alsaW] can be NULL */
566     if( optionWidgets[alsaW] )
567         optionWidgets[alsaW]->setVisible( ( value == "alsa" ) );
568 #endif
569     optionWidgets[fileW]->setVisible( ( value == "aout_file" ) );
570 }
571
572 void SPrefsPanel::updateAudioVolume( int volume )
573 {
574     qobject_cast<QSpinBox *>(optionWidgets[volLW])
575         ->setValue( volume * 100 / 256 );
576 }
577
578
579 /* Function called from the main Preferences dialog on each SPrefs Panel */
580 void SPrefsPanel::apply()
581 {
582     /* Generic save for ever panel */
583     QList<ConfigControl *>::Iterator i;
584     for( i = controls.begin() ; i != controls.end() ; i++ )
585     {
586         ConfigControl *c = qobject_cast<ConfigControl *>(*i);
587         c->doApply( p_intf );
588     }
589
590     switch( number )
591     {
592     case SPrefsInputAndCodecs:
593     {
594         /* Device default selection */
595         char *psz_devicepath =
596               qtu( qobject_cast<QLineEdit *>(optionWidgets[inputLE] )->text() );
597         if( !EMPTY_STR( psz_devicepath ) )
598         {
599             config_PutPsz( p_intf, "dvd", psz_devicepath );
600             config_PutPsz( p_intf, "vcd", psz_devicepath );
601             config_PutPsz( p_intf, "cd-audio", psz_devicepath );
602         }
603
604         /* Access filters */
605 #define saveBox( name, box ) {\
606         if( box->isChecked() ) { \
607             if( b_first ) { \
608                 qs_filter.append( name ); \
609                 b_first = false; \
610             } \
611             else qs_filter.append( ":" ).append( name ); \
612         } }
613
614         bool b_first = true;
615         qs_filter.clear();
616         saveBox( "record", qobject_cast<QCheckBox *>(optionWidgets[recordChB]) );
617         saveBox( "dump", qobject_cast<QCheckBox *>(optionWidgets[dumpChB]) );
618         saveBox( "timeshift", qobject_cast<QCheckBox *>(optionWidgets[timeshiftChB]) );
619         saveBox( "bandwidth", qobject_cast<QCheckBox *>(optionWidgets[bandwidthChB] ) );
620         config_PutPsz( p_intf, "access-filter", qtu( qs_filter ) );
621
622 #define CaCi( name, int ) config_PutInt( p_intf, name, int * i_comboValue )
623 #define CaC( name ) CaCi( name, 1 )
624         /* Caching */
625         QComboBox *cachingCombo = qobject_cast<QComboBox *>(optionWidgets[cachingCoB]);
626         int i_comboValue = cachingCombo->itemData( cachingCombo->currentIndex() ).toInt();
627         if( i_comboValue )
628         {
629             CaC( "udp-caching" );
630             if (module_Exists (p_intf, "dvdread" ))
631                 CaC( "dvdread-caching" );
632             if (module_Exists (p_intf, "dvdnav" ))
633                 CaC( "dvdnav-caching" );
634             CaC( "tcp-caching" ); CaC( "vcd-caching" );
635             CaC( "fake-caching" ); CaC( "cdda-caching" ); CaC( "file-caching" );
636             CaC( "screen-caching" );
637             CaCi( "rtsp-caching", 4 ); CaCi( "ftp-caching", 2 );
638             CaCi( "http-caching", 4 );
639             if (module_Exists (p_intf, "access_realrtsp" ))
640                 CaCi( "realrtsp-caching", 10 );
641             CaCi( "mms-caching", 19 );
642             #ifdef WIN32
643             CaC( "dshow-caching" );
644             #else
645             if (module_Exists (p_intf, "v4l" ))
646                 CaC( "v4l-caching" );
647             if (module_Exists (p_intf, "access_jack" ))
648             CaC( "jack-input-caching" );
649             if (module_Exists (p_intf, "v4l2" ))
650                 CaC( "v4l2-caching" );
651             if (module_Exists (p_intf, "pvr" ))
652                 CaC( "pvr-caching" );
653             #endif
654             //CaCi( "dv-caching" ) too short...
655         }
656         break;
657     }
658
659     /* Interfaces */
660     case SPrefsInterface:
661     {
662         if( qobject_cast<QRadioButton *>(optionWidgets[skinRB])->isChecked() )
663             config_PutPsz( p_intf, "intf", "skins2" );
664         if( qobject_cast<QRadioButton *>(optionWidgets[qtRB])->isChecked() )
665             config_PutPsz( p_intf, "intf", "qt4" );
666         break;
667     }
668
669     case SPrefsAudio:
670     {
671         bool b_normChecked =
672             qobject_cast<QCheckBox *>(optionWidgets[normalizerChB])->isChecked();
673         if( qs_filter.isEmpty() )
674         {
675             /* the psz_filter is already empty, so we just append it needed */
676             if( b_normChecked ) qs_filter = "volnorm";
677         }
678         else /* Not Empty */
679         {
680             if( qs_filter.contains( "volnorm" ) )
681             {
682                 /* The qs_filter not empty and contains "volnorm"
683                    that we have to remove */
684                 if( !b_normChecked )
685                 {
686                     /* Ugly :D */
687                     qs_filter.remove( "volnorm:" );
688                     qs_filter.remove( ":volnorm" );
689                     qs_filter.remove( "volnorm" );
690                 }
691             }
692             else /* qs_filter not empty, but doesn't have volnorm inside */
693                 if( b_normChecked ) qs_filter.append( ":volnorm" );
694         }
695         config_PutPsz( p_intf, "audio-filter", qtu( qs_filter ) );
696         break;
697     }
698     }
699 }
700
701 void SPrefsPanel::clean()
702 {}
703
704 void SPrefsPanel::lastfm_Changed( int i_state )
705 {
706     if( i_state == Qt::Checked )
707         config_AddIntf( VLC_OBJECT( p_intf ), "audioscrobbler" );
708     else if( i_state == Qt::Unchecked )
709         config_RemoveIntf( VLC_OBJECT( p_intf ), "audioscrobbler" );
710 }
711
712 #ifdef WIN32
713 #include <QDialogButtonBox>
714 #include <QHeaderView>
715 #include "util/registry.hpp"
716
717 bool SPrefsPanel::addType( const char * psz_ext, QTreeWidgetItem* current,
718                            QTreeWidgetItem* parent, QVLCRegistry *qvReg )
719 {
720     bool b_temp;
721     const char* psz_VLC = "VLC";
722     current = new QTreeWidgetItem( parent, QStringList( psz_ext ) );
723
724     if( strstr( qvReg->ReadRegistryString( psz_ext, "", ""  ), psz_VLC ) )
725     {
726         current->setCheckState( 0, Qt::Checked );
727         b_temp = false;
728     }
729     else
730     {
731         current->setCheckState( 0, Qt::Unchecked );
732         b_temp = true;
733     }
734     listAsso.append( current );
735     return b_temp;
736 }
737
738 void SPrefsPanel::assoDialog()
739 {
740     QDialog *d = new QDialog( this );
741     QGridLayout *assoLayout = new QGridLayout( d );
742
743     QTreeWidget *filetypeList = new QTreeWidget;
744     assoLayout->addWidget( filetypeList, 0, 0, 1, 4 );
745     filetypeList->header()->hide();
746
747     QVLCRegistry * qvReg = new QVLCRegistry( HKEY_CLASSES_ROOT );
748
749     QTreeWidgetItem *audioType = new QTreeWidgetItem( QStringList( qtr( "Audio Files" ) ) );
750     QTreeWidgetItem *videoType = new QTreeWidgetItem( QStringList( qtr( "Video Files" ) ) );
751     QTreeWidgetItem *otherType = new QTreeWidgetItem( QStringList( qtr( "Playlist Files" ) ) );
752
753     filetypeList->addTopLevelItem( audioType );
754     filetypeList->addTopLevelItem( videoType );
755     filetypeList->addTopLevelItem( otherType );
756
757     audioType->setExpanded( true ); audioType->setCheckState( 0, Qt::Unchecked );
758     videoType->setExpanded( true ); videoType->setCheckState( 0, Qt::Unchecked );
759     otherType->setExpanded( true ); otherType->setCheckState( 0, Qt::Unchecked );
760
761     QTreeWidgetItem *currentItem;
762
763     int i_temp = 0;
764 #define aTa( name ) i_temp += addType( name, currentItem, audioType, qvReg )
765 #define aTv( name ) i_temp += addType( name, currentItem, videoType, qvReg )
766 #define aTo( name ) i_temp += addType( name, currentItem, otherType, qvReg )
767
768     aTa( ".a52" ); aTa( ".aac" ); aTa( ".ac3" ); aTa( ".dts" ); aTa( ".flac" );
769     aTa( ".m4a" ); aTa( ".m4p" ); aTa( ".mka" ); aTa( ".mod" ); aTa( ".mp1" );
770     aTa( ".mp2" ); aTa( ".mp3" ); aTa( ".ogg" ); aTa( ".spx" ); aTa( ".wav" );
771     aTa( ".wma" ); aTa( ".xm" );
772     audioType->setCheckState( 0, ( i_temp > 0 ) ?
773                               ( ( i_temp == audioType->childCount() ) ?
774                                Qt::Checked : Qt::PartiallyChecked )
775                             : Qt::Unchecked );
776
777     i_temp = 0;
778     aTv( ".asf" ); aTv( ".avi" ); aTv( ".divx" ); aTv( ".dv" ); aTv( ".flv" );
779     aTv( ".gxf" ); aTv( ".m1v" ); aTv( ".m2v" ); aTv( ".m4v" ); aTv( ".mkv" );
780     aTv( ".mov" ); aTv( ".mp2" ); aTv( ".mp4" ); aTv( ".mpeg" );
781     aTv( ".mpeg1" ); aTv( ".mpeg2" ); aTv( ".mpeg4" ); aTv( ".mpg" );
782     aTv( ".mxf" ); aTv( ".ogm" ); aTv( ".ps" ); aTv( ".ts" );
783     aTv( ".vob" ); aTv( ".wmv" );
784     videoType->setCheckState( 0, ( i_temp > 0 ) ?
785                               ( ( i_temp == audioType->childCount() ) ?
786                                Qt::Checked : Qt::PartiallyChecked )
787                             : Qt::Unchecked );
788
789     i_temp = 0;
790     aTo( ".asx" ); aTo( ".b4s" ); aTo( ".m3u" ); aTo( ".pls" ); aTo( ".vlc" );
791     aTo( ".xspf" );
792     otherType->setCheckState( 0, ( i_temp > 0 ) ?
793                               ( ( i_temp == audioType->childCount() ) ?
794                                Qt::Checked : Qt::PartiallyChecked )
795                             : Qt::Unchecked );
796
797     QDialogButtonBox *buttonBox = new QDialogButtonBox( d );
798     QPushButton *closeButton = new QPushButton( qtr( "&Apply" ) );
799     QPushButton *clearButton = new QPushButton( qtr( "&Cancel" ) );
800     buttonBox->addButton( closeButton, QDialogButtonBox::AcceptRole );
801     buttonBox->addButton( clearButton, QDialogButtonBox::ActionRole );
802
803     assoLayout->addWidget( buttonBox, 1, 2, 1, 2 );
804
805     CONNECT( closeButton, clicked(), this, saveAsso() );
806     CONNECT( clearButton, clicked(), d, reject() );
807     d->resize( 300, 400 );
808     d->exec();
809     delete d;
810     delete qvReg;
811     listAsso.clear();
812 }
813
814 void addAsso( QVLCRegistry *qvReg, char *psz_ext )
815 {
816     std::string s_path( "VLC" ); s_path += psz_ext;
817     std::string s_path2 = s_path;
818
819     /* Save a backup if already assigned */
820     char *psz_value = qvReg->ReadRegistryString( psz_ext, "", ""  );
821
822     if( psz_value && strlen( psz_value ) > 0 )
823         qvReg->WriteRegistryString( psz_ext, "VLC.backup", psz_value );
824     delete psz_value;
825
826     /* Put a "link" to VLC.EXT as default */
827     qvReg->WriteRegistryString( psz_ext, "", s_path.c_str() );
828
829     /* Create the needed Key if they weren't done in the installer */
830     if( !qvReg->RegistryKeyExists( s_path.c_str() ) )
831     {
832         qvReg->WriteRegistryString( psz_ext, "", s_path.c_str() );
833         qvReg->WriteRegistryString( s_path.c_str(), "", "Media file" );
834         qvReg->WriteRegistryString( s_path.append( "\\shell" ).c_str() , "", "Play" );
835
836         /* Get the installer path */
837         QVLCRegistry *qvReg2 = new QVLCRegistry( HKEY_LOCAL_MACHINE );
838         std::string str_temp; str_temp.assign(
839             qvReg2->ReadRegistryString( "Software\\VideoLAN\\VLC", "", "" ) );
840
841         if( str_temp.size() )
842         {
843             qvReg->WriteRegistryString( s_path.append( "\\Play\\command" ).c_str(),
844                 "", str_temp.append(" --started-from-file \"%1\"" ).c_str() );
845
846             qvReg->WriteRegistryString( s_path2.append( "\\DefaultIcon" ).c_str(),
847                         "", str_temp.append(",0").c_str() );
848         }
849         delete qvReg2;
850     }
851 }
852
853 void delAsso( QVLCRegistry *qvReg, char *psz_ext )
854 {
855     char psz_VLC[] = "VLC";
856     char *psz_value = qvReg->ReadRegistryString( psz_ext, "", ""  );
857
858     if( psz_value && !strcmp( strcat( psz_VLC, psz_ext ), psz_value ) )
859     {
860         free( psz_value );
861         psz_value = qvReg->ReadRegistryString( psz_ext, "VLC.backup", "" );
862         if( psz_value )
863             qvReg->WriteRegistryString( psz_ext, "", psz_value );
864
865         qvReg->DeleteKey( psz_ext, "VLC.backup" );
866     }
867     delete( psz_value );
868 }
869 void SPrefsPanel::saveAsso()
870 {
871     QVLCRegistry * qvReg;
872     for( int i = 0; i < listAsso.size(); i ++ )
873     {
874         qvReg  = new QVLCRegistry( HKEY_CLASSES_ROOT );
875         if( listAsso[i]->checkState( 0 ) > 0 )
876         {
877             addAsso( qvReg, qtu( listAsso[i]->text( 0 ) ) );
878         }
879         else
880         {
881             delAsso( qvReg, qtu( listAsso[i]->text( 0 ) ) );
882         }
883     }
884     /* Gruik ? Naaah */
885     qobject_cast<QDialog *>(listAsso[0]->treeWidget()->parent())->accept();
886     delete qvReg;
887 }
888
889 #endif /* WIN32 */
890