Diferentiate adding an already managed torrent from adding a new, unmanaged torrent, to the session.

This commit is contained in:
Pedro Algarvio 2010-12-08 19:33:05 +00:00
parent 14ec9464aa
commit 6cefb49f28
8 changed files with 56 additions and 15 deletions

View File

@ -476,10 +476,24 @@ class TorrentManager(component.Component):
# Save the session state # Save the session state
self.save_state() self.save_state()
# Emit signals
if torrent_info and state is None:
# Emit the torrent_added signal # Emit the torrent_added signal
component.get("EventManager").emit(TorrentAddedEvent(torrent.torrent_id)) 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 return torrent.torrent_id
def load_torrent(self, torrent_id): def load_torrent(self, torrent_id):

View File

@ -86,6 +86,18 @@ class TorrentAddedEvent(DelugeEvent):
""" """
self._args = [torrent_id] 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): class TorrentRemovedEvent(DelugeEvent):
""" """
Emitted when a torrent has been removed from the session. Emitted when a torrent has been removed from the session.

View File

@ -55,6 +55,7 @@ EXECUTE_COMMAND = 2
EVENT_MAP = { EVENT_MAP = {
"complete": "TorrentFinishedEvent", "complete": "TorrentFinishedEvent",
"loaded": "TorrentLoadedEvent",
"added": "TorrentAddedEvent" "added": "TorrentAddedEvent"
} }

View File

@ -110,7 +110,8 @@ class Core(CorePluginBase):
self.clean_initial_config() 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) component.get("EventManager").register_event_handler("TorrentRemovedEvent", self.post_torrent_remove)
#register tree: #register tree:
@ -121,6 +122,9 @@ class Core(CorePluginBase):
def disable(self): def disable(self):
self.plugin.deregister_status_field("label") self.plugin.deregister_status_field("label")
component.get("FilterManager").deregister_tree_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): def update(self):
pass pass
@ -129,8 +133,8 @@ class Core(CorePluginBase):
return dict( [(label, 0) for label in self.labels.keys()]) return dict( [(label, 0) for label in self.labels.keys()])
## Plugin hooks ## ## Plugin hooks ##
def post_torrent_add(self, torrent_id): def post_torrent_load_or_add(self, torrent_id):
log.debug("post_torrent_add") log.debug("post_torrent_load_or_add")
torrent = self.torrents[torrent_id] torrent = self.torrents[torrent_id]
for label_id, options in self.labels.iteritems(): for label_id, options in self.labels.iteritems():

View File

@ -50,6 +50,7 @@ class EventLog(component.Component):
self.console = component.get("ConsoleUI") self.console = component.get("ConsoleUI")
self.prefix = "{!event!}* " 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("TorrentAddedEvent", self.on_torrent_added_event)
client.register_event_handler("PreTorrentRemovedEvent", self.on_torrent_removed_event) client.register_event_handler("PreTorrentRemovedEvent", self.on_torrent_removed_event)
client.register_event_handler("TorrentStateChangedEvent", self.on_torrent_state_changed_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("PluginEnabledEvent", self.on_plugin_enabled_event)
client.register_event_handler("PluginDisabledEvent", self.on_plugin_disabled_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_added_event(self, torrent_id):
def on_torrent_status(status): def on_torrent_status(status):
self.console.write(self.prefix + "TorrentAdded: {!info!}%s (%s)" % (status["name"], torrent_id)) self.console.write(self.prefix + "TorrentAdded: {!info!}%s (%s)" % (status["name"], torrent_id))

View File

@ -241,7 +241,8 @@ class ConsoleUI(component.Component):
client.core.get_session_state().addCallback(on_session_state) client.core.get_session_state().addCallback(on_session_state)
# Register some event handlers to keep the torrent list up-to-date # 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) client.register_event_handler("TorrentRemovedEvent", self.on_torrent_removed_event)
def update(self): def update(self):
@ -439,7 +440,7 @@ class ConsoleUI(component.Component):
return ret 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): def on_torrent_status(status):
self.torrents.append((torrent_id, status["name"])) self.torrents.append((torrent_id, status["name"]))
client.core.get_torrent_status(torrent_id, ["name"]).addCallback(on_torrent_status) client.core.get_torrent_status(torrent_id, ["name"]).addCallback(on_torrent_status)

View File

@ -257,7 +257,8 @@ class TorrentView(listview.ListView, component.Component):
self.treeview.connect("key-press-event", self.on_key_press_event) self.treeview.connect("key-press-event", self.on_key_press_event)
client.register_event_handler("TorrentStateChangedEvent", self.on_torrentstatechanged_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("TorrentRemovedEvent", self.on_torrentremoved_event)
client.register_event_handler("SessionPausedEvent", self.on_sessionpaused_event) client.register_event_handler("SessionPausedEvent", self.on_sessionpaused_event)
client.register_event_handler("SessionResumedEvent", self.on_sessionresumed_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): def on_drag_drop(self, widget, drag_context, x, y, timestamp):
widget.stop_emission("drag-drop") 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.add_row(torrent_id)
self.mark_dirty(torrent_id) self.mark_dirty(torrent_id)

View File

@ -67,7 +67,8 @@ class SessionProxy(component.Component):
client.register_event_handler("TorrentStateChangedEvent", self.on_torrent_state_changed) client.register_event_handler("TorrentStateChangedEvent", self.on_torrent_state_changed)
client.register_event_handler("TorrentRemovedEvent", self.on_torrent_removed) 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 start(self):
def on_torrent_status(status): def on_torrent_status(status):
@ -84,7 +85,8 @@ class SessionProxy(component.Component):
def stop(self): def stop(self):
client.deregister_event_handler("TorrentStateChangedEvent", self.on_torrent_state_changed) client.deregister_event_handler("TorrentStateChangedEvent", self.on_torrent_state_changed)
client.deregister_event_handler("TorrentRemovedEvent", self.on_torrent_removed) 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 = {} self.torrents = {}
def create_status_dict(self, torrent_ids, keys): def create_status_dict(self, torrent_ids, keys):
@ -239,7 +241,7 @@ class SessionProxy(component.Component):
self.torrents[torrent_id][1]["state"] = state self.torrents[torrent_id][1]["state"] = state
self.cache_times[torrent_id]["state"] = time.time() 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.torrents[torrent_id] = [time.time() - self.cache_time - 1, {}]
self.cache_times[torrent_id] = {} self.cache_times[torrent_id] = {}
def on_status(status): def on_status(status):