summaryrefslogtreecommitdiff
path: root/setup/prefs.py
blob: 1b88908288dd82317489d4dc090d3e8d1b2e4673 (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
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
# 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-2013 Takao Fujiwara <takao.fujiwara1@gmail.com>
# Copyright (c) 2007-2013 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.repository import GLib
from gi.repository import IBus

class Prefs(object):
    _prefix = 'engine/dummy'

    def __init__(self, bus=None, config=None):
        self.default = {}
        self.modified = {}
        self.new = {}
        self.__no_key_warning = False

        # self._config is used by AnthyPrefs .
        self._config = config if config else \
                       bus.get_config() if bus else  \
                       IBus.Bus().get_config()

        # ibus_config_get_values enhances the performance.
        self.__has_config_get_values = False

        if self._config != None:
            self.__has_config_get_values = hasattr(self._config, 'get_values')
        else:
            self.printerr(
                'ibus-config is not running or bus address is not correct.')

    def __log_handler(self, domain, level, message, data):
        if not data:
            return
        GLib.log_default_handler(domain, level, message, '')

    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 == 'as':
            # 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:', type_string)
            sys.abrt()
        return variant

    def set_no_key_warning(self, no_key_warning):
        if no_key_warning and hasattr(IBus, 'unset_log_handler'):
            self.__no_key_warning = True
        else:
            self.__no_key_warning = False

    def keys(self, section):
        return self.default[section].keys()

    def sections(self):
        return self.default.keys()

    def set_new_section(self, section):
        self.default.setdefault(section, {})

    def set_new_key(self, section, key):
        self.default[section].setdefault(key)

    def get_value(self, section, key):
        try:
            return self.new[section][key]
        except:
            try:
                return self.modified[section][key]
            except:
                return self.default[section][key]

    def get_value_direct(self, section, key, default=None):
        if self._config == None:
            return default

        s = section
        section = '/'.join(
            [s for s in '/'.join([self._prefix, section]).split('/') if s])
        try:
            if self.__no_key_warning:
                IBus.set_log_handler(False)
            variant = self._config.get_value(section, key)
            if self.__no_key_warning:
                IBus.unset_log_handler()
            return self.variant_to_value(variant)
        except:
            return default

    def set_value(self, section, key, value):
        if section not in self.sections():
            self.set_new_section(section)
        if key not in self.keys(section):
            self.set_new_key(section, key)
        self.default[section][key]
        self.new.setdefault(section, {})[key] = value

    def fetch_all(self):
        for s in self.sections():
            self.fetch_section(s)

    def fetch_section(self, section):
        if self._config == None:
            return

        if not self.__has_config_get_values:
            for k in self.keys(section):
                self.fetch_item(section, k)
            return

        s = '/'.join(
            [s for s in '/'.join([self._prefix, section]).split('/') if s])
        variant = self._config.get_values(s)
        for key in variant.keys():
            v = variant[key]
            self.modified.setdefault(section, {})[key] = v if v != [''] else []
        # FIXME: ibus-dconf converts the keys.
        if section == 'common':
            self.fetch_item(section, 'show-input-mode')
            self.fetch_item(section, 'show-typing-method')
            self.fetch_item(section, 'show-segment-mode')
            self.fetch_item(section, 'show-dict-mode')
            self.fetch_item(section, 'show-dict-config')
            self.fetch_item(section, 'show-preferences')

    def fetch_item(self, section, key, readonly=False):
        if self._config == None:
            return

        s = '/'.join(
            [s for s in '/'.join([self._prefix, section]).split('/') if s])
        try:
            v = None
            # gobject-introspection has a bug.
            # https://bugzilla.gnome.org/show_bug.cgi?id=670509
            # GLib.log_set_handler("IBUS", GLib.LogLevelFlags.LEVEL_MASK,
            #                      self.__log_handler, False)
            if self.__no_key_warning:
                IBus.set_log_handler(False)
            variant = self._config.get_value(s, key)
            if self.__no_key_warning:
                IBus.unset_log_handler()
            v = self.variant_to_value(variant)
        except:
            v = None
        if readonly:
            return v != None
        if v != None:
            self.modified.setdefault(section, {})[key] = v if v != [''] else []
        return True

    def commit_all(self):
        for s in self.new.keys():
            self.commit_section(s)

    def commit_section(self, section):
        if section in self.new:
            for k in self.new[section].keys():
                self.commit_item(section, k)

    def commit_item(self, section, key):
        if section in self.new and key in self.new[section]:
            s = '/'.join(
                [s for s in '/'.join([self._prefix, section]).split('/') if s])
            v = self.new[section][key]
            if v == []:
                v = ['']
            variant = None
            if type(v) == str:
                variant = GLib.Variant.new_string(v)
            elif type(v) == int:
                variant = GLib.Variant.new_int32(v)
            elif type(v) == bool:
                variant = GLib.Variant.new_boolean(v)
            elif type(v) == list:
                variant = GLib.Variant.new_strv(v)
            if variant == None:
                self.printerr('Unknown value type:', type(v))
                sys.abrt()
            if self._config != None:
                self._config.set_value(s, key, variant)
            self.modified.setdefault(section, {})[key] = v
            del(self.new[section][key])

    def undo_all(self):
        self.new.clear()

    def undo_section(self, section):
        try:
            del(self.new[section])
        except:
            pass

    def undo_item(self, section, key):
        try:
            del(self.new[section][key])
        except:
            pass

    def set_default_all(self):
        for s in self.sections():
            self.set_default_section(s)

    def set_default_section(self, section):
        for k in self.keys(section):
            self.set_default_item(section, k)

    def set_default_item(self, section, key):
        try:
            if key in self.modified[section] or key in self.new[section]:
                self.new[section][key] = self.default[section][key]
        except:
            pass

    # 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
        if type(uni) == unicode:
            return uni.encode('utf-8')
        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
        if type(string) == unicode:
            return string
        return unicode(string, 'utf-8')

    # If the parent process exited, the std io/out/error will be lost.
    @staticmethod
    def printerr(sentence):
        try:
            print >> sys.stderr, sentence
        except IOError:
            pass