summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn Garland <johnnybg+deluge@gmail.com>2012-03-10 23:57:15 +1100
committerJohn Garland <johnnybg+deluge@gmail.com>2012-03-12 23:51:47 +1100
commitf102e988c9df02dcf11c40a3a352ea8d98f4a12d (patch)
treef437d4ff8f514686ad66d1ebddb5c531c3633af5
parent78df634fed4b6c5511bbe7370435bb326fdf2a33 (diff)
downloaddeluge-f102e988c9df02dcf11c40a3a352ea8d98f4a12d.zip
deluge-f102e988c9df02dcf11c40a3a352ea8d98f4a12d.tar.gz
deluge-f102e988c9df02dcf11c40a3a352ea8d98f4a12d.tar.bz2
Preserve order when moving multiple torrents in the queue
-rw-r--r--deluge/core/core.py50
1 files changed, 34 insertions, 16 deletions
diff --git a/deluge/core/core.py b/deluge/core/core.py
index 43ac424..856a66b 100644
--- a/deluge/core/core.py
+++ b/deluge/core/core.py
@@ -750,7 +750,8 @@ class Core(component.Component):
@export
def queue_top(self, torrent_ids):
log.debug("Attempting to queue %s to top", torrent_ids)
- for torrent_id in torrent_ids:
+ # torrent_ids must be sorted in reverse before moving to preserve order
+ for torrent_id in sorted(torrent_ids, key=self.torrentmanager.get_queue_position, reverse=True):
try:
# If the queue method returns True, then we should emit a signal
if self.torrentmanager.queue_top(torrent_id):
@@ -761,31 +762,48 @@ class Core(component.Component):
@export
def queue_up(self, torrent_ids):
log.debug("Attempting to queue %s to up", torrent_ids)
+ torrents = ((self.torrentmanager.get_queue_position(torrent_id), torrent_id) for torrent_id in torrent_ids)
+ torrent_moved = True
+ prev_queue_position = None
#torrent_ids must be sorted before moving.
- for torrent_id in sorted(torrent_ids, key=self.torrentmanager.get_queue_position):
- try:
- # If the queue method returns True, then we should emit a signal
- if self.torrentmanager.queue_up(torrent_id):
- component.get("EventManager").emit(TorrentQueueChangedEvent())
- except KeyError:
- log.warning("torrent_id: %s does not exist in the queue", torrent_id)
+ for queue_position, torrent_id in sorted(torrents):
+ # Move the torrent if and only if there is space (by not moving it we preserve the order)
+ if torrent_moved or queue_position - prev_queue_position > 1:
+ try:
+ torrent_moved = self.torrentmanager.queue_up(torrent_id)
+ except KeyError:
+ log.warning("torrent_id: %s does not exist in the queue", torrent_id)
+ # If the torrent moved, then we should emit a signal
+ if torrent_moved:
+ component.get("EventManager").emit(TorrentQueueChangedEvent())
+ else:
+ prev_queue_position = queue_position
@export
def queue_down(self, torrent_ids):
log.debug("Attempting to queue %s to down", torrent_ids)
+ torrents = ((self.torrentmanager.get_queue_position(torrent_id), torrent_id) for torrent_id in torrent_ids)
+ torrent_moved = True
+ prev_queue_position = None
#torrent_ids must be sorted before moving.
- for torrent_id in sorted(torrent_ids, key=self.torrentmanager.get_queue_position, reverse=True):
- try:
- # If the queue method returns True, then we should emit a signal
- if self.torrentmanager.queue_down(torrent_id):
- component.get("EventManager").emit(TorrentQueueChangedEvent())
- except KeyError:
- log.warning("torrent_id: %s does not exist in the queue", torrent_id)
+ for queue_position, torrent_id in sorted(torrents, reverse=True):
+ # Move the torrent if and only if there is space (by not moving it we preserve the order)
+ if torrent_moved or prev_queue_position - queue_position > 1:
+ try:
+ torrent_moved = self.torrentmanager.queue_down(torrent_id)
+ except KeyError:
+ log.warning("torrent_id: %s does not exist in the queue", torrent_id)
+ # If the torrent moved, then we should emit a signal
+ if torrent_moved:
+ component.get("EventManager").emit(TorrentQueueChangedEvent())
+ else:
+ prev_queue_position = queue_position
@export
def queue_bottom(self, torrent_ids):
log.debug("Attempting to queue %s to bottom", torrent_ids)
- for torrent_id in torrent_ids:
+ # torrent_ids must be sorted before moving to preserve order
+ for torrent_id in sorted(torrent_ids, key=self.torrentmanager.get_queue_position):
try:
# If the queue method returns True, then we should emit a signal
if self.torrentmanager.queue_bottom(torrent_id):