2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Transmission authors and contributors.
|
2022-01-20 18:27:56 +00:00
|
|
|
// It may be used under the MIT (SPDX: MIT) license.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2011-02-20 00:22:55 +00:00
|
|
|
|
|
|
|
#import "StatusBarController.h"
|
|
|
|
#import "NSStringAdditions.h"
|
|
|
|
|
2022-10-19 19:28:21 +00:00
|
|
|
typedef NSString* StatusRatioType NS_TYPED_EXTENSIBLE_ENUM;
|
|
|
|
|
|
|
|
static StatusRatioType const StatusRatioTypeTotal = @"RatioTotal";
|
|
|
|
static StatusRatioType const StatusRatioTypeSession = @"RatioSession";
|
|
|
|
|
|
|
|
typedef NSString* StatusTransferType NS_TYPED_EXTENSIBLE_ENUM;
|
|
|
|
|
|
|
|
static StatusTransferType const StatusTransferTypeTotal = @"TransferTotal";
|
|
|
|
static StatusTransferType const StatusTransferTypeSession = @"TransferSession";
|
2011-02-20 00:22:55 +00:00
|
|
|
|
2023-04-15 21:33:43 +00:00
|
|
|
typedef NS_ENUM(NSUInteger, StatusTag) {
|
|
|
|
StatusTagTotalRatio = 0,
|
|
|
|
StatusTagSessionRatio = 1,
|
|
|
|
StatusTagTotalTransfer = 2,
|
|
|
|
StatusTagSessionTransfer = 3
|
2021-08-07 07:27:56 +00:00
|
|
|
};
|
2011-02-20 00:22:55 +00:00
|
|
|
|
2022-01-24 01:32:45 +00:00
|
|
|
@interface StatusBarController ()
|
2011-02-20 00:22:55 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
@property(nonatomic) IBOutlet NSButton* fStatusButton;
|
|
|
|
@property(nonatomic) IBOutlet NSTextField* fTotalDLField;
|
|
|
|
@property(nonatomic) IBOutlet NSTextField* fTotalULField;
|
|
|
|
@property(nonatomic) IBOutlet NSImageView* fTotalDLImageView;
|
|
|
|
@property(nonatomic) IBOutlet NSImageView* fTotalULImageView;
|
|
|
|
|
|
|
|
@property(nonatomic, readonly) tr_session* fLib;
|
|
|
|
|
|
|
|
@property(nonatomic) CGFloat fPreviousDownloadRate;
|
|
|
|
@property(nonatomic) CGFloat fPreviousUploadRate;
|
|
|
|
|
2011-02-20 00:22:55 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation StatusBarController
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (instancetype)initWithLib:(tr_session*)lib
|
2011-02-20 00:22:55 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
if ((self = [super initWithNibName:@"StatusBar" bundle:nil]))
|
2011-02-20 00:22:55 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
_fLib = lib;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
_fPreviousDownloadRate = -1.0;
|
|
|
|
_fPreviousUploadRate = -1.0;
|
2011-02-20 00:22:55 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2011-02-20 00:22:55 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)awakeFromNib
|
2011-02-20 00:22:55 +00:00
|
|
|
{
|
|
|
|
//localize menu items
|
2023-04-15 21:33:43 +00:00
|
|
|
[self.fStatusButton.menu itemWithTag:StatusTagTotalRatio].title = NSLocalizedString(@"Total Ratio", "Status Bar -> status menu");
|
|
|
|
[self.fStatusButton.menu itemWithTag:StatusTagSessionRatio].title = NSLocalizedString(@"Session Ratio", "Status Bar -> status menu");
|
|
|
|
[self.fStatusButton.menu itemWithTag:StatusTagTotalTransfer].title = NSLocalizedString(@"Total Transfer", "Status Bar -> status menu");
|
|
|
|
[self.fStatusButton.menu itemWithTag:StatusTagSessionTransfer].title = NSLocalizedString(@"Session Transfer", "Status Bar -> status menu");
|
2021-08-07 07:27:56 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fStatusButton.cell.backgroundStyle = NSBackgroundStyleRaised;
|
|
|
|
self.fTotalDLField.cell.backgroundStyle = NSBackgroundStyleRaised;
|
|
|
|
self.fTotalULField.cell.backgroundStyle = NSBackgroundStyleRaised;
|
|
|
|
self.fTotalDLImageView.cell.backgroundStyle = NSBackgroundStyleRaised;
|
|
|
|
self.fTotalULImageView.cell.backgroundStyle = NSBackgroundStyleRaised;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2011-02-20 00:22:55 +00:00
|
|
|
[self updateSpeedFieldsToolTips];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2011-02-20 00:22:55 +00:00
|
|
|
//update when speed limits are changed
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(updateSpeedFieldsToolTips) name:@"SpeedLimitUpdate"
|
|
|
|
object:nil];
|
2011-02-20 00:22:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)updateWithDownload:(CGFloat)dlRate upload:(CGFloat)ulRate
|
2011-02-20 00:22:55 +00:00
|
|
|
{
|
|
|
|
//set rates
|
2022-02-22 16:04:20 +00:00
|
|
|
if (dlRate != self.fPreviousDownloadRate)
|
2011-08-15 00:17:18 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fTotalDLField.stringValue = [NSString stringForSpeed:dlRate];
|
|
|
|
self.fPreviousDownloadRate = dlRate;
|
2011-08-15 00:17:18 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
if (ulRate != self.fPreviousUploadRate)
|
2011-08-15 00:17:18 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fTotalULField.stringValue = [NSString stringForSpeed:ulRate];
|
|
|
|
self.fPreviousUploadRate = ulRate;
|
2011-08-15 00:17:18 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2011-02-20 00:22:55 +00:00
|
|
|
//set status button text
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString *statusLabel = [NSUserDefaults.standardUserDefaults stringForKey:@"StatusLabel"], *statusString;
|
2011-02-20 00:22:55 +00:00
|
|
|
BOOL total;
|
2022-10-19 19:28:21 +00:00
|
|
|
if ((total = [statusLabel isEqualToString:StatusRatioTypeTotal]) || [statusLabel isEqualToString:StatusRatioTypeSession])
|
2011-02-20 00:22:55 +00:00
|
|
|
{
|
2022-07-28 23:56:40 +00:00
|
|
|
auto const stats = total ? tr_sessionGetCumulativeStats(self.fLib) : tr_sessionGetStats(self.fLib);
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
statusString = [NSLocalizedString(@"Ratio", "status bar -> status label")
|
|
|
|
stringByAppendingFormat:@": %@", [NSString stringForRatio:stats.ratio]];
|
2011-02-20 00:22:55 +00:00
|
|
|
}
|
2022-10-19 19:28:21 +00:00
|
|
|
else //StatusTransferTypeTotal or StatusTransferTypeSession
|
2011-02-20 00:22:55 +00:00
|
|
|
{
|
2022-10-19 19:28:21 +00:00
|
|
|
total = [statusLabel isEqualToString:StatusTransferTypeTotal];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-07-28 23:56:40 +00:00
|
|
|
auto const stats = total ? tr_sessionGetCumulativeStats(self.fLib) : tr_sessionGetStats(self.fLib);
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
statusString = [NSString stringWithFormat:@"%@: %@ %@: %@",
|
|
|
|
NSLocalizedString(@"DL", "status bar -> status label"),
|
|
|
|
[NSString stringForFileSize:stats.downloadedBytes],
|
|
|
|
NSLocalizedString(@"UL", "status bar -> status label"),
|
|
|
|
[NSString stringForFileSize:stats.uploadedBytes]];
|
2011-02-20 00:22:55 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
if (![self.fStatusButton.title isEqualToString:statusString])
|
2011-08-15 11:16:44 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fStatusButton.title = statusString;
|
2011-08-15 00:17:18 +00:00
|
|
|
}
|
2011-02-20 00:22:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)setStatusLabel:(id)sender
|
2011-02-20 00:22:55 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* statusLabel;
|
2011-02-20 00:22:55 +00:00
|
|
|
switch ([sender tag])
|
|
|
|
{
|
2023-04-15 21:33:43 +00:00
|
|
|
case StatusTagTotalRatio:
|
2022-10-19 19:28:21 +00:00
|
|
|
statusLabel = StatusRatioTypeTotal;
|
2021-08-15 09:41:48 +00:00
|
|
|
break;
|
2023-04-15 21:33:43 +00:00
|
|
|
case StatusTagSessionRatio:
|
2022-10-19 19:28:21 +00:00
|
|
|
statusLabel = StatusRatioTypeSession;
|
2021-08-15 09:41:48 +00:00
|
|
|
break;
|
2023-04-15 21:33:43 +00:00
|
|
|
case StatusTagTotalTransfer:
|
2022-10-19 19:28:21 +00:00
|
|
|
statusLabel = StatusTransferTypeTotal;
|
2021-08-15 09:41:48 +00:00
|
|
|
break;
|
2023-04-15 21:33:43 +00:00
|
|
|
case StatusTagSessionTransfer:
|
2022-10-19 19:28:21 +00:00
|
|
|
statusLabel = StatusTransferTypeSession;
|
2021-08-15 09:41:48 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NSAssert1(NO, @"Unknown status label tag received: %ld", [sender tag]);
|
|
|
|
return;
|
2011-02-20 00:22:55 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSUserDefaults.standardUserDefaults setObject:statusLabel forKey:@"StatusLabel"];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSNotificationCenter.defaultCenter postNotificationName:@"UpdateUI" object:nil];
|
2011-02-20 00:22:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)updateSpeedFieldsToolTips
|
2011-02-20 00:22:55 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString *uploadText, *downloadText;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
if ([NSUserDefaults.standardUserDefaults boolForKey:@"SpeedLimit"])
|
2011-02-20 00:22:55 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* speedString = [NSString stringWithFormat:@"%@ (%@)",
|
2022-05-14 05:31:24 +00:00
|
|
|
NSLocalizedString(@"%ld KB/s", "Status Bar -> speed tooltip"),
|
2021-08-15 09:41:48 +00:00
|
|
|
NSLocalizedString(@"Speed Limit", "Status Bar -> speed tooltip")];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
uploadText = [NSString stringWithFormat:speedString, [NSUserDefaults.standardUserDefaults integerForKey:@"SpeedLimitUploadLimit"]];
|
|
|
|
downloadText = [NSString stringWithFormat:speedString, [NSUserDefaults.standardUserDefaults integerForKey:@"SpeedLimitDownloadLimit"]];
|
2011-02-20 00:22:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
if ([NSUserDefaults.standardUserDefaults boolForKey:@"CheckUpload"])
|
|
|
|
{
|
2022-11-14 18:30:03 +00:00
|
|
|
uploadText = [NSString localizedStringWithFormat:NSLocalizedString(@"%ld KB/s", "Status Bar -> speed tooltip"),
|
|
|
|
[NSUserDefaults.standardUserDefaults integerForKey:@"UploadLimit"]];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2011-02-20 00:22:55 +00:00
|
|
|
else
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2011-02-20 00:22:55 +00:00
|
|
|
uploadText = NSLocalizedString(@"unlimited", "Status Bar -> speed tooltip");
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
if ([NSUserDefaults.standardUserDefaults boolForKey:@"CheckDownload"])
|
|
|
|
{
|
2022-11-14 18:30:03 +00:00
|
|
|
downloadText = [NSString localizedStringWithFormat:NSLocalizedString(@"%ld KB/s", "Status Bar -> speed tooltip"),
|
|
|
|
[NSUserDefaults.standardUserDefaults integerForKey:@"DownloadLimit"]];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2011-02-20 00:22:55 +00:00
|
|
|
else
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2011-02-20 00:22:55 +00:00
|
|
|
downloadText = NSLocalizedString(@"unlimited", "Status Bar -> speed tooltip");
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2011-02-20 00:22:55 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
uploadText = [NSLocalizedString(@"Global upload limit", "Status Bar -> speed tooltip") stringByAppendingFormat:@": %@", uploadText];
|
|
|
|
downloadText = [NSLocalizedString(@"Global download limit", "Status Bar -> speed tooltip") stringByAppendingFormat:@": %@", downloadText];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fTotalULField.toolTip = uploadText;
|
|
|
|
self.fTotalDLField.toolTip = downloadText;
|
2011-02-20 00:22:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (BOOL)validateMenuItem:(NSMenuItem*)menuItem
|
2011-02-20 00:22:55 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
SEL const action = menuItem.action;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2011-02-20 00:22:55 +00:00
|
|
|
//enable sort options
|
|
|
|
if (action == @selector(setStatusLabel:))
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* statusLabel;
|
2021-08-07 07:27:56 +00:00
|
|
|
switch (menuItem.tag)
|
2011-02-20 00:22:55 +00:00
|
|
|
{
|
2023-04-15 21:33:43 +00:00
|
|
|
case StatusTagTotalRatio:
|
2022-10-19 19:28:21 +00:00
|
|
|
statusLabel = StatusRatioTypeTotal;
|
2021-08-15 09:41:48 +00:00
|
|
|
break;
|
2023-04-15 21:33:43 +00:00
|
|
|
case StatusTagSessionRatio:
|
2022-10-19 19:28:21 +00:00
|
|
|
statusLabel = StatusRatioTypeSession;
|
2021-08-15 09:41:48 +00:00
|
|
|
break;
|
2023-04-15 21:33:43 +00:00
|
|
|
case StatusTagTotalTransfer:
|
2022-10-19 19:28:21 +00:00
|
|
|
statusLabel = StatusTransferTypeTotal;
|
2021-08-15 09:41:48 +00:00
|
|
|
break;
|
2023-04-15 21:33:43 +00:00
|
|
|
case StatusTagSessionTransfer:
|
2022-10-19 19:28:21 +00:00
|
|
|
statusLabel = StatusTransferTypeSession;
|
2021-08-15 09:41:48 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NSAssert1(NO, @"Unknown status label tag received: %ld", menuItem.tag);
|
2022-10-19 19:28:21 +00:00
|
|
|
statusLabel = StatusRatioTypeTotal;
|
2011-02-20 00:22:55 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-03-30 21:52:23 +00:00
|
|
|
menuItem.state = [statusLabel isEqualToString:[NSUserDefaults.standardUserDefaults stringForKey:@"StatusLabel"]] ?
|
|
|
|
NSControlStateValueOn :
|
|
|
|
NSControlStateValueOff;
|
2011-02-20 00:22:55 +00:00
|
|
|
return YES;
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2011-02-20 00:22:55 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|