source: trunk/macosx/Badger.m @ 3695

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

if the download finishes/becomes unfinished, keep trying to reset the time machine setting, and other miscellaneous cleanup

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