Details dialog for PlaylistItem objects
[panucci.git] / src / panucci / settings.py
blob76165ebc8c23a66387523f929476cdd1fd44c189
1 #!/usr/bin/python
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/>.
20 import logging
21 import os.path
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
30 default_settings = {
31 'allow_blanking' : True,
32 'db_location' : '~/.panucci.sqlite',
33 'disable_delayed_skip' : False,
34 'last_folder' : '~',
35 'max_recent_files' : 10,
36 'progress_locked' : False,
37 'seek_long' : 60,
38 'seek_short' : 10,
39 'skip_delay' : 0.5,
40 'temp_playlist' : '~/.panucci.m3u',
41 'volume' : 0.3,
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 )"""
51 def __init__(self):
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] )
61 else:
62 self.__log.warning('Setting "%s" doesn\'t exist.' % key)
63 raise AttributeError
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 )
72 return True
73 else:
74 self.__log.warning(
75 'Type of "%s" (%s) does not match default type (%s)',
76 key, type(value).__name__,
77 type(default_settings[key]).__name__ )
78 else:
79 object.__setattr__( self, key, value )
80 self.__log.warning('Setting "%s" doesn\'t exist.', key)
82 return False
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):
92 button.connect(
93 'toggled', lambda w: setattr( self, setting, w.get_active()) )
94 self.register(
95 setting + SIGNAL_NAME_SUFFIX, lambda v: button.set_active(v) )
96 button.set_active( getattr(self, setting) )
98 settings = Settings()