source: trunk/macosx/TrackerNode.m @ 10105

Last change on this file since 10105 was 10105, checked in by livings124, 12 years ago

improve how tracker tiers are sent to the inspector code

  • Property svn:keywords set to Date Rev Author Id
File size: 6.7 KB
Line 
1/******************************************************************************
2 * $Id: TrackerNode.m 10105 2010-02-06 02:35:27Z livings124 $
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.lastAnnounceTimedOut)
107        baseString = [NSLocalizedString(@"Announce timed out", "Tracker last announce") stringByAppendingFormat: @": %@", dateString];
108    else if (fStat.hasAnnounced && !fStat.lastAnnounceSucceeded)
109    {
110        baseString = NSLocalizedString(@"Announce error", "Tracker last announce");
111       
112        NSString * errorString = [NSString stringWithUTF8String: fStat.lastAnnounceResult];
113        if ([errorString isEqualToString: @""])
114            baseString = [baseString stringByAppendingFormat: @": %@", dateString];
115        else
116            baseString = [baseString stringByAppendingFormat: @": %@ - %@", errorString, dateString];
117    }
118    else
119    {
120        baseString = [NSLocalizedString(@"Last Announce", "Tracker last announce") stringByAppendingFormat: @": %@", dateString];
121        if (fStat.hasAnnounced && fStat.lastAnnounceSucceeded)
122        {
123            NSString * peerString;
124            if (fStat.lastAnnouncePeerCount == 1)
125                peerString = NSLocalizedString(@"got 1 peer", "Tracker last announce");
126            else
127                peerString = [NSString stringWithFormat: NSLocalizedString(@"got %d peers", "Tracker last announce"),
128                                        fStat.lastAnnouncePeerCount];
129            baseString = [baseString stringByAppendingFormat: @" (%@)", peerString];
130        }
131    }
132   
133    return baseString;
134}
135
136- (NSString *) nextAnnounceStatusString
137{
138    switch (fStat.announceState)
139    {
140        case TR_TRACKER_ACTIVE:
141            return [NSLocalizedString(@"Announce in progress", "Tracker next announce") stringByAppendingEllipsis];
142       
143        case TR_TRACKER_WAITING:
144            return [NSString stringWithFormat: NSLocalizedString(@"Next announce in %@", "Tracker next announce"),
145                    [NSString timeString: fStat.nextAnnounceTime - [[NSDate date] timeIntervalSince1970] showSeconds: YES]];
146       
147        case TR_TRACKER_QUEUED:
148            return [NSLocalizedString(@"Announce is queued", "Tracker next announce") stringByAppendingEllipsis];
149       
150        case TR_TRACKER_INACTIVE:
151            return fStat.isBackup ? NSLocalizedString(@"Tracker will be used as a backup", "Tracker next announce")
152                                    : NSLocalizedString(@"Announce not scheduled", "Tracker next announce");
153       
154        default:
155            NSAssert1(NO, @"unknown announce state: %d", fStat.announceState);
156            return nil;
157    }
158}
159
160- (NSString *) lastScrapeStatusString
161{
162    NSString * dateString;
163    if (fStat.hasScraped)
164    {
165        NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
166        [dateFormatter setDateStyle: NSDateFormatterFullStyle];
167        [dateFormatter setTimeStyle: NSDateFormatterShortStyle];
168       
169        if ([NSApp isOnSnowLeopardOrBetter])
170            [dateFormatter setDoesRelativeDateFormatting: YES];
171       
172        dateString = [dateFormatter stringFromDate: [NSDate dateWithTimeIntervalSince1970: fStat.lastScrapeTime]];
173        [dateFormatter release];
174    }
175    else
176        dateString = NSLocalizedString(@"N/A", "Tracker last scrape");
177   
178    NSString * baseString;
179    if (fStat.hasScraped && !fStat.lastScrapeSucceeded)
180    {
181        baseString = NSLocalizedString(@"Scrape error", "Tracker last scrape");
182       
183        NSString * errorString = [NSString stringWithUTF8String: fStat.lastScrapeResult];
184        if ([errorString isEqualToString: @""])
185            baseString = [baseString stringByAppendingFormat: @": %@", dateString];
186        else
187            baseString = [baseString stringByAppendingFormat: @": %@ - %@", errorString, dateString];
188    }
189    else
190        baseString = [NSLocalizedString(@"Last Scrape", "Tracker last scrape") stringByAppendingFormat: @": %@", dateString];
191   
192    return baseString;
193}
194
195@end
Note: See TracBrowser for help on using the repository browser.