1
/******************************************************************************
2
* $Id: Badger.m 4423 2008-01-02 16:55:05Z livings124 $
4
* Copyright (c) 2006-2008 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
*****************************************************************************/
27
#import "NSApplicationAdditions.h"
28
#import "NSStringAdditions.h"
29
#import "NSBezierPathAdditions.h"
31
#define COMPLETED_BOTTOM_PADDING 5.0
32
#define SPEED_BOTTOM_PADDING 2.0
33
#define SPEED_BETWEEN_PADDING 2.0
34
#define BADGE_HEIGHT 30.0
36
@interface Badger (Private)
38
- (void) badgeString: (NSString *) string forRect: (NSRect) rect;
42
@implementation Badger
44
- (id) initWithLib: (tr_handle *) lib
46
if ((self = [super init]))
54
if ([NSApp isOnLeopardOrBetter])
56
BadgeView * view = [[BadgeView alloc] initWithFrame: [[[NSApp dockTile] contentView] frame] lib: lib];
57
[[NSApp dockTile] setContentView: view];
63
//change that just impacts the dock badge
64
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateBadge) name: @"DockBadgeChange" object: nil];
72
[[NSNotificationCenter defaultCenter] removeObserver: self];
74
[NSApp setApplicationIconImage: nil]; //needed on 10.4
77
[fAttributes release];
84
if ([NSApp isOnLeopardOrBetter])
86
[[NSApp dockTile] display];
89
else if (fQuittingTiger)
93
//set completed badge to top right
94
BOOL completedChange = fCompleted != fCompletedBadged;
97
fCompletedBadged = fCompleted;
99
//force image to reload - copy does not work
100
NSImage * icon = [[NSImage imageNamed: @"NSApplicationIcon"] copy];
101
NSSize iconSize = [icon size];
104
fDockIcon = [[NSImage alloc] initWithSize: iconSize];
105
[fDockIcon addRepresentation: [icon bestRepresentationForDevice: nil]];
111
fBadge = [NSImage imageNamed: @"Badge"];
114
badgeRect.size = [fBadge size];
115
badgeRect.origin.x = iconSize.width - badgeRect.size.width;
116
badgeRect.origin.y = iconSize.height - badgeRect.size.height;
118
[fDockIcon lockFocus];
121
[fBadge compositeToPoint: badgeRect.origin operation: NSCompositeSourceOver];
123
//ignore shadow of badge when placing string
124
badgeRect.size.height -= COMPLETED_BOTTOM_PADDING;
125
badgeRect.origin.y += COMPLETED_BOTTOM_PADDING;
128
[self badgeString: [NSString stringWithFormat: @"%d", fCompleted] forRect: badgeRect];
130
[fDockIcon unlockFocus];
134
NSImage * dockIcon = nil;
135
BOOL speedBadge = NO;
137
BOOL checkDownload = [[NSUserDefaults standardUserDefaults] boolForKey: @"BadgeDownloadRate"],
138
checkUpload = [[NSUserDefaults standardUserDefaults] boolForKey: @"BadgeUploadRate"];
139
if (checkDownload || checkUpload)
141
//set upload and download rate badges
142
NSString * downloadRateString = nil, * uploadRateString = nil;
144
float downloadRate, uploadRate;
145
tr_torrentRates(fLib, &downloadRate, &uploadRate);
147
if (checkDownload && downloadRate >= 0.1)
148
downloadRateString = [NSString stringForSpeedAbbrev: downloadRate];
149
if (checkUpload && uploadRate >= 0.1)
150
uploadRateString = [NSString stringForSpeedAbbrev: uploadRate];
152
speedBadge = uploadRateString || downloadRateString;
156
fDockIcon = [[NSImage imageNamed: @"NSApplicationIcon"] copy];
157
dockIcon = [fDockIcon copy];
160
badgeRect.size = [[NSImage imageNamed: @"UploadBadge"] size];
161
badgeRect.origin = NSZeroPoint;
163
//ignore shadow of badge when placing string
164
NSRect stringRect = badgeRect;
165
stringRect.size.height -= SPEED_BOTTOM_PADDING;
166
stringRect.origin.y += SPEED_BOTTOM_PADDING;
168
[dockIcon lockFocus];
170
if (uploadRateString)
172
//place badge and text
173
[[NSImage imageNamed: @"UploadBadge"] compositeToPoint: badgeRect.origin operation: NSCompositeSourceOver];
174
[self badgeString: uploadRateString forRect: stringRect];
177
if (downloadRateString)
179
//download rate above upload rate
180
if (uploadRateString)
182
float spaceBetween = badgeRect.size.height + SPEED_BETWEEN_PADDING;
183
badgeRect.origin.y += spaceBetween;
184
stringRect.origin.y += spaceBetween;
187
//place badge and text
188
[[NSImage imageNamed: @"DownloadBadge"] compositeToPoint: badgeRect.origin operation: NSCompositeSourceOver];
189
[self badgeString: downloadRateString forRect: stringRect];
192
[dockIcon unlockFocus];
197
if (fSpeedBadge || speedBadge || completedChange)
199
[NSApp setApplicationIconImage: dockIcon ? dockIcon : fDockIcon];
202
fSpeedBadge = speedBadge;
206
- (void) incrementCompleted
210
if ([NSApp isOnLeopardOrBetter])
211
[[NSApp dockTile] setBadgeLabel: [NSString stringWithFormat: @"%d", fCompleted]];
216
- (void) clearCompleted
221
if ([NSApp isOnLeopardOrBetter])
222
[[NSApp dockTile] setBadgeLabel: @""];
230
if ([NSApp isOnLeopardOrBetter])
232
[self clearCompleted];
233
[(BadgeView *)[[NSApp dockTile] contentView] setQuitting];
238
fQuittingTiger = YES;
242
fCompletedBadged = 0;
244
NSImage * quitIcon = [[NSImage imageNamed: @"NSApplicationIcon"] copy];
245
NSRect rect = NSZeroRect;
246
rect.size = [quitIcon size];
248
NSRect badgeRect = NSMakeRect(0.0, (rect.size.height - BADGE_HEIGHT) * 0.5, rect.size.width, BADGE_HEIGHT);
249
NSBezierPath * bp = [NSBezierPath bezierPathWithRoundedRect: badgeRect radius: 15.0];
251
[quitIcon lockFocus];
253
[[NSColor colorWithCalibratedWhite: 0.0 alpha: 0.75] set];
256
[self badgeString: NSLocalizedString(@"Quitting", "Dock Badger -> quit message") forRect: badgeRect];
258
[quitIcon unlockFocus];
260
[NSApp setApplicationIconImage: quitIcon];
267
@implementation Badger (Private)
269
//dock icon must have locked focus
270
- (void) badgeString: (NSString *) string forRect: (NSRect) rect
274
NSShadow * stringShadow = [[NSShadow alloc] init];
275
[stringShadow setShadowOffset: NSMakeSize(2.0, -2.0)];
276
[stringShadow setShadowBlurRadius: 4.0];
278
fAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
279
[NSColor whiteColor], NSForegroundColorAttributeName,
280
[NSFont boldSystemFontOfSize: 26.0], NSFontAttributeName, stringShadow, NSShadowAttributeName, nil];
282
[stringShadow release];
285
NSSize stringSize = [string sizeWithAttributes: fAttributes];
287
//string is in center of image
288
rect.origin.x += (rect.size.width - stringSize.width) * 0.5;
289
rect.origin.y += (rect.size.height - stringSize.height) * 0.5;
290
rect.size = stringSize;
292
[string drawInRect: rect withAttributes: fAttributes];