summaryrefslogtreecommitdiffstats
path: root/pyanaconda/ui/gui/spokes/keyboard.py
blob: 796e09a7192f9341ac3c0494aeae108c8ba8c731 (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
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
# Keyboard selection and configuration spoke class
#
# Copyright (C) 2011-2012  Red Hat, Inc.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# the GNU General Public License v.2, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY expressed or implied, including the implied warranties 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.  Any Red Hat trademarks that are incorporated in the
# source code or documentation are not subject to the GNU General Public
# License and may only be used or replicated with the express permission of
# Red Hat, Inc.
#
# Red Hat Author(s): Chris Lumens <clumens@redhat.com>
#                    Vratislav Podzimek <vpodzime@redhat.com>
#

import gettext
_ = lambda x: gettext.ldgettext("anaconda", x)
N_ = lambda x: x

from gi.repository import GLib, AnacondaWidgets, Gkbd

from pyanaconda.ui.gui import UIObject
from pyanaconda.ui.gui.spokes import NormalSpoke
from pyanaconda.ui.gui.categories.localization import LocalizationCategory
from pyanaconda.ui.gui.utils import enlightbox
from pyanaconda import keyboard

__all__ = ["KeyboardSpoke"]

def _show_layout(column, renderer, model, itr, wrapper):
    value = wrapper.name_to_show_str[model[itr][0]]
    renderer.set_property("text", value)

class AddLayoutDialog(UIObject):
    builderObjects = ["addLayoutDialog", "newLayoutStore", "newLayoutStoreFilter"]
    mainWidgetName = "addLayoutDialog"
    uiFile = "spokes/keyboard.ui"

    def __init__(self, *args):
        UIObject.__init__(self, *args)
        self._xkl_wrapper = keyboard.XklWrapper.get_instance()

    def matches_entry(self, model, itr, user_data=None):
        value = model[itr][0]
        value = self._xkl_wrapper.name_to_show_str[value]
        entry_text = self._entry.get_text()
        if entry_text is not None:
            entry_text = entry_text.lower()
            entry_text_words = entry_text.split()
        else:
            return False
        try:
            if value:
                value = value.lower()
                for word in entry_text_words:
                    value.index(word)
                return True
            return False
        except ValueError as valerr:
            return False

    def refresh(self):
        self._treeModelFilter = self.builder.get_object("newLayoutStoreFilter")
        self._treeModelFilter.set_visible_func(self.matches_entry, None)
        self._entry = self.builder.get_object("addLayoutEntry")
        self._entry.grab_focus()

    def initialize(self):
        # We want to store layouts' names but show layouts as
        # 'language (description)'.
        layoutColumn = self.builder.get_object("newLayoutColumn")
        layoutRenderer = self.builder.get_object("newLayoutRenderer")
        layoutColumn.set_cell_data_func(layoutRenderer, _show_layout,
                                            self._xkl_wrapper)

        self._store = self.builder.get_object("newLayoutStore")
        for layout in self._xkl_wrapper.get_available_layouts():
            self._addLayout(self._store, layout)

    def run(self):
        rc = self.window.run()
        self.window.destroy()
        return rc

    @property
    def chosen_layouts(self):
        return self._chosen_layouts

    def on_confirm_add_clicked(self, *args):
        treeview = self.builder.get_object("newLayoutView")
        selection = treeview.get_selection()
        (store, pathlist) = selection.get_selected_rows()
        self._chosen_layouts = []
        for path in pathlist:
            itr = store.get_iter(path)
            self._chosen_layouts.append(store[itr][0])

    def on_cancel_clicked(self, *args):
        print "CANCELING"

    def on_entry_changed(self, *args):
        self._treeModelFilter.refilter()

    def on_entry_icon_clicked(self, *args):
        self._entry.set_text("")

    def _addLayout(self, store, name):
        store.append([name])

class KeyboardSpoke(NormalSpoke):
    builderObjects = ["addedLayoutStore", "keyboardWindow",
                      "addImage", "removeImage", "upImage", "downImage", "previewImage"]
    mainWidgetName = "keyboardWindow"
    uiFile = "spokes/keyboard.ui"

    category = LocalizationCategory

    icon = "input-keyboard-symbolic"
    title = N_("KEYBOARD")

    def __init__(self, *args):
        NormalSpoke.__init__(self, *args)
        self._remove_last_attempt = False
        self._xkl_wrapper = keyboard.XklWrapper.get_instance()

    def apply(self):
        # Clear and repopulate self.data with actual values
        self.data.keyboard.layouts_list = list()
        itr = self._store.get_iter_first()
        while itr:
            self.data.keyboard.layouts_list.append(self._store[itr][0])
            itr = self._store.iter_next(itr)
        # FIXME:  Set the keyboard layout here, too.

    @property
    def completed(self):
        # The keyboard spoke is always completed, as it does not require you do
        # anything.  There's always a default selected.
        return True

    @property
    def status(self):
        # We don't need to check that self._store is empty, because that isn't allowed.
        return self._xkl_wrapper.name_to_show_str[self._store[0][0]]

    def initialize(self):
        NormalSpoke.initialize(self)

        # We want to store layouts' names but show layouts as
        # 'language (description)'.
        layoutColumn = self.builder.get_object("layoutColumn")
        layoutRenderer = self.builder.get_object("layoutRenderer")
        layoutColumn.set_cell_data_func(layoutRenderer, _show_layout,
                                            self._xkl_wrapper)

        self._store = self.builder.get_object("addedLayoutStore")
        self._add_data_layouts()

    def refresh(self):
        NormalSpoke.refresh(self)

        # Clear and repopulate addedLayoutStore with values from self.data
        self._store.clear()
        self._add_data_layouts()

        self._upButton = self.builder.get_object("upButton")
        self._downButton = self.builder.get_object("downButton")
        self._removeButton = self.builder.get_object("removeLayoutButton")
        self._previewButton = self.builder.get_object("previewButton")

        # Start with no buttons enabled, since nothing is selected.
        self._upButton.set_sensitive(False)
        self._downButton.set_sensitive(False)
        self._removeButton.set_sensitive(False)
        self._previewButton.set_sensitive(False)

    def _addLayout(self, store, name):
        store.append([name])
        self._xkl_wrapper.add_layout(name)

    def _removeLayout(self, store, itr):
        """
        Remove the layout specified by store iterator from the store and
        X runtime configuration.

        """

        self._xkl_wrapper.remove_layout(store[itr][0])
        store.remove(itr)

    # Signal handlers.
    def on_add_clicked(self, button):
        dialog = AddLayoutDialog(self.data)
        dialog.refresh()
        dialog.initialize()

        with enlightbox(self.window, dialog.window):
            response = dialog.run()

        if response == 1:
            duplicates = set()
            itr = self._store.get_iter_first()
            while itr:
                item = self._store[itr][0]
                if item in dialog.chosen_layouts:
                    duplicates.add(item)
                itr = self._store.iter_next(itr)

            for layout in dialog.chosen_layouts:
                if layout not in duplicates:
                    self._addLayout(self._store, layout)

            if self._remove_last_attempt:
                itr = self._store.get_iter_first()
                self._removeLayout(self._store, itr)
                self._remove_last_attempt = False

    def on_remove_clicked(self, button):
        selection = self.builder.get_object("layoutSelection")
        if not selection.count_selected_rows():
            return

        (store, itr) = selection.get_selected()
        itr2 = store.get_iter_first()
        #if the first item is selected, try to select the next one
        if store[itr][0] == store[itr2][0]:
            itr2 = store.iter_next(itr2)
            if itr2: #next one existing
                selection.select_iter(itr2)
                self._removeLayout(store, itr)
                return

            #nothing left, run AddLayout dialog to replace the current layout
            #add it to GLib.idle to make sure the underlaying gui is correctly
            #redrawn
            self._remove_last_attempt = True
            add_button = self.builder.get_object("addLayoutButton")
            GLib.idle_add(self.on_add_clicked, add_button)
            return

        #the selected item is not the first, select the previous one
        #XXX: there is no model.iter_previous() so we have to find it this way
        itr3 = store.iter_next(itr2) #look-ahead iterator
        while itr3 and (store[itr3][0] != store[itr][0]):
            itr2 = store.iter_next(itr2)
            itr3 = store.iter_next(itr3)

        self._removeLayout(store, itr)
        selection.select_iter(itr2)

    def on_up_clicked(self, button):
        selection = self.builder.get_object("layoutSelection")
        if not selection.count_selected_rows():
            return

        (store, cur) = selection.get_selected()
        prev = cur.copy()
        if not store.iter_previous(prev):
            return

        store.swap(cur, prev)
        selection.emit("changed")

    def on_down_clicked(self, button):
        selection = self.builder.get_object("layoutSelection")
        if not selection.count_selected_rows():
            return

        (store, cur) = selection.get_selected()
        nxt = store.iter_next(cur)
        if not nxt:
            return

        store.swap(cur, nxt)
        selection.emit("changed")

    def on_preview_clicked(self, button):
        selection = self.builder.get_object("layoutSelection")
        (store, cur) = selection.get_selected()
        layout_row = store[cur]
        if not layout_row:
            return

        dialog = Gkbd.KeyboardDrawing.dialog_new()
        Gkbd.KeyboardDrawing.dialog_set_layout(dialog, self._xkl_wrapper.configreg,
                                               layout_row[0])
        with enlightbox(self.window, dialog):
            dialog.show_all()
            dialog.run()

    def on_selection_changed(self, selection, *args):
        # We don't have to worry about multiple rows being selected in this
        # function, because that's disabled by the widget.
        if not selection.count_selected_rows():
            self._upButton.set_sensitive(False)
            self._downButton.set_sensitive(False)
            self._removeButton.set_sensitive(False)
            self._previewButton.set_sensitive(False)
            return

        (store, selected) = selection.get_selected_rows()

        # If something's selected, always enable the remove and preview buttons.
        self._removeButton.set_sensitive(True)
        self._previewButton.set_sensitive(True)

        # Disable the Up button if the top row's selected, and disable the
        # Down button if the bottom row's selected.
        if selected[0].get_indices() == [0]:
            self._upButton.set_sensitive(False)
            self._downButton.set_sensitive(True)
        elif selected[0].get_indices() == [len(store)-1]:
            self._upButton.set_sensitive(True)
            self._downButton.set_sensitive(False)
        else:
            self._upButton.set_sensitive(True)
            self._downButton.set_sensitive(True)

    def _add_data_layouts(self):
        if self.data.keyboard.layouts_list:
            for layout in self.data.keyboard.layouts_list:
                self._addLayout(self._store, layout)
        else:
            self._addLayout(self._store, "us")