summaryrefslogtreecommitdiff
path: root/ibus/config.py
blob: 92936ae003b8999f1c99557502bdbafefa120d0b (plain)
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
# vim:set et sts=4 sw=4:
#
# ibus - The Input Bus
#
# Copyright (c) 2007-2010 Peng Huang <shawn.p.huang@gmail.com>
# Copyright (c) 2007-2010 Red Hat, Inc.
#
# 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.1 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 library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
# USA

__all__ = (
        "ConfigBase",
        "IBUS_SERVICE_CONFIG",
        "IBUS_PATH_CONFIG"
    )

IBUS_SERVICE_CONFIG = "org.freedesktop.IBus.Config"
IBUS_PATH_CONFIG = "/org/freedesktop/IBus/Config"

import gobject
import object
import interface
import dbus
from dbus.proxies import ProxyObject

class ConfigBase(object.Object):
    def __init__(self, bus):
        super(ConfigBase, self).__init__()
        self.__proxy = ConfigProxy(self, bus.get_dbusconn())

    def get_value(self, section, name):
        pass

    def get_values(self, section):
        pass

    def set_value(self, section, name, value):
        pass

    def unset_value(self, section, name):
        pass

    def value_changed(self, section, name, value):
        self.__proxy.ValueChanged(section, name, value)


class ConfigProxy(interface.IConfig):
    def __init__ (self, config, dbusconn):
        super(ConfigProxy, self).__init__(dbusconn, IBUS_PATH_CONFIG)
        self.__dbusconn = dbusconn
        self.__config = config

    def GetValue(self, section, name):
        return self.__config.get_value(section, name)

    def GetValues(self, section):
        return self.__config.get_values(section)

    def SetValue(self, section, name, value):
        return self.__config.set_value(section, name, value)

    def UnsetValue(self, section, name):
        return self.__config.unset_value(section, name)

    def Destroy(self):
        self.__config.destroy()

class Config(object.Object):
    __gtype_name__ = "PYIBusConfig"
    __gsignals__ = {
        "reloaded" : (
            gobject.SIGNAL_RUN_LAST,
            gobject.TYPE_NONE,
            ()
        ),
        "value-changed" : (
            gobject.SIGNAL_RUN_LAST,
            gobject.TYPE_NONE,
            (gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)
        ),
    }

    def __init__(self, bus):
        super(Config, self).__init__()
        self.__bus = bus
        self.__bus_name = None

        self.__bus.add_match("type='signal',\
                              sender='org.freedesktop.DBus',\
                              member='NameOwnerChanged',\
                              arg0='org.freedesktop.IBus.Config'")
        self.__bus.get_dbusconn().add_signal_receiver(self.__name_owner_changed_cb, signal_name="NameOwnerChanged")

        try:
            self.__init_config()
        except:
            self.__config = None

    def __name_owner_changed_cb(self, name, old_name, new_name):
        if name == "org.freedesktop.IBus.Config":
            if new_name == "":
                self.__config = None
            else:
                self.__init_config(new_name)

    def __init_config(self, bus_name=None):
        if bus_name == None:
            bus_name = self.__bus.get_name_owner(IBUS_SERVICE_CONFIG)

        match_rule = "type='signal',\
                      sender='%s',\
                      member='ValueChanged',\
                      path='/org/freedesktop/IBus/Config'"

        if self.__bus_name:
            self.__bus.remove_match(match_rule % self.__bus_name)
            self.__bus_name = None

        _config = self.__bus.get_dbusconn().get_object(bus_name, IBUS_PATH_CONFIG)
        self.__config = dbus.Interface(_config, dbus_interface="org.freedesktop.IBus.Config")
        self.__config.connect_to_signal("ValueChanged", self.__value_changed_cb)

        self.__bus_name = bus_name
        self.__bus.add_match(match_rule % self.__bus_name)
        self.emit("reloaded")

    def __value_changed_cb(self, section, name, value):
        self.emit("value-changed", section, name, value)

    def get_value(self, section, name, default_value):
        try:
            return self.__config.GetValue(section, name)
        except:
            return default_value

    def get_values(self, section):
        try:
            return self.__config.GetValues(section)
        except:
            return None

    def set_value(self, section, name, value):
        try:
            return self.__config.SetValue(section, name, value)
        except:
            return

    def set_list(self, section, name, value, signature):
        return self.set_value(section, name, dbus.Array(value, signature=signature))

    def unset_value(self, section, name):
        try:
            return self.__config.UnsetValue(section, name)
        except:
            return