1 | /****************************************************************************** |
---|
2 | * $Id: StatsWindowController.m 9105 2009-09-12 02:39:45Z 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 | |
---|
41 | StatsWindowController * fStatsWindowInstance = nil; |
---|
42 | tr_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 | - (NSString *) windowFrameAutosaveName |
---|
155 | { |
---|
156 | return @"StatsWindow"; |
---|
157 | } |
---|
158 | |
---|
159 | @end |
---|
160 | |
---|
161 | @implementation StatsWindowController (Private) |
---|
162 | |
---|
163 | - (void) updateStats |
---|
164 | { |
---|
165 | tr_session_stats statsAll, statsSession; |
---|
166 | tr_sessionGetCumulativeStats(fLib, &statsAll); |
---|
167 | tr_sessionGetStats(fLib, &statsSession); |
---|
168 | |
---|
169 | [fUploadedField setStringValue: [NSString stringForFileSize: statsSession.uploadedBytes]]; |
---|
170 | [fUploadedField setToolTip: [NSString stringWithFormat: NSLocalizedString(@"%llu bytes", "stats -> bytes"), |
---|
171 | statsSession.uploadedBytes]]; |
---|
172 | [fUploadedAllField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%@ total", "stats total"), |
---|
173 | [NSString stringForFileSize: statsAll.uploadedBytes]]]; |
---|
174 | [fUploadedAllField setToolTip: [NSString stringWithFormat: NSLocalizedString(@"%llu bytes", "stats -> bytes"), |
---|
175 | statsAll.uploadedBytes]]; |
---|
176 | |
---|
177 | [fDownloadedField setStringValue: [NSString stringForFileSize: statsSession.downloadedBytes]]; |
---|
178 | [fDownloadedField setToolTip: [NSString stringWithFormat: NSLocalizedString(@"%llu bytes", "stats -> bytes"), |
---|
179 | statsSession.downloadedBytes]]; |
---|
180 | [fDownloadedAllField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%@ total", "stats total"), |
---|
181 | [NSString stringForFileSize: statsAll.downloadedBytes]]]; |
---|
182 | [fDownloadedAllField setToolTip: [NSString stringWithFormat: NSLocalizedString(@"%llu bytes", "stats -> bytes"), |
---|
183 | statsAll.downloadedBytes]]; |
---|
184 | |
---|
185 | [fRatioField setStringValue: [NSString stringForRatio: statsSession.ratio]]; |
---|
186 | |
---|
187 | NSString * totalRatioString = statsAll.ratio != TR_RATIO_NA |
---|
188 | ? [NSString stringWithFormat: NSLocalizedString(@"%@ total", "stats total"), [NSString stringForRatio: statsAll.ratio]] |
---|
189 | : NSLocalizedString(@"Total N/A", "stats total"); |
---|
190 | [fRatioAllField setStringValue: totalRatioString]; |
---|
191 | |
---|
192 | [fTimeField setStringValue: [NSString timeString: statsSession.secondsActive showSeconds: NO]]; |
---|
193 | [fTimeAllField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%@ total", "stats total"), |
---|
194 | [NSString timeString: statsAll.secondsActive showSeconds: NO]]]; |
---|
195 | |
---|
196 | if (statsAll.sessionCount == 1) |
---|
197 | [fNumOpenedField setStringValue: NSLocalizedString(@"1 time", "stats window -> times opened")]; |
---|
198 | else |
---|
199 | [fNumOpenedField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%llu times", "stats window -> times opened"), |
---|
200 | statsAll.sessionCount]]; |
---|
201 | } |
---|
202 | |
---|
203 | - (void) performResetStats |
---|
204 | { |
---|
205 | tr_sessionClearStats(fLib); |
---|
206 | [self updateStats]; |
---|
207 | } |
---|
208 | |
---|
209 | - (void) resetSheetClosed: (NSAlert *) alert returnCode: (NSInteger) code contextInfo: (void *) info |
---|
210 | { |
---|
211 | [[alert window] orderOut: nil]; |
---|
212 | |
---|
213 | if ([[alert suppressionButton] state] == NSOnState) |
---|
214 | [[NSUserDefaults standardUserDefaults] setBool: NO forKey: @"WarningResetStats"]; |
---|
215 | |
---|
216 | if (code == NSAlertFirstButtonReturn) |
---|
217 | [self performResetStats]; |
---|
218 | } |
---|
219 | |
---|
220 | @end |
---|