source: trunk/macosx/StatsWindowController.m @ 13248

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

#4822 Enable Automatic Reference Counting

  • Property svn:keywords set to Date Rev Author Id
File size: 9.3 KB
Line 
1/******************************************************************************
2 * $Id: StatsWindowController.m 13248 2012-03-10 03:38:58Z livings124 $
3 *
4 * Copyright (c) 2007-2012 Transmission authors and contributors
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
34- (void) performResetStats;
35- (void) resetSheetClosed: (NSAlert *) alert returnCode: (NSInteger) code contextInfo: (void *) info;
36
37@end
38
39@implementation StatsWindowController
40
41StatsWindowController * fStatsWindowInstance = nil;
42tr_session * fLib;
43+ (StatsWindowController *) statsWindow: (tr_session *) lib
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];
63   
64    [[self window] setTitle: NSLocalizedString(@"Statistics", "Stats window -> title")];
65   
66    //set label text
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: @":"]];
73   
74    //size all elements
75    const CGFloat oldWidth = [fUploadedLabelField frame].size.width;
76   
77    [fUploadedLabelField sizeToFit];
78    [fDownloadedLabelField sizeToFit];
79    [fRatioLabelField sizeToFit];
80    [fTimeLabelField sizeToFit];
81    [fNumOpenedLabelField sizeToFit];
82   
83    CGFloat maxWidth = MAX([fUploadedLabelField frame].size.width, [fDownloadedLabelField frame].size.width);
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   
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];
112   
113    //resize reset button
114    const CGFloat oldButtonWidth = [fResetButton frame].size.width;
115   
116    [fResetButton setTitle: NSLocalizedString(@"Reset", "Stats window -> reset button")];
117    [fResetButton sizeToFit];
118   
119    NSRect buttonFrame = [fResetButton frame];
120    buttonFrame.size.width += 10.0;
121    buttonFrame.origin.x -= buttonFrame.size.width - oldButtonWidth;
122    [fResetButton setFrame: buttonFrame];
123}
124
125- (void) windowWillClose: (id) sender
126{
127    [fTimer invalidate];
128   
129    fStatsWindowInstance = nil;
130}
131
132- (void) resetStats: (id) sender
133{
134    if (![[NSUserDefaults standardUserDefaults] boolForKey: @"WarningResetStats"])
135    {
136        [self performResetStats];
137        return;
138    }
139   
140    NSAlert * alert = [[NSAlert alloc] init];
141    [alert setMessageText: NSLocalizedString(@"Are you sure you want to reset usage statistics?", "Stats reset -> title")];
142    [alert setInformativeText: NSLocalizedString(@"This will clear the global statistics displayed by Transmission."
143                                " Individual transfer statistics will not be affected.", "Stats reset -> message")];
144    [alert setAlertStyle: NSWarningAlertStyle];
145    [alert addButtonWithTitle: NSLocalizedString(@"Reset", "Stats reset -> button")];
146    [alert addButtonWithTitle: NSLocalizedString(@"Cancel", "Stats reset -> button")];
147    [alert setShowsSuppressionButton: YES];
148   
149    [alert beginSheetModalForWindow: [self window] modalDelegate: self
150        didEndSelector: @selector(resetSheetClosed:returnCode:contextInfo:) contextInfo: nil];
151}
152
153- (NSString *) windowFrameAutosaveName
154{
155    return @"StatsWindow";
156}
157
158@end
159
160@implementation StatsWindowController (Private)
161
162- (void) updateStats
163{
164    tr_session_stats statsAll, statsSession;
165    tr_sessionGetCumulativeStats(fLib, &statsAll);
166    tr_sessionGetStats(fLib, &statsSession);
167   
168    [fUploadedField setStringValue: [NSString stringForFileSize: statsSession.uploadedBytes]];
169    [fUploadedField setToolTip: [NSString stringWithFormat: NSLocalizedString(@"%@ bytes", "stats -> bytes"),
170                                    [NSString formattedUInteger: statsSession.uploadedBytes]]];
171    [fUploadedAllField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%@ total", "stats total"),
172                                        [NSString stringForFileSize: statsAll.uploadedBytes]]];
173    [fUploadedAllField setToolTip: [NSString stringWithFormat: NSLocalizedString(@"%@ bytes", "stats -> bytes"),
174                                    [NSString formattedUInteger: statsAll.uploadedBytes]]];
175   
176    [fDownloadedField setStringValue: [NSString stringForFileSize: statsSession.downloadedBytes]];
177    [fDownloadedField setToolTip: [NSString stringWithFormat: NSLocalizedString(@"%@ bytes", "stats -> bytes"),
178                                    [NSString formattedUInteger: statsSession.downloadedBytes]]];
179    [fDownloadedAllField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%@ total", "stats total"),
180                                        [NSString stringForFileSize: statsAll.downloadedBytes]]];
181    [fDownloadedAllField setToolTip: [NSString stringWithFormat: NSLocalizedString(@"%@ bytes", "stats -> bytes"),
182                                        [NSString formattedUInteger: statsAll.downloadedBytes]]];
183   
184    [fRatioField setStringValue: [NSString stringForRatio: statsSession.ratio]];
185   
186    NSString * totalRatioString = statsAll.ratio != TR_RATIO_NA
187        ? [NSString stringWithFormat: NSLocalizedString(@"%@ total", "stats total"), [NSString stringForRatio: statsAll.ratio]]
188        : NSLocalizedString(@"Total N/A", "stats total");
189    [fRatioAllField setStringValue: totalRatioString];
190   
191    [fTimeField setStringValue: [NSString timeString: statsSession.secondsActive showSeconds: NO]];
192    [fTimeAllField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%@ total", "stats total"),
193                                        [NSString timeString: statsAll.secondsActive showSeconds: NO]]];
194   
195    if (statsAll.sessionCount == 1)
196        [fNumOpenedField setStringValue: NSLocalizedString(@"1 time", "stats window -> times opened")];
197    else
198        [fNumOpenedField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%@ times", "stats window -> times opened"),
199                                            [NSString formattedUInteger: statsAll.sessionCount]]];
200}
201
202- (void) performResetStats
203{
204    tr_sessionClearStats(fLib);
205    [self updateStats];
206}
207
208- (void) resetSheetClosed: (NSAlert *) alert returnCode: (NSInteger) code contextInfo: (void *) info
209{
210    [[alert window] orderOut: nil];
211   
212    if ([[alert suppressionButton] state] == NSOnState)
213        [[NSUserDefaults standardUserDefaults] setBool: NO forKey: @"WarningResetStats"];
214   
215    if (code == NSAlertFirstButtonReturn)
216        [self performResetStats];
217}
218
219@end
Note: See TracBrowser for help on using the repository browser.