forked from rhinstaller/anaconda
/
lang_locale_handler.py
177 lines (140 loc) · 6.54 KB
/
lang_locale_handler.py
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
#
# 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): Vratislav Podzimek <vpodzime@redhat.com>
#
"""
Module providing the LangLocaleHandler class that could be used as a mixin for
screens handling languages or locales configuration.
"""
import gi
gi.require_version("Gtk", "3.0")
gi.require_version("Pango", "1.0")
from gi.repository import Gtk, Pango
import os
from pyanaconda import localization
from pyanaconda.iutil import strip_accents
from pyanaconda.ui.gui.utils import set_treeview_selection, timed_action, override_cell_property
class LangLocaleHandler(object):
"""
Class that could be used as a mixin for screens handling languages or
locales configuration.
"""
def __init__(self):
# the class inheriting from this class is responsible for populating
# these items with actual objects
self._languageStore = None
self._languageStoreFilter = None
self._languageEntry = None
self._langSelection = None
self._langSelectedRenderer = None
self._langSelectedColumn = None
self._langView = None
self._localeView = None
self._localeStore = None
self._localeSelection = None
self._arrow = None
def initialize(self):
# Render an arrow for the chosen language
datadir = os.environ.get("ANACONDA_WIDGETS_DATA", "/usr/share/anaconda")
self._right_arrow = Gtk.Image.new_from_file(os.path.join(datadir, "pixmaps", "right-arrow-icon.png"))
self._left_arrow = Gtk.Image.new_from_file(os.path.join(datadir, "pixmaps", "left-arrow-icon.png"))
override_cell_property(self._langSelectedColumn, self._langSelectedRenderer,
"pixbuf", self._render_lang_selected)
# fill the list with available translations
for lang in localization.get_available_translations():
self._add_language(self._languageStore,
localization.get_native_name(lang),
localization.get_english_name(lang), lang)
# make filtering work
self._languageStoreFilter.set_visible_func(self._matches_entry, None)
def _matches_entry(self, model, itr, *args):
# Nothing in the text entry? Display everything.
entry = self._languageEntry.get_text().strip()
if not entry:
return True
# Need to strip out the pango markup before attempting to match.
# Otherwise, starting to type "span" for "spanish" will match everything
# due to the enclosing span tag.
# (success, attrs, native, accel)
native = Pango.parse_markup(model[itr][0], -1, "_")[2]
english = model[itr][1]
# Otherwise, filter the list showing only what is matched by the
# text entry. Either the English or native names can match.
lowered = entry.lower()
translit = strip_accents(native).lower()
if lowered in native.lower() or lowered in english.lower() or lowered in translit:
return True
else:
return False
def _render_lang_selected(self, column, renderer, model, itr, user_data=None):
(lang_store, sel_itr) = self._langSelection.get_selected()
if Gtk.get_locale_direction() == Gtk.TextDirection.LTR:
_arrow = self._right_arrow
else:
_arrow = self._left_arrow
if sel_itr and lang_store[sel_itr][2] == model[itr][2]:
return _arrow.get_pixbuf()
else:
return None
def _add_language(self, store, native, english, lang):
"""Override this method with a valid implementation"""
raise NotImplementedError()
def _add_locale(self, store, native, locale):
"""Override this method with a valid implementation"""
raise NotImplementedError()
def _select_locale(self, locale):
"""
Try to select the given locale in the language and locale
treeviews. This method tries to find the best match for the given
locale.
:return: a pair of selected iterators (language and locale)
:rtype: a pair of GtkTreeIter or None objects
"""
# get lang and select it
parts = localization.parse_langcode(locale)
if "language" not in parts:
# invalid locale, cannot select
return (None, None)
lang_itr = set_treeview_selection(self._langView, parts["language"], col=2)
# find matches and use the one with the highest rank
locales = localization.get_language_locales(locale)
locale_itr = set_treeview_selection(self._localeView, locales[0], col=1)
return (lang_itr, locale_itr)
def _refresh_locale_store(self, lang):
"""Refresh the localeStore with locales for the given language."""
self._localeStore.clear()
locales = localization.get_language_locales(lang)
for locale in locales:
self._add_locale(self._localeStore,
localization.get_native_name(locale),
locale)
# select the first locale (with the highest rank)
set_treeview_selection(self._localeView, locales[0], col=1)
def on_lang_selection_changed(self, selection):
(store, selected) = selection.get_selected_rows()
if selected:
lang = store[selected[0]][2]
self._refresh_locale_store(lang)
else:
self._localeStore.clear()
def on_clear_icon_clicked(self, entry, icon_pos, event):
if icon_pos == Gtk.EntryIconPosition.SECONDARY:
entry.set_text("")
@timed_action()
def on_entry_changed(self, *args):
self._languageStoreFilter.refilter()