summaryrefslogtreecommitdiff
path: root/caribou/common/settings.py
blob: 3f13b17f8865eebf160a0f7e4a483b511d678c47 (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
import os
from setting_types import *
from gettext import gettext as _
import caribou.common.const as const
import caribou.ui.i18n
import xml.dom.minidom

try:
    import json
except ImportError:
    HAS_JSON = False
else:
    HAS_JSON = True

def fetch_keyboards():
    try:
        files = os.listdir(const.KEYBOARDS_DIR)
    except:
        files = []
    kbds = []
    for f in files:
        if (HAS_JSON and f.endswith('.json')) or f.endswith('.xml'):
            module = f.rsplit('.', 1)[0]
            # TODO: verify keyboard before adding it to the list
            kbds.append(module)
    return kbds

settings = SettingsGroup("_top", "", [
        SettingsGroup("keyboard", _("Keyboard"), [
                SettingsGroup("general", _("General"), [
                        StringSetting(
                            "layout", _("Keyboard layout"), "qwerty",
                            _("The layout Caribou should use."),
                            _("The layout should be in the data directory of "
                              "Caribou (usually /usr/share/caribou/keyboards) "
                              "and should be a .xml or .json file."),
                            allowed=[(a,a) for a in fetch_keyboards()])]),
                SettingsGroup("color", _("Color"), [
                        BooleanSetting(
                            "default_colors", _("Use system theme"), True,
                            _("Use the default theme colors"),
                            insensitive_when_true=["normal_color",
                                                    "mouse_over_color"]),
                        ColorSetting(
                            "normal_color", _("Normal state"), "grey80",
                            _("Color of the keys when there is no "
                              "event on them")),
                        ColorSetting(
                            "mouse_over_color", _("Mouse over"), "yellow",
                            _("Color of the keys when the mouse goes "
                              "over the key"))]),
                SettingsGroup("fontandsize", _("Font and size"), [
                        BooleanSetting(
                            "default_font", _("Use system fonts"), True,
                            _("Use the default system font for keyboard"),
                            insensitive_when_true=["key_font"]),
                        FontSetting("key_font", _("Key font"), "Sans 12",
                                    _("Custom font for keyboard"))
                        ])
                ]),
        SettingsGroup("scanning", _("Scanning"), [
                BooleanSetting(
                    "scan_enabled", _("Enable scanning"), False,
                    _("Enable switch scanning"),
                    insensitive_when_false=["scanning_general",
                                            "scanning_input",
                                            "scanning_color"]),
                SettingsGroup("scanning_general", _("General"), [
                        StringSetting("scanning_type", _("Scanning mode"),
                                      "block",
                                      _("Scanning type, block or row"),
                                      allowed=[("block", _("Block")),
                                                ("row", _("Row"))]),
                        FloatSetting("step_time", _("Step time"), 1.0,
                                     _("Time between key transitions"),
                                     min=0.1, max=60.0),
                        BooleanSetting("reverse_scanning",
                                       _("Reverse scanning"), False,
                                       _("Scan in reverse order"))
                        ]),
                SettingsGroup("scanning_input", _("Input"), [
                        StringSetting("switch_type", _("Switch device"),
                                      "keyboard",
                                      _("Switch device, keyboard or mouse"),
                                      entry_type=ENTRY_RADIO,
                                      allowed=[("keyboard", _("Keyboard")),
                                               ("mouse", _("Mouse"))],
                                      children=[
                                StringSetting("keyboard_key", _("Switch key"),
                                              "Shift_R",
                                              _(
                                        "Key to use with scanning mode"),
                                              allowed=[
                                        ("Shift_R", _("Right shift")),
                                        ("Shift_L", _("Left shift")),
                                        ("ISO_Level3_Shift", _("Alt Gr")),
                                        ("Num_Lock", _("Num lock"))]),
                                StringSetting("mouse_button", _("Switch button"),
                                              "2",
                                              _(
                                        "Mouse button to use in the scanning "
                                        "mode"), 
                                              allowed=[("1", _("Button 1")),
                                                       ("2", _("Button 2")),
                                                       ("3", _("Button 3"))])
                                ]),
                        ]),
                SettingsGroup("scanning_color", _("Color"), [
                        ColorSetting("block_scanning_color", _("Block color"),
                                     "purple", _("Color of block scans")),
                        ColorSetting("row_scanning_color", _("Row color"),
                                     "green", _("Color of row scans")),
                        ColorSetting("button_scanning_color", _("Key color"),
                                      "cyan", _("Color of key scans")),
                        ColorSetting("cancel_scanning_color",
                                     _("Cancel color"),
                                     "red", _("Color of cancel scan"))
                        ])
                ])
        ])

if __name__ == "__main__":
    class SchemasMaker:
        def create_schemas(self):
            doc = xml.dom.minidom.Document()
            gconfschemafile =  doc.createElement('gconfschemafile')
            schemalist = doc.createElement('schemalist')
            gconfschemafile.appendChild(schemalist)
            self._create_schema(settings, doc, schemalist)

            self._pretty_xml(gconfschemafile)

        def _attribs(self, e):
            if not e.attributes.items():
                return ""
            return ' ' + ' '.join(['%s="%s"' % (k,v) \
                                       for k,v in e.attributes.items()])

        def _pretty_xml(self, e, indent=0):
            if not e.childNodes or \
                    (len(e.childNodes) == 1 and \
                         e.firstChild.nodeType == e.TEXT_NODE):
                print '%s%s' % (' '*indent*2, e.toxml().strip())
            else:
                print '%s<%s%s>' % (' '*indent*2, e.tagName, self._attribs(e))
                for c in e.childNodes:
                    self._pretty_xml(c, indent + 1)
                print '%s</%s>' % (' '*indent*2, e.tagName)

        def _append_children_element_value_pairs(self, doc, element, pairs):
            for e, t in pairs:
                el = doc.createElement(e)
                te = doc.createTextNode(str(t))
                el.appendChild(te)
                element.appendChild(el)

        def _create_schema(self, setting, doc, schemalist):
            if hasattr(setting, 'gconf_key'):
                schema = doc.createElement('schema')
                schemalist.appendChild(schema)
                self._append_children_element_value_pairs(
                    doc, schema, [('key', '/schemas' + setting.gconf_key),
                                  ('applyto', setting.gconf_key),
                                  ('owner', 'caribou'),
                                  ('type', setting.gconf_type.value_nick),
                                  ('default', setting.gconf_default)])
                locale = doc.createElement('locale')
                locale.setAttribute('name', 'C')
                schema.appendChild(locale)
                self._append_children_element_value_pairs(
                    doc, locale, [('short', setting.short_desc),
                                  ('long', setting.long_desc)])

            for s in setting:
                self._create_schema(s, doc, schemalist)

    maker = SchemasMaker()
    maker.create_schemas()