[Core] Emit TorrentStateChangedEvent in update_state

This commit is contained in:
Calum Lind 2015-09-29 19:23:20 +01:00
parent 084329f9f1
commit 824067e238
3 changed files with 14 additions and 30 deletions

View File

@ -598,35 +598,35 @@ class Torrent(object):
def update_state(self): def update_state(self):
"""Updates the state, based on libtorrent's torrent state""" """Updates the state, based on libtorrent's torrent state"""
status = self.handle.status() status = self.handle.status()
session_is_paused = component.get("Core").session.is_paused() session_paused = component.get("Core").session.is_paused()
old_state = self.state
if status.error or self.error_statusmsg: if status.error or self.error_statusmsg:
self.state = "Error" self.state = "Error"
# This will be reverted upon resuming. # This will be reverted upon resuming.
self.handle.auto_managed(False) self.handle.auto_managed(False)
if not status.paused: if not status.paused:
self.handle.pause() self.handle.pause()
if status.error: if status.error:
self.set_error_statusmsg(decode_string(status.error)) self.set_error_statusmsg(decode_string(status.error))
log.debug("Error state from lt: %s", self.error_statusmsg) log.debug("Error state from lt: %s", self.error_statusmsg)
else: else:
# As this is not a libtorrent Error we should emit a state changed event
component.get("EventManager").emit(TorrentStateChangedEvent(self.torrent_id, "Error"))
log.debug("Error state forced by Deluge, error_statusmsg: %s", self.error_statusmsg) log.debug("Error state forced by Deluge, error_statusmsg: %s", self.error_statusmsg)
self.set_status_message(self.error_statusmsg) self.set_status_message(self.error_statusmsg)
elif self.moving_storage: elif self.moving_storage:
self.state = "Moving" self.state = "Moving"
elif not session_is_paused and status.paused and status.auto_managed: elif not session_paused and status.paused and status.auto_managed:
self.state = "Queued" self.state = "Queued"
elif session_is_paused or status.paused: elif session_paused or status.paused:
self.state = "Paused" self.state = "Paused"
else: else:
self.state = LT_TORRENT_STATE_MAP.get(str(status.state), str(status.state)) self.state = LT_TORRENT_STATE_MAP.get(str(status.state), str(status.state))
if self.state != old_state:
component.get("EventManager").emit(TorrentStateChangedEvent(self.torrent_id, self.state))
if log.isEnabledFor(logging.DEBUG): if log.isEnabledFor(logging.DEBUG):
log.debug("State from lt was: %s | Session is paused: %s", status.state, session_is_paused) log.debug("State from lt was: %s | Session is paused: %s\nTorrent state set from '%s' to '%s' (%s)",
log.debug("Torrent state set to '%s' (%s)", self.state, self.torrent_id) status.state, session_paused, old_state, self.state, self.torrent_id)
def set_status_message(self, message): def set_status_message(self, message):
"""Sets the torrent status message. """Sets the torrent status message.

View File

@ -28,8 +28,7 @@ from deluge.core.authmanager import AUTH_LEVEL_ADMIN
from deluge.core.torrent import Torrent, TorrentOptions, sanitize_filepath from deluge.core.torrent import Torrent, TorrentOptions, sanitize_filepath
from deluge.error import InvalidTorrentError from deluge.error import InvalidTorrentError
from deluge.event import (PreTorrentRemovedEvent, SessionStartedEvent, TorrentAddedEvent, TorrentFileCompletedEvent, from deluge.event import (PreTorrentRemovedEvent, SessionStartedEvent, TorrentAddedEvent, TorrentFileCompletedEvent,
TorrentFileRenamedEvent, TorrentFinishedEvent, TorrentRemovedEvent, TorrentResumedEvent, TorrentFileRenamedEvent, TorrentFinishedEvent, TorrentRemovedEvent, TorrentResumedEvent)
TorrentStateChangedEvent)
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -888,12 +887,7 @@ class TorrentManager(component.Component):
torrent = self.torrents[torrent_id] torrent = self.torrents[torrent_id]
except (RuntimeError, KeyError): except (RuntimeError, KeyError):
return return
# Set the torrent state
old_state = torrent.state
torrent.update_state() torrent.update_state()
if torrent.state != old_state:
component.get("EventManager").emit(TorrentStateChangedEvent(torrent_id, torrent.state))
# Write the fastresume file if we are not waiting on a bulk write # Write the fastresume file if we are not waiting on a bulk write
if torrent_id not in self.waiting_on_resume_data: if torrent_id not in self.waiting_on_resume_data:
self.save_resume_data((torrent_id,)) self.save_resume_data((torrent_id,))
@ -907,14 +901,12 @@ class TorrentManager(component.Component):
except (RuntimeError, KeyError): except (RuntimeError, KeyError):
return return
# Check to see if we're forcing a recheck and set it back to paused # Check to see if we're forcing a recheck and set it back to paused if necessary.
# if necessary
if torrent.forcing_recheck: if torrent.forcing_recheck:
torrent.forcing_recheck = False torrent.forcing_recheck = False
if torrent.forcing_recheck_paused: if torrent.forcing_recheck_paused:
torrent.handle.pause() torrent.handle.pause()
# Set the torrent state
torrent.update_state() torrent.update_state()
def on_alert_tracker_reply(self, alert): def on_alert_tracker_reply(self, alert):
@ -1016,11 +1008,7 @@ class TorrentManager(component.Component):
torrent = self.torrents[torrent_id] torrent = self.torrents[torrent_id]
except (RuntimeError, KeyError): except (RuntimeError, KeyError):
return return
old_state = torrent.state
torrent.update_state() torrent.update_state()
if torrent.state != old_state:
# We need to emit a TorrentStateChangedEvent too
component.get("EventManager").emit(TorrentStateChangedEvent(torrent_id, torrent.state))
component.get("EventManager").emit(TorrentResumedEvent(torrent_id)) component.get("EventManager").emit(TorrentResumedEvent(torrent_id))
def on_alert_state_changed(self, alert): def on_alert_state_changed(self, alert):
@ -1035,18 +1023,12 @@ class TorrentManager(component.Component):
except (RuntimeError, KeyError): except (RuntimeError, KeyError):
return return
old_state = torrent.state
torrent.update_state() torrent.update_state()
# Torrent may need to download data after checking. # Torrent may need to download data after checking.
if torrent.state in ('Checking', 'Checking Resume Data', 'Downloading'): if torrent.state in ('Checking', 'Downloading'):
torrent.is_finished = False torrent.is_finished = False
self.queued_torrents.add(torrent_id) self.queued_torrents.add(torrent_id)
# Only emit a state changed event if the state has actually changed
if torrent.state != old_state:
component.get("EventManager").emit(TorrentStateChangedEvent(torrent_id, torrent.state))
def on_alert_save_resume_data(self, alert): def on_alert_save_resume_data(self, alert):
"""Alert handler for libtorrent save_resume_data_alert""" """Alert handler for libtorrent save_resume_data_alert"""
if log.isEnabledFor(logging.DEBUG): if log.isEnabledFor(logging.DEBUG):

View File

@ -25,6 +25,8 @@ def get(key):
return core return core
elif key is "RPCServer": elif key is "RPCServer":
return rpcserver return rpcserver
elif key is "EventManager":
return core.eventmanager
else: else:
return None return None