1 | /****************************************************************************** |
---|
2 | * $Id: Badger.m 3386 2007-10-13 03:16:51Z livings124 $ |
---|
3 | * |
---|
4 | * Copyright (c) 2006-2007 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 "Badger.h" |
---|
26 | #import "NSStringAdditions.h" |
---|
27 | |
---|
28 | @interface Badger (Private) |
---|
29 | |
---|
30 | - (void) badgeString: (NSString *) string forRect: (NSRect) rect; |
---|
31 | |
---|
32 | @end |
---|
33 | |
---|
34 | @implementation Badger |
---|
35 | |
---|
36 | - (id) initWithLib: (tr_handle *) lib |
---|
37 | { |
---|
38 | if ((self = [super init])) |
---|
39 | { |
---|
40 | fLib = lib; |
---|
41 | |
---|
42 | fCompleted = 0; |
---|
43 | fCompletedBadged = 0; |
---|
44 | fSpeedBadge = NO; |
---|
45 | } |
---|
46 | |
---|
47 | return self; |
---|
48 | } |
---|
49 | |
---|
50 | - (void) dealloc |
---|
51 | { |
---|
52 | [fDockIcon release]; |
---|
53 | [fAttributes release]; |
---|
54 | |
---|
55 | [super dealloc]; |
---|
56 | } |
---|
57 | |
---|
58 | - (void) updateBadge |
---|
59 | { |
---|
60 | //set completed badge to top right |
---|
61 | BOOL baseChange = fCompleted != fCompletedBadged; |
---|
62 | if (baseChange) |
---|
63 | { |
---|
64 | fCompletedBadged = fCompleted; |
---|
65 | |
---|
66 | //force image to reload - copy does not work |
---|
67 | NSImage * icon = [[NSImage imageNamed: @"NSApplicationIcon"] copy]; |
---|
68 | NSSize iconSize = [icon size]; |
---|
69 | |
---|
70 | [fDockIcon release]; |
---|
71 | fDockIcon = [[NSImage alloc] initWithSize: iconSize]; |
---|
72 | [fDockIcon addRepresentation: [icon bestRepresentationForDevice: nil]]; |
---|
73 | [icon release]; |
---|
74 | |
---|
75 | if (fCompleted > 0) |
---|
76 | { |
---|
77 | if (!fBadge) |
---|
78 | fBadge = [NSImage imageNamed: @"Badge"]; |
---|
79 | |
---|
80 | NSRect badgeRect; |
---|
81 | badgeRect.size = [fBadge size]; |
---|
82 | badgeRect.origin.x = iconSize.width - badgeRect.size.width; |
---|
83 | badgeRect.origin.y = iconSize.height - badgeRect.size.height; |
---|
84 | |
---|
85 | [fDockIcon lockFocus]; |
---|
86 | |
---|
87 | //place badge |
---|
88 | [fBadge compositeToPoint: badgeRect.origin operation: NSCompositeSourceOver]; |
---|
89 | |
---|
90 | //ignore shadow of badge when placing string |
---|
91 | float badgeBottomExtra = 5.0; |
---|
92 | badgeRect.size.height -= badgeBottomExtra; |
---|
93 | badgeRect.origin.y += badgeBottomExtra; |
---|
94 | |
---|
95 | //place badge text |
---|
96 | [self badgeString: [NSString stringWithFormat: @"%d", fCompleted] forRect: badgeRect]; |
---|
97 | |
---|
98 | [fDockIcon unlockFocus]; |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | NSImage * dockIcon = nil; |
---|
103 | BOOL speedBadge = NO; |
---|
104 | |
---|
105 | BOOL checkDownload = [[NSUserDefaults standardUserDefaults] boolForKey: @"BadgeDownloadRate"], |
---|
106 | checkUpload = [[NSUserDefaults standardUserDefaults] boolForKey: @"BadgeUploadRate"]; |
---|
107 | if (checkDownload || checkUpload) |
---|
108 | { |
---|
109 | //set upload and download rate badges |
---|
110 | NSString * downloadRateString = nil, * uploadRateString = nil; |
---|
111 | |
---|
112 | float downloadRate, uploadRate; |
---|
113 | tr_torrentRates(fLib, &downloadRate, &uploadRate); |
---|
114 | |
---|
115 | if (checkDownload && downloadRate >= 0.1) |
---|
116 | downloadRateString = [NSString stringForSpeedAbbrev: downloadRate]; |
---|
117 | if (checkUpload && uploadRate >= 0.1) |
---|
118 | uploadRateString = [NSString stringForSpeedAbbrev: uploadRate]; |
---|
119 | |
---|
120 | speedBadge = uploadRateString || downloadRateString; |
---|
121 | if (speedBadge) |
---|
122 | { |
---|
123 | if (!fDockIcon) |
---|
124 | fDockIcon = [[NSImage imageNamed: @"NSApplicationIcon"] copy]; |
---|
125 | dockIcon = [fDockIcon copy]; |
---|
126 | |
---|
127 | if (!fUploadBadge) |
---|
128 | fUploadBadge = [NSImage imageNamed: @"UploadBadge"]; |
---|
129 | if (!fDownloadBadge) |
---|
130 | fDownloadBadge = [NSImage imageNamed: @"DownloadBadge"]; |
---|
131 | |
---|
132 | NSRect badgeRect; |
---|
133 | badgeRect.size = [fUploadBadge size]; |
---|
134 | badgeRect.origin = NSZeroPoint; |
---|
135 | |
---|
136 | //ignore shadow of badge when placing string |
---|
137 | NSRect stringRect = badgeRect; |
---|
138 | float badgeBottomExtra = 2.0; |
---|
139 | stringRect.size.height -= badgeBottomExtra; |
---|
140 | stringRect.origin.y += badgeBottomExtra; |
---|
141 | |
---|
142 | [dockIcon lockFocus]; |
---|
143 | |
---|
144 | if (uploadRateString) |
---|
145 | { |
---|
146 | //place badge and text |
---|
147 | [fUploadBadge compositeToPoint: badgeRect.origin operation: NSCompositeSourceOver]; |
---|
148 | [self badgeString: uploadRateString forRect: stringRect]; |
---|
149 | } |
---|
150 | |
---|
151 | if (downloadRateString) |
---|
152 | { |
---|
153 | //download rate above upload rate |
---|
154 | if (uploadRateString) |
---|
155 | { |
---|
156 | float spaceBetween = badgeRect.size.height + 2.0; |
---|
157 | badgeRect.origin.y += spaceBetween; |
---|
158 | stringRect.origin.y += spaceBetween; |
---|
159 | } |
---|
160 | |
---|
161 | //place badge and text |
---|
162 | [fDownloadBadge compositeToPoint: badgeRect.origin operation: NSCompositeSourceOver]; |
---|
163 | [self badgeString: downloadRateString forRect: stringRect]; |
---|
164 | } |
---|
165 | |
---|
166 | [dockIcon unlockFocus]; |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | //update dock badge |
---|
171 | if (baseChange || fSpeedBadge || speedBadge) |
---|
172 | { |
---|
173 | [NSApp setApplicationIconImage: dockIcon ? dockIcon : fDockIcon]; |
---|
174 | [dockIcon release]; |
---|
175 | |
---|
176 | fSpeedBadge = speedBadge; |
---|
177 | } |
---|
178 | } |
---|
179 | |
---|
180 | - (void) incrementCompleted |
---|
181 | { |
---|
182 | fCompleted++; |
---|
183 | [self updateBadge]; |
---|
184 | } |
---|
185 | |
---|
186 | - (void) clearCompleted |
---|
187 | { |
---|
188 | if (fCompleted != 0) |
---|
189 | { |
---|
190 | fCompleted = 0; |
---|
191 | [self updateBadge]; |
---|
192 | } |
---|
193 | } |
---|
194 | |
---|
195 | - (void) clearBadge |
---|
196 | { |
---|
197 | fCompleted = 0; |
---|
198 | fCompletedBadged = 0; |
---|
199 | fSpeedBadge = NO; |
---|
200 | [NSApp setApplicationIconImage: [NSImage imageNamed: @"NSApplicationIcon"]]; |
---|
201 | } |
---|
202 | |
---|
203 | @end |
---|
204 | |
---|
205 | @implementation Badger (Private) |
---|
206 | |
---|
207 | //dock icon must have locked focus |
---|
208 | - (void) badgeString: (NSString *) string forRect: (NSRect) rect |
---|
209 | { |
---|
210 | if (!fAttributes) |
---|
211 | { |
---|
212 | NSShadow * stringShadow = [[NSShadow alloc] init]; |
---|
213 | [stringShadow setShadowOffset: NSMakeSize(2.0, -2.0)]; |
---|
214 | [stringShadow setShadowBlurRadius: 4.0]; |
---|
215 | |
---|
216 | fAttributes = [[NSDictionary alloc] initWithObjectsAndKeys: |
---|
217 | [NSColor whiteColor], NSForegroundColorAttributeName, |
---|
218 | [NSFont boldSystemFontOfSize: 26.0], NSFontAttributeName, stringShadow, NSShadowAttributeName, nil]; |
---|
219 | |
---|
220 | [stringShadow release]; |
---|
221 | } |
---|
222 | |
---|
223 | NSSize stringSize = [string sizeWithAttributes: fAttributes]; |
---|
224 | |
---|
225 | //string is in center of image |
---|
226 | rect.origin.x += (rect.size.width - stringSize.width) * 0.5; |
---|
227 | rect.origin.y += (rect.size.height - stringSize.height) * 0.5; |
---|
228 | |
---|
229 | [string drawAtPoint: rect.origin withAttributes: fAttributes]; |
---|
230 | } |
---|
231 | |
---|
232 | @end |
---|