Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/extensions/cpsection/keyboard/model.py
blob: 9bc90ebf2d2ad9e219eedcb37bdd291a746f8c80 (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
# Copyright (C) 2007, 2008 One Laptop Per Child
#
# 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
#

import xklavier
import gobject
import gconf


GRP_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'

class XKB(gobject.GObject):
    def __init__(self, display):
        gobject.GObject.__init__(self)
        
        self._engine = xklavier.Engine(display)
        self._configreg = xklavier.ConfigRegistry(self._engine)
        self._configreg.load(False)
        self._configrec = xklavier.ConfigRec()
        self._configrec.get_from_server(self._engine)
	
        self._gconf_client = gconf.client_get_default()

    def _populate_one(self, c_reg, item, store):
        store.append([item.get_description(), item.get_name()])

    def _populate_two(self, c_reg, item, subitem, store):
        layout = item.get_name()
        if subitem:
            desc = '%s, %s' % (subitem.get_description(), item.get_description())
            variant = subitem.get_name()
        else:
            desc = 'Default layout, %s' % item.get_description()
            variant = ''
        
        store.append([desc, ('%s(%s)' % (layout, variant))])

    def get_models(self):
        models = []
        self._configreg.foreach_model(self._populate_one, models)
        models.sort()
        return models

    def get_languages(self):
        languages = []
        self._configreg.foreach_language(self._populate_one, languages)
        languages.sort()
        return languages

    def get_layouts_for_language(self, language):
        layouts = []
        self._configreg.foreach_language_variant(language, self._populate_two, layouts)
        layouts.sort()
        return layouts

    def get_options_grp(self):
        options = []
        self._configreg.foreach_option(GRP_NAME, self._populate_one, options)
        options.sort()
        return options

    def get_current_model(self):
        model = self._gconf_client.get_string(MODEL_KEY)
        if model:
            return model
        else:
            return self._configrec.get_model()

    def get_current_layouts(self):
        layouts = self._gconf_client.get_list(LAYOUTS_KEY, 'string')
        if layouts:
            return layouts
        
        layouts = self._configrec.get_layouts()
        variants = self._configrec.get_variants()

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

        return ret

    def get_current_option_grp(self):
        options = self._gconf_client.get_list(OPTIONS_KEY, 'string')
        
        if not options:
            options = self._configrec.get_options()

        for option in options:
            if option.startswith(GRP_NAME):
                return option
        
        return None
    
    def get_max_layouts(self):
        return self._engine.get_max_num_groups()

    def set_model(self, model):
        #XXX: Which one goes first ?
        self._gconf_client.set_string(MODEL_KEY, model)
        self._configrec.set_model(model)
        self._configrec.activate(self._engine)

    def set_option_grp(self, option_grp):
        #XXX: Take a backup of existing settings first (there may be other hand set values)
        options = [option_grp]
        self._gconf_client.set_list(OPTIONS_KEY, gconf.VALUE_STRING, options)
        self._configrec.set_options(options)
        self._configrec.activate(self._engine)

    def set_layouts(self, layouts):
        self._gconf_client.set_list(LAYOUTS_KEY, gconf.VALUE_STRING, 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)