Commits

Anonymous committed 93aa84e

Re-use existent terminal through DBus

Comments (0)

Files changed (1)

 import difflib
 import unicodedata
 import urllib
+import dbus
+import dbus.service
+
+from dbus.mainloop.glib import DBusGMainLoop
+
 
 synced_terminal = []
 
 
 url_re = re.compile('(' + '|'.join((url_re_str, www_ftp_re_str)) + ')')
 
+DBUS_BUS_NAME_BASE = 'org.ny.Yomogi'
+DBUS_OBJECT_PATH = '/org/ny/Yomogi'
+DBUS_INTERFACE_NAME = 'org.ny.Yomogi'
+
 class VirtualTerminal(gtk.HBox):
     default_encoding = 'UTF-8'
     #default_encoding = 'EUC-JP'
         terminal.set_highlight_pos(base_highlight_pos)
 
 
+class DBusInterface(dbus.service.Object):
+    '''DBus API to control Terminals
+    '''
+    def __init__(self):
+        bus_name = dbus.service.BusName(make_bus_name_for_display(),
+                                        bus=dbus.SessionBus())
+        super(DBusInterface, self).__init__(bus_name, DBUS_OBJECT_PATH)
+
+    @dbus.service.method(DBUS_INTERFACE_NAME, in_signature='a{ss}')
+    def open_terminal(self, opts):
+        MainWindow(opts)
+
+
+def make_bus_name_for_display():
+    display_name = gtk.gdk.display_get_default().get_name()
+    disp = ''.join(c.isalnum() and c or '_' for c in display_name)
+    return DBUS_BUS_NAME_BASE + disp
+
+
+def get_dbus_interface():
+    bus = dbus.SessionBus()
+    sess_obj = bus.get_object(make_bus_name_for_display(), DBUS_OBJECT_PATH)
+    return dbus.Interface(sess_obj, DBUS_INTERFACE_NAME)
+
+
+
 def main():
     from optparse import OptionParser
 
                        'inside the terminal.')
     op.add_option('--working-directory', dest='work_dir',
                   help='Set the terminal\'s working directory')
+    op.add_option('--disable-factory',  dest='use_dbus',
+                  action='store_false', default=True,
+                  help='Do not register with the activation nameserver,'
+                       'do not re-use an active terminal')
 
     opt, args = op.parse_args()
     kwargs = {}
     if opt.execute is not None:
         kwargs['command'] = args
 
-    MainWindow(kwargs)
-    gtk.main()
+    if opt.use_dbus:
+        DBusGMainLoop(set_as_default=True)
+
+        try:
+            # try to re-use existent terminal
+            interface = get_dbus_interface()
+            interface.open_terminal(kwargs)
+
+        except dbus.DBusException:
+            # start new terminal
+            DBusInterface()
+
+            MainWindow(kwargs)
+
+            gtk.main()
+
+    else:
+        MainWindow(kwargs)
+        gtk.main()
 
 
 if __name__ == '__main__':