3 # This file is part of Panucci.
4 # Copyright (c) 2008-2009 The Panucci Audiobook and Podcast Player Project
6 # Panucci is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # Panucci is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Panucci. If not, see <http://www.gnu.org/licenses/>.
23 from simplegconf
import gconf
24 from services
import ObservableService
26 SIGNAL_NAME_SUFFIX
= '_changed'
28 # If you add a setting _please_ update the schemas file
31 'allow_blanking' : True,
32 'db_location' : '~/.panucci.sqlite',
33 'disable_delayed_skip' : False,
35 'max_recent_files' : 10,
36 'progress_locked' : False,
40 'temp_playlist' : '~/.panucci.m3u',
44 class Settings(ObservableService
):
45 """ A settings manager for Panucci. A signal is emitted when any
46 setting is changed; signal names are devised as follows:
47 setting_name + SIGNAL_NAME_SUFFIX
49 The signal prototype is: def callback( new_value )"""
52 self
.__log
= logging
.getLogger('panucci.settings.Settings')
53 signals
= [ k
+ SIGNAL_NAME_SUFFIX
for k
in default_settings
.keys() ]
54 ObservableService
.__init
__( self
, signals
, log
=self
.__log
)
56 gconf
.snotify( self
.__on
_directory
_changed
)
58 def __getattr__(self
, key
):
59 if default_settings
.has_key(key
):
60 return gconf
.sget( key
, default
=default_settings
[key
] )
62 self
.__log
.warning('Setting "%s" doesn\'t exist.' % key
)
65 def __setattr__(self
, key
, value
):
66 if default_settings
.has_key(key
):
67 if type(value
) == type(default_settings
[key
]):
68 # Don't set the value if it's the same as it used to be
69 if getattr(self
, key
) != value
:
70 gconf
.sset( key
, value
)
75 'Type of "%s" (%s) does not match default type (%s)',
76 key
, type(value
).__name
__,
77 type(default_settings
[key
]).__name
__ )
79 object.__setattr
__( self
, key
, value
)
80 self
.__log
.warning('Setting "%s" doesn\'t exist.', key
)
84 def __on_directory_changed(self
, client
, connection_id
, entry
, args
):
85 directory
, key
= os
.path
.split(entry
.get_key())
86 new_value
= getattr(self
, key
)
87 self
.__log
.debug('gconf key %s changed to: %s', key
, new_value
)
88 self
.notify( key
+ SIGNAL_NAME_SUFFIX
, new_value
,
89 caller
=self
.__on
_directory
_changed
)
91 def attach_checkbutton(self
, button
, setting
):
93 'toggled', lambda w
: setattr( self
, setting
, w
.get_active()) )
95 setting
+ SIGNAL_NAME_SUFFIX
, lambda v
: button
.set_active(v
) )
96 button
.set_active( getattr(self
, setting
) )