1
# ImConfig.py (c) 2012 Canonical
2
# Author: Gunnar Hjalmarsson <gunnarhj@ubuntu.com>
4
# Released under the GPL
10
class ImConfig(object):
16
return os.path.exists('/usr/bin/im-config')
18
def getAvailableInputMethods(self):
19
# FIXME: This function is a little hackish. It should be
20
# possible to make use of im-config code instead.
22
packages_to_check = ['ibus', 'fcitx', 'uim', 'hime', 'gcin',
23
'scim', 'nabi', 'gtk3-im-libthai']
24
packinfo = subprocess.Popen(['dpkg-query', '-l'] +
25
packages_to_check, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
26
universal_newlines=True).communicate()[0]
27
for pack in packages_to_check:
28
if 'ii %s' % pack in packinfo:
30
inputMethods.append('hangul')
31
elif pack == 'gtk3-im-libthai':
32
inputMethods.append('thai')
34
inputMethods.append(pack)
35
return ['default'] + sorted(inputMethods) + ['none']
37
def getCurrentInputMethod(self):
38
user_conf_file = os.path.expanduser('~/.xinputrc')
39
if os.path.exists(user_conf_file):
40
for line in open(user_conf_file):
41
if line.startswith('run_im'):
42
return line.split()[1]
45
def setInputMethod(self, im):
46
arg = 'REMOVE' if im == 'default' else im
47
subprocess.call(['im-config', '-n', arg])
49
if __name__ == '__main__':
51
print('available input methods: %s' % im.getAvailableInputMethods())
52
print("setting method 'fcitx'")
53
im.setInputMethod('fcitx')
54
print('current method: %s' % im.getCurrentInputMethod())
55
print("setting method 'default' (i.e. removing ~/.xinputrc)")
56
im.setInputMethod('default')
57
print('current method: %s' % im.getCurrentInputMethod())