source: trunk/macosx/TrackerCell.m @ 9209

Last change on this file since 9209 was 9209, checked in by livings124, 13 years ago

don't use a NSCache until we go 10.6-only

File size: 9.4 KB
Line 
1/******************************************************************************
2 * $Id$
3 *
4 * Copyright (c) 2009 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
37@interface TrackerCell (Private)
38
39- (NSImage *) favIcon;
40- (void) loadTrackerIcon: (NSString *) baseAddress;
41
42- (NSRect) imageRectForBounds: (NSRect) bounds;
43- (NSRect) rectForNameWithString: (NSAttributedString *) string inBounds: (NSRect) bounds;
44- (NSRect) rectForStatusWithString: (NSAttributedString *) string withAboveRect: (NSRect) nameRect inBounds: (NSRect) bounds;
45
46- (NSAttributedString *) attributedNameWithColor: (NSColor *) color;
47- (NSAttributedString *) attributedStatusWithString: (NSString *) statusString color: (NSColor *) color;
48
49@end
50
51@implementation TrackerCell
52
53//make the favicons accessible to all tracker cells
54#warning make NSCache when 10.6-only
55NSMutableDictionary * fTrackerIconCache;
56NSMutableSet * fTrackerIconLoading;
57
58+ (void) initialize
59{
60    fTrackerIconCache = [[NSMutableDictionary alloc] init];
61    fTrackerIconLoading = [[NSMutableSet alloc] init];
62}
63
64- (id) init
65{
66    if ((self = [super init]))
67    {
68        NSMutableParagraphStyle * paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
69        [paragraphStyle setLineBreakMode: NSLineBreakByTruncatingTail];
70       
71        fNameAttributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
72                            [NSFont messageFontOfSize: 11.0], NSFontAttributeName,
73                            paragraphStyle, NSParagraphStyleAttributeName, nil];
74       
75        fStatusAttributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
76                                [NSFont messageFontOfSize: 9.0], NSFontAttributeName,
77                                paragraphStyle, NSParagraphStyleAttributeName, nil];
78       
79        [paragraphStyle release];
80    }
81    return self;
82}
83
84- (void) dealloc
85{
86    [fNameAttributes release];
87    [fStatusAttributes release];
88   
89    [super dealloc];
90}
91
92- (id) copyWithZone: (NSZone *) zone
93{
94    TrackerCell * copy = [super copyWithZone: zone];
95   
96    copy->fNameAttributes = [fNameAttributes retain];
97    copy->fStatusAttributes = [fStatusAttributes retain];
98   
99    return copy;
100}
101
102- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) controlView
103{
104    //icon
105    if ([NSApp isOnSnowLeopardOrBetter])
106        [[self favIcon] drawInRect: [self imageRectForBounds: cellFrame] fromRect: NSZeroRect operation: NSCompositeSourceOver
107                        fraction: 1.0 respectFlipped: YES hints: nil];
108    else
109    {
110        NSImage * icon = [self favIcon];
111        [icon setFlipped: YES];
112        [icon drawInRect: [self imageRectForBounds: cellFrame] fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
113    }
114
115   
116    NSColor * nameColor, * statusColor;
117    if ([self backgroundStyle] == NSBackgroundStyleDark)
118        nameColor = statusColor = [NSColor whiteColor];
119    else
120    {
121        nameColor = [NSColor controlTextColor];
122        statusColor = [NSColor darkGrayColor];
123    }
124   
125    //name
126    NSAttributedString * nameString = [self attributedNameWithColor: nameColor];
127    const NSRect nameRect = [self rectForNameWithString: nameString inBounds: cellFrame];
128    [nameString drawInRect: nameRect];
129   
130    //status strings
131    TrackerNode * node = (TrackerNode *)[self objectValue];
132   
133    NSAttributedString * lastAnnounceString = [self attributedStatusWithString: [node lastAnnounceStatusString] color: statusColor];
134    const NSRect lastAnnounceRect = [self rectForStatusWithString: lastAnnounceString withAboveRect: nameRect inBounds: cellFrame];
135    [lastAnnounceString drawInRect: lastAnnounceRect];
136   
137    NSAttributedString * nextAnnounceString = [self attributedStatusWithString: [node nextAnnounceStatusString] color: statusColor];
138    const NSRect nextAnnounceRect = [self rectForStatusWithString: nextAnnounceString withAboveRect: lastAnnounceRect
139                                        inBounds: cellFrame];
140    [nextAnnounceString drawInRect: nextAnnounceRect];
141   
142    NSAttributedString * lastScrapeString = [self attributedStatusWithString: [node lastScrapeStatusString] color: statusColor];
143    const NSRect lastScrapeRect = [self rectForStatusWithString: lastScrapeString withAboveRect: nextAnnounceRect inBounds: cellFrame];
144    [lastScrapeString drawInRect: lastScrapeRect];
145}
146
147@end
148
149@implementation TrackerCell (Private)
150
151- (NSImage *) favIcon
152{
153    NSURL * address = [NSURL URLWithString: [(TrackerNode *)[self objectValue] host]];
154    NSArray * hostComponents = [[address host] componentsSeparatedByString: @"."];
155   
156    //let's try getting the tracker address without using any subdomains
157    NSString * baseAddress;
158    if ([hostComponents count] > 1)
159        baseAddress = [NSString stringWithFormat: @"http://%@.%@",
160                        [hostComponents objectAtIndex: [hostComponents count] - 2], [hostComponents lastObject]];
161    else
162        baseAddress = [NSString stringWithFormat: @"http://%@", [hostComponents lastObject]];
163   
164    id icon = [fTrackerIconCache objectForKey: baseAddress];
165    if (!icon && ![fTrackerIconLoading containsObject: baseAddress])
166    {
167        [fTrackerIconLoading addObject: baseAddress];
168        [NSThread detachNewThreadSelector: @selector(loadTrackerIcon:) toTarget: self withObject: baseAddress];
169    }
170   
171    return (icon && icon != [NSNull null]) ? icon : [NSImage imageNamed: @"FavIcon.png"];
172}
173
174- (void) loadTrackerIcon: (NSString *) baseAddress
175{
176    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
177   
178    NSURL * favIconUrl = [NSURL URLWithString: [baseAddress stringByAppendingPathComponent: @"favicon.ico"]];
179   
180    NSURLRequest * request = [NSURLRequest requestWithURL: favIconUrl cachePolicy: NSURLRequestUseProtocolCachePolicy
181                                timeoutInterval: 30.0];
182    NSData * iconData = [NSURLConnection sendSynchronousRequest: request returningResponse: NULL error: NULL];
183    NSImage * icon = [[NSImage alloc] initWithData: iconData];
184   
185    if (icon)
186    {
187        [fTrackerIconCache setObject: icon forKey: baseAddress];
188        [icon release];
189    }
190    else
191        [fTrackerIconCache setObject: [NSNull null] forKey: baseAddress];
192   
193    [fTrackerIconLoading removeObject: baseAddress];
194
195    [pool drain];
196}
197
198- (NSRect) imageRectForBounds: (NSRect) bounds
199{
200    NSRect result = bounds;
201    result.origin.x += PADDING_HORIZONAL;
202    result.origin.y += PADDING_ABOVE_ICON;
203    result.size = NSMakeSize(ICON_SIZE, ICON_SIZE);
204   
205    return result;
206}
207
208- (NSRect) rectForNameWithString: (NSAttributedString *) string inBounds: (NSRect) bounds
209{
210    const NSSize nameSize = [string size];
211   
212    NSRect result = bounds;
213    result.origin.x += PADDING_HORIZONAL + ICON_SIZE + PADDING_BETWEEN_ICON_AND_NAME;
214    result.origin.y += PADDING_ABOVE_NAME;
215       
216    result.size = nameSize;
217    result.size.width = MIN(result.size.width, NSMaxX(bounds) - NSMinX(result));
218   
219    return result;
220}
221
222- (NSRect) rectForStatusWithString: (NSAttributedString *) string withAboveRect: (NSRect) nameRect inBounds: (NSRect) bounds;
223{
224    const NSSize statusSize = [string size];
225   
226    NSRect result = bounds;
227    result.origin.x += PADDING_STATUS_HORIZONAL;
228    result.origin.y = NSMaxY(nameRect) + PADDING_BETWEEN_LINES;
229   
230    result.size = statusSize;
231    result.size.width = MIN(result.size.width, NSMaxX(bounds) - NSMinX(result));
232   
233    return result;
234}
235
236- (NSAttributedString *) attributedNameWithColor: (NSColor *) color
237{
238    if (color)
239        [fNameAttributes setObject: color forKey: NSForegroundColorAttributeName];
240       
241    NSString * name = [(TrackerNode *)[self objectValue] host];
242    return [[[NSAttributedString alloc] initWithString: name attributes: fNameAttributes] autorelease];
243}
244
245- (NSAttributedString *) attributedStatusWithString: (NSString *) statusString color: (NSColor *) color
246{
247    if (color)
248        [fStatusAttributes setObject: color forKey: NSForegroundColorAttributeName];
249   
250    return [[[NSAttributedString alloc] initWithString: statusString attributes: fStatusAttributes] autorelease];
251}
252
253@end
Note: See TracBrowser for help on using the repository browser.