source: trunk/macosx/TrackerNode.m @ 9255

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

minimal efficiency change: when drawing on the torrent cell, don't copy the bound rect unnecessarily

File size: 5.7 KB
Line 
1/******************************************************************************
2 * $Id$
3 *
4 * Copyright (c) 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 "TrackerNode.h"
26#import "NSApplicationAdditions.h"
27#import "NSStringAdditions.h"
28
29@implementation TrackerNode
30
31- (id) initWithTrackerStat: (tr_tracker_stat *) stat
32{
33    if ((self = [super init]))
34    {
35        fStat = *stat;
36    }
37   
38    return self;
39}
40
41- (id) copyWithZone: (NSZone *) zone
42{
43    //this object is essentially immutable after initial setup
44    return [self retain];
45}
46
47- (NSString *) host
48{
49    return [NSString stringWithUTF8String: fStat.host];
50}
51
52- (NSString *) fullAnnounceAddress
53{
54    return [NSString stringWithUTF8String: fStat.announce];
55}
56
57- (NSInteger) tier
58{
59    return fStat.tier;
60}
61
62- (NSInteger) totalSeeders
63{
64    return fStat.seederCount;
65}
66
67- (NSInteger) totalLeechers
68{
69    return fStat.leecherCount;
70}
71
72- (NSInteger) totalDownloaded
73{
74    return fStat.downloadCount;
75}
76
77- (NSString *) lastAnnounceStatusString
78{
79    NSString * dateString;
80    if (fStat.hasAnnounced)
81    {
82        NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
83        [dateFormatter setDateStyle: NSDateFormatterFullStyle];
84        [dateFormatter setTimeStyle: NSDateFormatterShortStyle];
85       
86        if ([NSApp isOnSnowLeopardOrBetter])
87            [dateFormatter setDoesRelativeDateFormatting: YES];
88       
89        dateString = [dateFormatter stringFromDate: [NSDate dateWithTimeIntervalSince1970: fStat.lastAnnounceTime]];
90        [dateFormatter release];
91    }
92    else
93        dateString = NSLocalizedString(@"N/A", "Tracker last announce");
94   
95    if (fStat.hasAnnounced && !fStat.lastAnnounceSucceeded)
96        dateString = [NSLocalizedString(@"Announce error", "Tracker last announce") stringByAppendingFormat: @": %@ - %@",
97                        [NSString stringWithUTF8String: fStat.lastAnnounceResult], dateString];
98    else
99    {
100        dateString = [NSLocalizedString(@"Last Announce", "Tracker last announce") stringByAppendingFormat: @": %@", dateString];
101        if (fStat.hasAnnounced && fStat.lastAnnounceSucceeded)
102        {
103            NSString * peerString = [NSString stringWithFormat: NSLocalizedString(@"got %d peers", "Tracker last announce"),
104                                        fStat.lastAnnouncePeerCount];
105            dateString = [dateString stringByAppendingFormat: @" (%@)", peerString];
106        }
107    }
108   
109    return dateString;
110}
111
112- (NSString *) nextAnnounceStatusString
113{
114    switch (fStat.announceState)
115    {
116        case TR_TRACKER_ACTIVE:
117            return [NSLocalizedString(@"Announce in progress", "Tracker next announce") stringByAppendingEllipsis];
118       
119        case TR_TRACKER_WAITING:
120            return [NSString stringWithFormat: NSLocalizedString(@"Next announce in %@", "Tracker next announce"),
121                    [NSString timeString: fStat.nextAnnounceTime - [[NSDate date] timeIntervalSince1970] showSeconds: YES]];
122       
123        case TR_TRACKER_QUEUED:
124            return [NSLocalizedString(@"Announce is queued", "Tracker next announce") stringByAppendingEllipsis];
125       
126        case TR_TRACKER_INACTIVE:
127            return fStat.isActive ? NSLocalizedString(@"Announce not scheduled", "Tracker next announce")
128                                    : NSLocalizedString(@"Tracker will be used as a backup", "Tracker next announce");
129       
130        default:
131            NSAssert(NO, @"unknown announce state: %d", fStat.announceState);
132            return nil;
133    }
134}
135
136- (NSString *) lastScrapeStatusString
137{
138    NSString * dateString;
139    if (fStat.hasScraped)
140    {
141        NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
142        [dateFormatter setDateStyle: NSDateFormatterFullStyle];
143        [dateFormatter setTimeStyle: NSDateFormatterShortStyle];
144       
145        if ([NSApp isOnSnowLeopardOrBetter])
146            [dateFormatter setDoesRelativeDateFormatting: YES];
147       
148        dateString = [dateFormatter stringFromDate: [NSDate dateWithTimeIntervalSince1970: fStat.lastScrapeTime]];
149        [dateFormatter release];
150    }
151    else
152        dateString = NSLocalizedString(@"N/A", "Tracker last scrape");
153   
154    if (fStat.hasScraped && !fStat.lastScrapeSucceeded)
155        dateString = [NSLocalizedString(@"Scrape error", "Tracker last scrape") stringByAppendingFormat: @": %@ - %@",
156                        [NSString stringWithUTF8String: fStat.lastScrapeResult], dateString];
157    else
158        dateString = [NSLocalizedString(@"Last Scrape", "Tracker last scrape") stringByAppendingFormat: @": %@", dateString];
159   
160    return dateString;
161}
162
163@end
Note: See TracBrowser for help on using the repository browser.