ibus/gconf/config.py

145 строки
5.2 KiB
Python
Исходник Обычный вид История

2008-07-28 07:45:16 +04:00
# vim:set et sts=4 sw=4:
#
# ibus - The Input Bus
#
# Copyright(c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or(at your option) any later version.
#
# This library 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA 02111-1307 USA
2008-07-30 07:37:27 +04:00
__all__ = (
"Config",
)
import gobject
2008-07-05 05:17:58 +04:00
import gconf
import ibus
from ibus import interface
2008-07-29 15:19:31 +04:00
GCONF_IBUS_PATH = "/desktop/ibus"
2008-07-28 07:44:09 +04:00
class Config(ibus.Object):
2008-07-30 07:37:27 +04:00
__gsignals__ = {
"value-changed" : (
gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE,
(gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)),
}
2008-07-31 16:14:11 +04:00
def __init__ (self, bus = None, path = None):
2008-07-28 07:45:16 +04:00
super(Config, self).__init__()
2008-07-31 16:14:11 +04:00
conn = bus.get_dbusconn() if bus != None else None
2008-07-28 07:44:09 +04:00
self.__proxy = ConfigProxy(self, conn, path)
self.__client = gconf.Client()
2008-07-29 15:19:31 +04:00
self.__handler_id = self.__client.connect("value-changed", self.__value_changed_cb)
self.__client.add_dir(GCONF_IBUS_PATH, gconf.CLIENT_PRELOAD_NONE)
2008-07-05 05:17:58 +04:00
2008-07-30 06:37:25 +04:00
def get_value(self, key):
if not key.startswith("/"):
key = "/" + key
key = GCONF_IBUS_PATH + key
value = self.__client.get(key)
2008-07-31 12:12:01 +04:00
if value == None:
raise ibus.IBusException("key = \"%s\" does not exist" % key)
2008-07-30 06:37:25 +04:00
return self.__to_py_value(value)
2008-07-29 15:19:31 +04:00
2008-07-30 06:37:25 +04:00
def set_value(self, key, value):
if not key.startswith("/"):
key = "/" + key
key = GCONF_IBUS_PATH + key
value = self.__to_gconf_value(value)
self.__client.set(key, value)
2008-07-05 05:17:58 +04:00
2008-07-28 10:59:17 +04:00
def do_destroy(self):
2008-07-30 07:07:13 +04:00
if self.__proxy:
self.__proxy.Destriy()
self.__proxy = None
if self.__client:
self.__client.disconnect(self.__handler_id)
self.__client = None
2008-07-29 15:19:31 +04:00
def __to_py_value(self, value):
if value.type == gconf.VALUE_STRING:
return value.get_string()
if value.type == gconf.VALUE_INT:
return value.get_int()
if value.type == gconf.VALUE_FLOAT:
return value.get_float()
if value.type == gconf.VALUE_BOOL:
return value.get_bool()
if value.type == gconf.VALUE_PAIR:
return (self.__to_py_value(value.get_car()), self.__to_py_value(value.get_cdr()))
if value.type == gconf.VALUE_LIST:
return map(self.__to_py_value, value.get_list())
raise ibus.IBusException("Do not support type == %s" % str(value.type))
2008-07-29 15:19:31 +04:00
def __to_gconf_value(self, value):
2008-07-30 11:06:58 +04:00
if isinstance(value, str) or isinstance(value, unicode):
2008-07-29 15:19:31 +04:00
ret = gconf.Value(gconf.VALUE_STRING)
ret.set_string(value)
2008-07-30 11:57:03 +04:00
elif isinstance(value, bool):
ret = gconf.Value(gconf.VALUE_BOOL)
ret.set_bool(value)
2008-07-29 15:19:31 +04:00
elif isinstance(value, int):
ret = gconf.Value(gconf.VALUE_INT)
ret.set_int(value)
elif isinstance(value, float):
ret = gconf.Value(gconf.VALUE_FLOAT)
ret.set_float(value)
elif isinstance(value, tuple):
if len(value) != 2:
raise ibus.IBusException("Pair must have two value")
ret = gconf.Value(gconf.VALUE_PAIR)
ret.set_car(self.__to_gconf_value(value[0]))
2008-07-30 09:16:44 +04:00
ret.set_cdr(self.__to_gconf_value(value[1]))
2008-07-29 15:19:31 +04:00
elif isinstance(value, list):
ret = gconf.Value(gconf.VALUE_LIST)
if len(value) > 0:
value = map(self.__to_gconf_value, value)
_type = value[0].type
if any(map(lambda x: x.type != _type, value)):
raise ibus.IBusException("Items of a list must be in same type")
ret.set_list_type(_type)
ret.set_list(value)
else:
raise ibus.IBusException("Do not support type of %s." % type(value))
2008-07-29 15:19:31 +04:00
return ret
2008-07-28 10:59:17 +04:00
2008-07-28 07:44:09 +04:00
def __value_changed_cb(self, gconf, key, value):
2008-07-29 15:19:31 +04:00
value = self.__client.get(key)
value = self.__to_py_value(value)
2008-08-01 18:37:19 +04:00
if value == None:
value = 0
2008-07-31 12:12:01 +04:00
self.emit("value-changed", key.replace(GCONF_IBUS_PATH, ""), value)
2008-07-30 07:37:27 +04:00
gobject.type_register(Config)
2008-07-05 05:17:58 +04:00
2008-07-28 07:44:09 +04:00
class ConfigProxy(interface.IConfig):
def __init__ (self, config, conn, object_path):
2008-07-28 07:45:16 +04:00
super(ConfigProxy, self).__init__(conn, object_path)
2008-07-28 07:44:09 +04:00
self.__config = config
2008-07-30 07:37:27 +04:00
self.__config.connect("value-changed", lambda c, k, v: self.ValueChanged(k, v))
2008-07-05 05:17:58 +04:00
2008-07-30 06:37:25 +04:00
def GetValue(self, key):
return self.__config.get_value(key)
def SetValue(self, key, value):
self.__config.set_value(key, value)
2008-07-29 15:19:31 +04:00
2008-07-28 10:59:17 +04:00
def Destroy(self):
self.remove_from_connection()
2008-07-30 07:07:13 +04:00
if self.__config:
self.__config.destroy()
self.__config = None