Return empty if factory is "".
[platform/upstream/ibus.git] / engine / enchant / main.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 os
23 import sys
24 import getopt
25 import dbus
26 import dbus.connection
27 import dbus.mainloop.glib
28 import ibus
29 import factory
30 import gtk
31
32 class IMApp:
33         def __init__ (self):
34                 self._dbusconn = dbus.connection.Connection (ibus.IBUS_ADDR)
35                 self._dbusconn.add_signal_receiver (self._disconnected_cb, 
36                                                         "Disconnected", 
37                                                         dbus_interface = dbus.LOCAL_IFACE)
38                 self._engine = factory.DemoEngineFactory (self._dbusconn)
39                 self._ibus = self._dbusconn.get_object (ibus.IBUS_NAME, ibus.IBUS_PATH)
40                 self._ibus.RegisterFactories ([factory.FACTORY_PATH], **ibus.DEFAULT_ASYNC_HANDLERS)
41
42         def run (self):
43                 gtk.main ()
44
45         def _disconnected_cb (self):
46                 print "disconnected"
47                 gtk.main_quit ()
48
49
50 def launch_engine ():
51         dbus.mainloop.glib.DBusGMainLoop (set_as_default=True)
52         IMApp ().run ()
53
54 def print_help (out, v = 0):
55         print >> out, "-h, --help             show this message."
56         print >> out, "-d, --daemonize        daemonize ibus"
57         sys.exit (v)
58
59 def main ():
60         daemonize = False
61         shortopt = "hd"
62         longopt = ["help", "daemonize"]
63         try:
64                 opts, args = getopt.getopt (sys.argv[1:], shortopt, longopt)
65         except getopt.GetoptError, err:
66                 print_help (sys.stderr, 1)
67
68         for o, a in opts:
69                 if o in ("-h", "--help"):
70                         print_help (sys.stdout)
71                 elif o in ("-d", "--daemonize"):
72                         daemonize = True
73                 else:
74                         print >> sys.stderr, "Unknown argument: %s" % o
75                         print_help (sys.stderr, 1)
76
77         if daemonize:
78                 if os.fork ():
79                         sys.exit ()
80
81         launch_engine ()
82
83 if __name__ == "__main__":
84         main ()