source: trunk/macosx/TrackerNode.m @ 9931

Last change on this file since 9931 was 9931, checked in by charles, 13 years ago

(trunk) housekeeping: (1) add standard svn properties for $Id$ substitution in some files (2) add #include guards in a couple of libtransmission headers (3) refresh build instructions in README

  • Property svn:keywords set to Date Rev Author Id
File size: 6.6 KB
Line 
1/******************************************************************************
2 * $Id: TrackerNode.m 9931 2010-01-14 14:20:49Z charles $
3 *
4 * Copyright (c) 2009-2010 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- (NSString *) description
42{
43    return [@"Tracker: " stringByAppendingString: [self fullAnnounceAddress]];
44}
45
46- (id) copyWithZone: (NSZone *) zone
47{
48    //this object is essentially immutable after initial setup
49    return [self retain];
50}
51
52- (NSString *) host
53{
54    return [NSString stringWithUTF8String: fStat.host];
55}
56
57- (NSString *) fullAnnounceAddress
58{
59    return [NSString stringWithUTF8String: fStat.announce];
60}
61
62- (NSInteger) tier
63{
64    return fStat.tier;
65}
66
67- (NSUInteger) identifier
68{
69    return fStat.id;
70}
71
72- (NSInteger) totalSeeders
73{
74    return fStat.seederCount;
75}
76
77- (NSInteger) totalLeechers
78{
79    return fStat.leecherCount;
80}
81
82- (NSInteger) totalDownloaded
83{
84    return fStat.downloadCount;
85}
86
87- (NSString *) lastAnnounceStatusString
88{
89    NSString * dateString;
90    if (fStat.hasAnnounced)
91    {
92        NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
93        [dateFormatter setDateStyle: NSDateFormatterFullStyle];
94        [dateFormatter setTimeStyle: NSDateFormatterShortStyle];
95       
96        if ([NSApp isOnSnowLeopardOrBetter])
97            [dateFormatter setDoesRelativeDateFormatting: YES];
98       
99        dateString = [dateFormatter stringFromDate: [NSDate dateWithTimeIntervalSince1970: fStat.lastAnnounceTime]];
100        [dateFormatter release];
101    }
102    else
103        dateString = NSLocalizedString(@"N/A", "Tracker last announce");
104   
105    NSString * baseString;
106    if (fStat.hasAnnounced && !fStat.lastAnnounceSucceeded)
107    {
108        baseString = NSLocalizedString(@"Announce error", "Tracker last announce");
109        NSString * errorString = [NSString stringWithUTF8String: fStat.lastAnnounceResult];
110       
111        if ([errorString isEqualToString: @""])
112            baseString = [baseString stringByAppendingFormat: @": %@", dateString];
113        else
114            baseString = [baseString stringByAppendingFormat: @": %@ - %@", errorString, dateString];
115    }
116    else
117    {
118        baseString = [NSLocalizedString(@"Last Announce", "Tracker last announce") stringByAppendingFormat: @": %@", dateString];
119        if (fStat.hasAnnounced && fStat.lastAnnounceSucceeded)
120        {
121            #warning after 1.8 fix ugly hack
122            NSString * peerString;
123            if (fStat.lastAnnouncePeerCount == 1 && [[[NSLocale currentLocale] localeIdentifier] hasPrefix: @"en_"])
124                peerString = NSLocalizedString(@"got 1 peer", "Tracker last announce");
125            else
126                peerString = [NSString stringWithFormat: NSLocalizedString(@"got %d peers", "Tracker last announce"),
127                                        fStat.lastAnnouncePeerCount];
128            baseString = [baseString stringByAppendingFormat: @" (%@)", peerString];
129        }
130    }
131   
132    return baseString;
133}
134
135- (NSString *) nextAnnounceStatusString
136{
137    switch (fStat.announceState)
138    {
139        case TR_TRACKER_ACTIVE:
140            return [NSLocalizedString(@"Announce in progress", "Tracker next announce") stringByAppendingEllipsis];
141       
142        case TR_TRACKER_WAITING:
143            return [NSString stringWithFormat: NSLocalizedString(@"Next announce in %@", "Tracker next announce"),
144                    [NSString timeString: fStat.nextAnnounceTime - [[NSDate date] timeIntervalSince1970] showSeconds: YES]];
145       
146        case TR_TRACKER_QUEUED:
147            return [NSLocalizedString(@"Announce is queued", "Tracker next announce") stringByAppendingEllipsis];
148       
149        case TR_TRACKER_INACTIVE:
150            return fStat.isBackup ? NSLocalizedString(@"Tracker will be used as a backup", "Tracker next announce")
151                                    : NSLocalizedString(@"Announce not scheduled", "Tracker next announce");
152       
153        default:
154            NSAssert1(NO, @"unknown announce state: %d", fStat.announceState);
155            return nil;
156    }
157}
158
159- (NSString *) lastScrapeStatusString
160{
161    NSString * dateString;
162    if (fStat.hasScraped)
163    {
164        NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
165        [dateFormatter setDateStyle: NSDateFormatterFullStyle];
166        [dateFormatter setTimeStyle: NSDateFormatterShortStyle];
167       
168        if ([NSApp isOnSnowLeopardOrBetter])
169            [dateFormatter setDoesRelativeDateFormatting: YES];
170       
171        dateString = [dateFormatter stringFromDate: [NSDate dateWithTimeIntervalSince1970: fStat.lastScrapeTime]];
172        [dateFormatter release];
173    }
174    else
175        dateString = NSLocalizedString(@"N/A", "Tracker last scrape");
176   
177    NSString * baseString;
178    if (fStat.hasScraped && !fStat.lastScrapeSucceeded)
179    {
180        baseString = NSLocalizedString(@"Scrape error", "Tracker last scrape");
181        NSString * errorString = [NSString stringWithUTF8String: fStat.lastScrapeResult];
182       
183        if ([errorString isEqualToString: @""])
184            baseString = [baseString stringByAppendingFormat: @": %@", dateString];
185        else
186            baseString = [baseString stringByAppendingFormat: @": %@ - %@", errorString, dateString];
187    }
188    else
189        baseString = [NSLocalizedString(@"Last Scrape", "Tracker last scrape") stringByAppendingFormat: @": %@", dateString];
190   
191    return baseString;
192}
193
194@end
Note: See TracBrowser for help on using the repository browser.