OSDN Git Service

Add dbus support.
authorAiwota Programmer <aiwotaprog@tetteke.tk>
Mon, 21 Aug 2006 14:36:48 +0000 (23:36 +0900)
committerAiwota Programmer <aiwotaprog@tetteke.tk>
Mon, 21 Aug 2006 14:36:48 +0000 (23:36 +0900)
src/Hage1/dbus_object.py [new file with mode: 0644]
src/hage1

diff --git a/src/Hage1/dbus_object.py b/src/Hage1/dbus_object.py
new file mode 100644 (file)
index 0000000..0860638
--- /dev/null
@@ -0,0 +1,44 @@
+# Copyright (C) 2006 by Aiwota Programmer
+# aiwotaprog@tetteke.tk
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+import dbus
+import dbus.service
+if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
+    import dbus.glib
+
+import board_window
+import thread_window
+import brdlist_window
+
+dbus_interface_name = "tk.tetteke.Hage1"
+
+class DBusHage1Object(dbus.service.Object):
+
+    def __init__(self, bus_name, object_path):
+        dbus.service.Object.__init__(self, bus_name, object_path)
+
+    @dbus.service.method(dbus_interface_name)
+    def open_board(self, bbs, board):
+        board_window.open_board(bbs, board)
+
+    @dbus.service.method(dbus_interface_name)
+    def open_thread(self, bbs, board, thread):
+        thread_window.open_thread(bbs, board, thread)
+
+    @dbus.service.method(dbus_interface_name)
+    def open_brdlist(self, bbs):
+        brdlist_window.open_brdlist(bbs)
index 48aef07..f016cb8 100755 (executable)
--- a/src/hage1
+++ b/src/hage1
@@ -24,15 +24,32 @@ import gnome.ui
 import gobject
 import getopt
 import sys
+import dbus
+import dbus.service
 
 from Hage1 import thread_window
 from Hage1 import board_window
 from Hage1 import brdlist_window
 from Hage1 import brdlist
 from Hage1 import config
+from Hage1 import dbus_object
+
 APPNAME = 'Hage1'
 APPVERSION = '0.1'
 
+# where are these values ?
+DBUS_NAME_FLAG_ALLOW_REPLACEMENT = 1
+DBUS_NAME_FLAG_REPLACE_EXISTING = 2
+DBUS_NAME_FLAG_DO_NOT_QUEUE = 4
+DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER = 1
+DBUS_REQUEST_NAME_REPLY_IN_QUEUE = 2
+DBUS_REQUEST_NAME_REPLY_EXISTS = 3
+DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER = 4
+
+dbus_bus_name = "tk.tetteke.Hage1"
+dbus_object_path = "/tk/tetteke/Hage1"
+dbus_object.dbus_interface_name = "tk.tetteke.Hage1"
+
 def usage():
     print """usage: hage1 [-s bbs | -b board | -t thread]
 -s, --bbs bbs       : bbs id
@@ -69,19 +86,49 @@ if __name__ == "__main__":
     # get option
     bbs, board, thread = getoption()
 
-    gnome.init(APPNAME, APPVERSION)
-    gobject.threads_init()
-    config.APPNAME = APPNAME
-    # init brdlist after setting config.APPNAME
-    brdlist.init()
-
-    if not bbs:
-        brdlist_window.open_brdlist("2ch")
-    elif not board:
-        brdlist_window.open_brdlist(bbs)
-    elif not thread:
-        board_window.open_board(bbs, board)
+    # check if an instance exists
+    session_bus = dbus.SessionBus()
+    dbus_proxy_object = session_bus.get_object(
+        "org.freedesktop.DBus", "/org/freedesktop/DBus")
+    dbus_iface = dbus.Interface(dbus_proxy_object, "org.freedesktop.DBus")
+    req_name_ret = dbus_iface.RequestName(
+        dbus_bus_name, DBUS_NAME_FLAG_DO_NOT_QUEUE)
+
+    if req_name_ret == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER or \
+       req_name_ret == DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
+        # the instance does not exist. start normally.
+
+        bus_name = dbus.service.BusName(dbus_bus_name, bus=session_bus)
+        obj = dbus_object.DBusHage1Object(bus_name, dbus_object_path)
+
+        gnome.init(APPNAME, APPVERSION)
+        gobject.threads_init()
+        config.APPNAME = APPNAME
+
+        # init brdlist after setting config.APPNAME
+        brdlist.init()
+
+        if not bbs:
+            brdlist_window.open_brdlist("2ch")
+        elif not board:
+            brdlist_window.open_brdlist(bbs)
+        elif not thread:
+            board_window.open_board(bbs, board)
+        else:
+            thread_window.open_thread(bbs, board, thread)
+
+        gtk.main()
     else:
-        thread_window.open_thread(bbs, board, thread)
+        # the instance exists. send message and exit.
+
+        proxy_object = session_bus.get_object(dbus_bus_name, dbus_object_path)
+        iface = dbus.Interface(proxy_object, dbus_object.dbus_interface_name)
 
-    gtk.main()
+        if not bbs:
+            iface.open_brdlist("2ch")
+        elif not board:
+            iface.open_brdlist(bbs)
+        elif not thread:
+            iface.open_board(bbs, board)
+        else:
+            iface.open_thread(bbs, board, thread)