source: trunk/macosx/StatsWindowController.m @ 8227

Last change on this file since 8227 was 7983, checked in by livings124, 13 years ago

trivial changes

  • Property svn:keywords set to Date Rev Author Id
File size: 9.1 KB
Line 
1/******************************************************************************
2 * $Id: StatsWindowController.m 7983 2009-03-01 17:02:58Z livings124 $
3 *
4 * Copyright (c) 2007-2009 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    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    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 release];
130    fStatsWindowInstance = nil;
131}
132
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")];
143    [alert setInformativeText: NSLocalizedString(@"This will clear the global statistics displayed by Transmission."
144                                " Individual transfer statistics will not be affected.", "Stats reset -> message")];
145    [alert setAlertStyle: NSWarningAlertStyle];
146    [alert addButtonWithTitle: NSLocalizedString(@"Reset", "Stats reset -> button")];
147    [alert addButtonWithTitle: NSLocalizedString(@"Cancel", "Stats reset -> button")];
148    [alert setShowsSuppressionButton: YES];
149   
150    [alert beginSheetModalForWindow: [self window] modalDelegate: self
151        didEndSelector: @selector(resetSheetClosed:returnCode:contextInfo:) contextInfo: nil];
152}
153
154@end
155
156@implementation StatsWindowController (Private)
157
158- (void) updateStats
159{
160    tr_session_stats statsAll, statsSession;
161    tr_sessionGetCumulativeStats(fLib, &statsAll);
162    tr_sessionGetStats(fLib, &statsSession);
163   
164    [fUploadedField setStringValue: [NSString stringForFileSize: statsSession.uploadedBytes]];
165    [fUploadedField setToolTip: [NSString stringWithFormat: NSLocalizedString(@"%llu bytes", "stats -> bytes"),
166                                    statsSession.uploadedBytes]];
167    [fUploadedAllField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%@ total", "stats total"),
168                                        [NSString stringForFileSize: statsAll.uploadedBytes]]];
169    [fUploadedAllField setToolTip: [NSString stringWithFormat: NSLocalizedString(@"%llu bytes", "stats -> bytes"),
170                                    statsAll.uploadedBytes]];
171   
172    [fDownloadedField setStringValue: [NSString stringForFileSize: statsSession.downloadedBytes]];
173    [fDownloadedField setToolTip: [NSString stringWithFormat: NSLocalizedString(@"%llu bytes", "stats -> bytes"),
174                                    statsSession.downloadedBytes]];
175    [fDownloadedAllField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%@ total", "stats total"),
176                                        [NSString stringForFileSize: statsAll.downloadedBytes]]];
177    [fDownloadedAllField setToolTip: [NSString stringWithFormat: NSLocalizedString(@"%llu bytes", "stats -> bytes"),
178                                        statsAll.downloadedBytes]];
179   
180    [fRatioField setStringValue: [NSString stringForRatio: statsSession.ratio]];
181   
182    NSString * totalRatioString = statsAll.ratio != TR_RATIO_NA
183        ? [NSString stringWithFormat: NSLocalizedString(@"%@ total", "stats total"), [NSString stringForRatio: statsAll.ratio]]
184        : NSLocalizedString(@"Total N/A", "stats total");
185    [fRatioAllField setStringValue: totalRatioString];
186   
187    [fTimeField setStringValue: [NSString timeString: statsSession.secondsActive showSeconds: NO]];
188    [fTimeAllField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%@ total", "stats total"),
189                                        [NSString timeString: statsAll.secondsActive showSeconds: NO]]];
190   
191    if (statsAll.sessionCount == 1)
192        [fNumOpenedField setStringValue: NSLocalizedString(@"1 time", "stats window -> times opened")];
193    else
194        [fNumOpenedField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%llu times", "stats window -> times opened"),
195                                            statsAll.sessionCount]];
196}
197
198- (void) performResetStats
199{
200    tr_sessionClearStats(fLib);
201    [self updateStats];
202}
203
204- (void) resetSheetClosed: (NSAlert *) alert returnCode: (NSInteger) code contextInfo: (void *) info
205{
206    [[alert window] orderOut: nil];
207   
208    if ([[alert suppressionButton] state] == NSOnState)
209        [[NSUserDefaults standardUserDefaults] setBool: NO forKey: @"WarningResetStats"];
210   
211    if (code == NSAlertFirstButtonReturn)
212        [self performResetStats];
213}
214
215@end
Note: See TracBrowser for help on using the repository browser.