/pessulus-2.30.3/Pessulus/lockdownappliergconf.py

# · Python · 224 lines · 154 code · 43 blank · 27 comment · 41 complexity · 171590b58ebb8285cef7d48d1d89a73a MD5 · raw file

  1. #!/usr/bin/env python
  2. # vim: set ts=4 sw=4 et:
  3. #
  4. # Copyright (C) 2005 Vincent Untz <vuntz@gnome.org>
  5. #
  6. # This program 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 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program 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.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. #
  20. import gconf
  21. import gobject
  22. import lockdownapplier
  23. from config import *
  24. def can_edit_mandatory ():
  25. try:
  26. engine = gconf.engine_get_for_address (GCONF_MANDATORY_SOURCE)
  27. except gobject.GError:
  28. return False
  29. if engine == None:
  30. return False
  31. try:
  32. #entry = engine.get_entry ("/apps/gconf-editor/can_edit_source",
  33. # None,
  34. # False)
  35. #gconf_engine_get_entry() is not wrapped. Ugly workaround:
  36. client = gconf.client_get_for_engine (engine)
  37. entry = client.get_entry ("/apps/gconf-editor/can_edit_source", "",
  38. False)
  39. except gobject.GError:
  40. return False
  41. if entry != None:
  42. return entry.get_is_writable ()
  43. return False
  44. # We have this safe class to be able to use gconf methods even if we don't have
  45. # any gconf access. In the future, we might have non-gconf sources, so it's
  46. # important to keep the gconf applier working (by being effectively disabled),
  47. # even when gconf is not working.
  48. class SafeGConfClient:
  49. def __init__ (self):
  50. self.client = gconf.client_get_default ()
  51. try:
  52. self.client.get_bool ("/apps/gconf-editor/can_edit_source")
  53. self.can_use = True
  54. except gobject.GError:
  55. self.can_use = False
  56. def really_works (self):
  57. return self.can_use
  58. def get_schema (self, key):
  59. if not self.can_use:
  60. return None
  61. return self.client.get_schema (key)
  62. def get_bool (self, key):
  63. if not self.can_use:
  64. return False
  65. return self.client.get_bool (key)
  66. def set_bool (self, key, value):
  67. if not self.can_use:
  68. return
  69. self.client.set_bool (key, value)
  70. def get_list (self, key, list_type):
  71. if not self.can_use:
  72. return []
  73. return self.client.get_list (key, list_type)
  74. def set_list (self, key, list_type, value):
  75. if not self.can_use:
  76. return
  77. self.client.set_list (key, list_type, value)
  78. def key_is_writable (self, key):
  79. if not self.can_use:
  80. return False
  81. return self.client.key_is_writable (key)
  82. def notify_add (self, key, handler, data = None):
  83. if not self.can_use:
  84. return None
  85. return self.client.notify_add (key, handler, data)
  86. def notify_remove (self, monitor):
  87. if not self.can_use:
  88. return
  89. self.client.notify_remove (monitor)
  90. def add_dir (self, dir, preloadtype):
  91. if not self.can_use:
  92. return
  93. self.client.add_dir (dir, preloadtype)
  94. def remove_dir (self, dir):
  95. if not self.can_use:
  96. return
  97. self.client.remove_dir (dir)
  98. class PessulusLockdownApplierGconf (lockdownapplier.PessulusLockdownApplier):
  99. def __init__ (self):
  100. self.can_edit_mandatory = can_edit_mandatory ()
  101. self.client_mandatory = None
  102. if (self.can_edit_mandatory):
  103. engine = gconf.engine_get_for_address (GCONF_MANDATORY_SOURCE)
  104. self.client_mandatory = gconf.client_get_for_engine (engine)
  105. self.client = SafeGConfClient ()
  106. def supports_normal_settings (self):
  107. return self.client.really_works ()
  108. def supports_mandatory_settings (self):
  109. return self.can_edit_mandatory
  110. def get_schema (self, key):
  111. return self.client.get_schema (key)
  112. def get_bool (self, key):
  113. value = None
  114. is_mandatory = False
  115. if self.supports_mandatory_settings ():
  116. entry = self.client_mandatory.get_without_default (key)
  117. if entry != None:
  118. is_mandatory = True
  119. value = entry.get_bool ()
  120. else:
  121. value = self.client.get_bool (key)
  122. else:
  123. value = self.client.get_bool (key)
  124. return (value, is_mandatory)
  125. def set_bool (self, key, value, mandatory):
  126. if mandatory:
  127. if self.supports_mandatory_settings ():
  128. self.client_mandatory.set_bool (key, value)
  129. else:
  130. if self.supports_mandatory_settings ():
  131. self.client_mandatory.unset (key)
  132. self.client.set_bool (key, value)
  133. def get_list (self, key, list_type):
  134. value = None
  135. is_mandatory = False
  136. if self.supports_mandatory_settings ():
  137. entry = self.client_mandatory.get_without_default (key)
  138. if entry != None:
  139. is_mandatory = True
  140. list = entry.get_list ()
  141. type = entry.get_list_type ()
  142. value = []
  143. for element in list:
  144. if type == gconf.VALUE_STRING:
  145. value.append (element.get_string ())
  146. elif type == gconf.VALUE_BOOL:
  147. value.append (element.get_bool ())
  148. elif type == gconf.VALUE_INT:
  149. value.append (element.get_int ())
  150. elif type == gconf.VALUE_FLOAT:
  151. value.append (element.get_float ())
  152. else:
  153. value = self.client.get_list (key, list_type)
  154. else:
  155. value = self.client.get_list (key, list_type)
  156. return (value, is_mandatory)
  157. def set_list (self, key, list_type, value, mandatory):
  158. if mandatory:
  159. if self.supports_mandatory_settings ():
  160. self.client_mandatory.set_list (key, list_type, value)
  161. else:
  162. if self.supports_mandatory_settings ():
  163. self.client_mandatory.unset (key)
  164. self.client.set_list (key, list_type, value)
  165. def key_is_writable (self, key):
  166. if self.supports_mandatory_settings ():
  167. return True
  168. return self.client.key_is_writable (key)
  169. def notify_add (self, key, handler, data = None):
  170. def __gconf_notify_proxy (client, cnx_id, entry, monitor):
  171. handler = monitor[0]
  172. user_data = monitor[1]
  173. handler (user_data)
  174. return self.client.notify_add (key, __gconf_notify_proxy,
  175. (handler, data))
  176. def notify_remove (self, monitor):
  177. self.client.notify_remove (monitor)
  178. def add_dir (self, dir, preloadtype):
  179. self.client.add_dir (dir, preloadtype)
  180. def remove_dir (self, dir):
  181. self.client.remove_dir (dir)