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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
# vim:set noet ts=4:
#
# ibus-anthy - The Anthy engine for IBus
#
# Copyright (c) 2007-2008 Peng Huang <shawn.p.huang@gmail.com>
# Copyright (c) 2009 Hideaki ABE <abe.sendai@gmail.com>
# Copyright (c) 2010-2017 Takao Fujiwara <takao.fujiwara1@gmail.com>
# Copyright (c) 2007-2017 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import sys
from gi import require_version as gi_require_version
gi_require_version('Gio', '2.0')
gi_require_version('GLib', '2.0')
gi_require_version('IBus', '1.0')
from gi.repository import Gio
from gi.repository import GLib
from gi.repository import GObject
from gi.repository import IBus
class DictItem():
def __init__(self,
id='',
short_label='',
long_label='',
icon='',
is_system=False,
preview_lines=-1,
embed=False,
single=True,
reverse=False,
encoding='utf-8'):
self.id = id
self.short_label = short_label
self.long_label = long_label
self.icon = icon
self.is_system = is_system
self.preview_lines = preview_lines
self.embed = embed
self.single = single
self.reverse = reverse
self.encoding = encoding
def __str__(self):
retval = ('id:', self.id,
'short-label:', self.short_label,
'long-label:', self.long_label,
'icon:', self.icon,
'is-system:', self.is_system,
'preview-lines:', self.preview_lines,
'embed:', self.embed,
'single:', self.single,
'reverse:', self.reverse,
'encoding:', self.encoding)
return str(retval)
@classmethod
def serialize(cls, dict_item):
builder = GLib.VariantBuilder(GLib.VariantType('r'))
builder.add_value(GLib.Variant.new_string(dict_item.id))
builder.add_value(GLib.Variant.new_string(dict_item.short_label))
builder.add_value(GLib.Variant.new_string(dict_item.long_label))
builder.add_value(GLib.Variant.new_string(dict_item.icon))
builder.add_value(GLib.Variant.new_boolean(dict_item.is_system))
builder.add_value(GLib.Variant.new_int32(dict_item.preview_lines))
builder.add_value(GLib.Variant.new_boolean(dict_item.embed))
builder.add_value(GLib.Variant.new_boolean(dict_item.single))
builder.add_value(GLib.Variant.new_boolean(dict_item.reverse))
builder.add_value(GLib.Variant.new_string(dict_item.encoding))
return builder.end()
class Prefs(GObject.GObject):
__gsignals__ = {
'changed' : (
GObject.SignalFlags.RUN_FIRST,
None,
(str, str, GLib.Variant)),
}
def __init__(self):
super(Prefs, self).__init__()
self.__cache = {}
self.__settings = {}
self.__schema_prefix = 'org.freedesktop.ibus.engine.anthy.'
self.__schema_sections = ['common',
'shortcut',
'romaji-typing-rule',
'kana-typing-rule',
'thumb-typing-rule',
'thumb',
'dict']
for section in self.__schema_sections:
self.__settings[section] = Gio.Settings(
schema=self.__schema_prefix + section)
self.__settings[section].connect('changed',
self.__settings_on_changed)
def __settings_on_changed(self, settings, key):
section = settings.props.schema[len(self.__schema_prefix):]
variant_value = self.__settings[section].get_value(key)
variant_key = self.__cache.get(section)
if variant_key == None:
variant_key = {}
variant_key[key] = variant_value
self.__cache[section] = variant_key
self.emit('changed', section, key, variant_value)
def variant_to_value(self, variant):
if type(variant) != GLib.Variant:
return variant
type_string = variant.get_type_string()
if type_string == 's':
return variant.get_string()
elif type_string == 'i':
return variant.get_int32()
elif type_string == 'b':
return variant.get_boolean()
elif type_string == 'v':
return variant.unpack()
elif len(type_string) > 0 and type_string[0] == 'a':
# Use unpack() instead of dup_strv() in python.
# In the latest pygobject3 3.3.4 or later, g_variant_dup_strv
# returns the allocated strv but in the previous release,
# it returned the tuple of (strv, length)
return variant.unpack()
else:
self.printerr('Unknown variant type: %s' % type_string)
sys.abrt()
return variant
def variant_from_value(self, value):
variant = None
if type(value) == str:
variant = GLib.Variant.new_string(value)
elif type(value) == int:
variant = GLib.Variant.new_int32(value)
elif type(value) == bool:
variant = GLib.Variant.new_boolean(value)
elif type(value) == list:
variant = GLib.Variant.new_strv(value)
if variant == None:
self.printerr('Unknown value type: %s' % type(value))
return variant
def get_variant(self, section, key):
variant_key = self.__cache.get(section)
if variant_key != None:
variant_value = variant_key.get(key)
if variant_value != None:
return variant_value
variant_value = self.__settings[section].get_value(key)
if variant_key == None:
variant_key = {}
variant_key[key] = variant_value
self.__cache[section] = variant_key
return variant_value
def get_default_variant(self, section, key):
return self.__settings[section].get_default_value(key)
def get_readable_value(self, section, key, variant):
value = self.variant_to_value(variant)
if section == 'dict' and key == 'list':
dicts = {}
for item in value:
dict_item = DictItem(*item)
dicts[dict_item.id] = dict_item
value = dicts
if section == 'dict' and key == 'template':
value = DictItem(*value)
return value
def get_value(self, section, key):
variant = self.get_variant(section, key)
return self.get_readable_value(section, key, variant)
def get_default_value(self, section, key):
variant = self.get_default_variant(section, key)
return self.get_readable_value(section, key, variant)
def set_variant(self, section, key, variant):
self.__settings[section].set_value(key, variant)
self.__settings[section].apply()
def set_value(self, section, key, value):
variant = self.variant_from_value(value)
if variant == None:
return
self.set_variant(section, key, variant)
def set_list_item(self, section, key, item, values):
variant = self.get_variant(section, key)
if variant == None:
printerrr('%s:%s does not exist' % (section, key))
return
if section == 'shortcut':
variant_dict = GLib.VariantDict(variant)
array = []
for value in values:
array.append(GLib.Variant.new_string(value))
varray = GLib.Variant.new_array(GLib.VariantType('s'), array)
variant_dict.insert_value(item, varray)
# GVariantDict uses GHashTable internally and
# GVariantDict.end() does not support the order.
self.set_variant(section, key, variant_dict.end())
return
if section == 'romaji-typing-rule' or \
section == 'kana-typing-rule' or \
section == 'thumb-typing-rule':
(method, keymap_key) = item
variant_dict = GLib.VariantDict(variant)
keymap = variant_dict.lookup_value(method, None)
keymap_dict = GLib.VariantDict(keymap)
if section == 'thumb-typing-rule':
array = []
for value in values:
array.append(GLib.Variant.new_string(value))
vvalue = GLib.Variant.new_array(GLib.VariantType('s'), array)
else:
vvalue = GLib.Variant.new_string(values)
keymap_dict.insert_value(keymap_key, vvalue)
keymap = keymap_dict.end()
variant_dict.insert_value(method, keymap)
self.set_variant(section, key, variant_dict.end())
return
if section == 'dict' and key == 'files':
variant_dict = GLib.VariantDict(variant)
array = []
for value in values:
array.append(GLib.Variant.new_string(value))
varray = GLib.Variant.new_array(GLib.VariantType('s'), array)
variant_dict.insert_value(item, varray)
self.set_variant(section, key, variant_dict.end())
return
if section == 'dict' and key == 'list':
array = []
has_item = False
for v in variant:
dict_item = DictItem(*v)
if dict_item.id == values.id:
array.append(GLib.Variant.new_variant(
DictItem.serialize(values)))
has_item = True
else:
array.append(GLib.Variant.new_variant(
DictItem.serialize(dict_item)))
if not has_item:
array.append(GLib.Variant.new_variant(DictItem.serialize(values)))
varray = GLib.Variant.new_array(GLib.VariantType('v'), array)
self.set_variant(section, key, varray)
return
def delete_list_item(self, section, key, item):
variant = self.get_variant(section, key)
if variant == None:
printerrr('%s:%s does not exist' % (section, key))
return
if section == 'romaji-typing-rule' or \
section == 'kana-typing-rule' or \
section == 'thumb-typing-rule':
(method, keymap_key) = item
variant_dict = GLib.VariantDict(variant)
keymap = variant_dict.lookup_value(method, None)
keymap_dict = GLib.VariantDict(keymap)
keymap_dict.remove(keymap_key)
keymap = keymap_dict.end()
variant_dict.insert_value(method, keymap)
self.set_variant(section, key, variant_dict.end())
return
if section == 'dict' and key == 'files':
variant_dict = GLib.VariantDict(variant)
variant_dict.remove(item)
self.set_variant(section, key, variant_dict.end())
return
if section == 'dict' and key == 'list':
array = []
for v in variant:
dict_item = DictItem(*v)
if dict_item.id == item:
continue
else:
array.append(GLib.Variant.new_variant(
DictItem.serialize(dict_item)))
varray = GLib.Variant.new_array(GLib.VariantType('v'), array)
self.set_variant(section, key, varray)
return
def bind(self, section, key, object, property, flags):
self.__settings[section].bind(key, object, property, flags)
# Convert DBus.String to str
# sys.getdefaultencoding() == 'utf-8' with pygtk2 but
# sys.getdefaultencoding() == 'ascii' with gi gtk3
# so the simple str(unicode_string) causes an error and need to use
# unicode_string.encode('utf-8') instead.
def str(self, uni):
if uni == None:
return None
if type(uni) == str:
return uni
return str(uni)
# The simple unicode(string) causes an error and need to use
# unicode(string, 'utf-8') instead.
def unicode(self, string):
if string == None:
return None
return string
# If the parent process exited, the std io/out/error will be lost.
@staticmethod
def printerr(sentence):
try:
print(sentence, file=sys.stderr)
except IOError:
pass
|