]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/simple_preferences.cpp
[QT] cosmetic, use regexp instead of multiple removes in simple preferences
[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 <QVBoxLayout>
41
42 #include <QtAlgorithms>
43
44 #include <string>
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/prefs/" #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 && OSD"),
80                   spref_cone_Subtitles_64.png, 3 );
81     ADD_CATEGORY( SPrefsInputAndCodecs, qtr("Input && 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     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_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             CONFIG_GENERIC( "directx-hw-yuv", Bool, NULL, hwYUVBox );
197 #else
198             ui.directXBox->setVisible( false );
199             ui.hwYUVBox->setVisible( false );
200 #endif
201
202             CONFIG_GENERIC_FILE( "snapshot-path", Directory, NULL,
203                                  ui.snapshotsDirectory, ui.snapshotsDirectoryBrowse );
204             CONFIG_GENERIC( "snapshot-prefix", String, NULL, snapshotsPrefix );
205             CONFIG_GENERIC( "snapshot-sequential", Bool, NULL,
206                             snapshotsSequentialNumbering );
207             CONFIG_GENERIC( "snapshot-format", StringList, NULL,
208                             snapshotsFormat );
209          END_SPREFS_CAT;
210
211         /******************************
212          * AUDIO Panel Implementation *
213          ******************************/
214         START_SPREFS_CAT( Audio, qtr("General Audio Settings") );
215
216             CONFIG_GENERIC( "audio", Bool, NULL, enableAudio );
217
218 #define audioCommon( name ) \
219             QWidget * name ## Control = new QWidget( ui.outputAudioBox ); \
220             QHBoxLayout * name ## Layout = new QHBoxLayout( name ## Control); \
221             name ## Layout->setMargin( 0 ); \
222             name ## Layout->setSpacing( 0 ); \
223             QLabel * name ## Label = new QLabel( qtr( "Device:" ), name ## Control ); \
224             name ## Label->setMinimumSize(QSize(100, 0)); \
225             name ## Layout->addWidget( name ## Label ); \
226
227 #define audioControl( name) \
228             audioCommon( name ) \
229             QComboBox * name ## Device = new QComboBox( name ## Control ); \
230             name ## Layout->addWidget( name ## Device ); \
231             name ## Label->setBuddy( name ## Device ); \
232             outputAudioLayout->addWidget( name ## Control, outputAudioLayout->rowCount(), 0, 1, -1 );
233
234 #define audioControl2( name) \
235             audioCommon( name ) \
236             QLineEdit * name ## Device = new QLineEdit( name ## Control ); \
237             name ## Layout->addWidget( name ## Device ); \
238             name ## Label->setBuddy( name ## Device ); \
239             QPushButton * name ## Browse = new QPushButton( qtr( "Browse..." ), name ## Control); \
240             name ## Layout->addWidget( name ## Browse ); \
241             outputAudioLayout->addWidget( name ## Control, outputAudioLayout->rowCount(), 0, 1, -1 );
242
243             /* hide if necessary */
244             ui.lastfm_user_edit->hide();
245             ui.lastfm_user_label->hide();
246             ui.lastfm_pass_edit->hide();
247             ui.lastfm_pass_label->hide();
248
249             /* Build if necessary */
250             QGridLayout * outputAudioLayout = qobject_cast<QGridLayout *>(ui.outputAudioBox->layout());
251 #ifdef WIN32
252             audioControl( DirectX );
253             optionWidgets.append( DirectXControl );
254             CONFIG_GENERIC2( "directx-audio-device", IntegerList,
255                     DirectXLabel, DirectXDevice );
256 #else
257             if( module_exists( "alsa" ) )
258             {
259                 audioControl( alsa );
260                 optionWidgets.append( alsaControl );
261
262                 CONFIG_GENERIC2( "alsa-audio-device" , StringList , alsaLabel,
263                                 alsaDevice );
264             }
265             else
266                 optionWidgets.append( NULL );
267             if( module_exists( "oss" ) )
268             {
269                 audioControl2( OSS );
270                 optionWidgets.append( OSSControl );
271                 CONFIG_GENERIC_FILE( "oss-audio-device" , File , OSSLabel, OSSDevice,
272                                  OSSBrowse );
273             }
274             else
275                 optionWidgets.append( NULL );
276 #endif
277
278             /* General Audio Options */
279             CONFIG_GENERIC_NO_BOOL( "volume" , IntegerRangeSlider, NULL,
280                                      defaultVolume );
281             CONNECT( ui.defaultVolume, valueChanged( int ),
282                     this, updateAudioVolume( int ) );
283
284             CONFIG_GENERIC( "audio-language" , String , NULL,
285                             preferredAudioLanguage );
286
287             CONFIG_GENERIC( "spdif", Bool, NULL, spdifBox );
288             CONFIG_GENERIC( "qt-autosave-volume", Bool, NULL, saveVolBox );
289             CONFIG_GENERIC( "force-dolby-surround" , IntegerList , NULL,
290                             detectionDolby );
291
292             CONFIG_GENERIC( "headphone-dolby" , Bool , NULL, headphoneEffect );
293
294             CONFIG_GENERIC_NO_BOOL( "norm-max-level" , Float , NULL,
295                                     volNormSpin );
296             CONFIG_GENERIC( "audio-visual" , Module , NULL, visualisation);
297
298             /* Audio Output Specifics */
299             CONFIG_GENERIC( "aout", Module, NULL, outputModule );
300
301             CONNECT( ui.outputModule, currentIndexChanged( int ),
302                      this, updateAudioOptions( int ) );
303
304             /* File output exists on all platforms */
305             CONFIG_GENERIC_FILE( "audiofile-file" , File , ui.fileLabel,
306                                  ui.fileName, ui.fileBrowseButton );
307
308             optionWidgets.append( ui.fileControl );
309             optionWidgets.append( ui.outputModule );
310             optionWidgets.append( ui.volNormBox );
311             /*Little mofification of ui.volumeValue to compile with Qt < 4.3 */
312             ui.volumeValue->setButtonSymbols(QAbstractSpinBox::NoButtons);
313             optionWidgets.append( ui.volumeValue );
314             updateAudioOptions( ui.outputModule->currentIndex() );
315
316             /* LastFM */
317             if( module_exists( "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             optionWidgets.append( ui.DVDDevice );
387             optionWidgets.append( ui.cachingCombo );
388
389             /* Caching */
390             /* Add the things to the ComboBox */
391             #define addToCachingBox( str, cachingNumber ) \
392                 ui.cachingCombo->addItem( qtr(str), QVariant( cachingNumber ) );
393             addToCachingBox( N_("Custom"), CachingCustom );
394             addToCachingBox( N_("Lowest latency"), CachingLowest );
395             addToCachingBox( N_("Low latency"), CachingLow );
396             addToCachingBox( N_("Normal"), CachingNormal );
397             addToCachingBox( N_("High latency"), CachingHigh );
398             addToCachingBox( N_("Higher latency"), CachingHigher );
399
400 #define TestCaC( name ) \
401     b_cache_equal =  b_cache_equal && \
402      ( i_cache == config_GetInt( p_intf, name ) )
403
404 #define TestCaCi( name, int ) \
405     b_cache_equal = b_cache_equal &&  \
406     ( ( i_cache * int ) == config_GetInt( p_intf, name ) )
407             /* Select the accurate value of the ComboBox */
408             bool b_cache_equal = true;
409             int i_cache = config_GetInt( p_intf, "file-caching");
410
411             TestCaC( "udp-caching" );
412             if (module_exists ("dvdread"))
413                 TestCaC( "dvdread-caching" );
414             if (module_exists ("dvdnav"))
415                 TestCaC( "dvdnav-caching" );
416             TestCaC( "tcp-caching" );
417             TestCaC( "fake-caching" ); TestCaC( "cdda-caching" );
418             TestCaC( "screen-caching" ); TestCaC( "vcd-caching" );
419             #ifdef WIN32
420             TestCaC( "dshow-caching" );
421             #else
422             if (module_exists ("v4l"))
423                 TestCaC( "v4l-caching" );
424             if (module_exists ("access_jack"))
425                 TestCaC( "jack-input-caching" );
426             if (module_exists ("v4l2"))
427                 TestCaC( "v4l2-caching" );
428             if (module_exists ("pvr"))
429                 TestCaC( "pvr-caching" );
430             #endif
431             TestCaCi( "rtsp-caching", 4 ); TestCaCi( "ftp-caching", 2 );
432             TestCaCi( "http-caching", 4 );
433             if (module_exists ("access_realrtsp"))
434                 TestCaCi( "realrtsp-caching", 10 );
435             TestCaCi( "mms-caching", 19 );
436             if( b_cache_equal ) ui.cachingCombo->setCurrentIndex(
437                 ui.cachingCombo->findData( QVariant( i_cache ) ) );
438
439         END_SPREFS_CAT;
440         /*******************
441          * Interface Panel *
442          *******************/
443         START_SPREFS_CAT( Interface, qtr("Interface Settings") );
444             ui.defaultLabel->setFont( italicFont );
445             ui.skinsLabel->setFont( italicFont );
446
447 #if defined( WIN32 )
448             CONFIG_GENERIC( "language", StringList, NULL, language );
449             BUTTONACT( ui.assoButton, assoDialog() );
450 #else
451             ui.language->hide();
452             ui.languageLabel->hide();
453             ui.assoName->hide();
454             ui.assoButton->hide();
455 #endif
456
457             /* interface */
458             char *psz_intf = config_GetPsz( p_intf, "intf" );
459             if( psz_intf )
460             {
461                 if( strstr( psz_intf, "skin" ) )
462                     ui.skins->setChecked( true );
463                 else if( strstr( psz_intf, "qt" ) )
464                     ui.qt4->setChecked( true );
465             }
466             free( psz_intf );
467
468             optionWidgets.append( ui.skins );
469             optionWidgets.append( ui.qt4 );
470
471             CONFIG_GENERIC( "qt-display-mode", IntegerList, NULL,
472                             displayModeBox );
473             CONFIG_GENERIC( "embedded-video", Bool, NULL, embedVideo );
474             CONFIG_GENERIC( "qt-fs-controller", Bool, NULL, fsController );
475             CONFIG_GENERIC( "qt-system-tray", Bool, NULL, systrayBox );
476             CONFIG_GENERIC_FILE( "skins2-last", File, NULL, ui.fileSkin,
477                     ui.skinBrowse );
478
479             CONFIG_GENERIC( "album-art", IntegerList, ui.artFetchLabel,
480                                                       artFetcher );
481
482             /* UPDATE options */
483 #ifdef UPDATE_CHECK
484             CONFIG_GENERIC( "qt-updates-notif", Bool, NULL, updatesBox );
485             CONFIG_GENERIC_NO_BOOL( "qt-updates-days", Integer, NULL,
486                     updatesDays );
487             CONNECT( ui.updatesBox, toggled( bool ),
488                      ui.updatesDays, setEnabled( bool ) );
489 #else
490             ui.updatesBox->hide();
491             ui.updatesDays->hide();
492 #endif
493             /* ONE INSTANCE options */
494 #if defined( WIN32 ) || defined( HAVE_DBUS ) || defined(__APPLE__)
495             CONFIG_GENERIC( "one-instance", Bool, NULL, OneInterfaceMode );
496             CONFIG_GENERIC( "playlist-enqueue", Bool, NULL,
497                     EnqueueOneInterfaceMode );
498 #else
499             ui.OneInterfaceBox->hide();
500 #endif
501             /* RECENTLY PLAYED options */
502             CONNECT( ui.saveRecentlyPlayed, toggled( bool ),
503                      ui.recentlyPlayedFilters, setEnabled( bool ) );
504             ui.recentlyPlayedFilters->setEnabled( false );
505             CONFIG_GENERIC( "qt-recentplay", Bool, NULL, saveRecentlyPlayed );
506             CONFIG_GENERIC( "qt-recentplay-filter", String, NULL,
507                     recentlyPlayedFilters );
508
509         END_SPREFS_CAT;
510
511         START_SPREFS_CAT( Subtitles, qtr("Subtitles & On Screen Display Settings") );
512             CONFIG_GENERIC( "osd", Bool, NULL, OSDBox);
513             CONFIG_GENERIC( "video-title-show", Bool, NULL, OSDTitleBox);
514
515
516             CONFIG_GENERIC( "subsdec-encoding", StringList, NULL, encoding );
517             CONFIG_GENERIC( "sub-language", String, NULL, preferredLanguage );
518             CONFIG_GENERIC_FILE( "freetype-font", File, NULL, ui.font,
519                             ui.fontBrowse );
520             CONFIG_GENERIC( "freetype-color", IntegerList, NULL, fontColor );
521             CONFIG_GENERIC( "freetype-rel-fontsize", IntegerList, NULL,
522                             fontSize );
523             CONFIG_GENERIC( "freetype-effect", IntegerList, NULL, effect );
524             CONFIG_GENERIC_NO_BOOL( "sub-margin", Integer, NULL, subsPosition );
525
526         END_SPREFS_CAT;
527
528         case SPrefsHotkeys:
529         {
530             p_config = config_FindConfig( VLC_OBJECT(p_intf), "key-fullscreen" );
531
532             QGridLayout *gLayout = new QGridLayout;
533             panel->setLayout( gLayout );
534             int line = 0;
535
536             control = new KeySelectorControl( VLC_OBJECT(p_intf), p_config ,
537                                                 this, gLayout, line );
538
539             panel_label->setText( qtr( "Configure Hotkeys" ) );
540             controls.append( control );
541
542             break;
543         }
544     }
545
546     panel_layout->addWidget( panel_label );
547     panel_layout->addWidget( title_line );
548     panel_layout->addWidget( panel );
549     if( number != SPrefsHotkeys ) panel_layout->addStretch( 2 );
550
551     setLayout( panel_layout );
552 }
553
554 void SPrefsPanel::updateAudioOptions( int number)
555 {
556     QString value = qobject_cast<QComboBox *>(optionWidgets[audioOutCoB])
557                                             ->itemData( number ).toString();
558 #ifdef WIN32
559     optionWidgets[directxW]->setVisible( ( value == "directx" ) );
560 #else
561     /* optionWidgets[ossW] can be NULL */
562     if( optionWidgets[ossW] )
563         optionWidgets[ossW]->setVisible( ( value == "oss" ) );
564     /* optionWidgets[alsaW] can be NULL */
565     if( optionWidgets[alsaW] )
566         optionWidgets[alsaW]->setVisible( ( value == "alsa" ) );
567 #endif
568     optionWidgets[fileW]->setVisible( ( value == "aout_file" ) );
569 }
570
571
572 SPrefsPanel::~SPrefsPanel()
573 {
574     qDeleteAll( controls ); controls.clear();
575 }
576
577 void SPrefsPanel::updateAudioVolume( int volume )
578 {
579     qobject_cast<QSpinBox *>(optionWidgets[volLW])
580         ->setValue( volume * 100 / 256 );
581 }
582
583
584 /* Function called from the main Preferences dialog on each SPrefs Panel */
585 void SPrefsPanel::apply()
586 {
587     /* Generic save for ever panel */
588     QList<ConfigControl *>::Iterator i;
589     for( i = controls.begin() ; i != controls.end() ; i++ )
590     {
591         ConfigControl *c = qobject_cast<ConfigControl *>(*i);
592         c->doApply( p_intf );
593     }
594
595     switch( number )
596     {
597     case SPrefsInputAndCodecs:
598     {
599         /* Device default selection */
600         char *psz_devicepath =
601               qtu( qobject_cast<QLineEdit *>(optionWidgets[inputLE] )->text() );
602         if( !EMPTY_STR( psz_devicepath ) )
603         {
604             config_PutPsz( p_intf, "dvd", psz_devicepath );
605             config_PutPsz( p_intf, "vcd", psz_devicepath );
606             config_PutPsz( p_intf, "cd-audio", psz_devicepath );
607         }
608
609 #define CaCi( name, int ) config_PutInt( p_intf, name, int * i_comboValue )
610 #define CaC( name ) CaCi( name, 1 )
611         /* Caching */
612         QComboBox *cachingCombo = qobject_cast<QComboBox *>(optionWidgets[cachingCoB]);
613         int i_comboValue = cachingCombo->itemData( cachingCombo->currentIndex() ).toInt();
614         if( i_comboValue )
615         {
616             CaC( "udp-caching" );
617             if (module_exists ("dvdread" ))
618                 CaC( "dvdread-caching" );
619             if (module_exists ("dvdnav" ))
620                 CaC( "dvdnav-caching" );
621             CaC( "tcp-caching" ); CaC( "vcd-caching" );
622             CaC( "fake-caching" ); CaC( "cdda-caching" ); CaC( "file-caching" );
623             CaC( "screen-caching" );
624             CaCi( "rtsp-caching", 2 ); CaCi( "ftp-caching", 2 );
625             CaCi( "http-caching", 2 );
626             if (module_exists ("access_realrtsp" ))
627                 CaCi( "realrtsp-caching", 10 );
628             CaCi( "mms-caching", 10 );
629             #ifdef WIN32
630             CaC( "dshow-caching" );
631             #else
632             if (module_exists ( "v4l" ))
633                 CaC( "v4l-caching" );
634             if (module_exists ( "access_jack" ))
635             CaC( "jack-input-caching" );
636             if (module_exists ( "v4l2" ))
637                 CaC( "v4l2-caching" );
638             if (module_exists ( "pvr" ))
639                 CaC( "pvr-caching" );
640             #endif
641             //CaCi( "dv-caching" ) too short...
642         }
643         break;
644     }
645
646     /* Interfaces */
647     case SPrefsInterface:
648     {
649         if( qobject_cast<QRadioButton *>(optionWidgets[skinRB])->isChecked() )
650             config_PutPsz( p_intf, "intf", "skins2" );
651         if( qobject_cast<QRadioButton *>(optionWidgets[qtRB])->isChecked() )
652             config_PutPsz( p_intf, "intf", "qt" );
653         break;
654     }
655
656     case SPrefsAudio:
657     {
658         bool b_normChecked =
659             qobject_cast<QCheckBox *>(optionWidgets[normalizerChB])->isChecked();
660         if( qs_filter.isEmpty() )
661         {
662             /* the psz_filter is already empty, so we just append it needed */
663             if( b_normChecked ) qs_filter = "volnorm";
664         }
665         else /* Not Empty */
666         {
667             if( qs_filter.contains( "volnorm" ) )
668             {
669                 /* The qs_filter not empty and contains "volnorm"
670                    that we have to remove */
671                 if( !b_normChecked )
672                 {
673                     qs_filter.remove( QRegExp(":?volnorm:?") );
674                 }
675             }
676             else /* qs_filter not empty, but doesn't have volnorm inside */
677                 if( b_normChecked ) qs_filter.append( ":volnorm" );
678         }
679         config_PutPsz( p_intf, "audio-filter", qtu( qs_filter ) );
680         break;
681     }
682     }
683 }
684
685 void SPrefsPanel::clean()
686 {}
687
688 void SPrefsPanel::lastfm_Changed( int i_state )
689 {
690     if( i_state == Qt::Checked )
691         config_AddIntf( VLC_OBJECT( p_intf ), "audioscrobbler" );
692     else if( i_state == Qt::Unchecked )
693         config_RemoveIntf( VLC_OBJECT( p_intf ), "audioscrobbler" );
694 }
695
696 #ifdef WIN32
697 #include <QDialogButtonBox>
698 #include <QHeaderView>
699 #include "util/registry.hpp"
700
701 bool SPrefsPanel::addType( const char * psz_ext, QTreeWidgetItem* current,
702                            QTreeWidgetItem* parent, QVLCRegistry *qvReg )
703 {
704     bool b_temp;
705     const char* psz_VLC = "VLC";
706     current = new QTreeWidgetItem( parent, QStringList( psz_ext ) );
707
708     if( strstr( qvReg->ReadRegistryString( psz_ext, "", ""  ), psz_VLC ) )
709     {
710         current->setCheckState( 0, Qt::Checked );
711         b_temp = false;
712     }
713     else
714     {
715         current->setCheckState( 0, Qt::Unchecked );
716         b_temp = true;
717     }
718     listAsso.append( current );
719     return b_temp;
720 }
721
722 void SPrefsPanel::assoDialog()
723 {
724     QDialog *d = new QDialog( this );
725     QGridLayout *assoLayout = new QGridLayout( d );
726
727     QTreeWidget *filetypeList = new QTreeWidget;
728     assoLayout->addWidget( filetypeList, 0, 0, 1, 4 );
729     filetypeList->header()->hide();
730
731     QVLCRegistry * qvReg = new QVLCRegistry( HKEY_CLASSES_ROOT );
732
733     QTreeWidgetItem *audioType = new QTreeWidgetItem( QStringList( qtr( "Audio Files" ) ) );
734     QTreeWidgetItem *videoType = new QTreeWidgetItem( QStringList( qtr( "Video Files" ) ) );
735     QTreeWidgetItem *otherType = new QTreeWidgetItem( QStringList( qtr( "Playlist Files" ) ) );
736
737     filetypeList->addTopLevelItem( audioType );
738     filetypeList->addTopLevelItem( videoType );
739     filetypeList->addTopLevelItem( otherType );
740
741     audioType->setExpanded( true ); audioType->setCheckState( 0, Qt::Unchecked );
742     videoType->setExpanded( true ); videoType->setCheckState( 0, Qt::Unchecked );
743     otherType->setExpanded( true ); otherType->setCheckState( 0, Qt::Unchecked );
744
745     QTreeWidgetItem *currentItem;
746
747     int i_temp = 0;
748 #define aTa( name ) i_temp += addType( name, currentItem, audioType, qvReg )
749 #define aTv( name ) i_temp += addType( name, currentItem, videoType, qvReg )
750 #define aTo( name ) i_temp += addType( name, currentItem, otherType, qvReg )
751
752     aTa( ".a52" ); aTa( ".aac" ); aTa( ".ac3" ); aTa( ".dts" ); aTa( ".flac" );
753     aTa( ".m4a" ); aTa( ".m4p" ); aTa( ".mka" ); aTa( ".mod" ); aTa( ".mp1" );
754     aTa( ".mp2" ); aTa( ".mp3" ); aTa( ".oma" ); aTa( ".oga" ); aTa( ".spx" );
755     aTa( ".wav" ); aTa( ".wma" ); aTa( ".xm" );
756     audioType->setCheckState( 0, ( i_temp > 0 ) ?
757                               ( ( i_temp == audioType->childCount() ) ?
758                                Qt::Checked : Qt::PartiallyChecked )
759                             : Qt::Unchecked );
760
761     i_temp = 0;
762     aTv( ".asf" ); aTv( ".avi" ); aTv( ".divx" ); aTv( ".dv" ); aTv( ".flv" );
763     aTv( ".gxf" ); aTv( ".m1v" ); aTv( ".m2v" ); aTv( ".m2ts" ); aTv( ".m4v" );
764     aTv( ".mkv" ); aTv( ".mov" ); aTv( ".mp2" ); aTv( ".mp4" ); aTv( ".mpeg" );
765     aTv( ".mpeg1" ); aTv( ".mpeg2" ); aTv( ".mpeg4" ); aTv( ".mpg" );
766     aTv( ".mts" ); aTv( ".mxf" );
767     aTv( ".ogg" ); aTv( ".ogm" ); aTv( ".ogx" ); aTv( ".ogv" );  aTv( ".ts" );
768     aTv( ".vob" ); aTv( ".wmv" );
769     videoType->setCheckState( 0, ( i_temp > 0 ) ?
770                               ( ( i_temp == audioType->childCount() ) ?
771                                Qt::Checked : Qt::PartiallyChecked )
772                             : Qt::Unchecked );
773
774     i_temp = 0;
775     aTo( ".asx" ); aTo( ".b4s" ); aTo( ".m3u" ); aTo( ".pls" ); aTo( ".vlc" );
776     aTo( ".xspf" );
777     otherType->setCheckState( 0, ( i_temp > 0 ) ?
778                               ( ( i_temp == audioType->childCount() ) ?
779                                Qt::Checked : Qt::PartiallyChecked )
780                             : Qt::Unchecked );
781
782     QDialogButtonBox *buttonBox = new QDialogButtonBox( d );
783     QPushButton *closeButton = new QPushButton( qtr( "&Apply" ) );
784     QPushButton *clearButton = new QPushButton( qtr( "&Cancel" ) );
785     buttonBox->addButton( closeButton, QDialogButtonBox::AcceptRole );
786     buttonBox->addButton( clearButton, QDialogButtonBox::ActionRole );
787
788     assoLayout->addWidget( buttonBox, 1, 2, 1, 2 );
789
790     CONNECT( closeButton, clicked(), this, saveAsso() );
791     CONNECT( clearButton, clicked(), d, reject() );
792     d->resize( 300, 400 );
793     d->exec();
794     delete d;
795     delete qvReg;
796     listAsso.clear();
797 }
798
799 void addAsso( QVLCRegistry *qvReg, char *psz_ext )
800 {
801     std::string s_path( "VLC" ); s_path += psz_ext;
802     std::string s_path2 = s_path;
803
804     /* Save a backup if already assigned */
805     char *psz_value = qvReg->ReadRegistryString( psz_ext, "", ""  );
806
807     if( psz_value && strlen( psz_value ) > 0 )
808         qvReg->WriteRegistryString( psz_ext, "VLC.backup", psz_value );
809     delete psz_value;
810
811     /* Put a "link" to VLC.EXT as default */
812     qvReg->WriteRegistryString( psz_ext, "", s_path.c_str() );
813
814     /* Create the needed Key if they weren't done in the installer */
815     if( !qvReg->RegistryKeyExists( s_path.c_str() ) )
816     {
817         qvReg->WriteRegistryString( psz_ext, "", s_path.c_str() );
818         qvReg->WriteRegistryString( s_path.c_str(), "", "Media file" );
819         qvReg->WriteRegistryString( s_path.append( "\\shell" ).c_str() , "", "Play" );
820
821         /* Get the installer path */
822         QVLCRegistry *qvReg2 = new QVLCRegistry( HKEY_LOCAL_MACHINE );
823         std::string str_temp; str_temp.assign(
824             qvReg2->ReadRegistryString( "Software\\VideoLAN\\VLC", "", "" ) );
825
826         if( str_temp.size() )
827         {
828             qvReg->WriteRegistryString( s_path.append( "\\Play\\command" ).c_str(),
829                 "", str_temp.append(" --started-from-file \"%1\"" ).c_str() );
830
831             qvReg->WriteRegistryString( s_path2.append( "\\DefaultIcon" ).c_str(),
832                         "", str_temp.append(",0").c_str() );
833         }
834         delete qvReg2;
835     }
836 }
837
838 void delAsso( QVLCRegistry *qvReg, char *psz_ext )
839 {
840     char psz_VLC[] = "VLC";
841     char *psz_value = qvReg->ReadRegistryString( psz_ext, "", ""  );
842
843     if( psz_value && !strcmp( strcat( psz_VLC, psz_ext ), psz_value ) )
844     {
845         free( psz_value );
846         psz_value = qvReg->ReadRegistryString( psz_ext, "VLC.backup", "" );
847         if( psz_value )
848             qvReg->WriteRegistryString( psz_ext, "", psz_value );
849
850         qvReg->DeleteKey( psz_ext, "VLC.backup" );
851     }
852     delete( psz_value );
853 }
854 void SPrefsPanel::saveAsso()
855 {
856     QVLCRegistry * qvReg;
857     for( int i = 0; i < listAsso.size(); i ++ )
858     {
859         qvReg  = new QVLCRegistry( HKEY_CLASSES_ROOT );
860         if( listAsso[i]->checkState( 0 ) > 0 )
861         {
862             addAsso( qvReg, qtu( listAsso[i]->text( 0 ) ) );
863         }
864         else
865         {
866             delAsso( qvReg, qtu( listAsso[i]->text( 0 ) ) );
867         }
868     }
869     /* Gruik ? Naaah */
870     qobject_cast<QDialog *>(listAsso[0]->treeWidget()->parent())->accept();
871     delete qvReg;
872 }
873
874 #endif /* WIN32 */
875