summaryrefslogtreecommitdiffstats
path: root/daemon/config.py
blob: 2264e57234dbef192c5e9600898fc2c76ff1c49a (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
# 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

__all__ = (
    "Config",
    "DefaultConfig"
)

import weakref
import gobject
import ibus
import defaultconfig

class Config(ibus.Object):
    __gsignals__ = {
        "value-changed" : (
            gobject.SIGNAL_RUN_FIRST,
            gobject.TYPE_NONE,
            (gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)),
    }

    def __init__(self, ibusconn, object_path):
        super(Config, self).__init__()
        self.__ibusconn = ibusconn
        self.__object_path = object_path
        self.__config = self.__ibusconn.get_object(self.__object_path)

        self.__ibusconn.connect("destroy", self.__ibusconn_destroy_cb)
        self.__ibusconn.connect("dbus-signal", self.__dbus_signal_cb)

    def get_value(self, key, **kargs):
        return self.__config.GetValue(key, **kargs)

    def set_value(self, key, value, **kargs):
        return self.__config.GetValue(key, value, **kargs)

    def destroy(self):
        if self.__ibusconn != None:
            self.__config.Destroy(**ibus.DEFAULT_ASYNC_HANDLERS)

        self.__ibusconn = None
        self.__config = None
        ibus.Object.destroy(self)

    # signal callbacks
    def __ibusconn_destroy_cb(self, ibusconn):
        self.__ibusconn = None
        self.destroy()

    def __dbus_signal_cb(self, ibusconn, message):
        if message.is_signal(ibus.IBUS_CONFIG_IFACE, "ValueChanged"):
            args = message.get_args_list()
            self.emit("value-changed", args[0], args[1])
        else:
            return False
        return True

gobject.type_register(Config)

class DefaultConfig(ibus.Object):
    __gsignals__ = {
        "value-changed" : (
            gobject.SIGNAL_RUN_FIRST,
            gobject.TYPE_NONE,
            (gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)),
    }

    def __init__(self):
        super(DefaultConfig, self).__init__()
        self.__config = defaultconfig.Config()
        self.__handler_id = self.__config.connect("value-changed", self.__value_changed_cb)

    def get_value(self, key, **kargs):
        reply_handler = kargs.get("reply_handler", None)
        error_handler = kargs.get("error_handler", None)
        try:
            value = self.__config.get_value(key)
            if reply_handler:
                reply_handler(value)
            else:
                return value
        except Exception, e:
            if error_handler:
                error_handler(e)
            else:
                raise e

    def set_value(self, key, value, **kargs):
        reply_handler = kargs.get("reply_handler", None)
        error_handler = kargs.get("error_handler", None)
        try:
            self.__config.set_value(key, value)
            if reply_handler:
                reply_handler()
            else:
                return
        except Exception, e:
            if error_handler:
                error_handler(e)
            else:
                raise e

    def __value_changed_cb(self, config, key, value):
        self.emit("value-changed", key, value)

    def do_destroy(self):
        if self.__config:
            self.__config.disconnect(self.__handler_id)
            self.__config.destroy()
            self.__config = None

gobject.type_register(DefaultConfig)