5 # Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2 of the License, or (at your option) any later version.
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this program; if not, write to the
19 # Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 # Boston, MA 02111-1307 USA
25 from enginefactory import EngineFactory
27 class FactoryManager (ibus.Object):
29 'new-factories-added' : (
30 gobject.SIGNAL_RUN_FIRST,
32 (gobject.TYPE_PYOBJECT, )
36 ibus.Object.__init__ (self)
38 self._ibusconn_factory_dict = {}
39 self._default_factory = None
40 self._sorted_factories = None
42 def register_factories (self, object_paths, ibusconn):
43 if ibusconn in self._factories:
44 raise ibus.IBusException ("this conn has registered factories!")
46 self._ibusconn_factory_dict[ibusconn] = []
48 for object_path in object_paths:
49 if object_path in self._factories:
50 raise ibus.IBusException (
51 "Factory [%s] has been registered!" % object_path)
53 factory = EngineFactory (ibusconn, object_path)
54 self._factories[object_path] = factory
55 self._ibusconn_factory_dict[ibusconn].append (object_path)
57 ibusconn.connect ("destroy", self._ibusconn_destroy_cb)
59 self.emit ("new-factories-added",
60 self._ibusconn_factory_dict[ibusconn][:])
62 def get_default_factory (self):
63 if self._default_factory == None:
64 factories = self._get_sorted_factories ()
66 self._default_factory = factories[0]
68 return self._default_factory
70 def get_next_factory (self, factory):
71 factories = self._get_sorted_factories ()
72 i = factories.index (factory) + 1
73 if i >= len (factories):
78 def get_factories (self):
79 return self._factories.keys ()
81 def get_factory_info (self, factory_path):
82 factory = self._factories[factory_path]
83 return factory.get_info ()
85 def get_factory (self, factory_path):
86 factory = self._factories[factory_path]
89 def _get_sorted_factories (self, resort = False):
90 if not self._sorted_factories or resort:
91 factories = self._factories.values ()
93 self._sorted_factories = factories
94 return self._sorted_factories
96 def _ibusconn_destroy_cb (self, ibusconn):
97 assert ibusconn in self._ibusconn_factory_dict
99 for object_path in self._ibusconn_factory_dict[ibusconn]:
100 factory = self._factories[object_path]
101 if factory == self._default_factory:
102 self._default_factory = None
103 del self._factories[object_path]
105 del self._ibusconn_factory_dict[ibusconn]
106 self._sorted_factories = None
108 gobject.type_register (FactoryManager)