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:46:19 +1100
commitb9030dfb8b83c9627e6817878ee30010b45bbd43 (patch)
treedb7b4132d1fb3624da7dcfde18a9ec822692e907
parent3528549430914fde5bb8bd93819733ffca316b69 (diff)
downloaddeluge-b9030dfb8b83c9627e6817878ee30010b45bbd43.tar.gz
deluge-b9030dfb8b83c9627e6817878ee30010b45bbd43.tar.bz2
deluge-b9030dfb8b83c9627e6817878ee30010b45bbd43.zip
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 d4a6d0128..4bda8113a 100644
--- a/deluge/core/core.py
+++ b/deluge/core/core.py
@@ -706,7 +706,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):
@@ -717,31 +718,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):