diff --git a/deluge/config.py b/deluge/config.py index 187b2ffe1..bbb9dd6ed 100644 --- a/deluge/config.py +++ b/deluge/config.py @@ -138,10 +138,13 @@ class Config: """Returns the entire configuration as a dictionary.""" return self.config - def register_set_function(self, key, function): + def register_set_function(self, key, function, apply_now=True): """Register a function to be run when a config value changes.""" log.debug("Registering function for %s key..", key) self.set_functions[key] = function + # Run the function now if apply_now is set + if apply_now: + self.set_functions[key](key, self.config[key]) return def apply_all(self): diff --git a/deluge/core/core.py b/deluge/core/core.py index 5f668c2e5..714a4edd5 100644 --- a/deluge/core/core.py +++ b/deluge/core/core.py @@ -101,21 +101,6 @@ class Core(dbus.service.Object): # Load metadata extension self.session.add_extension(lt.create_metadata_plugin) - # Start the TorrentManager - self.torrents = TorrentManager(self.session) - - # Load plugins - self.plugins = PluginManager() - - # Start the AlertManager - self.alerts = AlertManager(self.session) - - # Register alert functions - self.alerts.register_handler("torrent_finished_alert", - self.on_alert_torrent_finished) - self.alerts.register_handler("torrent_paused_alert", - self.on_alert_torrent_paused) - # Register set functions in the Config self.config.register_set_function("listen_ports", self.on_set_listen_ports) @@ -141,14 +126,22 @@ class Core(dbus.service.Object): self.on_set_max_download_speed) self.config.register_set_function("max_upload_slots_global", self.on_set_max_upload_slots_global) - self.config.register_set_function("max_connections_per_torrent", - self.on_set_max_connections_per_torrent) - self.config.register_set_function("max_upload_slots_per_torrent", - self.on_set_max_upload_slots_per_torrent) - - # Run all the set functions now to set the config for the session - self.config.apply_all() + + # Start the TorrentManager + self.torrents = TorrentManager(self.session) + # Load plugins + self.plugins = PluginManager() + + # Start the AlertManager + self.alerts = AlertManager(self.session) + + # Register alert functions + self.alerts.register_handler("torrent_finished_alert", + self.on_alert_torrent_finished) + self.alerts.register_handler("torrent_paused_alert", + self.on_alert_torrent_paused) + log.debug("Starting main loop..") self.loop = gobject.MainLoop() self.loop.run() @@ -479,14 +472,6 @@ class Core(dbus.service.Object): log.debug("max_upload_slots_global set to %s..", value) self.session.set_max_uploads(value) - def on_set_max_connections_per_torrent(self, key, value): - log.debug("max_connections_per_torrent set to %s..", value) - self.torrents.set_max_connections(value) - - def on_set_max_upload_slots_per_torrent(self, key, value): - log.debug("max_upload_slots_per_torrent set to %s..", value) - self.torrents.set_max_uploads(value) - ## Alert handlers ## def on_alert_torrent_finished(self, alert): log.debug("on_alert_torrent_finished") diff --git a/deluge/core/torrentmanager.py b/deluge/core/torrentmanager.py index 8a810fb66..ab8478dd8 100644 --- a/deluge/core/torrentmanager.py +++ b/deluge/core/torrentmanager.py @@ -73,6 +73,12 @@ class TorrentManager: # Try to load the state from file self.load_state() + # Register set functions + self.config.register_set_function("max_connections_per_torrent", + self.on_set_max_connections_per_torrent) + self.config.register_set_function("max_upload_slots_per_torrent", + self.on_set_max_upload_slots_per_torrent) + def __del__(self): log.debug("TorrentManager shutting down..") # Save state on shutdown @@ -107,17 +113,19 @@ class TorrentManager: # Get the data from the file try: log.debug("Attempting to open %s for add.", filename) - filedump = open( - os.path.join(self.config["torrentfiles_location"], - filename), "rb").read() + _file = open( + os.path.join( + self.config["torrentfiles_location"], filename), "rb") + filedump = _file.read() + _file.close() except IOError: log.warning("Unable to open %s", filename) return None - + # Bdecode the filedata torrent_filedump = lt.bdecode(filedump) handle = None - + # Make sure we are adding it with the correct allocation method. if compact is None: compact = self.config["compact_allocation"] @@ -154,7 +162,7 @@ class TorrentManager: save_file = open(os.path.join(self.config["torrentfiles_location"], filename), "wb") - save_file.write(filedump) + save_file.write(lt.bencode(torrent_filedump)) save_file.close() except IOError: log.warning("Unable to save torrent file: %s", filename) @@ -302,15 +310,18 @@ class TorrentManager: fastresume.close() except IOError: log.warning("Error trying to save fastresume file") - - def set_max_connections(self, value): + + def on_set_max_connections_per_torrent(self, key, value): """Sets the per-torrent connection limit""" + log.debug("max_connections_per_torrent set to %s..", value) self.max_connections = value for key in self.torrents.keys(): self.torrents[key].handle.set_max_connections(value) - - def set_max_uploads(self, value): + + def on_set_max_upload_slots_per_torrent(self, key, value): """Sets the per-torrent upload slot limit""" + log.debug("max_upload_slots_per_torrent set to %s..", value) self.max_uploads = value for key in self.torrents.keys(): self.torrents[key].handle.set_max_uploads(value) +