source: trunk/macosx/Badger.m @ 3384

Last change on this file since 3384 was 3384, checked in by livings124, 15 years ago

trivial code cleanup

  • Property svn:keywords set to Date Rev Author Id
File size: 7.3 KB
Line 
1/******************************************************************************
2 * $Id: Badger.m 3384 2007-10-12 23:18:21Z 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;
62    if (baseChange = (fCompleted != fCompletedBadged))
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    //set upload and download rate badges
103    NSString * downloadRateString = nil, * uploadRateString = nil;
104   
105    BOOL checkDownload = [[NSUserDefaults standardUserDefaults] boolForKey: @"BadgeDownloadRate"],
106        checkUpload = [[NSUserDefaults standardUserDefaults] boolForKey: @"BadgeUploadRate"];
107    if (checkDownload || checkUpload)
108    {
109        float downloadRate, uploadRate;
110        tr_torrentRates(fLib, &downloadRate, &uploadRate);
111       
112        if (checkDownload && downloadRate >= 0.1)
113            downloadRateString = [NSString stringForSpeedAbbrev: downloadRate];
114        if (checkUpload && uploadRate >= 0.1)
115            uploadRateString = [NSString stringForSpeedAbbrev: uploadRate];
116    }
117   
118    NSImage * dockIcon = nil;
119    BOOL speedBadge = uploadRateString || downloadRateString;
120    if (speedBadge)
121    {
122        if (!fDockIcon)
123            fDockIcon = [[NSImage imageNamed: @"NSApplicationIcon"] copy];
124        dockIcon = [fDockIcon copy];
125       
126        if (!fUploadBadge)
127            fUploadBadge = [NSImage imageNamed: @"UploadBadge"];
128        if (!fDownloadBadge)
129            fDownloadBadge = [NSImage imageNamed: @"DownloadBadge"];
130       
131        NSRect badgeRect;
132        badgeRect.size = [fUploadBadge size];
133        badgeRect.origin = NSZeroPoint;
134       
135        //ignore shadow of badge when placing string
136        NSRect stringRect = badgeRect;
137        float badgeBottomExtra = 2.0;
138        stringRect.size.height -= badgeBottomExtra;
139        stringRect.origin.y += badgeBottomExtra;
140       
141        [dockIcon lockFocus];
142       
143        if (uploadRateString)
144        {
145            //place badge
146            [fUploadBadge compositeToPoint: badgeRect.origin operation: NSCompositeSourceOver];
147           
148            //place badge text
149            [self badgeString: uploadRateString forRect: stringRect];
150        }
151       
152        if (downloadRateString)
153        {
154            //download rate above upload rate
155            if (uploadRateString)
156            {
157                float spaceBetween = badgeRect.size.height + 2.0;
158                badgeRect.origin.y += spaceBetween;
159                stringRect.origin.y += spaceBetween;
160            }
161       
162            //place badge
163            [fDownloadBadge compositeToPoint: badgeRect.origin operation: NSCompositeSourceOver];
164           
165            //place badge text
166            [self badgeString: downloadRateString forRect: stringRect];
167        }
168       
169        [dockIcon unlockFocus];
170    }
171   
172    //update dock badge
173    if (baseChange || fSpeedBadge || speedBadge)
174    {
175        [NSApp setApplicationIconImage: dockIcon ? dockIcon : fDockIcon];
176        [dockIcon release];
177       
178        fSpeedBadge = speedBadge;
179    }
180}
181
182- (void) incrementCompleted
183{
184    fCompleted++;
185    [self updateBadge];
186}
187
188- (void) clearCompleted
189{
190    if (fCompleted != 0)
191    {
192        fCompleted = 0;
193        [self updateBadge];
194    }
195}
196
197- (void) clearBadge
198{
199    fCompleted = 0;
200    fCompletedBadged = 0;
201    fSpeedBadge = NO;
202    [NSApp setApplicationIconImage: [NSImage imageNamed: @"NSApplicationIcon"]];
203}
204
205@end
206
207@implementation Badger (Private)
208
209//dock icon must have locked focus
210- (void) badgeString: (NSString *) string forRect: (NSRect) rect
211{
212    if (!fAttributes)
213    {
214        NSShadow * stringShadow = [[NSShadow alloc] init];
215        [stringShadow setShadowOffset: NSMakeSize(2.0, -2.0)];
216        [stringShadow setShadowBlurRadius: 4.0];
217       
218        fAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
219            [NSColor whiteColor], NSForegroundColorAttributeName,
220            [NSFont boldSystemFontOfSize: 26.0], NSFontAttributeName, stringShadow, NSShadowAttributeName, nil];
221       
222        [stringShadow release];
223    }
224   
225    NSSize stringSize = [string sizeWithAttributes: fAttributes];
226   
227    //string is in center of image
228    rect.origin.x += (rect.size.width - stringSize.width) * 0.5;
229    rect.origin.y += (rect.size.height - stringSize.height) * 0.5;
230                       
231    [string drawAtPoint: rect.origin withAttributes: fAttributes];
232}
233
234@end
Note: See TracBrowser for help on using the repository browser.