1/******************************************************************************
2 * $Id: Badger.m 13571 2012-10-15 02:12:44Z livings124 $
3 *
4 * Copyright (c) 2006-2012 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 "NSStringAdditions.h"
28#import "Torrent.h"
29
30@implementation Badger
31
32- (id) initWithLib: (tr_session *) lib
33{
34    if ((self = [super init]))
35    {
36        fLib = lib;
37        
38        BadgeView * view = [[BadgeView alloc] initWithLib: lib];
39        [[NSApp dockTile] setContentView: view];
40        [view release];
41        
42        fHashes = [[NSMutableSet alloc] init];
43    }
44    
45    return self;
46}
47
48- (void) dealloc
49{
50    [fHashes release];
51    [super dealloc];
52}
53
54- (void) updateBadgeWithDownload: (CGFloat) downloadRate upload: (CGFloat) uploadRate
55{
56    const CGFloat displayDlRate = [[NSUserDefaults standardUserDefaults] boolForKey: @"BadgeDownloadRate"]
57                                    ? downloadRate : 0.0;
58    const CGFloat displayUlRate = [[NSUserDefaults standardUserDefaults] boolForKey: @"BadgeUploadRate"]
59                                    ? uploadRate : 0.0;
60    
61    //only update if the badged values change
62    if ([(BadgeView *)[[NSApp dockTile] contentView] setRatesWithDownload: displayDlRate upload: displayUlRate])
63        [[NSApp dockTile] display];
64}
65
66- (void) addCompletedTorrent: (Torrent *) torrent
67{
68    NSParameterAssert(torrent != nil);
69    
70    [fHashes addObject: [torrent hashString]];
71    [[NSApp dockTile] setBadgeLabel: [NSString formattedUInteger: [fHashes count]]];
72}
73
74- (void) removeTorrent: (Torrent *) torrent
75{
76    if ([fHashes member: [torrent hashString]])
77    {
78        [fHashes removeObject: [torrent hashString]];
79        if ([fHashes count] > 0)
80            [[NSApp dockTile] setBadgeLabel: [NSString formattedUInteger: [fHashes count]]];
81        else
82            [[NSApp dockTile] setBadgeLabel: @""];
83    }
84}
85
86- (void) clearCompleted
87{
88    if ([fHashes count] > 0)
89    {
90        [fHashes removeAllObjects];
91        [[NSApp dockTile] setBadgeLabel: @""];
92    }
93}
94
95- (void) setQuitting
96{
97    [self clearCompleted];
98    [(BadgeView *)[[NSApp dockTile] contentView] setQuitting];
99    [[NSApp dockTile] display];
100}
101
102@end
103