transmission/macosx/Badger.mm

81 lines
2.0 KiB
Plaintext
Raw Normal View History

// This file Copyright © 2006-2022 Transmission authors and contributors.
// It may be used under the MIT (SPDX: MIT) license.
// License text can be found in the licenses/ folder.
2007-09-16 01:02:06 +00:00
#import "Badger.h"
#import "BadgeView.h"
#import "NSStringAdditions.h"
#import "Torrent.h"
2007-09-16 01:02:06 +00:00
@interface Badger ()
@property(nonatomic, readonly) tr_session* fLib;
@property(nonatomic, readonly) NSMutableSet* fHashes;
@end
2007-09-16 01:02:06 +00:00
@implementation Badger
- (instancetype)initWithLib:(tr_session*)lib
2007-09-16 01:02:06 +00:00
{
if ((self = [super init]))
{
_fLib = lib;
BadgeView* view = [[BadgeView alloc] initWithLib:lib];
NSApp.dockTile.contentView = view;
_fHashes = [[NSMutableSet alloc] init];
2007-09-16 01:02:06 +00:00
}
2007-09-16 01:02:06 +00:00
return self;
}
- (void)updateBadgeWithDownload:(CGFloat)downloadRate upload:(CGFloat)uploadRate
2007-09-16 01:02:06 +00:00
{
CGFloat const displayDlRate = [NSUserDefaults.standardUserDefaults boolForKey:@"BadgeDownloadRate"] ? downloadRate : 0.0;
CGFloat const displayUlRate = [NSUserDefaults.standardUserDefaults boolForKey:@"BadgeUploadRate"] ? uploadRate : 0.0;
2009-01-03 06:06:21 +00:00
//only update if the badged values change
if ([(BadgeView*)NSApp.dockTile.contentView setRatesWithDownload:displayDlRate upload:displayUlRate])
{
[NSApp.dockTile display];
}
2007-09-16 01:02:06 +00:00
}
- (void)addCompletedTorrent:(Torrent*)torrent
{
NSParameterAssert(torrent != nil);
[self.fHashes addObject:torrent.hashString];
NSApp.dockTile.badgeLabel = [NSString localizedStringWithFormat:@"%lu", self.fHashes.count];
}
- (void)removeTorrent:(Torrent*)torrent
2007-09-16 01:02:06 +00:00
{
if ([self.fHashes member:torrent.hashString])
{
[self.fHashes removeObject:torrent.hashString];
if (self.fHashes.count > 0)
{
NSApp.dockTile.badgeLabel = [NSString localizedStringWithFormat:@"%lu", self.fHashes.count];
}
else
{
NSApp.dockTile.badgeLabel = @"";
}
}
2007-09-16 01:02:06 +00:00
}
- (void)clearCompleted
2007-09-16 01:02:06 +00:00
{
if (self.fHashes.count > 0)
2007-09-16 01:02:06 +00:00
{
[self.fHashes removeAllObjects];
NSApp.dockTile.badgeLabel = @"";
2007-09-16 01:02:06 +00:00
}
}
@end