source: trunk/macosx/StatsWindowController.m @ 7468

Last change on this file since 7468 was 7468, checked in by charles, 12 years ago

(trunk) patch from wereHamster to finish off tr_handle

  • Property svn:keywords set to Date Rev Author Id
File size: 9.4 KB
Line 
1/******************************************************************************
2 * $Id: StatsWindowController.m 7468 2008-12-22 19:14:43Z charles $
3 *
4 * Copyright (c) 2007-2008 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#import "NSApplicationAdditions.h"
28
29#define UPDATE_SECONDS 1.0
30
31@interface StatsWindowController (Private)
32
33- (void) updateStats;
34
35- (void) performResetStats;
36- (void) resetSheetClosed: (NSAlert *) alert returnCode: (NSInteger) code contextInfo: (void *) info;
37
38@end
39
40@implementation StatsWindowController
41
42StatsWindowController * fStatsWindowInstance = nil;
43tr_session * fLib;
44+ (StatsWindowController *) statsWindow: (tr_session *) lib
45{
46    if (!fStatsWindowInstance)
47    {
48        if ((fStatsWindowInstance = [[self alloc] initWithWindowNibName: @"StatsWindow"]))
49        {
50            fLib = lib;
51        }
52    }
53    return fStatsWindowInstance;
54}
55
56- (void) awakeFromNib
57{
58    [self updateStats];
59   
60    fTimer = [NSTimer scheduledTimerWithTimeInterval: UPDATE_SECONDS target: self
61                selector: @selector(updateStats) userInfo: nil repeats: YES];
62    [[NSRunLoop currentRunLoop] addTimer: fTimer forMode: NSModalPanelRunLoopMode];
63    [[NSRunLoop currentRunLoop] addTimer: fTimer forMode: NSEventTrackingRunLoopMode];
64   
65    [[self window] setTitle: NSLocalizedString(@"Statistics", "Stats window -> title")];
66   
67    //set label text
68    [fUploadedLabelField setStringValue: [NSLocalizedString(@"Uploaded", "Stats window -> label") stringByAppendingString: @":"]];
69    [fDownloadedLabelField setStringValue: [NSLocalizedString(@"Downloaded", "Stats window -> label") stringByAppendingString: @":"]];
70    [fRatioLabelField setStringValue: [NSLocalizedString(@"Ratio", "Stats window -> label") stringByAppendingString: @":"]];
71    [fTimeLabelField setStringValue: [NSLocalizedString(@"Running Time", "Stats window -> label") stringByAppendingString: @":"]];
72    [fNumOpenedLabelField setStringValue: [NSLocalizedString(@"Program Started", "Stats window -> label")
73                                            stringByAppendingString: @":"]];
74   
75    //size all elements
76    CGFloat oldWidth = [fUploadedLabelField frame].size.width;
77   
78    [fUploadedLabelField sizeToFit];
79    [fDownloadedLabelField sizeToFit];
80    [fRatioLabelField sizeToFit];
81    [fTimeLabelField sizeToFit];
82    [fNumOpenedLabelField sizeToFit];
83   
84    CGFloat maxWidth = MAX([fUploadedLabelField frame].size.width, [fDownloadedLabelField frame].size.width);
85    maxWidth = MAX(maxWidth, [fRatioLabelField frame].size.width);
86    maxWidth = MAX(maxWidth, [fTimeLabelField frame].size.width);
87    maxWidth = MAX(maxWidth, [fNumOpenedLabelField frame].size.width);
88   
89    NSRect frame = [fUploadedLabelField frame];
90    frame.size.width = maxWidth;
91    [fUploadedLabelField setFrame: frame];
92   
93    frame = [fDownloadedLabelField frame];
94    frame.size.width = maxWidth;
95    [fDownloadedLabelField setFrame: frame];
96   
97    frame = [fRatioLabelField frame];
98    frame.size.width = maxWidth;
99    [fRatioLabelField setFrame: frame];
100   
101    frame = [fTimeLabelField frame];
102    frame.size.width = maxWidth;
103    [fTimeLabelField setFrame: frame];
104   
105    frame = [fNumOpenedLabelField frame];
106    frame.size.width = maxWidth;
107    [fNumOpenedLabelField setFrame: frame];
108   
109    //resize window for new label width - fields are set in nib to adjust correctly
110    NSRect windowRect = [[self window] frame];
111    windowRect.size.width += maxWidth - oldWidth;
112    [[self window] setFrame: windowRect display: YES];
113   
114    //resize reset button
115    CGFloat oldButtonWidth = [fResetButton frame].size.width;
116   
117    [fResetButton setTitle: NSLocalizedString(@"Reset", "Stats window -> reset button")];
118    [fResetButton sizeToFit];
119   
120    NSRect buttonFrame = [fResetButton frame];
121    buttonFrame.size.width += 10.0f;
122    buttonFrame.origin.x -= buttonFrame.size.width - oldButtonWidth;
123    [fResetButton setFrame: buttonFrame];
124}
125
126- (void) windowWillClose: (id) sender
127{
128    [fTimer invalidate];
129   
130    [fStatsWindowInstance release];
131    fStatsWindowInstance = nil;
132}
133
134- (void) resetStats: (id) sender
135{
136    if (![[NSUserDefaults standardUserDefaults] boolForKey: @"WarningResetStats"])
137    {
138        [self performResetStats];
139        return;
140    }
141   
142    NSAlert * alert = [[NSAlert alloc] init];
143    [alert setMessageText: NSLocalizedString(@"Are you sure you want to reset usage statistics?", "Stats reset -> title")];
144    [alert setInformativeText: NSLocalizedString(@"This will clear the global statistics displayed by Transmission."
145                                " Individual transfer statistics will not be affected.", "Stats reset -> message")];
146    [alert setAlertStyle: NSWarningAlertStyle];
147    [alert addButtonWithTitle: NSLocalizedString(@"Reset", "Stats reset -> button")];
148    [alert addButtonWithTitle: NSLocalizedString(@"Cancel", "Stats reset -> button")];
149   
150    if ([NSApp isOnLeopardOrBetter])
151        [alert setShowsSuppressionButton: YES];
152    else
153        [alert addButtonWithTitle: NSLocalizedString(@"Don't Alert Again", "Stats reset -> button")];
154   
155    [alert beginSheetModalForWindow: [self window] modalDelegate: self
156        didEndSelector: @selector(resetSheetClosed:returnCode:contextInfo:) contextInfo: nil];
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 (([NSApp isOnLeopardOrBetter] ? [[alert suppressionButton] state] == NSOnState : code == NSAlertThirdButtonReturn))
214        [[NSUserDefaults standardUserDefaults] setBool: NO forKey: @"WarningResetStats"];
215   
216    if (code == NSAlertFirstButtonReturn)
217        [self performResetStats];
218}
219
220@end
Note: See TracBrowser for help on using the repository browser.