source: trunk/macosx/Badger.m @ 3634

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

typo in last commit

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