/usr/share/ibus/setup/enginecombobox.py is in ibus 1.5.9-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # vim:set et sts=4 sw=4:
#
# ibus - The Input Bus
#
# Copyright (c) 2007-2014 Peng Huang <shawn.p.huang@gmail.com>
# Copyright (c) 2007-2014 Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
# USA
import locale
import functools
import sys
from gi.repository import Gio
from gi.repository import GObject
from gi.repository import Gtk
from gi.repository import IBus
from gi.repository import Pango
from icon import load_icon
from i18n import _, N_
class EngineComboBox(Gtk.ComboBox):
__gtype_name__ = 'EngineComboBox'
__gproperties__ = {
'active-engine' : (
object,
'selected engine',
'selected engine',
GObject.ParamFlags.READABLE)
}
def __init__(self):
super(EngineComboBox, self).__init__()
self.connect("notify::active", self.__notify_active_cb)
self.__model = None
self.__all_model = None
self.__show_sub_lang = False
self.__settings_xkblayoutconfig = Gio.Settings(
"org.freedesktop.ibus.general.xkblayoutconfig");
renderer = Gtk.CellRendererPixbuf()
renderer.set_property("xalign", 0)
renderer.set_property("xpad", 2)
self.pack_start(renderer, False)
self.set_cell_data_func(renderer, self.__icon_cell_data_cb, None)
renderer = Gtk.CellRendererText()
renderer.set_property("xalign", 0)
renderer.set_property("xpad", 2)
self.pack_start(renderer, True)
self.set_cell_data_func(renderer, self.__name_cell_data_cb, None)
def __gconf_get_lang_list_from_locale(self):
common_list = ['en', 'Other']
loc = None
try:
loc = locale.setlocale (locale.LC_ALL)
except:
pass
if loc == None:
return common_list
current_lang = IBus.get_language_name(loc)
if current_lang == None:
return common_list
group_list = self.__settings_xkblayoutconfig.get_strv('group-list')
if len(group_list) == 0:
return [loc] + common_list
lang_list = None
for group in group_list:
group = group.replace('_', '-')
langs = self.__settings_xkblayoutconfig.get_strv(group)
for lang in langs:
if current_lang == IBus.get_language_name(lang):
lang_list = langs
break
if lang_list != None:
break
if lang_list == None:
return [loc] + common_list
return lang_list + common_list
def __has_engine_in_lang_list(self, engine, lang_list):
retval = False
for lang in lang_list:
if IBus.get_language_name(lang) == \
IBus.get_language_name(engine.props.language):
retval = True
break
return retval
def __model_append_langs(self, model, langs, visible):
keys = list(langs.keys())
keys.sort(key=functools.cmp_to_key(locale.strcoll))
loc = locale.getlocale()[0]
# None on C locale
if loc == None:
loc = 'en_US'
if loc == 'C':
loc = 'en_US'
current_lang = IBus.get_language_name(loc)
# move current language to the first place
if current_lang in keys:
keys.remove(current_lang)
keys.insert(0, current_lang)
#add "Others" to the end of the combo box
if IBus.get_language_name("Other") in keys:
keys.remove(IBus.get_language_name("Other"))
keys += [IBus.get_language_name("Other")]
for l in keys:
iter1 = model.append(None)
model.set(iter1, 0, l)
def cmp_engine(a, b):
if a.get_rank() == b.get_rank():
return locale.strcoll(a.get_longname(), b.get_longname())
return int(b.get_rank() - a.get_rank())
langs[l].sort(key=functools.cmp_to_key(cmp_engine))
for e in langs[l]:
iter2 = model.append(iter1)
model.set(iter2, 0, e)
def set_engines(self, engines):
self.__model = Gtk.TreeStore(object)
iter1 = self.__model.append(None)
self.__model.set(iter1, 0, 0)
lang_list = self.__gconf_get_lang_list_from_locale()
lang = {}
sub_lang = {}
for e in engines:
l = IBus.get_language_name(e.props.language)
if lang_list == None or \
self.__has_engine_in_lang_list(e, lang_list):
if l not in lang:
lang[l] = []
lang[l].append(e)
else:
if l not in sub_lang:
sub_lang[l] = []
sub_lang[l].append(e)
self.__model_append_langs(self.__model, lang, True)
iter1 = self.__model.append(None)
self.__model.set(iter1, 0, -1)
self.__all_model = Gtk.TreeStore(object)
iter1 = self.__all_model.append(None)
self.__all_model.set(iter1, 0, 0)
self.__model_append_langs(self.__all_model, lang, False)
iter1 = self.__all_model.append(None)
self.__all_model.set(iter1, 0, -1)
self.__model_append_langs(self.__all_model, sub_lang, False)
self.__toggle_sub_lang()
def __toggle_sub_lang(self):
self.set_model(None)
if self.__show_sub_lang:
self.set_model(self.__all_model)
else:
self.set_model(self.__model)
self.set_active(0)
def __icon_cell_data_cb(self, celllayout, renderer, model, iter, data):
model = self.get_model()
engine = model.get_value(iter, 0)
if isinstance(engine, str):
renderer.set_property("visible", False)
renderer.set_property("sensitive", False)
elif sys.version < '3' and isinstance (engine, unicode):
renderer.set_property("visible", False)
renderer.set_property("sensitive", False)
elif isinstance(engine, int):
if engine == 0:
renderer.set_property("visible", False)
renderer.set_property("sensitive", False)
renderer.set_property("pixbuf", None)
elif engine < 0:
if not self.__show_sub_lang:
pixbuf = load_icon("go-bottom", Gtk.IconSize.LARGE_TOOLBAR)
else:
pixbuf = load_icon("go-up", Gtk.IconSize.LARGE_TOOLBAR)
if pixbuf == None:
pixbuf = load_icon("image-missing",
Gtk.IconSize.LARGE_TOOLBAR)
if pixbuf == None:
renderer.set_property("visible", False)
renderer.set_property("sensitive", False)
return
renderer.set_property("visible", True)
renderer.set_property("sensitive", True)
renderer.set_property("pixbuf", pixbuf)
else:
renderer.set_property("visible", True)
renderer.set_property("sensitive", True)
pixbuf = load_icon(engine.get_icon(), Gtk.IconSize.LARGE_TOOLBAR)
renderer.set_property("pixbuf", pixbuf)
def __name_cell_data_cb(self, celllayout, renderer, model, iter, data):
model = self.get_model()
engine = model.get_value(iter, 0)
if isinstance (engine, str):
renderer.set_property("sensitive", False)
renderer.set_property("text", engine)
renderer.set_property("weight", Pango.Weight.NORMAL)
elif sys.version < '3' and isinstance (engine, unicode):
renderer.set_property("sensitive", False)
renderer.set_property("text", engine)
renderer.set_property("weight", Pango.Weight.NORMAL)
elif isinstance(engine, int):
renderer.set_property("sensitive", True)
if engine == 0:
renderer.set_property("text", _("Select an input method"))
renderer.set_property("weight", Pango.Weight.NORMAL)
elif engine < 0:
if not self.__show_sub_lang:
renderer.set_property("text", _("Show all input methods"))
else:
renderer.set_property("text", _("Show only input methods for your region"))
renderer.set_property("weight", Pango.Weight.BOLD)
else:
renderer.set_property("sensitive", True)
renderer.set_property("text", engine.get_longname())
renderer.set_property("weight",
Pango.Weight.BOLD if engine.get_rank() > 0 else Pango.Weight.NORMAL)
def __notify_active_cb(self, combobox, property):
self.notify("active-engine")
def do_get_property(self, property):
if property.name == "active-engine":
i = self.get_active()
if i == 0 or i == -1:
return None
iter = self.get_active_iter()
model = self.get_model()
if model[iter][0] == -1:
self.__show_sub_lang = not self.__show_sub_lang
self.__toggle_sub_lang()
return None
return model[iter][0]
else:
raise AttributeError('unknown property %s' % property.name)
def get_active_engine(self):
return self.get_property("active-engine")
if __name__ == "__main__":
combo = EngineComboBox()
combo.set_engines([IBus.EngineDesc(language="zh")])
w = Gtk.Window()
w.add(combo)
w.show_all()
Gtk.main()
|