From 3cd939ebca53e8abbe1dbb10f9bd48319fbf1a5d Mon Sep 17 00:00:00 2001 From: Mitchell Livingston Date: Sat, 12 Dec 2009 04:39:43 +0000 Subject: [PATCH] improve the tracker removing code --- macosx/InfoWindowController.m | 33 +++++++++++++++++++++------------ macosx/Torrent.h | 2 +- macosx/Torrent.m | 19 ++++++++++--------- macosx/TorrentCell.m | 8 ++++---- 4 files changed, 36 insertions(+), 26 deletions(-) diff --git a/macosx/InfoWindowController.m b/macosx/InfoWindowController.m index 78204ba89..ce8b05554 100644 --- a/macosx/InfoWindowController.m +++ b/macosx/InfoWindowController.m @@ -1747,31 +1747,40 @@ typedef enum - (void) removeTrackers { - const NSInteger oldCount = [fTrackers count] - [(TrackerNode *)[fTrackers lastObject] tier]; - NSMutableSet * addresses = [NSMutableSet setWithCapacity: oldCount]; + NSMutableIndexSet * removeIndexes = [NSMutableIndexSet indexSet]; - NSIndexSet * indexes = [fTrackerTable selectedRowIndexes]; - for (NSUInteger i = [indexes firstIndex]; i != NSNotFound; i = [indexes indexGreaterThanIndex: i]) + NSIndexSet * selectedIndexes = [fTrackerTable selectedRowIndexes]; + for (NSUInteger i=0, trackerIndex = 0; i <= [selectedIndexes lastIndex]; ++i) { id item = [fTrackers objectAtIndex: i]; - if ([item isKindOfClass: [NSNumber class]]) + const BOOL isSelected = [selectedIndexes containsIndex: i]; + const BOOL isGroup = [item isKindOfClass: [NSNumber class]]; + + if (isGroup) { - for (++i; i < [fTrackers count] && ![[fTrackers objectAtIndex: i] isKindOfClass: [NSNumber class]]; ++i) - [addresses addObject: [[fTrackers objectAtIndex: i] fullAnnounceAddress]]; - --i; + if (isSelected) + { + for (++i; i < [fTrackers count] && ![[fTrackers objectAtIndex: i] isKindOfClass: [NSNumber class]]; ++i) + [removeIndexes addIndex: trackerIndex++]; + --i; + } } else - [addresses addObject: [(TrackerNode *)item fullAnnounceAddress]]; + { + if (isSelected) + [removeIndexes addIndex: trackerIndex]; + ++trackerIndex; + } } if ([[NSUserDefaults standardUserDefaults] boolForKey: @"WarningRemoveTrackers"]) { NSAlert * alert = [[NSAlert alloc] init]; - if ([addresses count] > 1) + if ([removeIndexes count] > 1) { [alert setMessageText: [NSString stringWithFormat: NSLocalizedString(@"Are you sure you want to remove %d trackers?", - "Remove trackers alert -> title"), [addresses count]]]; + "Remove trackers alert -> title"), [removeIndexes count]]]; [alert setInformativeText: NSLocalizedString(@"Once removed, Transmission will no longer attempt to contact them." " This cannot be undone.", "Remove trackers alert -> message")]; } @@ -1797,7 +1806,7 @@ typedef enum } Torrent * torrent = [fTorrents objectAtIndex: 0]; - [torrent removeTrackersWithAnnounceAddresses: addresses]; + [torrent removeTrackersAtIndexes: removeIndexes]; //reset table with either new or old value [fTrackers release]; diff --git a/macosx/Torrent.h b/macosx/Torrent.h index d83157a36..00aae71d7 100644 --- a/macosx/Torrent.h +++ b/macosx/Torrent.h @@ -131,7 +131,7 @@ - (NSMutableArray *) allTrackerStats; - (NSArray *) allTrackersFlat; //used by GroupRules - (BOOL) addTrackerToNewTier: (NSString *) tracker; -- (void) removeTrackersWithAnnounceAddresses: (NSSet *) trackers; +- (void) removeTrackersAtIndexes: (NSIndexSet *) removeIndexes; - (NSString *) comment; - (NSString *) creator; diff --git a/macosx/Torrent.m b/macosx/Torrent.m index cc6767df4..629e45363 100644 --- a/macosx/Torrent.m +++ b/macosx/Torrent.m @@ -653,18 +653,19 @@ int trashDataFile(const char * filename) return result == TR_ANNOUNCE_LIST_OK; } -- (void) removeTrackersWithAnnounceAddresses: (NSSet *) trackers +- (void) removeTrackersAtIndexes: (NSIndexSet *) removeIndexes { + NSAssert([removeIndexes lastIndex] < fInfo->trackerCount, @"Trying to remove trackers outside the tracker count."); + + NSMutableIndexSet * indexes = [NSMutableIndexSet indexSetWithIndexesInRange: NSMakeRange(0, fInfo->trackerCount)]; + [indexes removeIndexes: removeIndexes]; + //recreate the tracker structure - const int oldTrackerCount = fInfo->trackerCount; - tr_tracker_info * trackerStructs = tr_new(tr_tracker_info, oldTrackerCount-1); + tr_tracker_info * trackerStructs = tr_new(tr_tracker_info, [indexes count]); - NSInteger newCount = 0; - for (NSInteger oldIndex = 0; oldIndex < oldTrackerCount; ++oldIndex) - { - if (![trackers member: [NSString stringWithUTF8String: fInfo->trackers[oldIndex].announce]]) - trackerStructs[newCount++] = fInfo->trackers[oldIndex]; - } + int newCount = 0; + for (NSUInteger oldIndex = [indexes firstIndex]; oldIndex != NSNotFound; oldIndex = [indexes indexGreaterThanIndex: oldIndex]) + trackerStructs[newCount++] = fInfo->trackers[oldIndex]; const tr_announce_list_err result = tr_torrentSetAnnounceList(fHandle, trackerStructs, newCount); NSAssert1(result == TR_ANNOUNCE_LIST_OK, @"Removing tracker addresses resulted in error: %d", result); diff --git a/macosx/TorrentCell.m b/macosx/TorrentCell.m index ce3852acd..11b605477 100644 --- a/macosx/TorrentCell.m +++ b/macosx/TorrentCell.m @@ -317,8 +317,8 @@ NSRect groupRect = NSInsetRect(iconRect, -1.0f, -2.0f); if (!minimal) { - groupRect.size.height--; - groupRect.origin.y--; + groupRect.size.height -= 1.0f; + groupRect.origin.y -= 1.0f; } const CGFloat radius = minimal ? 3.0f : 6.0f; @@ -545,8 +545,8 @@ NSDivideRect(missingRect, &wantedRect, &missingRect, widthRemaining, NSMinXEdge); //not-available section - if ([torrent isActive] && ![torrent isChecking] && [fDefaults boolForKey: @"DisplayProgressBarAvailable"] - && [torrent availableDesired] < 1.0) + if ([torrent isActive] && ![torrent isChecking] && [torrent availableDesired] < 1.0 + && [fDefaults boolForKey: @"DisplayProgressBarAvailable"]) { NSRect unavailableRect; NSDivideRect(wantedRect, &wantedRect, &unavailableRect, round(NSWidth(wantedRect) * [torrent availableDesired]), -- 2.40.0