Package list lightdm-gtk-greeter-settings / e870f8b1-f074-4c9e-b83e-c3008bb558d4/main lightdm_gtk_greeter_settings / OptionEntry.py
e870f8b1-f074-4c9e-b83e-c3008bb558d4/main

Tree @e870f8b1-f074-4c9e-b83e-c3008bb558d4/main (Download .tar.gz)

OptionEntry.py @e870f8b1-f074-4c9e-b83e-c3008bb558d4/mainraw · history · blame

  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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#!/usr/bin/env python3
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
#   LightDM GTK Greeter Settings
#   Copyright (C) 2014 Andrew P. <pan.pav.7c5@gmail.com>
#
#   This program is free software: you can redistribute it and/or modify it
#   under the terms of the GNU General Public License version 3, as published
#   by the Free Software Foundation.
#
#   This program is distributed in the hope that it will be useful, but
#   WITHOUT ANY WARRANTY; without even the implied warranties of
#   MERCHANTABILITY, SATISFACTORY QUALITY, 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, see <http://www.gnu.org/licenses/>.


import time
from locale import gettext as _

from gi.repository import (
    Gdk,
    GLib,
    GObject,
    Gtk)
from lightdm_gtk_greeter_settings.helpers import (
    C_,
    bool2string,
    string2bool,
    SimpleEnum)


__all__ = [
    'AccessibilityStatesEntry',
    'AdjustmentEntry',
    'BackgroundEntry',
    'BaseEntry',
    'BooleanEntry',
    'ChoiceEntry',
    'ClockFormatEntry',
    'InvertedBooleanEntry',
    'StringEntry',
    'StringPathEntry']


class BaseEntry(GObject.GObject):

    def __init__(self, widgets):
        super().__init__()
        self._widgets = widgets
        self.__widgets_to_disable = []

        self.__use = widgets['use']
        if self.__use:
            self.__use.connect('notify::active', self.__on_use_toggled)

        self.__error = widgets['error']

        self._add_label_widget(widgets['label_holder'])

    @property
    def value(self):
        '''Option value'''
        value = self._get_value()
        formatted = self.get.emit(value)
        return value if formatted is None else formatted

    @value.setter
    def value(self, value):
        if self.__use:
            self.__use.set_active(True)
        formatted = self.set.emit(value)
        self._set_value(value if formatted is None else formatted)

    @property
    def enabled(self):
        return self._get_enabled()

    @enabled.setter
    def enabled(self, value):
        self._set_enabled(value)

    @property
    def error(self):
        return self._get_error()

    @error.setter
    def error(self, value):
        self._set_error(value)

    @property
    def widgets(self):
        return self._widgets

    @GObject.Signal
    def changed(self):
        pass

    @GObject.Signal(flags=GObject.SIGNAL_RUN_CLEANUP)
    def get(self, value: str) -> str:
        pass

    @GObject.Signal(flags=GObject.SIGNAL_RUN_CLEANUP)
    def set(self, value: str) -> str:
        pass

    @GObject.Signal('show-menu')
    def show_menu(self):
        pass

    def __repr__(self):
        try:
            value = self._get_value()
        except NotImplemented:
            value = '<Undefined>'
        return '%s(%s:%s)' % (self.__class__.__name__, int(self.enabled),
                              value)

    def _get_value(self):
        raise NotImplementedError(self.__class__)

    def _set_value(self, value):
        raise NotImplementedError(self.__class__)

    def _get_error(self):
        if self.__error:
            return self.__error.props.tooltip_text
        return None

    def _set_error(self, text):
        if self.__error:
            self.__error.props.visible = text is not None
            self.__error.props.tooltip_text = text

    def _get_enabled(self):
        if self.__use:
            return self.__use.get_active()
        return True

    def _set_enabled(self, value):
        if self.__use:
            self.__use.set_active(value)
            for widget in self.__widgets_to_disable:
                widget.props.sensitive = value
        self._emit_changed()

    def _add_label_widget(self, *widgets):
        for widget in widgets:
            if widget:
                widget.connect('button-press-event', self.__on_label_clicked)

    def _add_controlled_by_state_widget(self, *widgets):
        self.__widgets_to_disable += widgets

    def _show_menu(self):
        self.__on_label_clicked()

    def _emit_changed(self, *unused):
        self.changed.emit()

    def __on_use_toggled(self, toggle, *unused):
        self._set_enabled(self.__use.props.active)

    def __on_label_clicked(self, widget, event):
        if event.button == 3:
            self.show_menu.emit()


