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"
|
2012-06-18 01:33:27 +00:00
|
|
|
#import "NSApplicationAdditions.h"
|
2007-11-19 18:13:41 +00:00
|
|
|
#import "NSStringAdditions.h"
|
|
|
|
|
|
|
|
#define UPDATE_SECONDS 1.0
|
|
|
|
|
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;
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)updateStats;
|
2007-11-19 18:13:41 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)performResetStats;
|
2008-04-22 21:04:01 +00:00
|
|
|
|
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-02-22 16:04:20 +00:00
|
|
|
self.fTimer = [NSTimer scheduledTimerWithTimeInterval:UPDATE_SECONDS target:self selector:@selector(updateStats) userInfo:nil
|
2021-08-15 09:41:48 +00:00
|
|
|
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
|
|
|
|
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
|
|
|
|
2012-09-24 02:43:44 +00:00
|
|
|
//size of all labels
|
2022-02-22 16:04:20 +00:00
|
|
|
CGFloat const oldWidth = self.fUploadedLabelField.frame.size.width;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
NSArray* labels = @[ self.fUploadedLabelField, self.fDownloadedLabelField, self.fRatioLabelField, self.fTimeLabelField, self.fNumOpenedLabelField ];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-09-24 02:43:44 +00:00
|
|
|
CGFloat maxWidth = CGFLOAT_MIN;
|
2021-08-15 09:41:48 +00:00
|
|
|
for (NSTextField* label in labels)
|
2012-09-24 02:43:44 +00:00
|
|
|
{
|
|
|
|
[label sizeToFit];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
CGFloat const width = label.frame.size.width;
|
2012-09-24 02:43:44 +00:00
|
|
|
maxWidth = MAX(maxWidth, width);
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
for (NSTextField* label in labels)
|
2012-09-24 02:43:44 +00:00
|
|
|
{
|
2021-08-07 07:27:56 +00:00
|
|
|
NSRect frame = label.frame;
|
2012-09-24 02:43:44 +00:00
|
|
|
frame.size.width = maxWidth;
|
2021-08-07 07:27:56 +00:00
|
|
|
label.frame = frame;
|
2012-09-24 02:43:44 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-03-25 23:10:49 +00:00
|
|
|
//resize window for new label width - fields are set in nib to adjust correctly
|
2021-08-07 07:27:56 +00:00
|
|
|
NSRect windowRect = self.window.frame;
|
2008-03-25 23:10:49 +00:00
|
|
|
windowRect.size.width += maxWidth - oldWidth;
|
2021-08-15 09:41:48 +00:00
|
|
|
[self.window setFrame:windowRect display:YES];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-04-22 21:04:01 +00:00
|
|
|
//resize reset button
|
2022-02-22 16:04:20 +00:00
|
|
|
CGFloat const oldButtonWidth = self.fResetButton.frame.size.width;
|
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");
|
|
|
|
[self.fResetButton sizeToFit];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
NSRect buttonFrame = self.fResetButton.frame;
|
2009-03-01 17:02:58 +00:00
|
|
|
buttonFrame.size.width += 10.0;
|
2008-04-22 21:04:01 +00:00
|
|
|
buttonFrame.origin.x -= buttonFrame.size.width - oldButtonWidth;
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fResetButton.frame = buttonFrame;
|
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-08-15 09:41:48 +00:00
|
|
|
[alert.window orderOut:nil];
|
2020-11-22 13:02:29 +00:00
|
|
|
|
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
|
|
|
{
|
2007-11-20 13:08:10 +00:00
|
|
|
tr_session_stats statsAll, statsSession;
|
2008-05-22 20:44:41 +00:00
|
|
|
tr_sessionGetCumulativeStats(fLib, &statsAll);
|
|
|
|
tr_sessionGetStats(fLib, &statsSession);
|
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
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* totalRatioString = statsAll.ratio != TR_RATIO_NA ?
|
|
|
|
[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"),
|
2021-08-15 09:41:48 +00:00
|
|
|
[timeFormatter stringFromTimeInterval:statsAll.secondsActive]];
|
|
|
|
|
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-02-22 16:04:20 +00:00
|
|
|
self.fNumOpenedField.stringValue = [NSString stringWithFormat:NSLocalizedString(@"%@ times", "stats window -> times opened"),
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSString formattedUInteger:statsAll.sessionCount]];
|
|
|
|
}
|
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
|