2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2011-2022 Transmission authors and contributors.
|
|
|
|
// 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
|
|
|
|
2018-09-30 10:37:30 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
|
2011-02-20 00:22:55 +00:00
|
|
|
#import "StatusBarController.h"
|
|
|
|
#import "NSStringAdditions.h"
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
#define STATUS_RATIO_TOTAL @"RatioTotal"
|
|
|
|
#define STATUS_RATIO_SESSION @"RatioSession"
|
|
|
|
#define STATUS_TRANSFER_TOTAL @"TransferTotal"
|
2011-02-20 00:22:55 +00:00
|
|
|
#define STATUS_TRANSFER_SESSION @"TransferSession"
|
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
typedef NS_ENUM(unsigned int, statusTag) {
|
2011-02-20 00:22:55 +00:00
|
|
|
STATUS_RATIO_TOTAL_TAG = 0,
|
|
|
|
STATUS_RATIO_SESSION_TAG = 1,
|
|
|
|
STATUS_TRANSFER_TOTAL_TAG = 2,
|
|
|
|
STATUS_TRANSFER_SESSION_TAG = 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;
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)resizeStatusButton;
|
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
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fStatusButton.menu itemWithTag:STATUS_RATIO_TOTAL_TAG].title = NSLocalizedString(@"Total Ratio", "Status Bar -> status menu");
|
|
|
|
[self.fStatusButton.menu itemWithTag:STATUS_RATIO_SESSION_TAG].title = NSLocalizedString(@"Session Ratio", "Status Bar -> status menu");
|
|
|
|
[self.fStatusButton.menu itemWithTag:STATUS_TRANSFER_TOTAL_TAG].title = NSLocalizedString(@"Total Transfer", "Status Bar -> status menu");
|
|
|
|
[self.fStatusButton.menu itemWithTag:STATUS_TRANSFER_SESSION_TAG].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];
|
|
|
|
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(resizeStatusButton)
|
|
|
|
name:NSWindowDidResizeNotification
|
|
|
|
object:self.view.window];
|
2011-02-20 00:22:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)dealloc
|
2011-02-20 00:22:55 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSNotificationCenter.defaultCenter removeObserver:self];
|
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;
|
2021-08-15 09:41:48 +00:00
|
|
|
if ((total = [statusLabel isEqualToString:STATUS_RATIO_TOTAL]) || [statusLabel isEqualToString:STATUS_RATIO_SESSION])
|
2011-02-20 00:22:55 +00:00
|
|
|
{
|
|
|
|
tr_session_stats stats;
|
|
|
|
if (total)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
tr_sessionGetCumulativeStats(self.fLib, &stats);
|
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
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
tr_sessionGetStats(self.fLib, &stats);
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
else //STATUS_TRANSFER_TOTAL or STATUS_TRANSFER_SESSION
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
total = [statusLabel isEqualToString:STATUS_TRANSFER_TOTAL];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2011-02-20 00:22:55 +00:00
|
|
|
tr_session_stats stats;
|
|
|
|
if (total)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
tr_sessionGetCumulativeStats(self.fLib, &stats);
|
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
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
tr_sessionGetStats(self.fLib, &stats);
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
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
|
|
|
[self resizeStatusButton];
|
|
|
|
}
|
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])
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
case STATUS_RATIO_TOTAL_TAG:
|
|
|
|
statusLabel = STATUS_RATIO_TOTAL;
|
|
|
|
break;
|
|
|
|
case STATUS_RATIO_SESSION_TAG:
|
|
|
|
statusLabel = STATUS_RATIO_SESSION;
|
|
|
|
break;
|
|
|
|
case STATUS_TRANSFER_TOTAL_TAG:
|
|
|
|
statusLabel = STATUS_TRANSFER_TOTAL;
|
|
|
|
break;
|
|
|
|
case STATUS_TRANSFER_SESSION_TAG:
|
|
|
|
statusLabel = STATUS_TRANSFER_SESSION;
|
|
|
|
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-05-14 05:31:24 +00:00
|
|
|
uploadText = [NSString stringWithFormat:NSLocalizedString(@"%ld KB/s", "Status Bar -> speed tooltip"),
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSUserDefaults.standardUserDefaults integerForKey:@"UploadLimit"]];
|
|
|
|
}
|
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-05-14 05:31:24 +00:00
|
|
|
downloadText = [NSString stringWithFormat:NSLocalizedString(@"%ld KB/s", "Status Bar -> speed tooltip"),
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSUserDefaults.standardUserDefaults integerForKey:@"DownloadLimit"]];
|
|
|
|
}
|
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
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
case STATUS_RATIO_TOTAL_TAG:
|
|
|
|
statusLabel = STATUS_RATIO_TOTAL;
|
|
|
|
break;
|
|
|
|
case STATUS_RATIO_SESSION_TAG:
|
|
|
|
statusLabel = STATUS_RATIO_SESSION;
|
|
|
|
break;
|
|
|
|
case STATUS_TRANSFER_TOTAL_TAG:
|
|
|
|
statusLabel = STATUS_TRANSFER_TOTAL;
|
|
|
|
break;
|
|
|
|
case STATUS_TRANSFER_SESSION_TAG:
|
|
|
|
statusLabel = STATUS_TRANSFER_SESSION;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NSAssert1(NO, @"Unknown status label tag received: %ld", menuItem.tag);
|
|
|
|
statusLabel = STATUS_RATIO_TOTAL;
|
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;
|
|
|
|
}
|
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
#pragma mark - Private
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)resizeStatusButton
|
2011-02-20 00:22:55 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fStatusButton sizeToFit];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2011-02-20 00:22:55 +00:00
|
|
|
//width ends up being too long
|
2022-02-22 16:04:20 +00:00
|
|
|
NSRect statusFrame = self.fStatusButton.frame;
|
2011-02-20 00:22:55 +00:00
|
|
|
statusFrame.size.width -= 25.0;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
CGFloat const difference = NSMaxX(statusFrame) + 5.0 - NSMinX(self.fTotalDLImageView.frame);
|
2011-02-20 15:05:01 +00:00
|
|
|
if (difference > 0.0)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2011-02-20 00:22:55 +00:00
|
|
|
statusFrame.size.width -= difference;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fStatusButton.frame = statusFrame;
|
2011-02-20 00:22:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|