2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2007-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-11-19 18:13:41 +00:00
|
|
|
|
|
|
|
#import "StatsWindowController.h"
|
2012-05-27 22:31:58 +00:00
|
|
|
#import "Controller.h"
|
2007-11-19 18:13:41 +00:00
|
|
|
#import "NSStringAdditions.h"
|
|
|
|
|
2022-10-19 19:28:21 +00:00
|
|
|
static NSTimeInterval const kUpdateSeconds = 1.0;
|
2007-11-19 18:13:41 +00:00
|
|
|
|
2022-01-24 01:32:45 +00:00
|
|
|
@interface StatsWindowController ()
|
2007-11-19 18:13:41 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
@property(nonatomic) IBOutlet NSTextField* fUploadedField;
|
|
|
|
@property(nonatomic) IBOutlet NSTextField* fUploadedAllField;
|
|
|
|
@property(nonatomic) IBOutlet NSTextField* fDownloadedField;
|
|
|
|
@property(nonatomic) IBOutlet NSTextField* fDownloadedAllField;
|
|
|
|
@property(nonatomic) IBOutlet NSTextField* fRatioField;
|
|
|
|
@property(nonatomic) IBOutlet NSTextField* fRatioAllField;
|
|
|
|
@property(nonatomic) IBOutlet NSTextField* fTimeField;
|
|
|
|
@property(nonatomic) IBOutlet NSTextField* fTimeAllField;
|
|
|
|
@property(nonatomic) IBOutlet NSTextField* fNumOpenedField;
|
|
|
|
@property(nonatomic) IBOutlet NSTextField* fUploadedLabelField;
|
|
|
|
@property(nonatomic) IBOutlet NSTextField* fDownloadedLabelField;
|
|
|
|
@property(nonatomic) IBOutlet NSTextField* fRatioLabelField;
|
|
|
|
@property(nonatomic) IBOutlet NSTextField* fTimeLabelField;
|
|
|
|
@property(nonatomic) IBOutlet NSTextField* fNumOpenedLabelField;
|
|
|
|
@property(nonatomic) IBOutlet NSButton* fResetButton;
|
|
|
|
@property(nonatomic) NSTimer* fTimer;
|
|
|
|
|
2007-11-19 18:13:41 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation StatsWindowController
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
StatsWindowController* fStatsWindowInstance = nil;
|
|
|
|
tr_session* fLib = NULL;
|
|
|
|
|
|
|
|
+ (StatsWindowController*)statsWindow
|
2007-11-19 18:13:41 +00:00
|
|
|
{
|
|
|
|
if (!fStatsWindowInstance)
|
|
|
|
{
|
2012-05-27 22:31:58 +00:00
|
|
|
if ((fStatsWindowInstance = [[self alloc] init]))
|
2007-11-19 18:13:41 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
fLib = ((Controller*)NSApp.delegate).sessionHandle;
|
2007-11-19 18:13:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return fStatsWindowInstance;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (instancetype)init
|
2012-05-27 22:31:58 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
return [super initWithWindowNibName:@"StatsWindow"];
|
2012-05-27 22:31:58 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)awakeFromNib
|
2007-11-19 18:13:41 +00:00
|
|
|
{
|
|
|
|
[self updateStats];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-10-19 19:28:21 +00:00
|
|
|
self.fTimer = [NSTimer scheduledTimerWithTimeInterval:kUpdateSeconds target:self selector:@selector(updateStats)
|
2022-03-30 21:52:23 +00:00
|
|
|
userInfo:nil
|
|
|
|
repeats:YES];
|
2022-02-22 16:04:20 +00:00
|
|
|
[NSRunLoop.currentRunLoop addTimer:self.fTimer forMode:NSModalPanelRunLoopMode];
|
|
|
|
[NSRunLoop.currentRunLoop addTimer:self.fTimer forMode:NSEventTrackingRunLoopMode];
|
2016-01-06 11:05:37 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
self.window.restorationClass = [self class];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
self.window.title = NSLocalizedString(@"Statistics", "Stats window -> title");
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-06-16 17:55:33 +00:00
|
|
|
//disable fullscreen support
|
2022-06-29 04:20:42 +00:00
|
|
|
self.window.collectionBehavior = NSWindowCollectionBehaviorFullScreenNone;
|
2022-06-13 00:54:52 +00:00
|
|
|
|
2008-03-16 06:20:35 +00:00
|
|
|
//set label text
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fUploadedLabelField.stringValue = [NSLocalizedString(@"Uploaded", "Stats window -> label") stringByAppendingString:@":"];
|
|
|
|
self.fDownloadedLabelField.stringValue = [NSLocalizedString(@"Downloaded", "Stats window -> label") stringByAppendingString:@":"];
|
|
|
|
self.fRatioLabelField.stringValue = [NSLocalizedString(@"Ratio", "Stats window -> label") stringByAppendingString:@":"];
|
|
|
|
self.fTimeLabelField.stringValue = [NSLocalizedString(@"Running Time", "Stats window -> label") stringByAppendingString:@":"];
|
|
|
|
self.fNumOpenedLabelField.stringValue = [NSLocalizedString(@"Program Started", "Stats window -> label") stringByAppendingString:@":"];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fResetButton.title = NSLocalizedString(@"Reset", "Stats window -> reset button");
|
2007-11-19 18:13:41 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)windowWillClose:(id)sender
|
2007-11-19 18:13:41 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fTimer invalidate];
|
|
|
|
self.fTimer = nil;
|
2017-08-01 10:57:27 +00:00
|
|
|
fStatsWindowInstance = nil;
|
2007-11-19 18:13:41 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
+ (void)restoreWindowWithIdentifier:(NSString*)identifier
|
|
|
|
state:(NSCoder*)state
|
|
|
|
completionHandler:(void (^)(NSWindow*, NSError*))completionHandler
|
2012-05-27 22:31:58 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSAssert1([identifier isEqualToString:@"StatsWindow"], @"Trying to restore unexpected identifier %@", identifier);
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
completionHandler(StatsWindowController.statsWindow.window, nil);
|
2012-05-27 22:31:58 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)resetStats:(id)sender
|
2008-04-22 21:04:01 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
if (![NSUserDefaults.standardUserDefaults boolForKey:@"WarningResetStats"])
|
2008-04-22 21:04:01 +00:00
|
|
|
{
|
|
|
|
[self performResetStats];
|
|
|
|
return;
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSAlert* alert = [[NSAlert alloc] init];
|
2021-08-07 07:27:56 +00:00
|
|
|
alert.messageText = NSLocalizedString(@"Are you sure you want to reset usage statistics?", "Stats reset -> title");
|
2021-08-15 09:41:48 +00:00
|
|
|
alert.informativeText = NSLocalizedString(
|
|
|
|
@"This will clear the global statistics displayed by Transmission."
|
|
|
|
" Individual transfer statistics will not be affected.",
|
|
|
|
"Stats reset -> message");
|
2021-10-31 15:18:27 +00:00
|
|
|
alert.alertStyle = NSAlertStyleWarning;
|
2021-08-15 09:41:48 +00:00
|
|
|
[alert addButtonWithTitle:NSLocalizedString(@"Reset", "Stats reset -> button")];
|
|
|
|
[alert addButtonWithTitle:NSLocalizedString(@"Cancel", "Stats reset -> button")];
|
2021-08-07 07:27:56 +00:00
|
|
|
alert.showsSuppressionButton = YES;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
[alert beginSheetModalForWindow:self.window completionHandler:^(NSModalResponse returnCode) {
|
2021-10-31 15:18:27 +00:00
|
|
|
if (alert.suppressionButton.state == NSControlStateValueOn)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
|
|
|
[NSUserDefaults.standardUserDefaults setBool:NO forKey:@"WarningResetStats"];
|
|
|
|
}
|
2020-11-22 13:02:29 +00:00
|
|
|
|
|
|
|
if (returnCode == NSAlertFirstButtonReturn)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2020-11-22 13:02:29 +00:00
|
|
|
[self performResetStats];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2020-11-22 13:02:29 +00:00
|
|
|
}];
|
2008-04-22 21:04:01 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSString*)windowFrameAutosaveName
|
2009-09-12 02:39:45 +00:00
|
|
|
{
|
|
|
|
return @"StatsWindow";
|
|
|
|
}
|
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
#pragma mark - Private
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)updateStats
|
2007-11-19 18:13:41 +00:00
|
|
|
{
|
2022-07-28 23:56:40 +00:00
|
|
|
auto const statsAll = tr_sessionGetCumulativeStats(fLib);
|
|
|
|
auto const statsSession = tr_sessionGetStats(fLib);
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSByteCountFormatter* byteFormatter = [[NSByteCountFormatter alloc] init];
|
2021-08-07 07:27:56 +00:00
|
|
|
byteFormatter.allowedUnits = NSByteCountFormatterUseBytes;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fUploadedField.stringValue = [NSString stringForFileSize:statsSession.uploadedBytes];
|
|
|
|
self.fUploadedField.toolTip = [byteFormatter stringFromByteCount:statsSession.uploadedBytes];
|
|
|
|
self.fUploadedAllField.stringValue = [NSString
|
2021-08-15 09:41:48 +00:00
|
|
|
stringWithFormat:NSLocalizedString(@"%@ total", "stats total"), [NSString stringForFileSize:statsAll.uploadedBytes]];
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fUploadedAllField.toolTip = [byteFormatter stringFromByteCount:statsAll.uploadedBytes];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fDownloadedField.stringValue = [NSString stringForFileSize:statsSession.downloadedBytes];
|
|
|
|
self.fDownloadedField.toolTip = [byteFormatter stringFromByteCount:statsSession.downloadedBytes];
|
|
|
|
self.fDownloadedAllField.stringValue = [NSString
|
2021-08-15 09:41:48 +00:00
|
|
|
stringWithFormat:NSLocalizedString(@"%@ total", "stats total"), [NSString stringForFileSize:statsAll.downloadedBytes]];
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fDownloadedAllField.toolTip = [byteFormatter stringFromByteCount:statsAll.downloadedBytes];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fRatioField.stringValue = [NSString stringForRatio:statsSession.ratio];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-12-21 20:21:16 +00:00
|
|
|
NSString* totalRatioString = static_cast<int>(statsAll.ratio) != TR_RATIO_NA ?
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSString stringWithFormat:NSLocalizedString(@"%@ total", "stats total"), [NSString stringForRatio:statsAll.ratio]] :
|
|
|
|
NSLocalizedString(@"Total N/A", "stats total");
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fRatioAllField.stringValue = totalRatioString;
|
2021-08-15 09:41:48 +00:00
|
|
|
|
|
|
|
static NSDateComponentsFormatter* timeFormatter;
|
2020-12-23 03:00:06 +00:00
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
timeFormatter = [NSDateComponentsFormatter new];
|
|
|
|
timeFormatter.unitsStyle = NSDateComponentsFormatterUnitsStyleFull;
|
|
|
|
timeFormatter.maximumUnitCount = 3;
|
2021-08-15 09:41:48 +00:00
|
|
|
timeFormatter.allowedUnits = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitWeekOfMonth | NSCalendarUnitDay |
|
|
|
|
NSCalendarUnitHour | NSCalendarUnitMinute;
|
2020-12-23 03:00:06 +00:00
|
|
|
});
|
2021-08-15 09:41:48 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fTimeField.stringValue = [timeFormatter stringFromTimeInterval:statsSession.secondsActive];
|
|
|
|
self.fTimeAllField.stringValue = [NSString stringWithFormat:NSLocalizedString(@"%@ total", "stats total"),
|
2022-03-30 21:52:23 +00:00
|
|
|
[timeFormatter stringFromTimeInterval:statsAll.secondsActive]];
|
2021-08-15 09:41:48 +00:00
|
|
|
|
2008-05-23 22:58:17 +00:00
|
|
|
if (statsAll.sessionCount == 1)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fNumOpenedField.stringValue = NSLocalizedString(@"1 time", "stats window -> times opened");
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2008-05-23 22:58:17 +00:00
|
|
|
else
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-05-14 05:31:24 +00:00
|
|
|
self.fNumOpenedField.stringValue = [NSString
|
2022-11-14 18:30:03 +00:00
|
|
|
localizedStringWithFormat:NSLocalizedString(@"%llu times", "stats window -> times opened"), statsAll.sessionCount];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2007-11-19 20:00:18 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)performResetStats
|
2008-04-22 21:04:01 +00:00
|
|
|
{
|
2008-05-22 20:44:41 +00:00
|
|
|
tr_sessionClearStats(fLib);
|
2008-04-22 21:04:01 +00:00
|
|
|
[self updateStats];
|
|
|
|
}
|
|
|
|
|
2007-11-19 18:13:41 +00:00
|
|
|
@end
|