OSDN Git Service

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