source: trunk/macosx/StatsWindowController.m @ 13251

Last change on this file since 13251 was 13251, checked in by livings124, 10 years ago

reverse r13248-r13250, since ARC doesn't support 32-bit.

  • Property svn:keywords set to Date Rev Author Id
File size: 9.4 KB
RevLine 
[3886]1/******************************************************************************
2 * $Id: StatsWindowController.m 13251 2012-03-13 02:52:11Z livings124 $
3 *
[13162]4 * Copyright (c) 2007-2012 Transmission authors and contributors
[3886]5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#import "StatsWindowController.h"
26#import "NSStringAdditions.h"
27
28#define UPDATE_SECONDS 1.0
29
30@interface StatsWindowController (Private)
31
32- (void) updateStats;
33
[5668]34- (void) performResetStats;
[6974]35- (void) resetSheetClosed: (NSAlert *) alert returnCode: (NSInteger) code contextInfo: (void *) info;
[5668]36
[3886]37@end
38
39@implementation StatsWindowController
40
41StatsWindowController * fStatsWindowInstance = nil;
[7468]42tr_session * fLib;
43+ (StatsWindowController *) statsWindow: (tr_session *) lib
[3886]44{
45    if (!fStatsWindowInstance)
46    {
47        if ((fStatsWindowInstance = [[self alloc] initWithWindowNibName: @"StatsWindow"]))
48        {
49            fLib = lib;
50        }
51    }
52    return fStatsWindowInstance;
53}
54
55- (void) awakeFromNib
56{
57    [self updateStats];
58   
59    fTimer = [NSTimer scheduledTimerWithTimeInterval: UPDATE_SECONDS target: self
60                selector: @selector(updateStats) userInfo: nil repeats: YES];
61    [[NSRunLoop currentRunLoop] addTimer: fTimer forMode: NSModalPanelRunLoopMode];
62    [[NSRunLoop currentRunLoop] addTimer: fTimer forMode: NSEventTrackingRunLoopMode];
[5258]63   
[5391]64    [[self window] setTitle: NSLocalizedString(@"Statistics", "Stats window -> title")];
65   
[5258]66    //set label text
[5296]67    [fUploadedLabelField setStringValue: [NSLocalizedString(@"Uploaded", "Stats window -> label") stringByAppendingString: @":"]];
68    [fDownloadedLabelField setStringValue: [NSLocalizedString(@"Downloaded", "Stats window -> label") stringByAppendingString: @":"]];
69    [fRatioLabelField setStringValue: [NSLocalizedString(@"Ratio", "Stats window -> label") stringByAppendingString: @":"]];
70    [fTimeLabelField setStringValue: [NSLocalizedString(@"Running Time", "Stats window -> label") stringByAppendingString: @":"]];
71    [fNumOpenedLabelField setStringValue: [NSLocalizedString(@"Program Started", "Stats window -> label")
72                                            stringByAppendingString: @":"]];
[5258]73   
74    //size all elements
[9316]75    const CGFloat oldWidth = [fUploadedLabelField frame].size.width;
[5258]76   
77    [fUploadedLabelField sizeToFit];
78    [fDownloadedLabelField sizeToFit];
79    [fRatioLabelField sizeToFit];
80    [fTimeLabelField sizeToFit];
81    [fNumOpenedLabelField sizeToFit];
82   
[6974]83    CGFloat maxWidth = MAX([fUploadedLabelField frame].size.width, [fDownloadedLabelField frame].size.width);
[5258]84    maxWidth = MAX(maxWidth, [fRatioLabelField frame].size.width);
85    maxWidth = MAX(maxWidth, [fTimeLabelField frame].size.width);
86    maxWidth = MAX(maxWidth, [fNumOpenedLabelField frame].size.width);
87   
88    NSRect frame = [fUploadedLabelField frame];
89    frame.size.width = maxWidth;
90    [fUploadedLabelField setFrame: frame];
91   
92    frame = [fDownloadedLabelField frame];
93    frame.size.width = maxWidth;
94    [fDownloadedLabelField setFrame: frame];
95   
96    frame = [fRatioLabelField frame];
97    frame.size.width = maxWidth;
98    [fRatioLabelField setFrame: frame];
99   
100    frame = [fTimeLabelField frame];
101    frame.size.width = maxWidth;
102    [fTimeLabelField setFrame: frame];
103   
104    frame = [fNumOpenedLabelField frame];
105    frame.size.width = maxWidth;
106    [fNumOpenedLabelField setFrame: frame];
107   
[5390]108    //resize window for new label width - fields are set in nib to adjust correctly
109    NSRect windowRect = [[self window] frame];
110    windowRect.size.width += maxWidth - oldWidth;
111    [[self window] setFrame: windowRect display: YES];
[5668]112   
113    //resize reset button
[9316]114    const CGFloat oldButtonWidth = [fResetButton frame].size.width;
[5668]115   
116    [fResetButton setTitle: NSLocalizedString(@"Reset", "Stats window -> reset button")];
117    [fResetButton sizeToFit];
118   
119    NSRect buttonFrame = [fResetButton frame];
[7983]120    buttonFrame.size.width += 10.0;
[5668]121    buttonFrame.origin.x -= buttonFrame.size.width - oldButtonWidth;
122    [fResetButton setFrame: buttonFrame];
[3886]123}
124
[5668]125- (void) windowWillClose: (id) sender
[3886]126{
[4320]127    [fTimer invalidate];
[3886]128   
[13251]129    [fStatsWindowInstance autorelease];
[3886]130    fStatsWindowInstance = nil;
131}
132
[5668]133- (void) resetStats: (id) sender
134{
135    if (![[NSUserDefaults standardUserDefaults] boolForKey: @"WarningResetStats"])
136    {
137        [self performResetStats];
138        return;
139    }
140   
141    NSAlert * alert = [[NSAlert alloc] init];
142    [alert setMessageText: NSLocalizedString(@"Are you sure you want to reset usage statistics?", "Stats reset -> title")];
[5671]143    [alert setInformativeText: NSLocalizedString(@"This will clear the global statistics displayed by Transmission."
[6951]144                                " Individual transfer statistics will not be affected.", "Stats reset -> message")];
[5668]145    [alert setAlertStyle: NSWarningAlertStyle];
146    [alert addButtonWithTitle: NSLocalizedString(@"Reset", "Stats reset -> button")];
147    [alert addButtonWithTitle: NSLocalizedString(@"Cancel", "Stats reset -> button")];
[7508]148    [alert setShowsSuppressionButton: YES];
[5668]149   
150    [alert beginSheetModalForWindow: [self window] modalDelegate: self
151        didEndSelector: @selector(resetSheetClosed:returnCode:contextInfo:) contextInfo: nil];
152}
153
[9105]154- (NSString *) windowFrameAutosaveName
155{
156    return @"StatsWindow";
157}
158
[3886]159@end
160
161@implementation StatsWindowController (Private)
162
163- (void) updateStats
164{
[3902]165    tr_session_stats statsAll, statsSession;
[5908]166    tr_sessionGetCumulativeStats(fLib, &statsAll);
167    tr_sessionGetStats(fLib, &statsSession);
[3886]168   
[3986]169    [fUploadedField setStringValue: [NSString stringForFileSize: statsSession.uploadedBytes]];
[11419]170    [fUploadedField setToolTip: [NSString stringWithFormat: NSLocalizedString(@"%@ bytes", "stats -> bytes"),
171                                    [NSString formattedUInteger: statsSession.uploadedBytes]]];
[5376]172    [fUploadedAllField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%@ total", "stats total"),
173                                        [NSString stringForFileSize: statsAll.uploadedBytes]]];
[11419]174    [fUploadedAllField setToolTip: [NSString stringWithFormat: NSLocalizedString(@"%@ bytes", "stats -> bytes"),
175                                    [NSString formattedUInteger: statsAll.uploadedBytes]]];
[3887]176   
[3986]177    [fDownloadedField setStringValue: [NSString stringForFileSize: statsSession.downloadedBytes]];
[11419]178    [fDownloadedField setToolTip: [NSString stringWithFormat: NSLocalizedString(@"%@ bytes", "stats -> bytes"),
179                                    [NSString formattedUInteger: statsSession.downloadedBytes]]];
[5376]180    [fDownloadedAllField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%@ total", "stats total"),
181                                        [NSString stringForFileSize: statsAll.downloadedBytes]]];
[11419]182    [fDownloadedAllField setToolTip: [NSString stringWithFormat: NSLocalizedString(@"%@ bytes", "stats -> bytes"),
183                                        [NSString formattedUInteger: statsAll.downloadedBytes]]];
[3891]184   
[3912]185    [fRatioField setStringValue: [NSString stringForRatio: statsSession.ratio]];
186   
[3978]187    NSString * totalRatioString = statsAll.ratio != TR_RATIO_NA
[5376]188        ? [NSString stringWithFormat: NSLocalizedString(@"%@ total", "stats total"), [NSString stringForRatio: statsAll.ratio]]
[3978]189        : NSLocalizedString(@"Total N/A", "stats total");
190    [fRatioAllField setStringValue: totalRatioString];
191   
[5091]192    [fTimeField setStringValue: [NSString timeString: statsSession.secondsActive showSeconds: NO]];
[5376]193    [fTimeAllField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%@ total", "stats total"),
194                                        [NSString timeString: statsAll.secondsActive showSeconds: NO]]];
[3912]195   
[5926]196    if (statsAll.sessionCount == 1)
197        [fNumOpenedField setStringValue: NSLocalizedString(@"1 time", "stats window -> times opened")];
198    else
[11859]199        [fNumOpenedField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%@ times", "stats window -> times opened"),
200                                            [NSString formattedUInteger: statsAll.sessionCount]]];
[3891]201}
202
[5668]203- (void) performResetStats
204{
[5908]205    tr_sessionClearStats(fLib);
[5668]206    [self updateStats];
207}
208
[6974]209- (void) resetSheetClosed: (NSAlert *) alert returnCode: (NSInteger) code contextInfo: (void *) info
[5668]210{
211    [[alert window] orderOut: nil];
212   
[7508]213    if ([[alert suppressionButton] state] == NSOnState)
[5668]214        [[NSUserDefaults standardUserDefaults] setBool: NO forKey: @"WarningResetStats"];
215   
216    if (code == NSAlertFirstButtonReturn)
217        [self performResetStats];
218}
219
[3886]220@end
Note: See TracBrowser for help on using the repository browser.