1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2
# Copyright 2012 Canonical
3
# Author: Thomi Richards
5
# This program is free software: you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License version 3, as published
7
# by the Free Software Foundation.
9
"Functions to deal with ibus service."
11
# without this the 'import ibus' imports us, and import ibus.common fails. 0.O
12
from __future__ import absolute_import
17
from time import sleep
19
logger = logging.getLogger(__name__)
23
"""Get the ibus bus object, possibly starting the ibus daemon if it's
28
for i in range(max_tries):
29
if ibus.common.get_address() is None:
30
pid = os.spawnlp(os.P_NOWAIT, "ibus-daemon", "ibus-daemon", "-d", "--xim")
31
logger.info("Started ibus-daemon with pid %i." % (pid))
35
raise RuntimeError("Could not start ibus-daemon after %d tries." % (max_tries))
38
def get_available_input_engines():
39
"""Get a list of available input engines."""
41
return [e.name for e in bus.list_engines()]
44
def get_active_input_engines():
45
"""Get the list of input engines that have been activated."""
47
return [e.name for e in bus.list_active_engines()]
49
def set_active_engines(engine_list):
50
"""Installs the engines in 'engine_list' into the list of active iBus engines.
52
The specified engines must appear in the return list from
53
get_available_input_engines(). This function removes all other engines.
55
This function returns the list of engines installed before this function was
56
called. The caller should pass this list to set_active_engines to restore
57
ibus to it's old state once the test has finished.
59
if type(engine_list) is not list:
60
raise TypeError("engine_list must be a list of valid engine names.")
61
available_engines = get_available_input_engines()
62
for engine in engine_list:
63
if not isinstance(engine, basestring):
64
raise TypeError("Engines in engine_list must all be strings.")
65
if engine not in available_engines:
66
raise ValueError("engine_list contains invalid engine name: '%s'", engine)
69
config = bus.get_config()
70
old_engines = get_active_input_engines()
71
config.set_list("general", "preload_engines", engine_list, "s")
72
# need to restart the ibus bus before it'll pick up the new engine.
73
# see bug report here:
74
# http://code.google.com/p/ibus/issues/detail?id=1418&thanks=1418&ts=1329885137
75
bus.exit(restart=True)
79
def set_global_input_engine(engine_name):
80
"""Set the global iBus input engine by name.
82
This function enables the global input engine. To turn it off again, pass None
86
if not (engine_name is None or isinstance(engine_name, basestring)):
87
raise TypeError("engine_name type must be either str or None.")
92
available_engines = get_available_input_engines()
93
if not engine_name in available_engines:
94
raise ValueError("Unknown engine '%s'" % (engine_name))
95
bus.get_config().set_value("general", "use_global_engine", True)
96
bus.set_global_engine(engine_name)
97
logger.info('Enabling global ibus engine "%s".' % (engine_name))
99
bus.get_config().set_value("general", "use_global_engine", False)
100
logger.info('Disabling global ibus engine.')