OSDN Git Service

Add dbus support.
[fukui-no-namari/fukui-no-namari.git] / src / hage1
1 #!/usr/bin/env python
2
3 # Copyright (C) 2006 by Aiwota Programmer
4 # aiwotaprog@tetteke.tk
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
20 import pygtk
21 pygtk.require('2.0')
22 import gtk
23 import gnome.ui
24 import gobject
25 import getopt
26 import sys
27 import dbus
28 import dbus.service
29
30 from Hage1 import thread_window
31 from Hage1 import board_window
32 from Hage1 import brdlist_window
33 from Hage1 import brdlist
34 from Hage1 import config
35 from Hage1 import dbus_object
36
37 APPNAME = 'Hage1'
38 APPVERSION = '0.1'
39
40 # where are these values ?
41 DBUS_NAME_FLAG_ALLOW_REPLACEMENT = 1
42 DBUS_NAME_FLAG_REPLACE_EXISTING = 2
43 DBUS_NAME_FLAG_DO_NOT_QUEUE = 4
44 DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER = 1
45 DBUS_REQUEST_NAME_REPLY_IN_QUEUE = 2
46 DBUS_REQUEST_NAME_REPLY_EXISTS = 3
47 DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER = 4
48
49 dbus_bus_name = "tk.tetteke.Hage1"
50 dbus_object_path = "/tk/tetteke/Hage1"
51 dbus_object.dbus_interface_name = "tk.tetteke.Hage1"
52
53 def usage():
54     print """usage: hage1 [-s bbs | -b board | -t thread]
55 -s, --bbs bbs       : bbs id
56 -b, --board board   : board id, ignored if bbs is empty
57 -t, --thread thread : thread id, ignored if bbs and board is empty"""
58
59 def getoption():
60     bbs = None
61     board = None
62     thread = None
63
64     try:
65         opts, args = getopt.getopt(
66             sys.argv[1:], "hs:b:t:", ["help", "bbs=", "board=", "thread="])
67     except getopt.GetoptError, msg:
68         print msg
69         usage()
70         sys.exit(2)
71     for option, value in opts:
72         if option in ("-h", "--help"):
73             usage()
74             sys.exit()
75         elif option in ("-s", "--bbs"):
76             bbs = value
77         elif option in ("-b", "--board"):
78             board = value
79         elif option in ("-t", "--thread"):
80             thread = value
81
82     return bbs, board, thread
83     
84 if __name__ == "__main__":
85
86     # get option
87     bbs, board, thread = getoption()
88
89     # check if an instance exists
90     session_bus = dbus.SessionBus()
91     dbus_proxy_object = session_bus.get_object(
92         "org.freedesktop.DBus", "/org/freedesktop/DBus")
93     dbus_iface = dbus.Interface(dbus_proxy_object, "org.freedesktop.DBus")
94     req_name_ret = dbus_iface.RequestName(
95         dbus_bus_name, DBUS_NAME_FLAG_DO_NOT_QUEUE)
96
97     if req_name_ret == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER or \
98        req_name_ret == DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
99         # the instance does not exist. start normally.
100
101         bus_name = dbus.service.BusName(dbus_bus_name, bus=session_bus)
102         obj = dbus_object.DBusHage1Object(bus_name, dbus_object_path)
103
104         gnome.init(APPNAME, APPVERSION)
105         gobject.threads_init()
106         config.APPNAME = APPNAME
107
108         # init brdlist after setting config.APPNAME
109         brdlist.init()
110
111         if not bbs:
112             brdlist_window.open_brdlist("2ch")
113         elif not board:
114             brdlist_window.open_brdlist(bbs)
115         elif not thread:
116             board_window.open_board(bbs, board)
117         else:
118             thread_window.open_thread(bbs, board, thread)
119
120         gtk.main()
121     else:
122         # the instance exists. send message and exit.
123
124         proxy_object = session_bus.get_object(dbus_bus_name, dbus_object_path)
125         iface = dbus.Interface(proxy_object, dbus_object.dbus_interface_name)
126
127         if not bbs:
128             iface.open_brdlist("2ch")
129         elif not board:
130             iface.open_brdlist(bbs)
131         elif not thread:
132             iface.open_board(bbs, board)
133         else:
134             iface.open_thread(bbs, board, thread)