~ubuntu-branches/ubuntu/maverick/transmission/maverick

« back to all changes in this revision

Viewing changes to macosx/Badger.m

  • Committer: Bazaar Package Importer
  • Author(s): Philipp Benner
  • Date: 2006-08-29 20:50:41 UTC
  • Revision ID: james.westby@ubuntu.com-20060829205041-mzhpiqksdf7pbk17
Tags: upstream-0.6.1.dfsg
ImportĀ upstreamĀ versionĀ 0.6.1.dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************************************
 
2
 * $Id: Badger.m 424 2006-06-21 23:46:41Z 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
        fBadge = [NSImage imageNamed: @"Badge"];
 
41
        fDockIcon = [[NSApp applicationIconImage] copy];
 
42
        fBadgedDockIcon = [fDockIcon copy];
 
43
        fUploadBadge = [NSImage imageNamed: @"UploadBadge"];
 
44
        fDownloadBadge = [NSImage imageNamed: @"DownloadBadge"];
 
45
        
 
46
        NSShadow * stringShadow = [[NSShadow alloc] init];
 
47
        [stringShadow setShadowOffset: NSMakeSize(2.0, -2.0)];
 
48
        [stringShadow setShadowBlurRadius: 4.0];
 
49
        
 
50
        fAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
 
51
            [NSColor whiteColor], NSForegroundColorAttributeName,
 
52
            [NSFont fontWithName: @"Helvetica-Bold" size: 28.0], NSFontAttributeName,
 
53
            stringShadow, NSShadowAttributeName, nil];
 
54
        
 
55
        [stringShadow release];
 
56
        
 
57
        fCompleted = 0;
 
58
        fSpeedShown = NO;
 
59
    }
 
60
    
 
61
    return self;
 
62
}
 
63
 
 
64
- (void) dealloc
 
65
{
 
66
    [fDockIcon release];
 
67
    [fBadgedDockIcon release];
 
68
    [fAttributes release];
 
69
 
 
70
    [super dealloc];
 
71
}
 
72
 
 
73
- (void) updateBadgeWithCompleted: (int) completed
 
74
                    uploadRate: (float) uploadRate
 
75
                    downloadRate: (float) downloadRate
 
76
{
 
77
    NSImage * dockIcon = nil;
 
78
    NSSize iconSize = [fDockIcon size];
 
79
 
 
80
    //set completed badge
 
81
    if (fCompleted != completed)
 
82
    {
 
83
        fCompleted = completed;
 
84
        dockIcon = [fDockIcon copy];
 
85
        
 
86
        //set completed badge to top right
 
87
        if (completed > 0)
 
88
        {
 
89
            NSRect badgeRect;
 
90
            badgeRect.size = [fBadge size];
 
91
            badgeRect.origin.x = iconSize.width - badgeRect.size.width;
 
92
            badgeRect.origin.y = iconSize.height - badgeRect.size.height;
 
93
                                        
 
94
            [dockIcon lockFocus];
 
95
            
 
96
            //place badge
 
97
            [fBadge compositeToPoint: badgeRect.origin
 
98
                        operation: NSCompositeSourceOver];
 
99
            
 
100
            //ignore shadow of badge when placing string
 
101
            float badgeBottomExtra = 5.0;
 
102
            badgeRect.size.height -= badgeBottomExtra;
 
103
            badgeRect.origin.y += badgeBottomExtra;
 
104
            
 
105
            //place badge text
 
106
            [self badgeString: [NSString stringWithInt: completed]
 
107
                        forRect: badgeRect];
 
108
                        
 
109
            [dockIcon unlockFocus];
 
110
        }
 
111
        
 
112
        [fBadgedDockIcon release];
 
113
        fBadgedDockIcon = [dockIcon copy];
 
114
    }
 
115
 
 
116
    //set upload and download rate badges
 
117
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
 
118
    NSString * uploadRateString = uploadRate >= 0.1 && [defaults boolForKey: @"BadgeUploadRate"]
 
119
                                    ? [NSString stringForSpeedAbbrev: uploadRate] : nil,
 
120
            * downloadRateString = downloadRate >= 0.1 && [defaults boolForKey: @"BadgeDownloadRate"]
 
121
                                    ? [NSString stringForSpeedAbbrev: downloadRate] : nil;
 
122
    
 
123
    BOOL speedShown = uploadRateString || downloadRateString;
 
124
    if (speedShown)
 
125
    {
 
126
        NSRect badgeRect;
 
127
        badgeRect.size = [fUploadBadge size];
 
128
        badgeRect.origin = NSZeroPoint;
 
129
        
 
130
        //ignore shadow of badge when placing string
 
131
        NSRect stringRect = badgeRect;
 
132
        float badgeBottomExtra = 2.0;
 
133
        stringRect.size.height -= badgeBottomExtra;
 
134
        stringRect.origin.y += badgeBottomExtra;
 
135
 
 
136
        if (!dockIcon)
 
137
            dockIcon = [fBadgedDockIcon copy];
 
138
        
 
139
        [dockIcon lockFocus];
 
140
        
 
141
        if (uploadRateString)
 
142
        {
 
143
            //place badge
 
144
            [fUploadBadge compositeToPoint: badgeRect.origin
 
145
                        operation: NSCompositeSourceOver];
 
146
            
 
147
            //place badge text
 
148
            [self badgeString: uploadRateString forRect: stringRect];
 
149
        }
 
150
        
 
151
        if (downloadRateString)
 
152
        {
 
153
            //download rate above upload rate
 
154
            if (uploadRateString)
 
155
            {
 
156
                float spaceBetween = badgeRect.size.height + 2.0;
 
157
                badgeRect.origin.y += spaceBetween;
 
158
                stringRect.origin.y += spaceBetween;
 
159
            }
 
160
        
 
161
            //place badge
 
162
            [fDownloadBadge compositeToPoint: badgeRect.origin
 
163
                        operation: NSCompositeSourceOver];
 
164
            
 
165
            //place badge text
 
166
            [self badgeString: downloadRateString forRect: stringRect];
 
167
        }
 
168
        
 
169
        [dockIcon unlockFocus];
 
170
    }
 
171
 
 
172
    if (dockIcon || fSpeedShown)
 
173
    {
 
174
        if (!dockIcon)
 
175
            dockIcon = [fBadgedDockIcon copy];
 
176
            
 
177
        [NSApp setApplicationIconImage: dockIcon];
 
178
        [dockIcon release];
 
179
    }
 
180
    
 
181
    fSpeedShown = speedShown;
 
182
}
 
183
 
 
184
- (void) clearBadge
 
185
{
 
186
    [fBadgedDockIcon release];
 
187
    fBadgedDockIcon = [fDockIcon copy];
 
188
 
 
189
    [NSApp setApplicationIconImage: fDockIcon];
 
190
}
 
191
 
 
192
@end
 
193
 
 
194
@implementation Badger (Private)
 
195
 
 
196
//dock icon must have locked focus
 
197
- (void) badgeString: (NSString *) string forRect: (NSRect) rect
 
198
{
 
199
    NSSize stringSize = [string sizeWithAttributes: fAttributes];
 
200
    
 
201
    //string is in center of image
 
202
    rect.origin.x += (rect.size.width - stringSize.width) * 0.5;
 
203
    rect.origin.y += (rect.size.height - stringSize.height) * 0.5;
 
204
                        
 
205
    [string drawAtPoint: rect.origin withAttributes: fAttributes];
 
206
}
 
207
 
 
208
@end