~ubuntu-branches/ubuntu/hardy/transmission/hardy-updates

« back to all changes in this revision

Viewing changes to macosx/Badger.m

  • Committer: Bazaar Package Importer
  • Author(s): Philipp Benner
  • Date: 2008-01-05 09:16:52 UTC
  • mto: This revision was merged to the branch mainline in revision 11.
  • Revision ID: james.westby@ubuntu.com-20080105091652-8cf0z4rb3pu8d6jt
Tags: upstream-1.00
ImportĀ upstreamĀ versionĀ 1.00

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************************************
 
2
 * $Id: Badger.m 4423 2008-01-02 16:55:05Z livings124 $
 
3
 *
 
4
 * Copyright (c) 2006-2008 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
#import "NSBezierPathAdditions.h"
 
30
 
 
31
#define COMPLETED_BOTTOM_PADDING 5.0
 
32
#define SPEED_BOTTOM_PADDING 2.0
 
33
#define SPEED_BETWEEN_PADDING 2.0
 
34
#define BADGE_HEIGHT 30.0
 
35
 
 
36
@interface Badger (Private)
 
37
 
 
38
- (void) badgeString: (NSString *) string forRect: (NSRect) rect;
 
39
 
 
40
@end
 
41
 
 
42
@implementation Badger
 
43
 
 
44
- (id) initWithLib: (tr_handle *) lib
 
45
{
 
46
    if ((self = [super init]))
 
47
    {
 
48
        fLib = lib;
 
49
        
 
50
        fCompleted = 0;
 
51
        fCompletedBadged = 0;
 
52
        fSpeedBadge = NO;
 
53
        
 
54
        if ([NSApp isOnLeopardOrBetter])
 
55
        {
 
56
            BadgeView * view = [[BadgeView alloc] initWithFrame: [[[NSApp dockTile] contentView] frame] lib: lib];
 
57
            [[NSApp dockTile] setContentView: view];
 
58
            [view release];
 
59
        }
 
60
        else
 
61
            fQuittingTiger = NO;
 
62
        
 
63
        //change that just impacts the dock badge
 
64
        [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateBadge) name: @"DockBadgeChange" object: nil];
 
65
    }
 
66
    
 
67
    return self;
 
68
}
 
69
 
 
70
- (void) dealloc
 
71
{
 
72
    [[NSNotificationCenter defaultCenter] removeObserver: self];
 
73
    
 
74
    [NSApp setApplicationIconImage: nil]; //needed on 10.4
 
75
    
 
76
    [fDockIcon release];
 
77
    [fAttributes release];
 
78
    
 
79
    [super dealloc];
 
80
}
 
81
 
 
82
- (void) updateBadge
 
