2007-11-19 18:13:41 +00:00
|
|
|
/******************************************************************************
|
2012-01-14 17:12:04 +00:00
|
|
|
* Copyright (c) 2007-2012 Transmission authors and contributors
|
2007-11-19 18:13:41 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
|
@interface StatsWindowController (Private)
|
|
|
|
|
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
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
fTimer = [NSTimer scheduledTimerWithTimeInterval:UPDATE_SECONDS target:self selector:@selector(updateStats) userInfo:nil
|
|
|
|
repeats:YES];
|
|
|
|
[NSRunLoop.currentRunLoop addTimer:fTimer forMode:NSModalPanelRunLoopMode];
|
|
|
|
[NSRunLoop.currentRunLoop addTimer: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
|
2021-08-15 09:41:48 +00:00
|
|
|
fUploadedLabelField.stringValue = [NSLocalizedString(@"Uploaded", "Stats window -> label") stringByAppendingString:@":"];
|
|
|
|
fDownloadedLabelField.stringValue = [NSLocalizedString(@"Downloaded", "Stats window -> label") stringByAppendingString:@":"];
|
|
|
|
fRatioLabelField.stringValue = [NSLocalizedString(@"Ratio", "Stats window -> label") stringByAppendingString:@":"];
|
|
|
|
fTimeLabelField.stringValue = [NSLocalizedString(@"Running Time", "Stats window -> label") stringByAppendingString:@":"];
|
|
|
|
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
|
2021-08-15 09:41:48 +00:00
|
|
|
CGFloat const oldWidth = fUploadedLabelField.frame.size.width;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSArray* labels = @[ fUploadedLabelField, fDownloadedLabelField, fRatioLabelField, fTimeLabelField, 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
|
2021-08-15 09:41:48 +00:00
|
|
|
CGFloat const oldButtonWidth = fResetButton.frame.size.width;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
fResetButton.title = NSLocalizedString(@"Reset", "Stats window -> reset button");
|
2008-04-22 21:04:01 +00:00
|
|
|
[fResetButton sizeToFit];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
NSRect buttonFrame = 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;
|
2021-08-07 07:27:56 +00:00
|
|
|
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
|
|
|
{
|
2007-12-24 17:57:19 +00:00
|
|
|
[fTimer invalidate];
|
2012-09-10 02:37:29 +00:00
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
2007-11-19 18:13:41 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation StatsWindowController (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
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
fUploadedField.stringValue = [NSString stringForFileSize:statsSession.uploadedBytes];
|
|
|
|
fUploadedField.toolTip = [byteFormatter stringFromByteCount:statsSession.uploadedBytes];
|
|
|
|
fUploadedAllField.stringValue = [NSString
|
|
|
|
stringWithFormat:NSLocalizedString(@"%@ total", "stats total"), [NSString stringForFileSize:statsAll.uploadedBytes]];
|
|
|
|
fUploadedAllField.toolTip = [byteFormatter stringFromByteCount:statsAll.uploadedBytes];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
fDownloadedField.stringValue = [NSString stringForFileSize:statsSession.downloadedBytes];
|
|
|
|
fDownloadedField.toolTip = [byteFormatter stringFromByteCount:statsSession.downloadedBytes];
|
|
|
|
fDownloadedAllField.stringValue = [NSString
|
|
|
|
stringWithFormat:NSLocalizedString(@"%@ total", "stats total"), [NSString stringForFileSize:statsAll.downloadedBytes]];
|
|
|
|
fDownloadedAllField.toolTip = [byteFormatter stringFromByteCount:statsAll.downloadedBytes];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
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");
|
2021-08-07 07:27:56 +00:00
|
|
|
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
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
fTimeField.stringValue = [timeFormatter stringFromTimeInterval:statsSession.secondsActive];
|
2021-08-15 09:41:48 +00:00
|
|
|
fTimeAllField.stringValue = [NSString stringWithFormat:NSLocalizedString(@"%@ total", "stats total"),
|
|
|
|
[timeFormatter stringFromTimeInterval:statsAll.secondsActive]];
|
|
|
|
|
2008-05-23 22:58:17 +00:00
|
|
|
if (statsAll.sessionCount == 1)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2021-08-07 07:27:56 +00:00
|
|
|
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
|
|
|
{
|
|
|
|
fNumOpenedField.stringValue = [NSString stringWithFormat:NSLocalizedString(@"%@ times", "stats window -> times opened"),
|
|
|
|
[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
|