1 | /****************************************************************************** |
---|
2 | * $Id$ |
---|
3 | * |
---|
4 | * Copyright (c) 2007 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 | - (NSString *) timeString: (uint64_t) seconds; |
---|
34 | |
---|
35 | @end |
---|
36 | |
---|
37 | @implementation StatsWindowController |
---|
38 | |
---|
39 | StatsWindowController * fStatsWindowInstance = nil; |
---|
40 | tr_handle * fLib; |
---|
41 | + (StatsWindowController *) statsWindow: (tr_handle *) lib |
---|
42 | { |
---|
43 | if (!fStatsWindowInstance) |
---|
44 | { |
---|
45 | if ((fStatsWindowInstance = [[self alloc] initWithWindowNibName: @"StatsWindow"])) |
---|
46 | { |
---|
47 | fLib = lib; |
---|
48 | } |
---|
49 | } |
---|
50 | return fStatsWindowInstance; |
---|
51 | } |
---|
52 | |
---|
53 | - (void) awakeFromNib |
---|
54 | { |
---|
55 | [self updateStats]; |
---|
56 | |
---|
57 | fTimer = [NSTimer scheduledTimerWithTimeInterval: UPDATE_SECONDS target: self |
---|
58 | selector: @selector(updateStats) userInfo: nil repeats: YES]; |
---|
59 | [[NSRunLoop currentRunLoop] addTimer: fTimer forMode: NSModalPanelRunLoopMode]; |
---|
60 | [[NSRunLoop currentRunLoop] addTimer: fTimer forMode: NSEventTrackingRunLoopMode]; |
---|
61 | } |
---|
62 | |
---|
63 | - (void) windowWillClose: (id)sender |
---|
64 | { |
---|
65 | if (fTimer) |
---|
66 | [fTimer invalidate]; |
---|
67 | |
---|
68 | [fStatsWindowInstance release]; |
---|
69 | fStatsWindowInstance = nil; |
---|
70 | } |
---|
71 | |
---|
72 | @end |
---|
73 | |
---|
74 | @implementation StatsWindowController (Private) |
---|
75 | |
---|
76 | - (void) updateStats |
---|
77 | { |
---|
78 | tr_global_stats stats; |
---|
79 | tr_getGlobalStats(fLib, &stats); |
---|
80 | |
---|
81 | [fUploadedField setStringValue: [NSString stringForLargeFileSizeGigs: stats.uploadedGigs bytes: stats.uploadedBytes]]; |
---|
82 | [fDownloadedField setStringValue: [NSString stringForLargeFileSizeGigs: stats.downloadedGigs bytes: stats.downloadedBytes]]; |
---|
83 | [fRatioField setStringValue: [NSString stringForRatio: stats.ratio]]; |
---|
84 | |
---|
85 | [fTimeField setStringValue: [self timeString: stats.secondsActive]]; |
---|
86 | |
---|
87 | [fNumOpenedField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%d Times", "stats window -> times opened"), |
---|
88 | stats.sessionCount]]; |
---|
89 | } |
---|
90 | |
---|
91 | - (NSString *) timeString: (uint64_t) seconds |
---|
92 | { |
---|
93 | NSMutableArray * timeArray = [NSMutableArray arrayWithCapacity: 4]; |
---|
94 | |
---|
95 | if (seconds >= 86400) //24 * 60 * 60 |
---|
96 | { |
---|
97 | int days = seconds / 86400; |
---|
98 | if (days == 1) |
---|
99 | [timeArray addObject: NSLocalizedString(@"1 day", "stats window -> running time")]; |
---|
100 | else |
---|
101 | [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%d days", "stats window -> running time"), |
---|
102 | seconds / 86400]]; |
---|
103 | seconds %= 86400; |
---|
104 | } |
---|
105 | if (seconds >= 3600) //60 * 60 |
---|
106 | { |
---|
107 | [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%d hours", "stats window -> running time"), |
---|
108 | seconds / 3600]]; |
---|
109 | seconds %= 3600; |
---|
110 | } |
---|
111 | if (seconds >= 60) |
---|
112 | { |
---|
113 | [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%d min", "stats window -> running time"), seconds / 60]]; |
---|
114 | seconds %= 60; |
---|
115 | } |
---|
116 | [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%d sec", "stats window -> running time"), seconds]]; |
---|
117 | |
---|
118 | return [timeArray componentsJoinedByString: @" "]; |
---|
119 | } |
---|
120 | |
---|
121 | @end |
---|