Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/extensions/cpsection/keyboard/model.py
blob: 16f6a01404303159a18a3de6076bc5222a87f663 (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
# Copyright (C) 2013 Sugar Labs
# Copyright (C) 2009 OLPC
# Author: Sayamindu Dasgupta <sayamindu@laptop.org>
#
# 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 St, Fifth Floor, Boston, MA  02110-1301  USA
#

from gi.repository import Xkl
from gi.repository import GConf
from gi.repository import SugarExt

import logging

_GROUP_NAME = 'grp'  # The XKB name for group switch options

_LAYOUTS_KEY = '/desktop/sugar/peripherals/keyboard/layouts'
_OPTIONS_KEY = '/desktop/sugar/peripherals/keyboard/options'
_MODEL_KEY = '/desktop/sugar/peripherals/keyboard/model'


def _item_str(s):
    '''Convert a zero-terminated byte array to a proper str'''

    i = s.find(b'\x00')
    return s[:i].decode("utf-8")


class KeyboardManager(object):
    def __init__(self, display):
        self._engine = Xkl.Engine.get_instance(display)
        self._configregistry = Xkl.ConfigRegistry.get_instance(self._engine)
        self._configregistry.load(False)
        self._configrec = Xkl.ConfigRec()
        self._configrec.get_from_server(self._engine)

        self._gconf_client = GConf.Client.get_default()

    def _populate_one(self, config_registry, item, store):
        store.append([_item_str(item.description), _item_str(item.name)])

    def _populate_two(self, config_registry, item, subitem, store):
        layout = _item_str(item.name)
        if subitem:
            description = '%s, %s' % (_item_str(subitem.description),
                                      _item_str(item.description))
            variant = _item_str(subitem.name)
        else:
            description = 'Default layout, %s' % _item_str(item.description)
            variant = ''
        store.append([description, ('%s(%s)' % (layout, variant))])

    def get_models(self):
        """Return list of supported keyboard models"""
        models = []
        self._configregistry.foreach_model(self._populate_one, models)
        models.sort()
        return models

    def get_languages(self):
        """Return list of supported keyboard languages"""
        languages = []
        self._configregistry.foreach_language(self._populate_one, languages)
        languages.sort()
        return languages

    def get_layouts_for_language(self, language):
        """Return list of supported keyboard layouts for a given language"""
        layouts = []
        self._configregistry.foreach_language_variant(language,
                                                      self._populate_two,
                                                      layouts)
        layouts.sort()
        return layouts

    def get_options_group(self):
        """Return list of supported options for switching keyboard group"""
        options = []
        self._configregistry.foreach_option(_GROUP_NAME, self._populate_one,
                                            options)
        options.sort()
        return options

    def get_current_model(self):
        """Return the enabled keyboard model"""
        model = self._gconf_client.get_string(_MODEL_KEY)
        if not model:
            model = self._configrec.model
            self.set_model(model)
        return model

    def get_current_layouts(self):
        """Return the enabled keyboard layouts with variants"""
        # FIXME, gconf_client_get_list not introspectable #681433
        layouts_from_gconf = self._gconf_client.get(_LAYOUTS_KEY)
        layouts = []
        if layouts_from_gconf:
            for gval in layouts_from_gconf.get_list():
                layout = gval.get_string()
                layouts.append(layout)
            if layouts:
                return layouts

        layouts = self._configrec.layouts
        variants = self._configrec.variants

        layout_list = []
        i = 0
        for layout in layouts:
            if len(variants) <= i or variants[i] == '':
                layout_list.append('%s(%s)' % (layout, ''))
            else:
                layout_list.append('%s(%s)' % (layout, variants[i]))
            i += 1

        self.set_layouts(layout_list)
        return layout_list

    def get_current_option_group(self):
        """Return the enabled option for switching keyboard group"""
        options = []
        # FIXME, gconf_client_get_list not introspectable #681433
        options_from_gconf = self._gconf_client.get(_OPTIONS_KEY)
        if options_from_gconf:
            for gval in options_from_gconf.get_list():
                option = gval.get_string()
                options.append(option)

        if not options:
            options = self._configrec.options
            self.set_option_group(options)

        for option in options:
            if option.startswith(_GROUP_NAME):
                return option

        return None

    def get_max_layouts(self):
        """Return the maximum number of layouts supported simultaneously"""
        return self._engine.get_max_num_groups()

    def set_model(self, model):
        """Sets the supplied keyboard model"""
        if model is None or not model:
            return
        self._gconf_client.set_string(_MODEL_KEY, model)
        self._configrec.set_model(model)
        self._configrec.activate(self._engine)

    def set_option_group(self, option_group):
        """Sets the supplied option for switching keyboard group"""
        #XXX: Merge, not overwrite previous options
        if not option_group:
            options = ['']
        elif isinstance(option_group, list):
            options = option_group
        else:
            options = [option_group]
        # FIXME, gconf_client_set_list not introspectable #681433
        # self._gconf_client.set_list(_OPTIONS_KEY, GConf.ValueType.STRING,
        #                             options)
        SugarExt.gconf_client_set_string_list(self._gconf_client,
                                              _OPTIONS_KEY, options)
        self._configrec.set_options(options)
        self._configrec.activate(self._engine)

    def set_layouts(self, layouts):
        """Sets the supplied keyboard layouts (with variants)"""
        if layouts is None or not layouts:
            return
        # FIXME, gconf_client_set_list not introspectable #681433
        # self._gconf_client.set_list(_LAYOUTS_KEY, GConf.ValueType.STRING,
        #                             layouts)
        SugarExt.gconf_client_set_string_list(self._gconf_client,
                                              _LAYOUTS_KEY, layouts)
        layouts_list = []
        variants_list = []
        for layout in layouts:
            layouts_list.append(layout.split('(')[0])
            variants_list.append(layout.split('(')[1][:-1])
        self._configrec.set_layouts(layouts_list)
        self._configrec.set_variants(variants_list)
        self._configrec.activate(self._engine)