~desktop-bugs/ubuntu/raring/language-selector/auth

« back to all changes in this revision

Viewing changes to LanguageSelector/ImConfig.py

  • Committer: Sebastien Bacher
  • Date: 2012-12-07 17:30:54 UTC
  • mfrom: (236.1.2 ubuntu)
  • Revision ID: seb128@ubuntu.com-20121207173054-cfp2fk06x2wyomjh
* LanguageSelector/gtk/GtkLanguageSelector.py:
  - Deprecated keyword "type" in Gtk.MessageDialog constructors
    replaced by "message_type".
* debian/control, LanguageSelector/gtk/GtkLanguageSelector.py,
  LanguageSelector/ImConfig.py, data/LanguageSelector.ui:
  - Transition from the im-switch framework for handling input
    method systems to im-config (LP: #1076975).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# ImConfig.py (c) 2012 Canonical
 
2
# Author: Gunnar Hjalmarsson <gunnarhj@ubuntu.com>
 
3
#
 
4
# Released under the GPL
 
5
#
 
6
 
 
7
import os
 
8
import subprocess
 
9
 
 
10
class ImConfig(object):
 
11
    
 
12
    def __init__(self):
 
13
        pass
 
14
 
 
15
    def available(self):
 
16
        return os.path.exists('/usr/bin/im-config')
 
17
 
 
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.
 
21
        inputMethods = []
 
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:
 
29
                if pack == 'nabi':
 
30
                    inputMethods.append('hangul')
 
31
                elif pack == 'gtk3-im-libthai':
 
32
                    inputMethods.append('thai')
 
33
                else:
 
34
                    inputMethods.append(pack)
 
35
        return ['default'] + sorted(inputMethods) + ['none']
 
36
 
 
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]
 
43
        return 'default'
 
44
 
 
45
    def setInputMethod(self, im):
 
46
        arg = 'REMOVE' if im == 'default' else im
 
47
        subprocess.call(['im-config', '-n', arg])
 
48
    
 
49
if __name__ == '__main__':
 
50
    im = ImConfig()
 
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())