Use GPLv2.
[ibus-anthy.git] / engine / main.py
blob019dcdbc487f296faa74c5c2e3ab73119665b5bd
1 # vim:set noet ts=4:
3 # ibus-tmpl - The Input Bus template project
5 # Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2, or (at your option)
10 # any later version.
12 # This program 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 General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 import os
22 import sys
23 import getopt
24 import dbus
25 import dbus.connection
26 import dbus.mainloop.glib
27 import ibus
28 import factory
29 import gtk
31 class IMApp:
32 def __init__ (self):
33 self._dbusconn = dbus.connection.Connection (ibus.IBUS_ADDR)
34 self._dbusconn.add_signal_receiver (self._disconnected_cb,
35 "Disconnected",
36 dbus_interface = dbus.LOCAL_IFACE)
37 self._engine = factory.DemoEngineFactory (self._dbusconn)
38 self._ibus = self._dbusconn.get_object (ibus.IBUS_NAME, ibus.IBUS_PATH)
39 self._ibus.RegisterFactories ([factory.FACTORY_PATH], **ibus.DEFAULT_ASYNC_HANDLERS)
41 def run (self):
42 gtk.main ()
44 def _disconnected_cb (self):
45 print "disconnected"
46 gtk.main_quit ()
49 def launch_engine ():
50 dbus.mainloop.glib.DBusGMainLoop (set_as_default=True)
51 IMApp ().run ()
53 def print_help (out, v = 0):
54 print >> out, "-h, --help show this message."
55 print >> out, "-d, --daemonize daemonize ibus"
56 sys.exit (v)
58 def main ():
59 daemonize = False
60 shortopt = "hd"
61 longopt = ["help", "daemonize"]
62 try:
63 opts, args = getopt.getopt (sys.argv[1:], shortopt, longopt)
64 except getopt.GetoptError, err:
65 print_help (sys.stderr, 1)
67 for o, a in opts:
68 if o in ("-h", "--help"):
69 print_help (sys.stdout)
70 elif o in ("-d", "--daemonize"):
71 daemonize = True
72 else:
73 print >> sys.stderr, "Unknown argument: %s" % o
74 print_help (sys.stderr, 1)
76 if daemonize:
77 if os.fork ():
78 sys.exit ()
80 launch_engine ()
82 if __name__ == "__main__":
83 main ()