This repository has been archived by the owner on Apr 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 196
/
Settings.py
114 lines (93 loc) · 3.36 KB
/
Settings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python
# Ubuntu Tweak - PyGTK based desktop configure tool
#
# Copyright (C) 2007-2008 TualatriX <tualatrix@gmail.com>
#
# Ubuntu Tweak is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Ubuntu Tweak is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ubuntu Tweak; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
import gconf
__all__ = (
'Setting',
'BoolSetting',
'StringSetting',
'NumSetting',
'ConstStringSetting',
)
class Setting:
"""
The base class of an option, client is shared by all subclass
Every Setting hold a key and a value
"""
client = gconf.client_get_default()
def __init__(self, key):
self.key = key
self.value = self.client.get(key)
dir = self.get_dir_from_key(key)
self.client.add_dir(dir, gconf.CLIENT_PRELOAD_NONE)
# self.client.notify_add(key, self.value_changed, key)
def value_changed(self, client, id, entry, data = None):
pass
def get_dir_from_key(self, key):
return "/".join(key.split("/")[0: -1])
class BoolSetting(Setting):
def __init__(self, key):
Setting.__init__(self, key)
self.bool = self.get_bool()
def get_bool(self):
self.value = self.client.get(self.key)
if self.value:
if self.value.type == gconf.VALUE_BOOL:
return self.value.get_bool()
elif self.value.type == gconf.VALUE_STRING:
return bool(self.value.get_string())
elif self.value.type == gconf.VALUE_INT:
return bool(self.value.get_int())
else:
return False
class StringSetting(Setting):
def __init__(self, key):
Setting.__init__(self, key)
self.string = self.get_string()
def get_string(self):
self.value = self.client.get(self.key)
if self.value:
return self.value.get_string()
else:
return None
class NumSetting(Setting):
def __init__(self, key):
Setting.__init__(self, key)
self.num = self.get_num()
def get_num(self):
self.value = self.client.get(self.key)
if self.value:
if self.value.type == gconf.VALUE_INT:
return self.value.get_int()
elif self.value.type == gconf.VALUE_FLOAT:
return self.value.get_float()
else:
return 0
def set_num(self, num):
self.value = self.client.get(self.key)
if self.value:
if self.value.type == gconf.VALUE_INT:
self.client.set_int(self.key, int(num))
elif self.value.type == gconf.VALUE_FLOAT:
self.client.set_float(self.key, num)
else:
self.client.set_float(self.key, num)
class ConstStringSetting(StringSetting):
def __init__(self, key, values):
StringSetting.__init__(self, key)
self.values = values