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