class BooleanEntry(BaseEntry):

    def __init__(self, widgets):
        super().__init__(widgets)
        self._value = widgets['value']
        self._value.connect('notify::active', self._emit_changed)
        self._add_controlled_by_state_widget(self._value)

    def _get_value(self):
        return bool2string(self._value.props.active)

    def _set_value(self, value):
        self._value.props.active = string2bool(value)


class InvertedBooleanEntry(BooleanEntry):

    def __init__(self, widgets):
        super().__init__(widgets)

    def _get_value(self):
        return bool2string(not self._value.props.active)

    def _set_value(self, value):
        self._value.props.active = not string2bool(value)


class StringEntry(BaseEntry):

    def __init__(self, widgets):
        super().__init__(widgets)
        self._value = widgets['value']
        self._add_controlled_by_state_widget(self._value)
        if isinstance(self._value.props.parent, Gtk.ComboBox):
            self._add_controlled_by_state_widget(self._value.props.parent)
        self._value.connect('changed', self._emit_changed)

    def _get_value(self):
        return self._value.props.text

    def _set_value(self, value):
        self._value.props.text = value or ''


class StringPathEntry(BaseEntry):

    class Row(SimpleEnum):
        Title = ()
        Type = ()

    class ItemType(SimpleEnum):
        Select = 'select-path'
        Value = 'value'
        Separator = 'separator'

    def __init__(self, widgets):
        super().__init__(widgets)

        self._file_dialog = None
        self._combo = widgets['combo']
        self._entry = widgets['entry']
        self._filters = ()
        self._add_controlled_by_state_widget(self._combo)

        self._entry.connect('changed', self._emit_changed)
        self._combo.connect('format-entry-text', self._on_combobox_format)

        self._combo.set_row_separator_func(self._row_separator_callback, None)

    def add_filter(self, file_filter):
        if self._file_dialog:
            self._file_dialog.add_filter(file_filter)
        elif self._filters:
            self._filters.append(file_filter)
        else:
            self._filters = [file_filter]

    def _get_value(self):
        return self._entry.props.text

    def _set_value(self, value):
        self._entry.props.text = value or ''

    def _row_separator_callback(self, model, rowiter, data):
        return model[rowiter][self.Row.Type] == self.ItemType.Separator

    def _on_combobox_format(self, combobox, path):
        value = self._entry.props.text
        item_id = combobox.get_active_id()
        if item_id == self.ItemType.Select:
            if not self._file_dialog:
                self._file_dialog = Gtk.FileChooserDialog(
                    parent=self._combo.get_toplevel(),
                    buttons=(_('_OK'), Gtk.ResponseType.OK,
                             _('_Cancel'), Gtk.ResponseType.CANCEL),
                    title=C_('option|StringPathEntry', 'Select path'))
                for f in self._filters:
                    self._file_dialog.add_filter(f)
            if self._file_dialog.run() == Gtk.ResponseType.OK:
                value = self._file_dialog.get_filename()
            self._file_dialog.hide()
        elif item_id == self.ItemType.Value:
            value = combobox.props.model[path][self.Row.Title]

        combobox.set_active(-1)
        combobox.grab_focus()
        return value


