source: trunk/macosx/TrackerCell.m @ 9931

Last change on this file since 9931 was 9931, checked in by charles, 13 years ago

(trunk) housekeeping: (1) add standard svn properties for $Id$ substitution in some files (2) add #include guards in a couple of libtransmission headers (3) refresh build instructions in README

  • Property svn:keywords set to Date Rev Author Id
File size: 12.9 KB
Line 
1/******************************************************************************
2 * $Id: TrackerCell.m 9931 2010-01-14 14:20:49Z charles $
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
29#define PADDING_HORIZONAL 3.0
30#define PADDING_STATUS_HORIZONAL 3.0
31#define ICON_SIZE 14.0
32#define PADDING_BETWEEN_ICON_AND_NAME 4.0
33#define PADDING_ABOVE_ICON 1.0
34#define PADDING_ABOVE_NAME 2.0
35#define PADDING_BETWEEN_LINES 1.0
36#define PADDING_BETWEEN_LINES_ON_SAME_LINE 4.0
37#define COUNT_WIDTH 40.0
38
39@interface TrackerCell (Private)
40
41- (NSImage *) favIcon;
42- (void) loadTrackerIcon: (NSString *) baseAddress;
43
44- (NSRect) imageRectForBounds: (NSRect) bounds;
45- (NSRect) rectForNameWithString: (NSAttributedString *) string inBounds: (NSRect) bounds;
46- (NSRect) rectForCountWithString: (NSAttributedString *) string withAboveRect: (NSRect) aboveRect inBounds: (NSRect) bounds;
47- (NSRect) rectForCountLabelWithString: (NSAttributedString *) string withRightRect: (NSRect) rightRect inBounds: (NSRect) bounds;
48- (NSRect) rectForStatusWithString: (NSAttributedString *) string withAboveRect: (NSRect) aboveRect withRightRect: (NSRect) rightRect
49            inBounds: (NSRect) bounds;
50
51- (NSAttributedString *) attributedName;
52- (NSAttributedString *) attributedStatusWithString: (NSString *) statusString;
53- (NSAttributedString *) attributedCount: (NSInteger) count;
54
55@end
56
57@implementation TrackerCell
58
59//make the favicons accessible to all tracker cells
60#warning make NSCache when 10.6-only
61NSMutableDictionary * fTrackerIconCache;
62NSMutableSet * fTrackerIconLoading;
63
64+ (void) initialize
65{
66    fTrackerIconCache = [[NSMutableDictionary alloc] init];
67    fTrackerIconLoading = [[NSMutableSet alloc] init];
68}
69
70- (id) init
71{
72    if ((self = [super init]))
73    {
74        NSMutableParagraphStyle * paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
75        [paragraphStyle setLineBreakMode: NSLineBreakByTruncatingTail];
76       
77        fNameAttributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
78                            [NSFont messageFontOfSize: 11.0], NSFontAttributeName,
79                            paragraphStyle, NSParagraphStyleAttributeName, nil];
80       
81        fStatusAttributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
82                                [NSFont messageFontOfSize: 9.0], NSFontAttributeName,
83                                paragraphStyle, NSParagraphStyleAttributeName, nil];
84       
85        [paragraphStyle release];
86    }
87    return self;
88}
89
90- (void) dealloc
91{
92    [fNameAttributes release];
93    [fStatusAttributes release];
94   
95    [super dealloc];
96}
97
98- (id) copyWithZone: (NSZone *) zone
99{
100    TrackerCell * copy = [super copyWithZone: zone];
101   
102    copy->fNameAttributes = [fNameAttributes retain];
103    copy->fStatusAttributes = [fStatusAttributes retain];
104   
105    return copy;
106}
107
108- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) controlView
109{
110    //icon
111    if ([NSApp isOnSnowLeopardOrBetter])
112        [[self favIcon] drawInRect: [self imageRectForBounds: cellFrame] fromRect: NSZeroRect operation: NSCompositeSourceOver
113                        fraction: 1.0 respectFlipped: YES hints: nil];
114    else
115    {
116        NSImage * icon = [self favIcon];
117        [icon setFlipped: YES];
118        [icon drawInRect: [self imageRectForBounds: cellFrame] fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
119    }
120
121    //set table colors
122    NSColor * nameColor, * statusColor;
123    if ([self backgroundStyle] == NSBackgroundStyleDark)
124        nameColor = statusColor = [NSColor whiteColor];
125    else
126    {
127        nameColor = [NSColor controlTextColor];
128        statusColor = [NSColor darkGrayColor];
129    }
130   
131    [fNameAttributes setObject: nameColor forKey: NSForegroundColorAttributeName];
132    [fStatusAttributes setObject: statusColor forKey: NSForegroundColorAttributeName];
133   
134    TrackerNode * node = (TrackerNode *)[self objectValue];
135   
136    //name
137    NSAttributedString * nameString = [self attributedName];
138    const NSRect nameRect = [self rectForNameWithString: nameString inBounds: cellFrame];
139    [nameString drawInRect: nameRect];
140   
141    //count strings
142    NSAttributedString * seederString = [self attributedCount: [node totalSeeders]];
143    const NSRect seederRect = [self rectForCountWithString: seederString withAboveRect: nameRect inBounds: cellFrame];
144    [seederString drawInRect: seederRect];
145   
146    NSAttributedString * leecherString = [self attributedCount: [node totalLeechers]];
147    const NSRect leecherRect = [self rectForCountWithString: leecherString withAboveRect: seederRect inBounds: cellFrame];
148    [leecherString drawInRect: leecherRect];
149   
150    NSAttributedString * downloadedString = [self attributedCount: [node totalDownloaded]];
151    const NSRect downloadedRect = [self rectForCountWithString: downloadedString withAboveRect: leecherRect inBounds: cellFrame];
152    [downloadedString drawInRect: downloadedRect];
153   
154    //count label strings
155    NSString * seederLabelBaseString = [NSLocalizedString(@"Seeders", "tracker peer stat") stringByAppendingFormat: @": "];
156    NSAttributedString * seederLabelString = [self attributedStatusWithString: seederLabelBaseString];
157    const NSRect seederLabelRect = [self rectForCountLabelWithString: seederLabelString withRightRect: seederRect
158                                        inBounds: cellFrame];
159    [seederLabelString drawInRect: seederLabelRect];
160   
161    NSString * leecherLabelBaseString = [NSLocalizedString(@"Leechers", "tracker peer stat") stringByAppendingFormat: @": "];
162    NSAttributedString * leecherLabelString = [self attributedStatusWithString: leecherLabelBaseString];
163    const NSRect leecherLabelRect = [self rectForCountLabelWithString: leecherLabelString withRightRect: leecherRect
164                                        inBounds: cellFrame];
165    [leecherLabelString drawInRect: leecherLabelRect];
166   
167    NSString * downloadedLabelBaseString = [NSLocalizedString(@"Downloaded", "tracker peer stat") stringByAppendingFormat: @": "];
168    NSAttributedString * downloadedLabelString = [self attributedStatusWithString: downloadedLabelBaseString];
169    const NSRect downloadedLabelRect = [self rectForCountLabelWithString: downloadedLabelString withRightRect: downloadedRect
170                                        inBounds: cellFrame];
171    [downloadedLabelString drawInRect: downloadedLabelRect];
172   
173    //status strings
174    NSAttributedString * lastAnnounceString = [self attributedStatusWithString: [node lastAnnounceStatusString]];
175    const NSRect lastAnnounceRect = [self rectForStatusWithString: lastAnnounceString withAboveRect: nameRect
176                                        withRightRect: seederLabelRect inBounds: cellFrame];
177    [lastAnnounceString drawInRect: lastAnnounceRect];
178   
179    NSAttributedString * nextAnnounceString = [self attributedStatusWithString: [node nextAnnounceStatusString]];
180    const NSRect nextAnnounceRect = [self rectForStatusWithString: nextAnnounceString withAboveRect: lastAnnounceRect
181                                        withRightRect: leecherLabelRect inBounds: cellFrame];
182    [nextAnnounceString drawInRect: nextAnnounceRect];
183   
184    NSAttributedString * lastScrapeString = [self attributedStatusWithString: [node lastScrapeStatusString]];
185    const NSRect lastScrapeRect = [self rectForStatusWithString: lastScrapeString withAboveRect: nextAnnounceRect
186                                    withRightRect: downloadedLabelRect inBounds: cellFrame];
187    [lastScrapeString drawInRect: lastScrapeRect];
188}
189
190@end
191
192@implementation TrackerCell (Private)
193
194- (NSImage *) favIcon
195{
196    NSURL * address = [NSURL URLWithString: [(TrackerNode *)[self objectValue] fullAnnounceAddress]];
197    NSArray * hostComponents = [[address host] componentsSeparatedByString: @"."];
198   
199    //let's try getting the tracker address without using any subdomains
200    NSString * baseAddress;
201    if ([hostComponents count] > 1)
202        baseAddress = [NSString stringWithFormat: @"http://%@.%@",
203                        [hostComponents objectAtIndex: [hostComponents count] - 2], [hostComponents lastObject]];
204    else
205        baseAddress = [NSString stringWithFormat: @"http://%@", [hostComponents lastObject]];
206   
207    id icon = [fTrackerIconCache objectForKey: baseAddress];
208    if (!icon && ![fTrackerIconLoading containsObject: baseAddress])
209    {
210        [fTrackerIconLoading addObject: baseAddress];
211        [NSThread detachNewThreadSelector: @selector(loadTrackerIcon:) toTarget: self withObject: baseAddress];
212    }
213   
214    return (icon && icon != [NSNull null]) ? icon : [NSImage imageNamed: @"FavIcon.png"];
215}
216
217- (void) loadTrackerIcon: (NSString *) baseAddress
218{
219    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
220   
221    NSURL * favIconUrl = [NSURL URLWithString: [baseAddress stringByAppendingPathComponent: @"favicon.ico"]];
222   
223    NSURLRequest * request = [NSURLRequest requestWithURL: favIconUrl cachePolicy: NSURLRequestUseProtocolCachePolicy
224                                timeoutInterval: 30.0];
225    NSData * iconData = [NSURLConnection sendSynchronousRequest: request returningResponse: NULL error: NULL];
226    NSImage * icon = [[NSImage alloc] initWithData: iconData];
227   
228    if (icon)
229    {
230        [fTrackerIconCache setObject: icon forKey: baseAddress];
231        [icon release];
232    }
233    else
234        [fTrackerIconCache setObject: [NSNull null] forKey: baseAddress];
235   
236    [fTrackerIconLoading removeObject: baseAddress];
237
238    [pool drain];
239}
240
241- (NSRect) imageRectForBounds: (NSRect) bounds
242{
243    return NSMakeRect(NSMinX(bounds) + PADDING_HORIZONAL, NSMinY(bounds) + PADDING_ABOVE_ICON, ICON_SIZE, ICON_SIZE);
244}
245
246- (NSRect) rectForNameWithString: (NSAttributedString *) string inBounds: (NSRect) bounds
247{
248    NSRect result;
249    result.origin.x = NSMinX(bounds) + PADDING_HORIZONAL + ICON_SIZE + PADDING_BETWEEN_ICON_AND_NAME;
250    result.origin.y = NSMinY(bounds) + PADDING_ABOVE_NAME;
251       
252    result.size.height = [string size].height;
253    result.size.width = NSMaxX(bounds) - NSMinX(result) - PADDING_HORIZONAL;
254   
255    return result;
256}
257
258- (NSRect) rectForCountWithString: (NSAttributedString *) string withAboveRect: (NSRect) aboveRect inBounds: (NSRect) bounds
259{
260    return NSMakeRect(NSMaxX(bounds) - PADDING_HORIZONAL - COUNT_WIDTH,
261                        NSMaxY(aboveRect) + PADDING_BETWEEN_LINES,
262                        COUNT_WIDTH, [string size].height);
263}
264
265- (NSRect) rectForCountLabelWithString: (NSAttributedString *) string withRightRect: (NSRect) rightRect inBounds: (NSRect) bounds
266{
267    NSRect result = rightRect;
268    result.size.width = [string size].width;
269    result.origin.x -= NSWidth(result);
270   
271    return result;
272}
273
274- (NSRect) rectForStatusWithString: (NSAttributedString *) string withAboveRect: (NSRect) aboveRect withRightRect: (NSRect) rightRect
275            inBounds: (NSRect) bounds
276{
277    NSRect result;
278    result.origin.x = NSMinX(bounds) + PADDING_STATUS_HORIZONAL;
279    result.origin.y = NSMaxY(aboveRect) + PADDING_BETWEEN_LINES;
280   
281    result.size.height = [string size].height;
282    result.size.width = NSMinX(rightRect) - PADDING_BETWEEN_LINES_ON_SAME_LINE - NSMinX(result);
283   
284    return result;
285}
286
287- (NSAttributedString *) attributedName
288{
289    NSString * name = [(TrackerNode *)[self objectValue] host];
290    return [[[NSAttributedString alloc] initWithString: name attributes: fNameAttributes] autorelease];
291}
292
293- (NSAttributedString *) attributedStatusWithString: (NSString *) statusString
294{
295    return [[[NSAttributedString alloc] initWithString: statusString attributes: fStatusAttributes] autorelease];
296}
297
298- (NSAttributedString *) attributedCount: (NSInteger) count
299{
300    NSString * countString = count != -1 ? [NSString stringWithFormat: @"%d", count] : NSLocalizedString(@"N/A", "tracker peer stat");
301    return [[[NSAttributedString alloc] initWithString: countString attributes: fStatusAttributes] autorelease];
302}
303
304@end
Note: See TracBrowser for help on using the repository browser.