1
/******************************************************************************
2
* $Id: Badger.m 424 2006-06-21 23:46:41Z livings124 $
4
* Copyright (c) 2006 Transmission authors and contributors
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:
13
* The above copyright notice and this permission notice shall be included in
14
* all copies or substantial portions of the Software.
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
*****************************************************************************/
26
#import "StringAdditions.h"
28
@interface Badger (Private)
30
- (void) badgeString: (NSString *) string forRect: (NSRect) rect;
34
@implementation Badger
38
if ((self = [super init]))
40
fBadge = [NSImage imageNamed: @"Badge"];
41
fDockIcon = [[NSApp applicationIconImage] copy];
42
fBadgedDockIcon = [fDockIcon copy];
43
fUploadBadge = [NSImage imageNamed: @"UploadBadge"];
44
fDownloadBadge = [NSImage imageNamed: @"DownloadBadge"];
46
NSShadow * stringShadow = [[NSShadow alloc] init];
47
[stringShadow setShadowOffset: NSMakeSize(2.0, -2.0)];
48
[stringShadow setShadowBlurRadius: 4.0];
50
fAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
51
[NSColor whiteColor], NSForegroundColorAttributeName,
52
[NSFont fontWithName: @"Helvetica-Bold" size: 28.0], NSFontAttributeName,
53
stringShadow, NSShadowAttributeName, nil];
55
[stringShadow release];
67
[fBadgedDockIcon release];
68
[fAttributes release];
73
- (void) updateBadgeWithCompleted: (int) completed
74
uploadRate: (float) uploadRate
75
downloadRate: (float) downloadRate
77
NSImage * dockIcon = nil;
78
NSSize iconSize = [fDockIcon size];
81
if (fCompleted != completed)
83
fCompleted = completed;
84
dockIcon = [fDockIcon copy];
86
//set completed badge to top right
90
badgeRect.size = [fBadge size];
91
badgeRect.origin.x = iconSize.width - badgeRect.size.width;
92
badgeRect.origin.y = iconSize.height - badgeRect.size.height;
97
[fBadge compositeToPoint: badgeRect.origin
98
operation: NSCompositeSourceOver];
100
//ignore shadow of badge when placing string
101
float badgeBottomExtra = 5.0;
102
badgeRect.size.height -= badgeBottomExtra;
103
badgeRect.origin.y += badgeBottomExtra;
106
[self badgeString: [NSString stringWithInt: completed]
109
[dockIcon unlockFocus];
112
[fBadgedDockIcon release];
113
fBadgedDockIcon = [dockIcon copy];
116
//set upload and download rate badges
117
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
118
NSString * uploadRateString = uploadRate >= 0.1 && [defaults boolForKey: @"BadgeUploadRate"]
119
? [NSString stringForSpeedAbbrev: uploadRate] : nil,
120
* downloadRateString = downloadRate >= 0.1 && [defaults boolForKey: @"BadgeDownloadRate"]
121
? [NSString stringForSpeedAbbrev: downloadRate] : nil;
123
BOOL speedShown = uploadRateString || downloadRateString;
127
badgeRect.size = [fUploadBadge size];
128
badgeRect.origin = NSZeroPoint;
130
//ignore shadow of badge when placing string
131
NSRect stringRect = badgeRect;
132
float badgeBottomExtra = 2.0;
133
stringRect.size.height -= badgeBottomExtra;
134
stringRect.origin.y += badgeBottomExtra;
137
dockIcon = [fBadgedDockIcon copy];
139
[dockIcon lockFocus];
141
if (uploadRateString)
144
[fUploadBadge compositeToPoint: badgeRect.origin
145
operation: NSCompositeSourceOver];
148
[self badgeString: uploadRateString forRect: stringRect];
151
if (downloadRateString)
153
//download rate above upload rate
154
if (uploadRateString)
156
float spaceBetween = badgeRect.size.height + 2.0;
157
badgeRect.origin.y += spaceBetween;
158
stringRect.origin.y += spaceBetween;
162
[fDownloadBadge compositeToPoint: badgeRect.origin
163
operation: NSCompositeSourceOver];
166
[self badgeString: downloadRateString forRect: stringRect];
169
[dockIcon unlockFocus];
172
if (dockIcon || fSpeedShown)
175
dockIcon = [fBadgedDockIcon copy];
177
[NSApp setApplicationIconImage: dockIcon];
181
fSpeedShown = speedShown;
186
[fBadgedDockIcon release];
187
fBadgedDockIcon = [fDockIcon copy];
189
[NSApp setApplicationIconImage: fDockIcon];
194
@implementation Badger (Private)
196
//dock icon must have locked focus
197
- (void) badgeString: (NSString *) string forRect: (NSRect) rect
199
NSSize stringSize = [string sizeWithAttributes: fAttributes];
201
//string is in center of image
202
rect.origin.x += (rect.size.width - stringSize.width) * 0.5;
203
rect.origin.y += (rect.size.height - stringSize.height) * 0.5;
205
[string drawAtPoint: rect.origin withAttributes: fAttributes];