Make CoreProxy a GObject.

This commit is contained in:
Andrew Resch 2007-12-17 11:14:22 +00:00
parent 60fc0d24fe
commit 68642443a5
2 changed files with 27 additions and 31 deletions

View File

@ -36,6 +36,8 @@ import pickle
import socket import socket
import time import time
import gobject
import deluge.xmlrpclib as xmlrpclib import deluge.xmlrpclib as xmlrpclib
import deluge.common import deluge.common
@ -50,11 +52,6 @@ class cache:
#self.cache_values = {(args, kwargs): (time, ret)} #self.cache_values = {(args, kwargs): (time, ret)}
self.cache_values = {} self.cache_values = {}
self.args = None
self.kwargs = None
self.ret = None
self.time = None
def __call__(self, *__args, **__kw): def __call__(self, *__args, **__kw):
# Turn the arguments into hashable values # Turn the arguments into hashable values
if __args == (): if __args == ():
@ -75,41 +72,34 @@ class cache:
# No return value in cache # No return value in cache
ret = self.func(*__args, **__kw) ret = self.func(*__args, **__kw)
self.cache_values[(args, kw)] = [None, None] self.cache_values[(args, kw)] = [time.time(), ret]
self.cache_values[(args, kw)][1] = ret
self.cache_values[(args, kw)][0] = time.time()
return ret return ret
class CoreProxy: class CoreProxy(gobject.GObject):
__gsignals__ = {
"new_core" : (
gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, []),
"no_core" : (
gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, []),
}
def __init__(self): def __init__(self):
log.debug("CoreProxy init..")
gobject.GObject.__init__(self)
self._uri = None self._uri = None
self._core = None self._core = None
self._on_new_core_callbacks = []
self._on_no_core_callbacks = []
def connect_on_new_core(self, callback):
"""Connect a callback to be called when a new core is connected to."""
self._on_new_core_callbacks.append(callback)
def connect_on_no_core(self, callback):
"""Connect a callback to be called when the current core is disconnected
from."""
self._on_no_core_callbacks.append(callback)
def set_core_uri(self, uri): def set_core_uri(self, uri):
log.info("Setting core uri as %s", uri) log.info("Setting core uri as %s", uri)
if uri == None and self._uri != None: if uri == None and self._uri != None:
for callback in self._on_no_core_callbacks: self.emit("no_core")
callback()
self._uri = None self._uri = None
self._core = None self._core = None
return return
if uri != self._uri and self._uri != None: if uri != self._uri and self._uri != None:
self._core = None self._core = None
for callback in self._on_no_core_callbacks: self.emit("no_core")
callback()
self._uri = uri self._uri = uri
# Get a new core # Get a new core
@ -124,8 +114,7 @@ class CoreProxy:
log.debug("Creating ServerProxy..") log.debug("Creating ServerProxy..")
self._core = xmlrpclib.ServerProxy(self._uri) self._core = xmlrpclib.ServerProxy(self._uri)
# Call any callbacks registered # Call any callbacks registered
for callback in self._on_new_core_callbacks: self.emit("new_core")
callback()
return self._core return self._core
@ -146,11 +135,11 @@ def get_core_plugin(plugin):
def connect_on_new_core(callback): def connect_on_new_core(callback):
"""Connect a callback whenever a new core is connected to.""" """Connect a callback whenever a new core is connected to."""
return _core.connect_on_new_core(callback) return _core.connect("new_core", callback)
def connect_on_no_core(callback): def connect_on_no_core(callback):
"""Connect a callback whenever the core is disconnected from.""" """Connect a callback whenever the core is disconnected from."""
return _core.connect_on_no_core(callback) return _core.connect("no_core", callback)
def set_core_uri(uri): def set_core_uri(uri):
"""Sets the core uri""" """Sets the core uri"""

View File

@ -117,8 +117,8 @@ class GtkUI:
self.dbusinterface = DbusInterface(args) self.dbusinterface = DbusInterface(args)
# We make sure that the UI components start once we get a core URI # We make sure that the UI components start once we get a core URI
client.connect_on_new_core(component.start) client.connect_on_new_core(self._on_new_core)
client.connect_on_no_core(component.stop) client.connect_on_no_core(self._on_no_core)
# Initialize various components of the gtkui # Initialize various components of the gtkui
self.mainwindow = MainWindow() self.mainwindow = MainWindow()
@ -165,3 +165,10 @@ class GtkUI:
del self.signal_receiver del self.signal_receiver
del self.plugins del self.plugins
del deluge.configmanager del deluge.configmanager
def _on_new_core(self, data):
component.start()
def _on_no_core(self, data):
component.stop()