2015-06-26 01:36:36 +01:00
|
|
|
#!/usr/bin/python2
|
2013-03-06 01:31:58 +00:00
|
|
|
|
|
|
|
from gi.repository import Gio
|
|
|
|
|
2019-02-11 13:54:20 +00:00
|
|
|
|
2013-03-06 01:31:58 +00:00
|
|
|
class EasyGSettings:
|
|
|
|
|
2019-02-11 13:54:20 +00:00
|
|
|
def __init__(self, schema = None):
|
2013-03-06 01:31:58 +00:00
|
|
|
self.schema = schema
|
|
|
|
self.settings = Gio.Settings.new(self.schema)
|
2019-02-11 13:54:20 +00:00
|
|
|
self.handlerIds = []
|
2013-03-06 01:31:58 +00:00
|
|
|
|
2019-02-11 13:54:20 +00:00
|
|
|
def get(self, type, key):
|
2013-03-06 01:31:58 +00:00
|
|
|
|
|
|
|
if type == "bool":
|
2019-02-11 13:54:20 +00:00
|
|
|
return self.settings.get_boolean(key)
|
2013-03-06 01:31:58 +00:00
|
|
|
if type == "string":
|
2019-02-11 13:54:20 +00:00
|
|
|
return self.settings.get_string(key)
|
2013-03-06 01:31:58 +00:00
|
|
|
if type == "int":
|
2019-02-11 13:54:20 +00:00
|
|
|
return self.settings.get_int(key)
|
2013-03-06 01:31:58 +00:00
|
|
|
if type == "color":
|
2019-02-11 13:54:20 +00:00
|
|
|
color = self.settings.get_string(key)
|
|
|
|
if not self.evalColor(color):
|
2013-03-06 01:31:58 +00:00
|
|
|
self.settings.set_string(key, "#ffffff")
|
|
|
|
return "#ffffff"
|
|
|
|
return color
|
|
|
|
|
|
|
|
t = type.split("-")
|
|
|
|
if len(t) == 2 and t[0] == "list":
|
2019-02-11 13:54:20 +00:00
|
|
|
return self.settings.get_strv(key)
|
2013-03-06 01:31:58 +00:00
|
|
|
|
2019-02-11 13:54:20 +00:00
|
|
|
return self.settings.get(key)
|
2013-03-06 01:31:58 +00:00
|
|
|
|
2019-02-11 13:54:20 +00:00
|
|
|
def set(self, type, key, value):
|
2013-03-06 01:31:58 +00:00
|
|
|
|
|
|
|
if type == "bool":
|
2019-02-11 13:54:20 +00:00
|
|
|
return self.settings.set_boolean(key, value)
|
2013-03-06 01:31:58 +00:00
|
|
|
|
|
|
|
if type == "string":
|
2019-02-11 13:54:20 +00:00
|
|
|
return self.settings.set_string(key, value)
|
2013-03-06 01:31:58 +00:00
|
|
|
|
|
|
|
if type == "int":
|
2019-02-11 13:54:20 +00:00
|
|
|
return self.settings.set_int(key, value)
|
2013-03-06 01:31:58 +00:00
|
|
|
|
|
|
|
if type == "color":
|
2019-02-11 13:54:20 +00:00
|
|
|
if self.evalColor(value):
|
|
|
|
return self.settings.set_string(key, value)
|
2013-03-06 01:31:58 +00:00
|
|
|
else:
|
2019-02-11 13:54:20 +00:00
|
|
|
return self.settings.set_string(key, "#ffffff")
|
2013-03-06 01:31:58 +00:00
|
|
|
|
|
|
|
t = type.split("-")
|
|
|
|
if len(t) == 2 and t[0] == "list":
|
2019-02-11 13:54:20 +00:00
|
|
|
return self.settings.set_strv(key, value)
|
2013-03-06 01:31:58 +00:00
|
|
|
|
2019-02-11 13:54:20 +00:00
|
|
|
return self.settings.set(key, value)
|
2013-03-06 01:31:58 +00:00
|
|
|
|
2019-02-11 13:54:20 +00:00
|
|
|
def notifyAdd(self, key, callback, args = None):
|
2013-03-06 01:31:58 +00:00
|
|
|
handlerId = self.settings.connect("changed::"+key, callback, args)
|
2019-02-11 13:54:20 +00:00
|
|
|
self.handlerIds.append(handlerId)
|
2013-03-06 01:31:58 +00:00
|
|
|
return handlerId
|
|
|
|
|
2019-02-11 13:54:20 +00:00
|
|
|
def notifyRemove(self, handlerId):
|
2013-03-06 01:31:58 +00:00
|
|
|
return self.settings.disconnect(handlerId)
|
|
|
|
|
2019-02-11 13:54:20 +00:00
|
|
|
def notifyRemoveAll(self):
|
2013-03-06 01:31:58 +00:00
|
|
|
for handlerId in self.handlerIds:
|
2019-02-11 13:54:20 +00:00
|
|
|
self.settings.disconnect(handlerId)
|
2013-03-06 01:31:58 +00:00
|
|
|
|
2019-02-11 13:54:20 +00:00
|
|
|
def evalColor(self, colorToTest):
|
|
|
|
if colorToTest[0] != '#' or len(colorToTest) != 7:
|
2013-03-06 01:31:58 +00:00
|
|
|
return False
|
|
|
|
for i in colorToTest[1:]:
|
|
|
|
if i not in ['a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']:
|
|
|
|
return False
|
|
|
|
return True
|
2016-05-21 17:11:36 +01:00
|
|
|
|
2019-02-11 13:54:20 +00:00
|
|
|
def bindGSettingsEntryToVar(self, type, key, obj, varName):
|
|
|
|
return self.notifyAdd(key, self.setVar, (type, obj, varName))
|
2016-05-21 17:11:36 +01:00
|
|
|
|
2019-02-11 13:54:20 +00:00
|
|
|
def setVar(self, settings, key, args):
|
2013-03-06 01:31:58 +00:00
|
|
|
type, obj, varName = args
|
|
|
|
|
|
|
|
if type == "string":
|
2019-02-11 13:54:20 +00:00
|
|
|
setattr(obj, varName, settings.get_string(key))
|
2013-03-06 01:31:58 +00:00
|
|
|
elif type == "int":
|
2019-02-11 13:54:20 +00:00
|
|
|
setattr(obj, varName, settings.get_int(key))
|
2013-03-06 01:31:58 +00:00
|
|
|
elif type == "float":
|
2019-02-11 13:54:20 +00:00
|
|
|
setattr(obj, varName, settings.get_float(key))
|
2013-03-06 01:31:58 +00:00
|
|
|
elif type == "bool":
|
2019-02-11 13:54:20 +00:00
|
|
|
setattr(obj, varName, settings.get_boolean(key))
|
2013-03-06 01:31:58 +00:00
|
|
|
else:
|
2019-02-11 13:54:20 +00:00
|
|
|
setattr(obj, varName, settings.get_value(key))
|