class AdjustmentEntry(BaseEntry):

    def __init__(self, widgets):
        super().__init__(widgets)
        self._value = widgets['adjustment']
        self._value.connect('value-changed', self._emit_changed)
        self._view = widgets['view']
        self._add_controlled_by_state_widget(self._view)

    def _get_value(self):
        return str(self._value.props.value)

    def _set_value(self, value):
        try:
            self._value.props.value = float(value or '')
        except ValueError:
            self._value.props.value = 0


class ChoiceEntry(BaseEntry):

    def __init__(self, widgets):
        super().__init__(widgets)
        self._value = widgets['value']
        self._value.connect('changed', self._emit_changed)
        self._add_controlled_by_state_widget(self._value)

    def _get_value(self):
        return self._value.props.active_id

    def _set_value(self, value):
        self._value.props.active_id = value or ''


class ClockFormatEntry(StringEntry):

    def __init__(self, widgets):
        super().__init__(widgets)
        self._preview = widgets['preview']
        self._value.connect('changed', self._on_changed)
        GLib.timeout_add_seconds(1, self._on_changed, self._value)

    def _on_changed(self, entry):
        self._preview.props.label = time.strftime(self._value.props.text)
        return True


class BackgroundEntry(BaseEntry):

    def __init__(self, widgets):
        super().__init__(widgets)
        self._image_choice = widgets['image_choice']
        self._color_choice = widgets['color_choice']
        self._image_value = widgets['image_value']
        self._color_value = widgets['color_value']

        self._add_controlled_by_state_widget(self._image_choice, self._color_choice,
                                             self._image_value, self._color_value)

        self._on_choice_id = self._color_choice.connect('toggled', self._on_color_choice_toggled)
        self._color_value.connect('color-set', self._on_color_set)
        self._image_value.connect('file-set', self._on_file_set)

    def _get_value(self):
        if self._image_choice.props.active:
            return self._image_value.get_filename() or ''
        else:
            r, g, b, __ = (int(0xFF * v) for v in self._color_value.props.rgba)
            return '#%02x%02x%02x' % (r, g, b)

    def _set_value(self, value):
        if value is None:
            value = ''

        rgba = Gdk.RGBA()
        if not rgba.parse(value):
            rgba = None

        with self._color_choice.handler_block(self._on_choice_id):
            self._color_choice.props.active = rgba is not None
            self._image_choice.props.active = rgba is None

        if rgba is not None:
            self._color_value.props.rgba = rgba
            self._image_value.unselect_all()
        else:
            if value:
                self._image_value.select_filename(value)
            else:
                self._image_value.unselect_all()

        self._emit_changed()

    def _on_color_choice_toggled(self, toggle):
        self._emit_changed()

    def _on_color_set(self, button):
        if not self._color_choice.props.active:
            self._color_choice.props.active = True
        else:
            self._emit_changed()

    def _on_file_set(self, button):
        if not self._image_choice.props.active:
            self._image_choice.props.active = True
        else:
            self._emit_changed()


class FontEntry(BaseEntry):

    def __init__(self, widgets):
        super().__init__(widgets)
        self._value = widgets['value']
        self._value.connect('font-set', self._emit_changed)
        self._add_controlled_by_state_widget(self._value)

    def _get_value(self):
        return self._value.get_font_name()

    def _set_value(self, value):
        self._value.props.font_name = value or ''


class AccessibilityStatesEntry(BaseEntry):

    Options = ('keyboard', 'reader', 'contrast', 'font')

    def __init__(self, widgets):
        super().__init__(widgets)

        self._states = {name: widgets[name] for name in self.Options}

        for w in self._states.values():
            w.connect('changed', self._emit_changed)

    def _get_value(self):
        states = {name: widget.props.active_id
                  for (name, widget) in self._states.items()}
        return ';'.join(state + name
                        for (name, state) in states.items() if state not in {None, '-'})

    def _set_value(self, value):
        if value:
            states = dict((v[1:], v[0]) if v[0] in ('-', '+', '~') else (v, '-')
                          for v in value.split(';') if v)
        else:
            states = {}
        for name in self.Options:
            self._states[name].props.active_id = states.get(name, '-')