1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-25 17:17:31 +00:00

fix favicons

This commit is contained in:
Mitchell Livingston 2009-09-28 03:42:35 +00:00
parent e85b92b305
commit fbecc713a6
5 changed files with 82 additions and 117 deletions

View file

@ -53,6 +53,7 @@
IBOutlet NSTextView * fCommentView;
IBOutlet NSButton * fRevealDataButton;
NSArray * fTrackers;
IBOutlet TrackerTableView * fTrackerTable;
TrackerCell * fTrackerCell;
IBOutlet NSSegmentedControl * fTrackerAddRemoveControl;
@ -76,11 +77,6 @@
* fUploadLimitLabel, * fDownloadLimitLabel, * fPeersConnectLabel,
* fPeersConnectField;
NSArray * fTrackers;
NSCache * fTrackerIconCache;
NSMutableDictionary * fTrackerIconCacheLeopard;
NSMutableSet * fTrackerIconLoading;
NSString * fInitialString;
QLPreviewPanel * fPreviewPanel;

View file

@ -90,6 +90,7 @@ typedef enum
- (BOOL) canQuickLook;
- (BOOL) canQuickLookFile: (FileListNode *) item;
#warning lookie
/*- (void) addTrackers;
- (void) removeTrackers;*/
@ -201,12 +202,6 @@ typedef enum
[self setWebSeedTableHidden: YES animate: NO];
if ([NSApp isOnSnowLeopardOrBetter])
fTrackerIconCache = [[NSCache alloc] init];
else
fTrackerIconCacheLeopard = [[NSMutableDictionary alloc] init];
fTrackerIconLoading = [[NSMutableSet alloc] init];
//set blank inspector
[self setInfoForTorrents: [NSArray array]];
@ -246,10 +241,6 @@ typedef enum
[fTrackerCell release];
[fTrackerIconCache release];
[fTrackerIconCacheLeopard release];
[fTrackerIconLoading release];
[fPreviewPanel release];
[super dealloc];
@ -900,73 +891,16 @@ typedef enum
}
else if (tableView == fTrackerTable)
{
//NSString * ident = [column identifier];
id item = [fTrackers objectAtIndex: row];
#warning isn't used
/*if ([ident isEqualToString: @"Icon"])
{
NSAssert([item isKindOfClass: [TrackerNode class]], @"Value passed to tracker table's icon row is not a TrackerNode!");
NSURL * address = [NSURL URLWithString: [(TrackerNode *)item host]];
NSArray * hostComponents = [[address host] componentsSeparatedByString: @"."];
//let's try getting the tracker address without using any subdomains
NSString * baseAddress;
if ([hostComponents count] > 1)
baseAddress = [NSString stringWithFormat: @"http://%@.%@",
[hostComponents objectAtIndex: [hostComponents count] - 2], [hostComponents lastObject]];
else
baseAddress = [NSString stringWithFormat: @"http://%@", [hostComponents lastObject]];
id icon = [NSApp isOnSnowLeopardOrBetter] ? [fTrackerIconCache objectForKey: baseAddress]
: [fTrackerIconCacheLeopard objectForKey: baseAddress];
if (!icon && ![fTrackerIconLoading containsObject: baseAddress])
{
[fTrackerIconLoading addObject: baseAddress];
[NSThread detachNewThreadSelector: @selector(loadTrackerIcon:) toTarget: self withObject: baseAddress];
}
return (icon && icon != [NSNull null]) ? icon : [NSImage imageNamed: @"FavIcon.png"];
}*/
if ([item isKindOfClass: [NSNumber class]])
return [NSString stringWithFormat: NSLocalizedString(@"Tier %d", "Inspector -> tracker table"), [item integerValue]];
else
return item;
}
return nil;
}
- (void) loadTrackerIcon: (NSString *) baseAddress
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSURL * favIconUrl = [NSURL URLWithString: [baseAddress stringByAppendingPathComponent: @"favicon.ico"]];
NSURLRequest * request = [NSURLRequest requestWithURL: favIconUrl cachePolicy: NSURLRequestUseProtocolCachePolicy
timeoutInterval: 30.0];
NSData * iconData = [NSURLConnection sendSynchronousRequest: request returningResponse: NULL error: NULL];
NSImage * icon = [[NSImage alloc] initWithData: iconData];
if (icon)
{
[fTrackerIconCache setObject: icon forKey: baseAddress];
[fTrackerIconCacheLeopard setObject: icon forKey: baseAddress];
[icon release];
}
else
{
[fTrackerIconCache setObject: [NSNull null] forKey: baseAddress];
[fTrackerIconCacheLeopard setObject: [NSNull null] forKey: baseAddress];
}
[fTrackerIconLoading removeObject: baseAddress];
[pool drain];
}
- (NSCell *) tableView: (NSTableView *) tableView dataCellForTableColumn: (NSTableColumn *) tableColumn row: (NSInteger) row
{
if (tableView == fTrackerTable)

View file

@ -23,18 +23,21 @@
*****************************************************************************/
#import "TrackerCell.h"
#import "NSApplicationAdditions.h"
#import "TrackerNode.h"
#define PADDING_HORIZONAL 2.0
#define PADDING_STATUS_HORIZONAL 4.0
#define PADDING_HORIZONAL 3.0
#define PADDING_STATUS_HORIZONAL 3.0
#define ICON_SIZE 14.0
#define PADDING_BETWEEN_IMAGE_AND_NAME 4.0
#define PADDING_BETWEEN_ICON_AND_NAME 4.0
#define PADDING_ABOVE_ICON 1.0
#define PADDING_ABOVE_NAME 2.0
#define PADDING_BETWEEN_LINES 1.0
@interface TrackerCell (Private)
- (NSImage *) favIcon;
- (void) loadTrackerIcon: (NSString *) baseAddress;
- (NSRect) imageRectForBounds: (NSRect) bounds;
- (NSRect) rectForNameWithString: (NSAttributedString *) string inBounds: (NSRect) bounds;
@ -47,6 +50,20 @@
@implementation TrackerCell
//make the favicons accessible to all tracker cells
NSCache * fTrackerIconCache;
NSMutableDictionary * fTrackerIconCacheLeopard;
NSMutableSet * fTrackerIconLoading;
+ (void) initialize
{
if ([NSApp isOnSnowLeopardOrBetter])
fTrackerIconCache = [[NSCache alloc] init];
else
fTrackerIconCacheLeopard = [[NSMutableDictionary alloc] init];
fTrackerIconLoading = [[NSMutableSet alloc] init];
}
- (id) init
{
if ((self = [super init]))
@ -88,8 +105,16 @@
- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) controlView
{
//icon
[[self favIcon] drawInRect: [self imageRectForBounds: cellFrame] fromRect: NSZeroRect operation: NSCompositeSourceOver
fraction: 1.0];
if ([NSApp isOnSnowLeopardOrBetter])
[[self favIcon] drawInRect: [self imageRectForBounds: cellFrame] fromRect: NSZeroRect operation: NSCompositeSourceOver
fraction: 1.0 respectFlipped: YES hints: nil];
else
{
NSImage * icon = [self favIcon];
[icon setFlipped: YES];
[icon drawInRect: [self imageRectForBounds: cellFrame] fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
}
NSColor * nameColor, * statusColor;
if ([self backgroundStyle] == NSBackgroundStyleDark)
@ -125,20 +150,65 @@
@end
@implementation TrackerCell (Private)
#warning needed?
- (NSImage *) favIcon
{
return [NSImage imageNamed: @"FavIcon.png"];
NSURL * address = [NSURL URLWithString: [(TrackerNode *)[self objectValue] host]];
NSArray * hostComponents = [[address host] componentsSeparatedByString: @"."];
//let's try getting the tracker address without using any subdomains
NSString * baseAddress;
if ([hostComponents count] > 1)
baseAddress = [NSString stringWithFormat: @"http://%@.%@",
[hostComponents objectAtIndex: [hostComponents count] - 2], [hostComponents lastObject]];
else
baseAddress = [NSString stringWithFormat: @"http://%@", [hostComponents lastObject]];
id icon = [NSApp isOnSnowLeopardOrBetter] ? [fTrackerIconCache objectForKey: baseAddress]
: [fTrackerIconCacheLeopard objectForKey: baseAddress];
if (!icon && ![fTrackerIconLoading containsObject: baseAddress])
{
[fTrackerIconLoading addObject: baseAddress];
[NSThread detachNewThreadSelector: @selector(loadTrackerIcon:) toTarget: self withObject: baseAddress];
}
return (icon && icon != [NSNull null]) ? icon : [NSImage imageNamed: @"FavIcon.png"];
}
- (void) loadTrackerIcon: (NSString *) baseAddress
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSURL * favIconUrl = [NSURL URLWithString: [baseAddress stringByAppendingPathComponent: @"favicon.ico"]];
NSURLRequest * request = [NSURLRequest requestWithURL: favIconUrl cachePolicy: NSURLRequestUseProtocolCachePolicy
timeoutInterval: 30.0];
NSData * iconData = [NSURLConnection sendSynchronousRequest: request returningResponse: NULL error: NULL];
NSImage * icon = [[NSImage alloc] initWithData: iconData];
if (icon)
{
[fTrackerIconCache setObject: icon forKey: baseAddress];
[fTrackerIconCacheLeopard setObject: icon forKey: baseAddress];
[icon release];
}
else
{
[fTrackerIconCache setObject: [NSNull null] forKey: baseAddress];
[fTrackerIconCacheLeopard setObject: [NSNull null] forKey: baseAddress];
}
[fTrackerIconLoading removeObject: baseAddress];
[pool drain];
}
- (NSRect) imageRectForBounds: (NSRect) bounds
{
NSRect result = bounds;
result.origin.x += PADDING_HORIZONAL;
result.origin.y += PADDING_ABOVE_NAME;
result.origin.y += PADDING_ABOVE_ICON;
result.size = NSMakeSize(ICON_SIZE, ICON_SIZE);
return result;
@ -149,7 +219,7 @@
const NSSize nameSize = [string size];
NSRect result = bounds;
result.origin.x += PADDING_HORIZONAL + ICON_SIZE + PADDING_BETWEEN_IMAGE_AND_NAME;
result.origin.x += PADDING_HORIZONAL + ICON_SIZE + PADDING_BETWEEN_ICON_AND_NAME;
result.origin.y += PADDING_ABOVE_NAME;
result.size = nameSize;

View file

@ -39,38 +39,4 @@
- (NSString *) nextAnnounceStatusString;
- (NSString *) lastScrapeStatusString;
#if 0
- (BOOL) isActive;
- (int) tier;
- (BOOL) isAnnouncing;
- (BOOL) hasAnnounced;
- (BOOL) willAnnounce;
- (NSDate *) nextAnnounceDate;
- (BOOL) isScraping;
- (BOOL) hasScraped;
- (BOOL) willScrape;
- (NSDate *) nextScrapeDate;
- (int) lastAnnouncePeerCount;
- (NSString *) lastAnnounceResult;
- (NSDate *) lastAnnounceDate;
- (NSDate *) lastAnnounceStartDate;
- (BOOL) lastAnnounceSucceeded;
- (NSDate *) lastAnnounceDate;
- (NSString *) lastScrapeResult;
- (NSDate *) lastScrapeDate;
- (NSDate *) lastScrapeStartDate;
- (BOOL) lastScrapeSucceeded;
- (NSDate *) lastScrapeDate;
- (int) seederCount;
- (int) leecherCount;
- (int) downloadCount;
#endif
@end

View file

@ -90,7 +90,6 @@
return dateString;
}
#warning consider "isActive"
- (NSString *) nextAnnounceStatusString
{
if (fStat.isAnnouncing)