83
{
 
84
    if ([NSApp isOnLeopardOrBetter])
 
85
    {
 
86
        [[NSApp dockTile] display];
 
87
        return;
 
88
    }
 
89
    else if (fQuittingTiger)
 
90
        return;
 
91
    else;
 
92
    
 
93
    //set completed badge to top right
 
94
    BOOL completedChange = fCompleted != fCompletedBadged;
 
95
    if (completedChange)
 
96
    {
 
97
        fCompletedBadged = fCompleted;
 
98
        
 
99
        //force image to reload - copy does not work
 
100
        NSImage * icon = [[NSImage imageNamed: @"NSApplicationIcon"] copy];
 
101
        NSSize iconSize = [icon size];
 
102
        
 
103
        [fDockIcon release];
 
104
        fDockIcon = [[NSImage alloc] initWithSize: iconSize];
 
105
        [fDockIcon addRepresentation: [icon bestRepresentationForDevice: nil]];
 
106
        [icon release];
 
107
        
 
108
        if (fCompleted > 0)
 
109
        {
 
110
            if (!fBadge)
 
111
                fBadge = [NSImage imageNamed: @"Badge"];
 
112
            
 
113
            NSRect badgeRect;
 
114
            badgeRect.size = [fBadge size];
 
115
            badgeRect.origin.x = iconSize.width - badgeRect.size.width;
 
116
            badgeRect.origin.y = iconSize.height - badgeRect.size.height;
 
117
            
 
118
            [fDockIcon lockFocus];
 
119
            
 
120
            //place badge
 
121
            [fBadge compositeToPoint: badgeRect.origin operation: NSCompositeSourceOver];
 
122
            
 
123
            //ignore shadow of badge when placing string
 
124
            badgeRect.size.height -= COMPLETED_BOTTOM_PADDING;
 
125
            badgeRect.origin.y += COMPLETED_BOTTOM_PADDING;
 
126
            
 
127
            //place badge text
 
128
            [self badgeString: [NSString stringWithFormat: @"%d", fCompleted] forRect: badgeRect];
 
129
                        
 
130
            [fDockIcon unlockFocus];
 
131
        }
 
132
    }
 
133
    
 
134
    NSImage * dockIcon = nil;
 
135
    BOOL speedBadge = NO;
 
136
    
 
137
    BOOL checkDownload = [[NSUserDefaults standardUserDefaults] boolForKey: @"BadgeDownloadRate"],
 
138
        checkUpload = [[NSUserDefaults standardUserDefaults] boolForKey: @"BadgeUploadRate"];
 
139
    if (checkDownload || checkUpload)
 
140
    {
 
141
        //set upload and download rate badges
 
142
        NSString * downloadRateString = nil, * uploadRateString = nil;
 
143
        
 
144
        float downloadRate, uploadRate;
 
145
        tr_torrentRates(fLib, &downloadRate, &uploadRate);
 
146
        
 
147
        if (checkDownload && downloadRate >= 0.1)
 
148
            downloadRateString = [NSString stringForSpeedAbbrev: downloadRate];
 
149
        if (checkUpload && uploadRate >= 0.1)
 
150
            uploadRateString = [NSString stringForSpeedAbbrev: uploadRate];
 
151
        
 
152
        speedBadge = uploadRateString || downloadRateString;
 
153
        if (speedBadge)
 
154
        {
 
155
            if (!fDockIcon)
 
156
                fDockIcon = [[NSImage imageNamed: @"NSApplicationIcon"] copy];
 
157
            dockIcon = [fDockIcon copy];
 
158
            
 
159
            NSRect badgeRect;
 
160
            badgeRect.size = [[NSImage imageNamed: @"UploadBadge"] size];
 
161
            badgeRect.origin = NSZeroPoint;
 
162
            
 
163
            //ignore shadow of badge when placing string
 
164
            NSRect stringRect = badgeRect;
 
165
            stringRect.size.height -= SPEED_BOTTOM_PADDING;
 
166
            stringRect.origin.y += SPEED_BOTTOM_PADDING;
 
167
            
 
168
            [dockIcon lockFocus];
 
169
            
 
170
            if (uploadRateString)
 
171
            {
 
172
                //place badge and text
 
173
                [[NSImage imageNamed: @"UploadBadge"] compositeToPoint: badgeRect.origin operation: NSCompositeSourceOver];
 
174
                [self badgeString: uploadRateString forRect: stringRect];
 
175
            }
 
176
            
 
177
            if (downloadRateString)
 
178
            {
 
179
                //download rate above upload rate
 
180
                if (uploadRateString)
 
181
                {
 
182
                    float spaceBetween = badgeRect.size.height + SPEED_BETWEEN_PADDING;
 
183
                    badgeRect.origin.y += spaceBetween;
 
184
                    stringRect.origin.y += spaceBetween;
 
185
                }
 
186
                
 
187
                //place badge and text
 
188
                [[NSImage imageNamed: @"DownloadBadge"] compositeToPoint: badgeRect.origin operation: NSCompositeSourceOver];
 
189
                [self badgeString: downloadRateString forRect: stringRect];
 
190
            }
 
191
            
 
192
            [dockIcon unlockFocus];
 
193
        }
 
194
    }
 
195
    
 
196
    //update dock badge
 
197
    if (fSpeedBadge || speedBadge || completedChange)
 
198
    {
 
199
        [NSApp setApplicationIconImage: dockIcon ? dockIcon : fDockIcon];
 
200
        [dockIcon release];
 
201
        
 
202
        fSpeedBadge = speedBadge;
 
203
    }
 
204
}
 
