summaryrefslogtreecommitdiffstats
path: root/ibus/property.py
blob: 407b07928ebb11bfbe642ede6f58ae3dc5d6ef4b (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# vim:set et sts=4 sw=4:
#
# ibus - The Input Bus
#
# Copyright (c) 2007-2009 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__ = (
        "PROP_TYPE_NORMAL",
        "PROP_TYPE_TOGGLE",
        "PROP_TYPE_RADIO",
        "PROP_TYPE_SEPARATOR",
        "PROP_TYPE_MENU",
        "PROP_STATE_UNCHECKED",
        "PROP_STATE_CHECKED",
        "PROP_STATE_INCONSISTENT",
        "Property",
        "PropList",
    )

import dbus
from text import Text
from serializable import *

PROP_TYPE_NORMAL = 0
PROP_TYPE_TOGGLE = 1
PROP_TYPE_RADIO = 2
PROP_TYPE_MENU = 3
PROP_TYPE_SEPARATOR = 4

PROP_STATE_UNCHECKED = 0
PROP_STATE_CHECKED = 1
PROP_STATE_INCONSISTENT = 2

def _to_unicode(text):
    if isinstance(text, unicode):
        return text
    if isinstance(text, str):
        return unicode(text, "utf8")
    raise TypeError("text must be instance of unicode or str")
def _to_text(text):
    if isinstance(text, Text):
        return text
    text = _to_unicode(text)
    return Text(text)

class Property(Serializable):
    __NAME__ = "IBusProperty"
    def __init__(self, key="", type=PROP_TYPE_NORMAL, label=u"", icon=u"", tooltip=u"",
                 sensitive=True, visible=True, state=PROP_STATE_UNCHECKED):
        super(Property, self).__init__()
        self.__key = _to_unicode(key)
        self.__type = type
        self.label = label
        self.icon = icon
        self.tooltip = tooltip
        self.sensitive = sensitive
        self.visible = visible
        self.state = state
        self.sub_props = PropList()

    def set_sub_props(self, props):
        self.__sub_props = props

    def get_sub_props(self):
        return self.__sub_props

    def get_key(self):
        return self.__key

    def get_type(self):
        return self.__type

    def set_label(self, label):
        self.__label = _to_text(label)

    def get_label(self):
        return self.__label

    def set_icon(self, icon):
        self.__icon = _to_unicode(icon)

    def get_icon(self):
        return self.__icon

    def set_tooltip(self, tooltip):
        self.__tooltip = _to_text(tooltip)

    def get_tooltip(self):
        return self.__tooltip

    def set_state(self, state):
        self.__state = state

    def get_state(self):
        return self.__state

    def set_sensitive(self, sensitive):
        self.__sensitive = sensitive

    def get_sensitive(self):
        return self.__sensitive

    def set_visible(self, visible):
        self.__visible = visible

    def get_visible(self):
        return self.__visible

    key         = property(get_key)
    type        = property(get_type)
    label       = property(get_label, set_label)
    icon        = property(get_icon, set_icon)
    tooltip     = property(get_tooltip, set_tooltip)
    state       = property(get_state, set_state)
    sensitive   = property(get_sensitive, set_sensitive)
    visible     = property(get_visible, set_visible)
    sub_props   = property(get_sub_props, set_sub_props)

    def is_same(self, prop, test_all = True):
        if self.__name != prop.__name or self.__type != prop.__type:
            return False
        if not test_all:
            return True
        if self.__label != prop.__label or \
            self.__icon != prop.__icon or \
            self.__tooltip != prop.__tooltip or \
            self.__sensitive != prop.__sensitive or \
            self.__visible != prop.__visible or \
            self.__state != prop.__state:
            return False
        return self.__sub_props.is_same(prop.__sub_props, test_all)


    def serialize(self, struct):
        super(Property, self).serialize(struct)
        struct.append(dbus.String(self.__key))
        struct.append(dbus.UInt32(self.__type))
        struct.append(serialize_object(self.__label))
        struct.append(dbus.String(self.__icon))
        struct.append(serialize_object(self.__tooltip))
        struct.append(dbus.Boolean(self.__sensitive))
        struct.append(dbus.Boolean(self.__visible))
        struct.append(dbus.UInt32(self.__state))
        sub_props = serialize_object(self.__sub_props)
        struct.append(sub_props)

    def deserialize(self, struct):
        super(Property, self).deserialize(struct)
        self.__key = struct.pop(0)
        self.__type = struct.pop(0)
        self.__label = deserialize_object(struct.pop(0))
        self.__icon = struct.pop(0)
        self.__tooltip = deserialize_object(struct.pop(0))
        self.__sensitive = deserialize_object(struct.pop(0))
        self.__visible = struct.pop(0)
        self.__state = struct.pop(0)
        props = struct.pop(0)

        self.__sub_props = deserialize_object(props)

serializable_register(Property)

class PropList(Serializable):
    __NAME__ = "IBusPropList"
    def __init__(self):
        super(PropList, self).__init__()
        self.__props = []

    def append(self, prop):
        self.__props.append(prop)

    def prepand(self, prop):
        self.__props.insert(0, prop)

    def insert(self, index, prop):
        self.__props.insert(index, prop)

    def get_properties(self):
        return self.__props[:]

    def is_same(self, props, test_all = True):
        if len(props.get_properties()) != len(self.get_properties()):
            return False

        for a, b in zip(self.get_properties(), props.get_properties()):
            if not a.is_same(b, test_all):
                return False
        return False

    def serialize(self, struct):
        super(PropList, self).serialize(struct)
        props = map(lambda p: serialize_object(p), self.__props)
        struct.append (dbus.Array(props, signature = "v"))

    def deserialize(self, struct):
        super(PropList, self).deserialize(struct)
        props = map(lambda v: deserialize_object(v), struct.pop(0))
        self.__props = props

    def __iter__(self):
        return self.__props.__iter__()

    def __getitem__(self, i):
        return self.__props.__getitem__(i)

serializable_register(PropList)

def test():
    props = PropList()
    props.append(Property(u"a"))
    props.append(Property(u"b"))
    props.append(Property(u"c"))
    props.append(Property(u"d"))
    value = serialize_object(props)
    props = deserialize_object(value)
    print props

if __name__ == "__main__":
    test()