1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-01-31 11:23:40 +00:00

better use of the cache object for tracker favicons, so if an icon is removed from the cache, it will be fetched again the next time it's needed

This commit is contained in:
Mitchell Livingston 2009-09-21 12:55:04 +00:00
parent 0b39cd5f29
commit 8fd3fd8c2a
2 changed files with 21 additions and 18 deletions

View file

@ -78,7 +78,7 @@
* fPeersConnectField; * fPeersConnectField;
NSCache * fTrackerIconCache; NSCache * fTrackerIconCache;
NSMutableSet * fTrackerIconLoaded; NSMutableSet * fTrackerIconLoading;
NSString * fInitialString; NSString * fInitialString;

View file

@ -195,7 +195,7 @@ typedef enum
if ([NSApp isOnSnowLeopardOrBetter]) if ([NSApp isOnSnowLeopardOrBetter])
{ {
fTrackerIconCache = [[NSCache alloc] init]; fTrackerIconCache = [[NSCache alloc] init];
fTrackerIconLoaded = [[NSMutableSet alloc] init]; fTrackerIconLoading = [[NSMutableSet alloc] init];
} }
else else
[fTrackerTable removeTableColumn: [fTrackerTable tableColumnWithIdentifier: @"Icon"]]; [fTrackerTable removeTableColumn: [fTrackerTable tableColumnWithIdentifier: @"Icon"]];
@ -238,7 +238,7 @@ typedef enum
[fWebSeedTableAnimation release]; [fWebSeedTableAnimation release];
[fTrackerIconCache release]; [fTrackerIconCache release];
[fTrackerIconLoaded release]; [fTrackerIconLoading release];
[fPreviewPanel release]; [fPreviewPanel release];
@ -929,21 +929,21 @@ typedef enum
NSArray * hostComponents = [[address host] componentsSeparatedByString: @"."]; NSArray * hostComponents = [[address host] componentsSeparatedByString: @"."];
//let's try getting the favicon without using any subdomains //let's try getting the favicon without using any subdomains
NSURL * favIconUrl; NSString * baseAddress;
if ([hostComponents count] > 1) if ([hostComponents count] > 1)
favIconUrl = [NSURL URLWithString: [NSString stringWithFormat: @"%@://%@.%@/favicon.ico", [address scheme], baseAddress = [NSString stringWithFormat: @"%@://%@.%@", [address scheme],
[hostComponents objectAtIndex: [hostComponents count] - 2], [hostComponents lastObject]]]; [hostComponents objectAtIndex: [hostComponents count] - 2], [hostComponents lastObject]];
else else
favIconUrl = [NSURL URLWithString: [NSString stringWithFormat: @"%@://%@/favicon.ico", [address scheme], baseAddress = [NSString stringWithFormat: @"%@://%@", [address scheme], [hostComponents lastObject]];
[hostComponents lastObject]]];
NSImage * icon = nil; id icon = [fTrackerIconCache objectForKey: baseAddress];
if ([fTrackerIconLoaded containsObject: favIconUrl]) if (!icon && ![fTrackerIconLoading containsObject: baseAddress])
icon = [fTrackerIconCache objectForKey: favIconUrl]; {
else [fTrackerIconLoading addObject: baseAddress];
[NSThread detachNewThreadSelector: @selector(loadTrackerIcon:) toTarget: self withObject: favIconUrl]; [NSThread detachNewThreadSelector: @selector(loadTrackerIcon:) toTarget: self withObject: baseAddress];
}
return icon; return (icon && icon != [NSNull null]) ? icon : nil;
} }
if ([ident isEqualToString: @"Address"]) if ([ident isEqualToString: @"Address"])
return item; return item;
@ -953,18 +953,21 @@ typedef enum
return nil; return nil;
} }
- (void) loadTrackerIcon: (NSURL *) favIconUrl - (void) loadTrackerIcon: (NSString *) baseAddress
{ {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
[fTrackerIconLoaded addObject: favIconUrl]; NSURL * favIconUrl = [NSURL URLWithString: [baseAddress stringByAppendingString: @"/favicon.ico"]];
NSImage * icon = [[NSImage alloc] initWithContentsOfURL: favIconUrl]; NSImage * icon = [[NSImage alloc] initWithContentsOfURL: favIconUrl];
if (icon) if (icon)
{ {
[fTrackerIconCache setObject: icon forKey: favIconUrl]; [fTrackerIconCache setObject: icon forKey: baseAddress];
[icon release]; [icon release];
} }
else
[fTrackerIconCache setObject: [NSNull null] forKey: baseAddress];
[fTrackerIconLoading removeObject: baseAddress];
[pool drain]; [pool drain];
} }