Define mod_path in spec file.
[platform/upstream/ibus.git] / daemon / factorymanager.py
1 # vim:set noet ts=4:
2 #
3 # ibus - The Input Bus
4 #
5 # Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
6 #
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.
11 #
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.
16 #
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
21
22 import weakref
23 import gobject
24 import ibus
25 from enginefactory import EngineFactory
26
27 class FactoryManager (ibus.Object):
28         __gsignals__ = {
29                 'new-factories-added' : (
30                         gobject.SIGNAL_RUN_FIRST,
31                         gobject.TYPE_NONE,
32                         (gobject.TYPE_PYOBJECT, )
33                 )
34         }
35         def __init__ (self):
36                 ibus.Object.__init__ (self)
37                 self._factories = {}
38                 self._ibusconn_factory_dict = {}
39                 self._default_factory = None
40                 self._sorted_factories = None
41
42         def register_factories (self, object_paths, ibusconn):
43                 if ibusconn in self._factories:
44                         raise ibus.IBusException ("this conn has registered factories!")
45
46                 self._ibusconn_factory_dict[ibusconn] = []
47
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)
52
53                         factory = EngineFactory (ibusconn, object_path)
54                         self._factories[object_path] = factory
55                         self._ibusconn_factory_dict[ibusconn].append (object_path)
56
57                 ibusconn.connect ("destroy", self._ibusconn_destroy_cb)
58
59                 self.emit ("new-factories-added",
60                                         self._ibusconn_factory_dict[ibusconn][:])
61
62         def get_default_factory (self):
63                 if self._default_factory == None:
64                         factories = self._get_sorted_factories ()
65                         if factories:
66                                 self._default_factory = factories[0]
67
68                 return self._default_factory
69
70         def get_next_factory (self, factory):
71                 factories = self._get_sorted_factories ()
72                 i = factories.index (factory) + 1
73                 if i >= len (factories):
74                         i = 0
75
76                 return factories[i]
77
78         def get_factories (self):
79                 return self._factories.keys ()
80
81         def get_factory_info (self, factory_path):
82                 factory = self._factories[factory_path]
83                 return factory.get_info ()
84
85         def get_factory (self, factory_path):
86                 factory = self._factories[factory_path]
87                 return factory
88
89         def _get_sorted_factories (self, resort = False):
90                 if not self._sorted_factories or resort:
91                         factories = self._factories.values ()
92                         factories.sort ()
93                         self._sorted_factories = factories
94                 return self._sorted_factories
95
96         def _ibusconn_destroy_cb (self, ibusconn):
97                 assert ibusconn in self._ibusconn_factory_dict
98
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]
104
105                 del self._ibusconn_factory_dict[ibusconn]
106                 self._sorted_factories = None
107
108 gobject.type_register (FactoryManager)
109