OSDN Git Service

step two, modify script.
[fukui-no-namari/fukui-no-namari.git] / src / fukui-no-namari
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 APPNAME = 'Fukui-no-Namari'
31 APPVERSION = '0.1'
32
33 from FukuiNoNamari import config
34 config.APPNAME = APPNAME.lower()
35 from FukuiNoNamari import thread_window
36 from FukuiNoNamari import board_window
37 from FukuiNoNamari import dbus_object
38 from FukuiNoNamari import uri_opener
39 from FukuiNoNamari import session
40
41 # where are these values ?
42 DBUS_NAME_FLAG_ALLOW_REPLACEMENT = 1
43 DBUS_NAME_FLAG_REPLACE_EXISTING = 2
44 DBUS_NAME_FLAG_DO_NOT_QUEUE = 4
45 DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER = 1
46 DBUS_REQUEST_NAME_REPLY_IN_QUEUE = 2
47 DBUS_REQUEST_NAME_REPLY_EXISTS = 3
48 DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER = 4
49
50 dbus_bus_name = "tk.tetteke.FukuiNoNamari"
51 dbus_object_path = "/tk/tetteke/FukuiNoNamari"
52 dbus_object.dbus_interface_name = "tk.tetteke.FukuiNoNamari"
53
54 def usage():
55     print """usage: hage1 uri"""
56
57 def getoption():
58     try:
59         opts, args = getopt.getopt(
60             sys.argv[1:], "h", ["help"])
61     except getopt.GetoptError, msg:
62         print msg
63         usage()
64         sys.exit(2)
65     for option, value in opts:
66         if option in ("-h", "--help"):
67             usage()
68             sys.exit()
69     return args
70
71     
72 if __name__ == "__main__":
73
74     # get option
75     uris = getoption()
76
77     # check if an instance exists
78     session_bus = dbus.SessionBus()
79     dbus_proxy_object = session_bus.get_object(
80         "org.freedesktop.DBus", "/org/freedesktop/DBus")
81     dbus_iface = dbus.Interface(dbus_proxy_object, "org.freedesktop.DBus")
82     req_name_ret = dbus_iface.RequestName(
83         dbus_bus_name, DBUS_NAME_FLAG_DO_NOT_QUEUE)
84
85     if req_name_ret == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER or \
86        req_name_ret == DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
87         # the instance does not exist. start normally.
88
89         bus_name = dbus.service.BusName(dbus_bus_name, bus=session_bus)
90         obj = dbus_object.DBusFukuiNoNamariObject(bus_name, dbus_object_path)
91
92         gnome.init(APPNAME, APPVERSION)
93         gobject.threads_init()
94
95         # open some windows
96         if uris:
97             from FukuiNoNamari.BbsType import bbs_type_judge_uri
98             from FukuiNoNamari.BbsType import bbs_type_exception
99             for uri in uris:
100                 try:
101                     uri_opener.open_uri(uri)
102                 except bbs_type_exception.BbsTypeError, msg:
103                     print msg
104
105         session.start()
106
107     else:
108         # the instance exists. send message and exit.
109
110         proxy_object = session_bus.get_object(dbus_bus_name, dbus_object_path)
111         iface = dbus.Interface(proxy_object, dbus_object.dbus_interface_name)
112
113         # open some windows
114         if uris:
115             from FukuiNoNamari.BbsType import bbs_type_judge_uri
116             from FukuiNoNamari.BbsType import bbs_type_exception
117             for uri in uris:
118                 try:
119                     iface.open_uri(uri)
120                 except bbs_type_exception.BbsTypeError, msg:
121                     print msg