source: trunk/macosx/Badger.m @ 1125

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

When loading from url, if set to download at the torrent location ask the user for a location instead

  • Property svn:keywords set to Date Rev Author Id
File size: 6.1 KB
Line 
1/******************************************************************************
2 * $Id: Badger.m 1089 2006-11-14 05:27:43Z livings124 $
3 *
4 * Copyright (c) 2006 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 "StringAdditions.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) init
37{
38    if ((self = [super init]))
39    {
40        fDockIcon = [[NSApp applicationIconImage] copy];
41        fBadge = [NSImage imageNamed: @"Badge"];
42        fUploadBadge = [NSImage imageNamed: @"UploadBadge"];
43        fDownloadBadge = [NSImage imageNamed: @"DownloadBadge"];
44       
45        NSShadow * stringShadow = [[NSShadow alloc] init];
46        [stringShadow setShadowOffset: NSMakeSize(2.0, -2.0)];
47        [stringShadow setShadowBlurRadius: 4.0];
48       
49        NSFont * boldFont = [[NSFontManager sharedFontManager] convertFont:
50                                [NSFont fontWithName: @"Helvetica" size: 28.0] toHaveTrait: NSBoldFontMask];
51       
52        fAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
53            [NSColor whiteColor], NSForegroundColorAttributeName,
54            boldFont, NSFontAttributeName, stringShadow, NSShadowAttributeName, nil];
55       
56        [stringShadow release];
57    }
58   
59    return self;
60}
61
62- (void) dealloc
63{
64    [fDockIcon release];
65    [fAttributes release];
66
67    [super dealloc];
68}
69
70- (void) updateBadgeWithCompleted: (int) completed uploadRate: (float) uploadRate downloadRate: (float) downloadRate
71{
72    NSImage * dockIcon = nil;
73       
74    //set completed badge to top right
75    if (completed > 0)
76    {
77        dockIcon = [fDockIcon copy];
78   
79        NSRect badgeRect;
80        NSSize iconSize = [dockIcon size];
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        [dockIcon 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 stringWithInt: completed] forRect: badgeRect];
97                   
98        [dockIcon unlockFocus];
99    }
100
101    //set upload and download rate badges
102    NSString * uploadRateString = uploadRate >= 0.1 && [[NSUserDefaults standardUserDefaults]
103                    boolForKey: @"BadgeUploadRate"] ? [NSString stringForSpeedAbbrev: uploadRate] : nil,
104            * downloadRateString = downloadRate >= 0.1 && [[NSUserDefaults standardUserDefaults]
105                    boolForKey: @"BadgeDownloadRate"] ? [NSString stringForSpeedAbbrev: downloadRate] : nil;
106   
107    if (uploadRateString || downloadRateString)
108    {
109        if (!dockIcon)
110            dockIcon = [fDockIcon copy];
111       
112        NSRect badgeRect;
113        badgeRect.size = [fUploadBadge size];
114        badgeRect.origin = NSZeroPoint;
115       
116        //ignore shadow of badge when placing string
117        NSRect stringRect = badgeRect;
118        float badgeBottomExtra = 2.0;
119        stringRect.size.height -= badgeBottomExtra;
120        stringRect.origin.y += badgeBottomExtra;
121       
122        [dockIcon lockFocus];
123       
124        if (uploadRateString)
125        {
126            //place badge
127            [fUploadBadge compositeToPoint: badgeRect.origin operation: NSCompositeSourceOver];
128           
129            //place badge text
130            [self badgeString: uploadRateString forRect: stringRect];
131        }
132       
133        if (downloadRateString)
134        {
135            //download rate above upload rate
136            if (uploadRateString)
137            {
138                float spaceBetween = badgeRect.size.height + 2.0;
139                badgeRect.origin.y += spaceBetween;
140                stringRect.origin.y += spaceBetween;
141            }
142       
143            //place badge
144            [fDownloadBadge compositeToPoint: badgeRect.origin operation: NSCompositeSourceOver];
145           
146            //place badge text
147            [self badgeString: downloadRateString forRect: stringRect];
148        }
149       
150        [dockIcon unlockFocus];
151    }
152   
153    //update dock badge
154    if (!dockIcon)
155        dockIcon = [fDockIcon retain];
156   
157    [NSApp setApplicationIconImage: dockIcon];
158    [dockIcon release];
159}
160
161- (void) clearBadge
162{
163    [NSApp setApplicationIconImage: fDockIcon];
164}
165
166@end
167
168@implementation Badger (Private)
169
170//dock icon must have locked focus
171- (void) badgeString: (NSString *) string forRect: (NSRect) rect
172{
173    NSSize stringSize = [string sizeWithAttributes: fAttributes];
174   
175    //string is in center of image
176    rect.origin.x += (rect.size.width - stringSize.width) * 0.5;
177    rect.origin.y += (rect.size.height - stringSize.height) * 0.5;
178                       
179    [string drawAtPoint: rect.origin withAttributes: fAttributes];
180}
181
182@end
Note: See TracBrowser for help on using the repository browser.