/usr/share/cinnamon/cinnamon-settings/bin/JsonSettingsWidgets.py is in cinnamon-common 3.6.7-8ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | from gi.repository import Gio, GObject
from SettingsWidgets import * # noqa
from TreeListWidgets import List # noqa
import collections
import json
CAN_BACKEND.append("List")
JSON_SETTINGS_PROPERTIES_MAP = {
"description": "label",
"min": "mini",
"max": "maxi",
"step": "step",
"units": "units",
"show-value": "show_value",
"select-dir": "dir_select",
"height": "height",
"tooltip": "tooltip",
"possible": "possible",
"dependency": "dep_key",
"expand-width": "expand_width",
"columns": "columns",
}
class JSONSettingsHandler(object):
def __init__(self, filepath, notify_callback=None):
super(JSONSettingsHandler, self).__init__()
self.resume_timeout = None
self.notify_callback = notify_callback
self.filepath = filepath
self.file_obj = Gio.File.new_for_path(self.filepath)
self.file_monitor = self.file_obj.monitor_file(Gio.FileMonitorFlags.SEND_MOVED, None)
self.file_monitor.connect("changed", self.check_settings)
self.bindings = {}
self.listeners = {}
self.deps = {}
self.settings = self.get_settings()
def bind(self, key, obj, prop, direction, map_get=None, map_set=None):
if direction & (Gio.SettingsBindFlags.SET | Gio.SettingsBindFlags.GET) == 0:
direction |= Gio.SettingsBindFlags.SET | Gio.SettingsBindFlags.GET
binding_info = {"obj": obj, "prop": prop, "dir": direction, "map_get": map_get, "map_set": map_set}
if key not in self.bindings:
self.bindings[key] = []
self.bindings[key].append(binding_info)
if direction & Gio.SettingsBindFlags.GET != 0:
self.set_object_value(binding_info, self.get_value(key))
if direction & Gio.SettingsBindFlags.SET != 0:
binding_info["oid"] = obj.connect("notify::" + prop, self.object_value_changed, key)
def listen(self, key, callback):
if key not in self.listeners:
self.listeners[key] = []
self.listeners[key].append(callback)
def get_value(self, key):
return self.get_property(key, "value")
def set_value(self, key, value):
if value != self.settings[key]["value"]:
self.settings[key]["value"] = value
self.save_settings()
if self.notify_callback:
self.notify_callback(self, key, value)
def get_property(self, key, prop):
props = self.settings[key]
return props[prop]
def has_property(self, key, prop):
return prop in self.settings.keys()
def has_key(self, key):
return key in self.settings
def object_value_changed(self, obj, value, key):
for info in self.bindings[key]:
if obj == info["obj"]:
value = info["obj"].get_property(info["prop"])
if "map_set" in info.keys() and info["map_set"] is not None:
value = info["map_set"](value)
else:
self.set_object_value(info, value)
self.set_value(key, value)
def set_object_value(self, info, value):
if info["dir"] & Gio.SettingsBindFlags.GET == 0:
return
with info["obj"].freeze_notify():
if "map_get" in info.keys() and info["map_get"] is not None:
value = info["map_get"](value)
if value != info["obj"].get_property(info["prop"]):
info["obj"].set_property(info["prop"], value)
def check_settings(self, *args):
old_settings = self.settings
self.settings = self.get_settings()
for key in self.bindings:
new_value = self.settings[key]["value"]
if new_value != old_settings[key]["value"]:
for info in self.bindings[key]:
self.set_object_value(info, new_value)
for key, callback_list in self.listeners.items():
new_value = self.settings[key]["value"]
if new_value != old_settings[key]["value"]:
for callback in callback_list:
callback(key, new_value)
def get_settings(self):
file = open(self.filepath)
raw_data = file.read()
file.close()
try:
settings = json.loads(raw_data, encoding=None, object_pairs_hook=collections.OrderedDict)
except Exception:
raise Exception("Failed to parse settings JSON data for file %s" % (self.filepath))
return settings
def save_settings(self):
self.pause_monitor()
if os.path.exists(self.filepath):
os.remove(self.filepath)
raw_data = json.dumps(self.settings, indent=4)
new_file = open(self.filepath, 'w+')
new_file.write(raw_data)
new_file.close()
self.resume_monitor()
def pause_monitor(self):
self.file_monitor.cancel()
self.handler = None
def resume_monitor(self):
if self.resume_timeout:
GObject.source_remove(self.resume_timeout)
self.resume_timeout = GObject.timeout_add(2000, self.do_resume)
def do_resume(self):
self.file_monitor = self.file_obj.monitor_file(Gio.FileMonitorFlags.SEND_MOVED, None)
self.handler = self.file_monitor.connect("changed", self.check_settings)
self.resume_timeout = None
return False
def reset_to_defaults(self):
for key in self.settings:
if "value" in self.settings[key]:
self.settings[key]["value"] = self.settings[key]["default"]
self.do_key_update(key)
self.save_settings()
def do_key_update(self, key):
if key in self.bindings:
for info in self.bindings[key]:
self.set_object_value(info, self.settings[key]["value"])
if key in self.listeners:
for callback in self.listeners[key]:
callback(key, self.settings[key]["value"])
def load_from_file(self, filepath):
file = open(filepath)
raw_data = file.read()
file.close()
try:
settings = json.loads(raw_data, encoding=None, object_pairs_hook=collections.OrderedDict)
except Exception:
raise Exception("Failed to parse settings JSON data for file %s" % (self.filepath))
for key in self.settings:
if "value" not in self.settings[key]:
continue
if key in settings and "value" in self.settings[key]:
self.settings[key]["value"] = settings[key]["value"]
self.do_key_update(key)
else:
print("Skipping key %s: the key does not exist in %s or has no value" % (key, filepath))
self.save_settings()
def save_to_file(self, filepath):
if os.path.exists(filepath):
os.remove(filepath)
raw_data = json.dumps(self.settings, indent=4)
new_file = open(filepath, 'w+')
new_file.write(raw_data)
new_file.close()
class JSONSettingsBackend(object):
def attach(self):
if hasattr(self, "set_rounding") and self.settings.has_property(self.key, "round"):
self.set_rounding(self.settings.get_property(self.key, "round"))
if hasattr(self, "bind_object"):
bind_object = self.bind_object
else:
bind_object = self.content_widget
if self.bind_dir is not None:
self.settings.bind(self.key, bind_object, self.bind_prop, self.bind_dir,
self.map_get if hasattr(self, "map_get") else None,
self.map_set if hasattr(self, "map_set") else None)
else:
self.settings.listen(self.key, self.on_setting_changed)
self.on_setting_changed()
self.connect_widget_handlers()
def set_value(self, value):
self.settings.set_value(self.key, value)
def get_value(self):
return self.settings.get_value(self.key)
def get_range(self):
min = self.settings.get_property(self.key, "min")
max = self.settings.get_property(self.key, "max")
return [min, max]
def on_setting_changed(self, *args):
raise NotImplementedError("SettingsWidget class must implement on_setting_changed().")
def connect_widget_handlers(self, *args):
if self.bind_dir is None:
raise NotImplementedError("SettingsWidget classes with no .bind_dir must implement connect_widget_handlers().")
def json_settings_factory(subclass):
class NewClass(globals()[subclass], JSONSettingsBackend):
def __init__(self, key, settings, properties):
self.key = key
self.settings = settings
kwargs = {}
for prop in properties:
if prop in JSON_SETTINGS_PROPERTIES_MAP:
kwargs[JSON_SETTINGS_PROPERTIES_MAP[prop]] = properties[prop]
elif prop == "options":
kwargs["options"] = []
for value, label in properties[prop].items():
kwargs["options"].append((label, value))
super(NewClass, self).__init__(**kwargs)
self.attach()
def set_dep_key(self, dep_key):
if self.settings.has_key(dep_key): # noqa
self.settings.bind(dep_key, self, "sensitive", Gio.SettingsBindFlags.GET)
else:
print("Ignoring dependency on key '%s': no such key in the schema" % dep_key)
return NewClass
for widget in CAN_BACKEND:
globals()["JSONSettings" + widget] = json_settings_factory(widget)
|