diff --git a/scripts/deluge b/scripts/deluge index b7f345a98..ccebe5b6c 100755 --- a/scripts/deluge +++ b/scripts/deluge @@ -39,22 +39,10 @@ from optparse import OptionParser import re import sys -try: - import dbus - dbus_version = getattr(dbus, 'version', (0,0,0)) - if dbus_version >= (0,41,0) and dbus_version < (0,80,0): - import dbus.glib - elif dbus_version >= (0,80,0): - from dbus.mainloop.glib import DBusGMainLoop - DBusGMainLoop(set_as_default=True) - else: - pass -except: dbus_imported = False -else: dbus_imported = True - import deluge import deluge.common import deluge.core +import deluge._dbus as dbus import deluge.interface parser = OptionParser(usage="%prog [options] [torrents to add]", @@ -123,26 +111,21 @@ def start_deluge(): interface.start(get_cmd_line_torrents()) -if dbus_imported: - bus = dbus.SessionBus() - dbus_objects = dbus.Interface(bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus'), 'org.freedesktop.DBus').ListNames() +bus = dbus.SessionBus() - interface = None +dbus_objects = dbus.Interface(bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus'), 'org.freedesktop.DBus').ListNames() - if not "org.deluge_torrent.Deluge" in dbus_objects: - print "no existing Deluge session" - start_deluge() - else: - ## This connects to the deluge interface - print "create proxy object" - proxy = bus.get_object('org.deluge_torrent.Deluge', '/org/deluge_torrent/DelugeObject') - print "create iface" - deluge_iface = dbus.Interface(proxy, 'org.deluge_torrent.Deluge') - print "send to iface" - - for filename in get_cmd_line_torrents(): - deluge_iface.interactive_add_torrent(filename) -else: +if not "org.deluge_torrent.Deluge" in dbus_objects: print "no existing Deluge session" start_deluge() +else: + ## This connects to the deluge interface + print "create proxy object" + proxy = bus.get_object('org.deluge_torrent.Deluge', '/org/deluge_torrent/DelugeObject') + print "create iface" + deluge_iface = dbus.Interface(proxy, 'org.deluge_torrent.Deluge') + print "send to iface" + + for filename in get_cmd_line_torrents(): + deluge_iface.interactive_add_torrent(filename) diff --git a/src/_dbus.py b/src/_dbus.py new file mode 100644 index 000000000..93cc41ca2 --- /dev/null +++ b/src/_dbus.py @@ -0,0 +1,12 @@ +# Import all we will use in deluge +from dbus import Interface, SessionBus, version + +# Code for dbus_importing borrowed from Listen (http://listen-project.org) +# I couldn't figure out how to use dbus without breaking on versions past +# 0.80.0. I finally found a solution by reading the source code from the +# Listen project. +if version >= (0,41,0) and version < (0,80,0): + import dbus.glib +elif version >= (0,80,0): + from dbus.mainloop.glib import DBusGMainLoop + DBusGMainLoop(set_as_default=True) diff --git a/src/ipc_manager.py b/src/ipc_manager.py index 615a3716a..2d8b9d9b2 100644 --- a/src/ipc_manager.py +++ b/src/ipc_manager.py @@ -30,44 +30,17 @@ # this exception statement from your version. If you delete this exception # statement from all source files in the program, then also delete it here. +import deluge._dbus as dbus +import dbus.service -# Code for dbus_importing borrowed from Listen (http://listen-project.org) -# I couldn't figure out how to use dbus without breaking on versions past -# 0.80.0. I finally found a solution by reading the source code from the -# Listen project. -try: - import dbus - import dbus.service - dbus_version = getattr(dbus, 'version', (0,0,0)) - if dbus_version >= (0,41,0) and dbus_version < (0,80,0): - dbus.SessionBus() - import dbus.glib - elif dbus_version >= (0,80,0): - from dbus.mainloop.glib import DBusGMainLoop - DBusGMainLoop(set_as_default=True) - dbus.SessionBus() - else: - pass -except: dbus_imported = False -else: dbus_imported = True +class Manager(dbus.service.Object): + def __init__(self, interface, object_path='/org/deluge_torrent/DelugeObject'): + self.interface = interface + self.bus = dbus.SessionBus() + bus_name = dbus.service.BusName("org.deluge_torrent.Deluge", bus=self.bus) + dbus.service.Object.__init__(self, bus_name, object_path) -if dbus_imported: - class Manager(dbus.service.Object): - def __init__(self, interface, object_path='/org/deluge_torrent/DelugeObject'): - self.interface = interface - self.bus = dbus.SessionBus() - bus_name = dbus.service.BusName("org.deluge_torrent.Deluge", bus=self.bus) - dbus.service.Object.__init__(self, bus_name, object_path) - - ## interactive_add_torrent should only be called from outside the class - @dbus.service.method('org.deluge_torrent.Deluge') - def interactive_add_torrent(self, torrent_file): - self.interface.interactive_add_torrent(torrent_file) -else: - # This is a fallback class in case dbus is not available - class Manager: - def __init__(self, interface, object_path=None): - self.interface = interface - - def interactive_add_torrent(self, torrent_file): - print "I can't do anything with this." + ## interactive_add_torrent should only be called from outside the class + @dbus.service.method('org.deluge_torrent.Deluge') + def interactive_add_torrent(self, torrent_file): + self.interface.interactive_add_torrent(torrent_file)