source: trunk/macosx/TrackerCell.m @ 10471

Last change on this file since 10471 was 10471, checked in by livings124, 12 years ago

#3133 Inspector > Trackers: Look for favicon.png

  • Property svn:keywords set to Date Rev Author Id
File size: 13.5 KB
Line 
1/******************************************************************************
2 * $Id: TrackerCell.m 10471 2010-04-12 02:55:50Z livings124 $
3 *
4 * Copyright (c) 2009-2010 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#import "TrackerCell.h"
26#import "NSApplicationAdditions.h"
27#import "TrackerNode.h"
28#import "utils.h"
29
30#define PADDING_HORIZONAL 3.0
31#define PADDING_STATUS_HORIZONAL 3.0
32#define ICON_SIZE 16.0
33#define PADDING_BETWEEN_ICON_AND_NAME 4.0
34#define PADDING_ABOVE_ICON 1.0
35#define PADDING_ABOVE_NAME 1.0
36#define PADDING_BETWEEN_LINES 1.0
37#define PADDING_BETWEEN_LINES_ON_SAME_LINE 4.0
38#define COUNT_WIDTH 40.0
39
40@interface TrackerCell (Private)
41
42- (NSImage *) favIcon;
43- (void) loadTrackerIcon: (NSString *) baseAddress;
44
45- (NSRect) imageRectForBounds: (NSRect) bounds;
46- (NSRect) rectForNameWithString: (NSAttributedString *) string inBounds: (NSRect) bounds;
47- (NSRect) rectForCountWithString: (NSAttributedString *) string withAboveRect: (NSRect) aboveRect inBounds: (NSRect) bounds;
48- (NSRect) rectForCountLabelWithString: (NSAttributedString *) string withRightRect: (NSRect) rightRect inBounds: (NSRect) bounds;
49- (NSRect) rectForStatusWithString: (NSAttributedString *) string withAboveRect: (NSRect) aboveRect withRightRect: (NSRect) rightRect
50            inBounds: (NSRect) bounds;
51
52- (NSAttributedString *) attributedName;
53- (NSAttributedString *) attributedStatusWithString: (NSString *) statusString;
54- (NSAttributedString *) attributedCount: (NSInteger) count;
55
56@end
57
58@implementation TrackerCell
59
60//make the favicons accessible to all tracker cells
61#warning make NSCache when 10.6-only
62NSMutableDictionary * fTrackerIconCache;
63NSMutableSet * fTrackerIconLoading;
64
65+ (void) initialize
66{
67    fTrackerIconCache = [[NSMutableDictionary alloc] init];
68    fTrackerIconLoading = [[NSMutableSet alloc] init];
69}
70
71- (id) init
72{
73    if ((self = [super init]))
74    {
75        NSMutableParagraphStyle * paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
76        [paragraphStyle setLineBreakMode: NSLineBreakByTruncatingTail];
77       
78        fNameAttributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
79                            [NSFont messageFontOfSize: 12.0], NSFontAttributeName,
80                            paragraphStyle, NSParagraphStyleAttributeName, nil];
81       
82        fStatusAttributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
83                                [NSFont messageFontOfSize: 9.0], NSFontAttributeName,
84                                paragraphStyle, NSParagraphStyleAttributeName, nil];
85       
86        [paragraphStyle release];
87    }
88    return self;
89}
90
91- (void) dealloc
92{
93    [fNameAttributes release];
94    [fStatusAttributes release];
95   
96    [super dealloc];
97}
98
99- (id) copyWithZone: (NSZone *) zone
100{
101    TrackerCell * copy = [super copyWithZone: zone];
102   
103    copy->fNameAttributes = [fNameAttributes retain];
104    copy->fStatusAttributes = [fStatusAttributes retain];
105   
106    return copy;
107}
108
109- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) controlView
110{
111    //icon
112    if ([NSApp isOnSnowLeopardOrBetter])
113        [[self favIcon] drawInRect: [self imageRectForBounds: cellFrame] fromRect: NSZeroRect operation: NSCompositeSourceOver
114                        fraction: 1.0 respectFlipped: YES hints: nil];
115    else
116    {
117        NSImage * icon = [self favIcon];
118        [icon setFlipped: YES];
119        [icon drawInRect: [self imageRectForBounds: cellFrame] fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
120    }
121
122    //set table colors
123    NSColor * nameColor, * statusColor;
124    if ([self backgroundStyle] == NSBackgroundStyleDark)
125        nameColor = statusColor = [NSColor whiteColor];
126    else
127    {
128        nameColor = [NSColor controlTextColor];
129        statusColor = [NSColor darkGrayColor];
130    }
131   
132    [fNameAttributes setObject: nameColor forKey: NSForegroundColorAttributeName];
133    [fStatusAttributes setObject: statusColor forKey: NSForegroundColorAttributeName];
134   
135    TrackerNode * node = (TrackerNode *)[self objectValue];
136   
137    //name
138    NSAttributedString * nameString = [self attributedName];
139    const NSRect nameRect = [self rectForNameWithString: nameString inBounds: cellFrame];
140    [nameString drawInRect: nameRect];
141   
142    //count strings
143    NSAttributedString * seederString = [self attributedCount: [node totalSeeders]];
144    const NSRect seederRect = [self rectForCountWithString: seederString withAboveRect: nameRect inBounds: cellFrame];
145    [seederString drawInRect: seederRect];
146   
147    NSAttributedString * leecherString = [self attributedCount: [node totalLeechers]];
148    const NSRect leecherRect = [self rectForCountWithString: leecherString withAboveRect: seederRect inBounds: cellFrame];
149    [leecherString drawInRect: leecherRect];
150   
151    NSAttributedString * downloadedString = [self attributedCount: [node totalDownloaded]];
152    const NSRect downloadedRect = [self rectForCountWithString: downloadedString withAboveRect: leecherRect inBounds: cellFrame];
153    [downloadedString drawInRect: downloadedRect];
154   
155    //count label strings
156    NSString * seederLabelBaseString = [NSLocalizedString(@"Seeders", "tracker peer stat") stringByAppendingFormat: @": "];
157    NSAttributedString * seederLabelString = [self attributedStatusWithString: seederLabelBaseString];
158    const NSRect seederLabelRect = [self rectForCountLabelWithString: seederLabelString withRightRect: seederRect
159                                        inBounds: cellFrame];
160    [seederLabelString drawInRect: seederLabelRect];
161   
162    NSString * leecherLabelBaseString = [NSLocalizedString(@"Leechers", "tracker peer stat") stringByAppendingFormat: @": "];
163    NSAttributedString * leecherLabelString = [self attributedStatusWithString: leecherLabelBaseString];
164    const NSRect leecherLabelRect = [self rectForCountLabelWithString: leecherLabelString withRightRect: leecherRect
165                                        inBounds: cellFrame];
166    [leecherLabelString drawInRect: leecherLabelRect];
167   
168    NSString * downloadedLabelBaseString = [NSLocalizedString(@"Downloaded", "tracker peer stat") stringByAppendingFormat: @": "];
169    NSAttributedString * downloadedLabelString = [self attributedStatusWithString: downloadedLabelBaseString];
170    const NSRect downloadedLabelRect = [self rectForCountLabelWithString: downloadedLabelString withRightRect: downloadedRect
171                                        inBounds: cellFrame];
172    [downloadedLabelString drawInRect: downloadedLabelRect];
173   
174    //status strings
175    NSAttributedString * lastAnnounceString = [self attributedStatusWithString: [node lastAnnounceStatusString]];
176    const NSRect lastAnnounceRect = [self rectForStatusWithString: lastAnnounceString withAboveRect: nameRect
177                                        withRightRect: seederLabelRect inBounds: cellFrame];
178    [lastAnnounceString drawInRect: lastAnnounceRect];
179   
180    NSAttributedString * nextAnnounceString = [self attributedStatusWithString: [node nextAnnounceStatusString]];
181    const NSRect nextAnnounceRect = [self rectForStatusWithString: nextAnnounceString withAboveRect: lastAnnounceRect
182                                        withRightRect: leecherLabelRect inBounds: cellFrame];
183    [nextAnnounceString drawInRect: nextAnnounceRect];
184   
185    NSAttributedString * lastScrapeString = [self attributedStatusWithString: [node lastScrapeStatusString]];
186    const NSRect lastScrapeRect = [self rectForStatusWithString: lastScrapeString withAboveRect: nextAnnounceRect
187                                    withRightRect: downloadedLabelRect inBounds: cellFrame];
188    [lastScrapeString drawInRect: lastScrapeRect];
189}
190
191@end
192
193@implementation TrackerCell (Private)
194
195- (NSImage *) favIcon
196{
197    NSURL * address = [NSURL URLWithString: [(TrackerNode *)[self objectValue] fullAnnounceAddress]];
198    NSString * host = [address host];
199   
200    //don't try to parse ip address
201    const BOOL separable = !tr_addressIsIP([host UTF8String]);
202   
203    NSArray * hostComponents = separable ? [host componentsSeparatedByString: @"."] : nil;
204   
205    //let's try getting the tracker address without using any subdomains
206    NSString * baseAddress;
207    if (separable && [hostComponents count] > 1)
208        baseAddress = [NSString stringWithFormat: @"http://%@.%@",
209                        [hostComponents objectAtIndex: [hostComponents count]-2], [hostComponents lastObject]];
210    else
211        baseAddress = [NSString stringWithFormat: @"http://%@", host];
212   
213    id icon = [fTrackerIconCache objectForKey: baseAddress];
214    if (!icon && ![fTrackerIconLoading containsObject: baseAddress])
215    {
216        [fTrackerIconLoading addObject: baseAddress];
217        [NSThread detachNewThreadSelector: @selector(loadTrackerIcon:) toTarget: self withObject: baseAddress];
218    }
219   
220    return (icon && icon != [NSNull null]) ? icon : [NSImage imageNamed: @"FavIcon.png"];
221}
222
223- (void) loadTrackerIcon: (NSString *) baseAddress
224{
225    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
226   
227    //try favicon.ico
228    NSURL * favIconUrl = [NSURL URLWithString: [baseAddress stringByAppendingPathComponent: @"favicon.ico"]];
229   
230    NSURLRequest * request = [NSURLRequest requestWithURL: favIconUrl cachePolicy: NSURLRequestUseProtocolCachePolicy
231                                timeoutInterval: 30.0];
232    NSData * iconData = [NSURLConnection sendSynchronousRequest: request returningResponse: NULL error: NULL];
233    NSImage * icon = [[NSImage alloc] initWithData: iconData];
234   
235    //try favicon.png
236    if (!icon)
237    {
238        favIconUrl = [NSURL URLWithString: [baseAddress stringByAppendingPathComponent: @"favicon.png"]];
239       
240        request = [NSURLRequest requestWithURL: favIconUrl cachePolicy: NSURLRequestUseProtocolCachePolicy
241                    timeoutInterval: 30.0];
242        iconData = [NSURLConnection sendSynchronousRequest: request returningResponse: NULL error: NULL];
243        icon = [[NSImage alloc] initWithData: iconData];
244    }
245   
246    if (icon)
247    {
248        [fTrackerIconCache setObject: icon forKey: baseAddress];
249        [icon release];
250    }
251    else
252        [fTrackerIconCache setObject: [NSNull null] forKey: baseAddress];
253   
254    [fTrackerIconLoading removeObject: baseAddress];
255
256    [pool drain];
257}
258
259- (NSRect) imageRectForBounds: (NSRect) bounds
260{
261    return NSMakeRect(NSMinX(bounds) + PADDING_HORIZONAL, NSMinY(bounds) + PADDING_ABOVE_ICON, ICON_SIZE, ICON_SIZE);
262}
263
264- (NSRect) rectForNameWithString: (NSAttributedString *) string inBounds: (NSRect) bounds
265{
266    NSRect result;
267    result.origin.x = NSMinX(bounds) + PADDING_HORIZONAL + ICON_SIZE + PADDING_BETWEEN_ICON_AND_NAME;
268    result.origin.y = NSMinY(bounds) + PADDING_ABOVE_NAME;
269       
270    result.size.height = [string size].height;
271    result.size.width = NSMaxX(bounds) - NSMinX(result) - PADDING_HORIZONAL;
272   
273    return result;
274}
275
276- (NSRect) rectForCountWithString: (NSAttributedString *) string withAboveRect: (NSRect) aboveRect inBounds: (NSRect) bounds
277{
278    return NSMakeRect(NSMaxX(bounds) - PADDING_HORIZONAL - COUNT_WIDTH,
279                        NSMaxY(aboveRect) + PADDING_BETWEEN_LINES,
280                        COUNT_WIDTH, [string size].height);
281}
282
283- (NSRect) rectForCountLabelWithString: (NSAttributedString *) string withRightRect: (NSRect) rightRect inBounds: (NSRect) bounds
284{
285    NSRect result = rightRect;
286    result.size.width = [string size].width;
287    result.origin.x -= NSWidth(result);
288   
289    return result;
290}
291
292- (NSRect) rectForStatusWithString: (NSAttributedString *) string withAboveRect: (NSRect) aboveRect withRightRect: (NSRect) rightRect
293            inBounds: (NSRect) bounds
294{
295    NSRect result;
296    result.origin.x = NSMinX(bounds) + PADDING_STATUS_HORIZONAL;
297    result.origin.y = NSMaxY(aboveRect) + PADDING_BETWEEN_LINES;
298   
299    result.size.height = [string size].height;
300    result.size.width = NSMinX(rightRect) - PADDING_BETWEEN_LINES_ON_SAME_LINE - NSMinX(result);
301   
302    return result;
303}
304
305- (NSAttributedString *) attributedName
306{
307    NSString * name = [(TrackerNode *)[self objectValue] host];
308    return [[[NSAttributedString alloc] initWithString: name attributes: fNameAttributes] autorelease];
309}
310
311- (NSAttributedString *) attributedStatusWithString: (NSString *) statusString
312{
313    return [[[NSAttributedString alloc] initWithString: statusString attributes: fStatusAttributes] autorelease];
314}
315
316- (NSAttributedString *) attributedCount: (NSInteger) count
317{
318    NSString * countString = count != -1 ? [NSString stringWithFormat: @"%d", count] : NSLocalizedString(@"N/A", "tracker peer stat");
319    return [[[NSAttributedString alloc] initWithString: countString attributes: fStatusAttributes] autorelease];
320}
321
322@end
Note: See TracBrowser for help on using the repository browser.