diff --git a/macosx/Controller.h b/macosx/Controller.h index baee8bd5e..c097a5a15 100644 --- a/macosx/Controller.h +++ b/macosx/Controller.h @@ -167,9 +167,11 @@ - (void) updateUI; - (void) updateTorrentsInQueue; +- (int) numToStartFromQueue: (BOOL) downloadQueue; - (void) torrentFinishedDownloading: (NSNotification *) notification; - (void) torrentRestartedDownloading: (NSNotification *) notification; + - (void) updateTorrentHistory; - (void) sortTorrents; diff --git a/macosx/Controller.m b/macosx/Controller.m index 5c8efbb51..e34f46643 100644 --- a/macosx/Controller.m +++ b/macosx/Controller.m @@ -1376,27 +1376,8 @@ static void sleepCallBack(void * controller, io_service_t y, natural_t messageTy BOOL download = [fDefaults boolForKey: @"Queue"], seed = [fDefaults boolForKey: @"QueueSeed"]; - //determine the number of downloads needed to start - int desiredDownloadActive = download ? [fDefaults integerForKey: @"QueueDownloadNumber"] : 0, - desiredSeedActive = seed ? [fDefaults integerForKey: @"QueueSeedNumber"] : 0; - - Torrent * torrent; - NSEnumerator * enumerator; - if (download || seed) - { - enumerator = [fTorrents objectEnumerator]; - while ((torrent = [enumerator nextObject])) - if ([torrent isActive] && ![torrent isStalled] && ![torrent isError]) - { - if ([torrent allDownloaded]) - desiredSeedActive--; - else - desiredDownloadActive--; - - if (desiredDownloadActive <= 0 && desiredSeedActive <= 0) - break; - } - } + int desiredDownloadActive = [self numToStartFromQueue: YES], + desiredSeedActive = [self numToStartFromQueue: NO]; //sort torrents by order value NSArray * sortedTorrents; @@ -1412,7 +1393,8 @@ static void sleepCallBack(void * controller, io_service_t y, natural_t messageTy else sortedTorrents = fTorrents; - enumerator = [sortedTorrents objectEnumerator]; + Torrent * torrent; + NSEnumerator * enumerator = [sortedTorrents objectEnumerator]; while ((torrent = [enumerator nextObject])) { if (![torrent isActive] && [torrent waitingToStart]) @@ -1445,6 +1427,29 @@ static void sleepCallBack(void * controller, io_service_t y, natural_t messageTy [self updateTorrentHistory]; } +- (int) numToStartFromQueue: (BOOL) downloadQueue +{ + if (![fDefaults boolForKey: downloadQueue ? @"Queue" : @"QueueSeed"]) + return 0; + + int desired = [fDefaults integerForKey: downloadQueue ? @"QueueDownloadNumber" : @"QueueSeedNumber"]; + + Torrent * torrent; + NSEnumerator * enumerator = [fTorrents objectEnumerator]; + while ((torrent = [enumerator nextObject])) + if ([torrent isActive] && ![torrent isStalled] && ![torrent isError]) + { + if ([torrent allDownloaded] != downloadQueue) + { + desired--; + if (desired <= 0) + return 0; + } + } + + return desired; +} + - (void) torrentFinishedDownloading: (NSNotification *) notification { Torrent * torrent = [notification object]; @@ -1467,8 +1472,7 @@ static void sleepCallBack(void * controller, io_service_t y, natural_t messageTy if (![fWindow isKeyWindow]) [fBadger incrementCompleted]; - #warning make better - if ([fDefaults boolForKey: @"QueueSeed"]) + if ([fDefaults boolForKey: @"QueueSeed"] && [self numToStartFromQueue: NO] <= 0) { [torrent stopTransfer]; [torrent setWaitToStart: YES]; @@ -1483,8 +1487,7 @@ static void sleepCallBack(void * controller, io_service_t y, natural_t messageTy [fInfoController updateInfoStats]; - #warning make better - if ([fDefaults boolForKey: @"Queue"]) + if ([fDefaults boolForKey: @"Queue"] && [self numToStartFromQueue: YES] <= 0) { [torrent stopTransfer]; [torrent setWaitToStart: YES]; diff --git a/macosx/Torrent.m b/macosx/Torrent.m index 6760b66e8..f76e5706e 100644 --- a/macosx/Torrent.m +++ b/macosx/Torrent.m @@ -1369,9 +1369,9 @@ static uint32_t kRed = BE(0xFF6450FF), //255, 100, 80 tr_torrentSetFilePriority(fHandle, index, actualPriority); } - #warning when going seeding to download, update queue [self update]; - [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateQueue" object: self]; //for paused torrents + if ([self isPaused]) + [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateQueue" object: self]; //for paused torrents } - (void) setFilePriority: (int) priority forIndexes: (NSIndexSet *) indexSet