PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/ubuntutweak/settings/gconfsettings.py

https://bitbucket.org/cfield/ubuntu-tweak
Python | 159 lines | 141 code | 14 blank | 4 comment | 28 complexity | 9ca146b2c2c3ae1cd8cc9e0c59eef16f MD5 | raw file
  1. import glob
  2. import logging
  3. from gi.repository import GConf
  4. from ubuntutweak.policykit.dbusproxy import proxy
  5. log = logging.getLogger('GconfSetting')
  6. class GconfSetting(object):
  7. """
  8. The base class of an option, client is shared by all subclass
  9. Every Setting hold a key and a value
  10. """
  11. client = GConf.Client.get_default()
  12. schema_override = {}
  13. def __init__(self, key=None, default=None, type=None):
  14. if not self.schema_override:
  15. self.load_override()
  16. self.key = key
  17. self.type = type
  18. self.default = default
  19. log.debug("Got the schema_default: %s for key: %s" % \
  20. (self.default, self.key))
  21. if default and self.get_value() is None:
  22. self.set_value(default)
  23. if self.get_dir():
  24. self.client.add_dir(self.get_dir(), GConf.ClientPreloadType.PRELOAD_NONE)
  25. def load_override(self):
  26. try:
  27. for override in glob.glob('/usr/share/gconf/defaults/*'):
  28. for line in open(override):
  29. splits = line.split()
  30. key, value = splits[0], ' '.join(splits[1:])
  31. if value == 'true':
  32. value = True
  33. elif value == 'false':
  34. value = False
  35. else:
  36. if value.startswith('"') and value.endswith('"'):
  37. value = eval(value)
  38. self.schema_override[key] = value
  39. except Exception, e:
  40. log.error('Exception (%s) while processing override' % e)
  41. def get_dir(self):
  42. if self.key:
  43. return '/'.join(self.key.split('/')[0: -1])
  44. else:
  45. return None
  46. def get_value(self):
  47. gconfvalue = self.client.get(self.key)
  48. if gconfvalue:
  49. if gconfvalue.type == GConf.ValueType.BOOL:
  50. return gconfvalue.get_bool()
  51. if gconfvalue.type == GConf.ValueType.STRING:
  52. return gconfvalue.get_string()
  53. if gconfvalue.type == GConf.ValueType.INT:
  54. return gconfvalue.get_int()
  55. if gconfvalue.type == GConf.ValueType.FLOAT:
  56. return gconfvalue.get_float()
  57. if gconfvalue.type == GConf.ValueType.LIST:
  58. final_list = []
  59. if gconfvalue.get_list_type() == GConf.ValueType.STRING:
  60. for item in gconfvalue.get_list():
  61. final_list.append(item.get_string())
  62. return final_list
  63. else:
  64. if self.type == int:
  65. return 0
  66. elif self.type == float:
  67. return 0.0
  68. elif self.type == bool:
  69. return False
  70. elif self.type == str:
  71. return ''
  72. else:
  73. return None
  74. def set_value(self, value):
  75. if self.type and type(value) != self.type:
  76. value = self.type(value)
  77. gconfvalue = GConf.Value()
  78. if type(value) == bool:
  79. gconfvalue.type = GConf.ValueType.BOOL
  80. gconfvalue.set_bool(value)
  81. elif type(value) == str:
  82. gconfvalue.type = GConf.ValueType.STRING
  83. gconfvalue.set_string(value)
  84. elif type(value) == int:
  85. gconfvalue.type = GConf.ValueType.INT
  86. gconfvalue.set_int(int(value))
  87. elif type(value) == float:
  88. gconfvalue.type = GConf.ValueType.FLOAT
  89. gconfvalue.set_float(value)
  90. self.client.set(self.key, gconfvalue)
  91. def unset(self):
  92. self.client.unset(self.key)
  93. def connect_notify(self, func, data=None):
  94. self.client.notify_add(self.key, func, data)
  95. def get_schema_value(self):
  96. if not self.default:
  97. if self.key in self.schema_override:
  98. value = self.schema_override[self.key]
  99. if self.type and self.type != type(value):
  100. log.debug("get_schema_value: %s, the type is wrong, so convert force" % value)
  101. return self.type(value)
  102. return value
  103. value = self.client.get_default_from_schema(self.key)
  104. if value:
  105. if value.type == GConf.ValueType.BOOL:
  106. return value.get_bool()
  107. elif value.type == GConf.ValueType.STRING:
  108. return value.get_string()
  109. elif value.type == GConf.ValueType.INT:
  110. return value.get_int()
  111. elif value.type == GConf.ValueType.FLOAT:
  112. return value.get_float()
  113. else:
  114. raise Exception("No schema value for %s" % self.key)
  115. else:
  116. return self.default
  117. class UserGconfSetting(GconfSetting):
  118. def get_value(self, user):
  119. data = str(proxy.get_user_gconf(user, self.key))
  120. log.debug('UserGconfSetting get the value from proxy: %s', data)
  121. if data == 'true':
  122. return True
  123. elif data == 'false':
  124. return False
  125. else:
  126. return data
  127. def set_value(self, user, value):
  128. if value:
  129. if type(value) == bool:
  130. proxy.set_user_gconf(user, self.key, 'true', 'bool', '')
  131. elif type(value) == str:
  132. proxy.set_user_gconf(user, self.key, value, 'string', '')
  133. else:
  134. proxy.set_user_gconf(user, self.key, 'false', 'bool', '')