source: trunk/macosx/StatsWindowController.m @ 3913

Last change on this file since 3913 was 3913, checked in by livings124, 15 years ago

don't pluralize "time" if opened only once

File size: 5.4 KB
Line 
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
39StatsWindowController * fStatsWindowInstance = nil;
40tr_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_session_stats statsAll, statsSession;
79    tr_getCumulativeSessionStats(fLib, &statsAll);
80    tr_getSessionStats(fLib, &statsSession);
81   
82    [fUploadedField setStringValue: [NSString stringForLargeFileSizeGigs: statsSession.uploadedGigs bytes: statsSession.uploadedBytes]];
83    [fUploadedAllField setStringValue: [[NSString stringForLargeFileSizeGigs: statsAll.uploadedGigs bytes: statsAll.uploadedBytes]
84                                        stringByAppendingString: NSLocalizedString(@" total", "stats total")]];
85   
86    [fDownloadedField setStringValue: [NSString stringForLargeFileSizeGigs: statsSession.downloadedGigs
87                                        bytes: statsSession.downloadedBytes]];
88    [fDownloadedAllField setStringValue: [[NSString stringForLargeFileSizeGigs: statsAll.downloadedGigs
89        bytes: statsAll.downloadedBytes] stringByAppendingString: NSLocalizedString(@" total", "stats total")]];
90   
91    [fRatioField setStringValue: [NSString stringForRatio: statsSession.ratio]];
92    [fRatioAllField setStringValue: [[NSString stringForRatio: statsAll.ratio]
93                                    stringByAppendingString: NSLocalizedString(@" total", "stats total")]];
94   
95    [fTimeField setStringValue: [self timeString: statsSession.secondsActive]];
96    [fTimeAllField setStringValue: [[self timeString: statsAll.secondsActive]
97                                    stringByAppendingString: NSLocalizedString(@" total", "stats total")]];
98   
99    if (statsAll.sessionCount == 1)
100        [fNumOpenedField setStringValue: NSLocalizedString(@"1 time", "stats window -> times opened")];
101    else
102        [fNumOpenedField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%d times", "stats window -> times opened"),
103                                        statsAll.sessionCount]];
104}
105
106- (NSString *) timeString: (uint64_t) seconds
107{
108    NSMutableArray * timeArray = [NSMutableArray arrayWithCapacity: 4];
109   
110    if (seconds >= 86400) //24 * 60 * 60
111    {
112        int days = seconds / 86400;
113        if (days == 1)
114            [timeArray addObject: NSLocalizedString(@"1 day", "stats window -> running time")];
115        else
116            [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%d days", "stats window -> running time"),
117                                    seconds / 86400]];
118        seconds %= 86400;
119    }
120    if (seconds >= 3600) //60 * 60
121    {
122        [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%d hours", "stats window -> running time"),
123                                seconds / 3600]];
124        seconds %= 3600;
125    }
126    if (seconds >= 60)
127    {
128        [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%d min", "stats window -> running time"), seconds / 60]];
129        seconds %= 60;
130    }
131    [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%d sec", "stats window -> running time"), seconds]];
132   
133    return [timeArray componentsJoinedByString: @" "];
134}
135
136@end
Note: See TracBrowser for help on using the repository browser.