From 15f1f18e56c83b2e70f79a98f746a1507584941c Mon Sep 17 00:00:00 2001 From: Mitchell Livingston Date: Tue, 29 Sep 2009 03:53:57 +0000 Subject: [PATCH] prepare for re-adding "remove tracker" functionality --- macosx/InfoWindowController.m | 51 +++++++++++------------------------ macosx/Torrent.h | 2 +- macosx/Torrent.m | 49 +++++++++++---------------------- 3 files changed, 32 insertions(+), 70 deletions(-) diff --git a/macosx/InfoWindowController.m b/macosx/InfoWindowController.m index ecd32981b..40c072789 100644 --- a/macosx/InfoWindowController.m +++ b/macosx/InfoWindowController.m @@ -1692,42 +1692,24 @@ typedef enum - (void) removeTrackers { - NSMutableIndexSet * indexes = [[[fTrackerTable selectedRowIndexes] mutableCopy] autorelease]; + const NSInteger oldCount = [fTrackers count] - [(TrackerNode *)[fTrackers lastObject] tier]; + NSMutableArray * addresses = [NSMutableArray arrayWithCapacity: oldCount]; - //get all rows to remove - NSUInteger i = 0, trackerCount = 0; - while (i < [fTrackers count]) + NSIndexSet * indexes = [fTrackerTable selectedRowIndexes]; + for (NSUInteger i = [indexes firstIndex]; i != NSNotFound; i = [indexes indexGreaterThanIndex: i]) { - //if a group is selected, remove all trackers in the group - if ([indexes containsIndex: i]) + id item = [fTrackers objectAtIndex: i]; + if ([item isKindOfClass: [NSNumber class]]) { - for (i = i+1; i < [fTrackers count] && ![[fTrackers objectAtIndex: i] isKindOfClass: [NSNumber class]]; i++) - { - [indexes addIndex: i]; - trackerCount++; - } + for (NSInteger j = i+1; j < [fTrackers count] && ![[fTrackers objectAtIndex: j] isKindOfClass: [NSNumber class]]; ++j, ++i) + [addresses addObject: [fTrackers objectAtIndex: j]]; + --i; } - //remove empty groups else - { - BOOL allSelected = YES; - NSUInteger j; - for (j = i+1; j < [fTrackers count] && ![[fTrackers objectAtIndex: j] isKindOfClass: [NSNumber class]]; j++) - { - if (![indexes containsIndex: j]) - allSelected = NO; - else - trackerCount++; - } - - if (allSelected) - [indexes addIndex: i]; - - i = j; - } + [addresses addObject: [(TrackerNode *)item fullAnnounceAddress]]; } - if ([fTrackers count] == [indexes count]) + if (oldCount == [addresses count]) { NSBeep(); return; @@ -1737,10 +1719,10 @@ typedef enum { NSAlert * alert = [[NSAlert alloc] init]; - if (trackerCount > 1) + if ([addresses count] > 1) { [alert setMessageText: [NSString stringWithFormat: NSLocalizedString(@"Are you sure you want to remove %d trackers?", - "Remove trackers alert -> title"), trackerCount]]; + "Remove trackers alert -> title"), [addresses count]]]; [alert setInformativeText: NSLocalizedString(@"Once removed, Transmission will no longer attempt to contact them." " This cannot be undone.", "Remove trackers alert -> message")]; } @@ -1765,12 +1747,8 @@ typedef enum return; } - [fTrackers removeObjectsAtIndexes: indexes]; - - #warning fix! Torrent * torrent = [fTorrents objectAtIndex: 0]; - [torrent updateAllTrackersForRemove: fTrackers]; - [fTrackerTable deselectAll: self]; + [torrent removeTrackersWithAnnounceAddresses: addresses]; //reset table with either new or old value [fTrackers release]; @@ -1778,6 +1756,7 @@ typedef enum [fTrackerTable setTrackers: fTrackers]; [fTrackerTable reloadData]; + [fTrackerTable deselectAll: self]; } @end diff --git a/macosx/Torrent.h b/macosx/Torrent.h index 5d91a26b8..259ce6e7e 100644 --- a/macosx/Torrent.h +++ b/macosx/Torrent.h @@ -135,7 +135,7 @@ - (NSMutableArray *) allTrackerStats; - (NSMutableArray *) allTrackersFlat; //used by GroupRules - (BOOL) addTrackerToNewTier: (NSString *) tracker; -- (void) updateAllTrackersForRemove: (NSMutableArray *) trackers; +- (void) removeTrackersWithAnnounceAddresses: (NSArray *) trackers; - (NSString *) comment; - (NSString *) creator; diff --git a/macosx/Torrent.m b/macosx/Torrent.m index ab5946e7d..1520f3be0 100644 --- a/macosx/Torrent.m +++ b/macosx/Torrent.m @@ -53,8 +53,6 @@ - (NSString *) etaString; -- (void) updateAllTrackers: (NSMutableArray *) trackers; - - (void) setTimeMachineExclude: (BOOL) exclude forPath: (NSString *) path; @end @@ -732,10 +730,23 @@ int trashDataFile(const char * filename) return YES; } -#warning this doesn't work -- (void) updateAllTrackersForRemove: (NSMutableArray *) trackers +- (void) removeTrackersWithAnnounceAddresses: (NSArray *) trackers { - [self updateAllTrackers: trackers]; + //recreate the tracker structure + const int oldTrackerCount = fInfo->trackerCount; + tr_tracker_info * trackerStructs = tr_new(tr_tracker_info, oldTrackerCount-1); + + NSInteger newCount = 0; + for (NSInteger oldIndex = 0; oldIndex < oldTrackerCount; ++newCount, ++oldIndex) + { + if (![trackers containsObject: [NSString stringWithUTF8String: fInfo->trackers[oldIndex].announce]]) + trackerStructs[newCount] = fInfo->trackers[oldIndex]; + else + --newCount; + } + + tr_torrentSetAnnounceList(fHandle, trackerStructs, newCount); + tr_free(trackerStructs); } - (NSString *) comment @@ -1806,34 +1817,6 @@ int trashDataFile(const char * filename) } } -#warning needs to be removed -- (void) updateAllTrackers: (NSMutableArray *) trackers -{ - //get count - NSInteger count = 0; - for (id object in trackers) - if (![object isKindOfClass: [NSNumber class]]) - count++; - - //recreate the tracker structure - tr_tracker_info * trackerStructs = tr_new(tr_tracker_info, count); - NSInteger tier = 0, i = 0; - for (id object in trackers) - { - if (![object isKindOfClass: [NSNumber class]]) - { - trackerStructs[i].tier = tier; - trackerStructs[i].announce = (char *)[object UTF8String]; - i++; - } - else - tier++; - } - - tr_torrentSetAnnounceList(fHandle, trackerStructs, count); - tr_free(trackerStructs); -} - - (void) setTimeMachineExclude: (BOOL) exclude forPath: (NSString *) path { CSBackupSetItemExcluded((CFURLRef)[NSURL fileURLWithPath: path], exclude, true); -- 2.40.0