205
 
 
206
- (void) incrementCompleted
 
207
{
 
208
    fCompleted++;
 
209
    
 
210
    if ([NSApp isOnLeopardOrBetter])
 
211
        [[NSApp dockTile] setBadgeLabel: [NSString stringWithFormat: @"%d", fCompleted]];
 
212
    else
 
213
        [self updateBadge];
 
214
}
 
215
 
 
216
- (void) clearCompleted
 
217
{
 
218
    if (fCompleted != 0)
 
219
    {
 
220
        fCompleted = 0;
 
221
        if ([NSApp isOnLeopardOrBetter])
 
222
            [[NSApp dockTile] setBadgeLabel: @""];
 
223
        else
 
224
            [self updateBadge];
 
225
    }
 
226
}
 
227
 
 
228
- (void) setQuitting
 
229
{
 
230
    if ([NSApp isOnLeopardOrBetter])
 
231
    {
 
232
        [self clearCompleted];
 
233
        [(BadgeView *)[[NSApp dockTile] contentView] setQuitting];
 
234
        [self updateBadge];
 
235
    }
 
236
    else
 
237
    {
 
238
        fQuittingTiger = YES;
 
239
        
 
240
        fSpeedBadge = NO;
 
241
        fCompleted = 0;
 
242
        fCompletedBadged = 0;
 
243
        
 
244
        NSImage * quitIcon = [[NSImage imageNamed: @"NSApplicationIcon"] copy];
 
245
        NSRect rect = NSZeroRect;
 
246
        rect.size = [quitIcon size];
 
247
        
 
248
        NSRect badgeRect = NSMakeRect(0.0, (rect.size.height - BADGE_HEIGHT) * 0.5, rect.size.width, BADGE_HEIGHT);
 
249
        NSBezierPath * bp = [NSBezierPath bezierPathWithRoundedRect: badgeRect radius: 15.0];
 
250
        
 
251
        [quitIcon lockFocus];
 
252
            
 
253
        [[NSColor colorWithCalibratedWhite: 0.0 alpha: 0.75] set];
 
254
        [bp fill];
 
255
        
 
256
        [self badgeString: NSLocalizedString(@"Quitting", "Dock Badger -> quit message") forRect: badgeRect];
 
257
        
 
258
        [quitIcon unlockFocus];
 
259
        
 
260
        [NSApp setApplicationIconImage: quitIcon];
 
261
        [quitIcon release];
 
262
    }
 
263
}
 
264
 
 
265
@end
 
266
 
 
267
@implementation Badger (Private)
 
268
 
 
269
//dock icon must have locked focus
 
270
- (void) badgeString: (NSString *) string forRect: (NSRect) rect
 
271
{
 
272
    if (!fAttributes)
 
273
    {
 
274
        NSShadow * stringShadow = [[NSShadow alloc] init];
 
275
        [stringShadow setShadowOffset: NSMakeSize(2.0, -2.0)];
 
276
        [stringShadow setShadowBlurRadius: 4.0];
 
277
        
 
278
        fAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
 
279
            [NSColor whiteColor], NSForegroundColorAttributeName,
 
280
            [NSFont boldSystemFontOfSize: 26.0], NSFontAttributeName, stringShadow, NSShadowAttributeName, nil];
 
281
        
 
282
        [stringShadow release];
 
283
    }
 
284
    
 
285
    NSSize stringSize = [string sizeWithAttributes: fAttributes];
 
286
    
 
287
    //string is in center of image
 
288
    rect.origin.x += (rect.size.width - stringSize.width) * 0.5;
 
289
    rect.origin.y += (rect.size.height - stringSize.height) * 0.5;
 
290
    rect.size = stringSize;
 
291
                        
 
292
    [string drawInRect: rect withAttributes: fAttributes];
 
293
}
 
294
 
 
295
@end