From 6cefb49f2874fd6987133b18da64b15735c6f8e7 Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Wed, 8 Dec 2010 19:33:05 +0000 Subject: [PATCH] Diferentiate adding an already managed torrent from adding a new, unmanaged torrent, to the session. --- deluge/core/torrentmanager.py | 24 +++++++++++++++++++----- deluge/event.py | 12 ++++++++++++ deluge/plugins/execute/execute/core.py | 1 + deluge/plugins/label/label/core.py | 10 +++++++--- deluge/ui/console/eventlog.py | 6 ++++++ deluge/ui/console/main.py | 5 +++-- deluge/ui/gtkui/torrentview.py | 5 +++-- deluge/ui/sessionproxy.py | 8 +++++--- 8 files changed, 56 insertions(+), 15 deletions(-) diff --git a/deluge/core/torrentmanager.py b/deluge/core/torrentmanager.py index 93f0a9794..7f483708a 100644 --- a/deluge/core/torrentmanager.py +++ b/deluge/core/torrentmanager.py @@ -476,10 +476,24 @@ class TorrentManager(component.Component): # Save the session state self.save_state() - # Emit the torrent_added signal - component.get("EventManager").emit(TorrentAddedEvent(torrent.torrent_id)) + # Emit signals + if torrent_info and state is None: + # Emit the torrent_added signal + component.get("EventManager").emit( + TorrentAddedEvent(torrent.torrent_id) + ) + signal_type = "added" + else: + # Emit the torrent_loaded signal + component.get("EventManager").emit( + TorrentLoadedEvent(torrent.torrent_id) + ) + signal_type = "loaded" - log.info("Torrent %s added by user: %s", torrent.get_status(["name"])["name"], component.get("RPCServer").get_session_user()) + log.info("Torrent %s %s by user: %s", + torrent.get_status(["name"])["name"], + signal_type, + component.get("RPCServer").get_session_user()) return torrent.torrent_id def load_torrent(self, torrent_id): @@ -857,14 +871,14 @@ class TorrentManager(component.Component): torrent = self.torrents[str(alert.handle.info_hash())] except: return - + # Check to see if we're forcing a recheck and set it back to paused # if necessary if torrent.forcing_recheck: torrent.forcing_recheck = False if torrent.forcing_recheck_paused: torrent.handle.pause() - + # Set the torrent state torrent.update_state() diff --git a/deluge/event.py b/deluge/event.py index 62aa011d0..d2c83c92a 100644 --- a/deluge/event.py +++ b/deluge/event.py @@ -86,6 +86,18 @@ class TorrentAddedEvent(DelugeEvent): """ self._args = [torrent_id] +class TorrentLoadedEvent(DelugeEvent): + """ + Emitted when an already managed torrent is loaded successfully and added to + the session. + """ + def __init__(self, torrent_id): + """ + :param torrent_id: the torrent_id of the torrent that was added + :type torrent_id: string + """ + self._args = [torrent_id] + class TorrentRemovedEvent(DelugeEvent): """ Emitted when a torrent has been removed from the session. diff --git a/deluge/plugins/execute/execute/core.py b/deluge/plugins/execute/execute/core.py index 4c5e66005..bdbed5bf5 100644 --- a/deluge/plugins/execute/execute/core.py +++ b/deluge/plugins/execute/execute/core.py @@ -55,6 +55,7 @@ EXECUTE_COMMAND = 2 EVENT_MAP = { "complete": "TorrentFinishedEvent", + "loaded": "TorrentLoadedEvent", "added": "TorrentAddedEvent" } diff --git a/deluge/plugins/label/label/core.py b/deluge/plugins/label/label/core.py index 70b8e3554..d77868cb4 100644 --- a/deluge/plugins/label/label/core.py +++ b/deluge/plugins/label/label/core.py @@ -110,7 +110,8 @@ class Core(CorePluginBase): self.clean_initial_config() - component.get("EventManager").register_event_handler("TorrentAddedEvent", self.post_torrent_add) + component.get("EventManager").register_event_handler("TorrentLoadedEvent", self.post_torrent_load_or_add) + component.get("EventManager").register_event_handler("TorrentAddedEvent", self.post_torrent_load_or_add) component.get("EventManager").register_event_handler("TorrentRemovedEvent", self.post_torrent_remove) #register tree: @@ -121,6 +122,9 @@ class Core(CorePluginBase): def disable(self): self.plugin.deregister_status_field("label") component.get("FilterManager").deregister_tree_field("label") + component.get("EventManager").deregister_event_handler("TorrentLoadedEvent", self.post_torrent_load_or_add) + component.get("EventManager").deregister_event_handler("TorrentAddedEvent", self.post_torrent_load_or_add) + component.get("EventManager").deregister_event_handler("TorrentRemovedEvent", self.post_torrent_remove) def update(self): pass @@ -129,8 +133,8 @@ class Core(CorePluginBase): return dict( [(label, 0) for label in self.labels.keys()]) ## Plugin hooks ## - def post_torrent_add(self, torrent_id): - log.debug("post_torrent_add") + def post_torrent_load_or_add(self, torrent_id): + log.debug("post_torrent_load_or_add") torrent = self.torrents[torrent_id] for label_id, options in self.labels.iteritems(): diff --git a/deluge/ui/console/eventlog.py b/deluge/ui/console/eventlog.py index 91e258ae1..f9b780970 100644 --- a/deluge/ui/console/eventlog.py +++ b/deluge/ui/console/eventlog.py @@ -50,6 +50,7 @@ class EventLog(component.Component): self.console = component.get("ConsoleUI") self.prefix = "{!event!}* " + client.register_event_handler("TorrentLoadedEvent", self.on_torrent_loaded_event) client.register_event_handler("TorrentAddedEvent", self.on_torrent_added_event) client.register_event_handler("PreTorrentRemovedEvent", self.on_torrent_removed_event) client.register_event_handler("TorrentStateChangedEvent", self.on_torrent_state_changed_event) @@ -61,6 +62,11 @@ class EventLog(component.Component): client.register_event_handler("PluginEnabledEvent", self.on_plugin_enabled_event) client.register_event_handler("PluginDisabledEvent", self.on_plugin_disabled_event) + def on_torrent_loaded_event(self, torrent_id): + def on_torrent_status(status): + self.console.write(self.prefix + "TorrentLoaded: {!info!}%s (%s)" % (status["name"], torrent_id)) + client.core.get_torrent_status(torrent_id, ["name"]).addCallback(on_torrent_status) + def on_torrent_added_event(self, torrent_id): def on_torrent_status(status): self.console.write(self.prefix + "TorrentAdded: {!info!}%s (%s)" % (status["name"], torrent_id)) diff --git a/deluge/ui/console/main.py b/deluge/ui/console/main.py index 8235ee907..0c05138f4 100644 --- a/deluge/ui/console/main.py +++ b/deluge/ui/console/main.py @@ -241,7 +241,8 @@ class ConsoleUI(component.Component): client.core.get_session_state().addCallback(on_session_state) # Register some event handlers to keep the torrent list up-to-date - client.register_event_handler("TorrentAddedEvent", self.on_torrent_added_event) + client.register_event_handler("TorrentLoadedEvent", self.on_torrent_loaded_or_added_event) + client.register_event_handler("TorrentAddedEvent", self.on_torrent_loaded_or_added_event) client.register_event_handler("TorrentRemovedEvent", self.on_torrent_removed_event) def update(self): @@ -439,7 +440,7 @@ class ConsoleUI(component.Component): return ret - def on_torrent_added_event(self, torrent_id): + def on_torrent_loaded_or_added_event(self, torrent_id): def on_torrent_status(status): self.torrents.append((torrent_id, status["name"])) client.core.get_torrent_status(torrent_id, ["name"]).addCallback(on_torrent_status) diff --git a/deluge/ui/gtkui/torrentview.py b/deluge/ui/gtkui/torrentview.py index 5f1e45228..fc25b7108 100644 --- a/deluge/ui/gtkui/torrentview.py +++ b/deluge/ui/gtkui/torrentview.py @@ -257,7 +257,8 @@ class TorrentView(listview.ListView, component.Component): self.treeview.connect("key-press-event", self.on_key_press_event) client.register_event_handler("TorrentStateChangedEvent", self.on_torrentstatechanged_event) - client.register_event_handler("TorrentAddedEvent", self.on_torrentadded_event) + client.register_event_handler("TorrentLoadedEvent", self.on_torrent_loaded_or_added_event) + client.register_event_handler("TorrentAddedEvent", self.on_torrent_loaded_or_added_event) client.register_event_handler("TorrentRemovedEvent", self.on_torrentremoved_event) client.register_event_handler("SessionPausedEvent", self.on_sessionpaused_event) client.register_event_handler("SessionResumedEvent", self.on_sessionresumed_event) @@ -512,7 +513,7 @@ class TorrentView(listview.ListView, component.Component): def on_drag_drop(self, widget, drag_context, x, y, timestamp): widget.stop_emission("drag-drop") - def on_torrentadded_event(self, torrent_id): + def on_torrent_loaded_or_added_event(self, torrent_id): self.add_row(torrent_id) self.mark_dirty(torrent_id) diff --git a/deluge/ui/sessionproxy.py b/deluge/ui/sessionproxy.py index 50b8af4ee..dc77b6819 100644 --- a/deluge/ui/sessionproxy.py +++ b/deluge/ui/sessionproxy.py @@ -67,7 +67,8 @@ class SessionProxy(component.Component): client.register_event_handler("TorrentStateChangedEvent", self.on_torrent_state_changed) client.register_event_handler("TorrentRemovedEvent", self.on_torrent_removed) - client.register_event_handler("TorrentAddedEvent", self.on_torrent_added) + client.register_event_handler("TorrentLoadedEvent", self.on_torrent_loaded_or_added) + client.register_event_handler("TorrentAddedEvent", self.on_torrent_loaded_or_added) def start(self): def on_torrent_status(status): @@ -84,7 +85,8 @@ class SessionProxy(component.Component): def stop(self): client.deregister_event_handler("TorrentStateChangedEvent", self.on_torrent_state_changed) client.deregister_event_handler("TorrentRemovedEvent", self.on_torrent_removed) - client.deregister_event_handler("TorrentAddedEvent", self.on_torrent_added) + client.deregister_event_handler("TorrentLoadedEvent", self.on_torrent_loaded_or_added) + client.deregister_event_handler("TorrentAddedEvent", self.on_torrent_loaded_or_added) self.torrents = {} def create_status_dict(self, torrent_ids, keys): @@ -239,7 +241,7 @@ class SessionProxy(component.Component): self.torrents[torrent_id][1]["state"] = state self.cache_times[torrent_id]["state"] = time.time() - def on_torrent_added(self, torrent_id): + def on_torrent_loaded_or_added(self, torrent_id): self.torrents[torrent_id] = [time.time() - self.cache_time - 1, {}] self.cache_times[torrent_id] = {} def on_status(status):