source: trunk/macosx/TrackerNode.m

Last change on this file was 14341, checked in by livings124, 7 years ago

Yosemite: use NSDateComponentsFormatter instead of custom time string code

  • Property svn:keywords set to Date Rev Author Id
File size: 8.2 KB
Line 
1/******************************************************************************
2 * $Id: TrackerNode.m 14341 2014-10-17 05:12:00Z mikedld $
3 *
4 * Copyright (c) 2009-2012 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#warning remove ivars in header when 64-bit only (or it compiles in 32-bit mode)
32@synthesize torrent = fTorrent;
33
34- (id) initWithTrackerStat: (tr_tracker_stat *) stat torrent: (Torrent *) torrent
35{
36    if ((self = [super init]))
37    {
38        fStat = *stat;
39        fTorrent = torrent; //weak reference
40    }
41   
42    return self;
43}
44
45- (NSString *) description
46{
47    return [@"Tracker: " stringByAppendingString: [self fullAnnounceAddress]];
48}
49
50- (id) copyWithZone: (NSZone *) zone
51{
52    //this object is essentially immutable after initial setup
53    return [self retain];
54}
55
56- (BOOL) isEqual: (id) object
57{
58    if (self == object)
59        return YES;
60   
61    if (![object isKindOfClass: [self class]])
62        return NO;
63   
64    if ([self torrent] != [object torrent])
65        return NO;
66   
67    return [self tier] == [object tier] && [[self fullAnnounceAddress] isEqualToString: [object fullAnnounceAddress]];
68}
69
70- (NSString *) host
71{
72    return [NSString stringWithUTF8String: fStat.host];
73}
74
75- (NSString *) fullAnnounceAddress
76{
77    return [NSString stringWithUTF8String: fStat.announce];
78}
79
80- (NSInteger) tier
81{
82    return fStat.tier;
83}
84
85- (NSUInteger) identifier
86{
87    return fStat.id;
88}
89
90- (NSInteger) totalSeeders
91{
92    return fStat.seederCount;
93}
94
95- (NSInteger) totalLeechers
96{
97    return fStat.leecherCount;
98}
99
100- (NSInteger) totalDownloaded
101{
102    return fStat.downloadCount;
103}
104
105- (NSString *) lastAnnounceStatusString
106{
107    NSString * dateString;
108    if (fStat.hasAnnounced)
109    {
110        NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
111        [dateFormatter setDateStyle: NSDateFormatterFullStyle];
112        [dateFormatter setTimeStyle: NSDateFormatterShortStyle];
113        [dateFormatter setDoesRelativeDateFormatting: YES];
114       
115        dateString = [dateFormatter stringFromDate: [NSDate dateWithTimeIntervalSince1970: fStat.lastAnnounceTime]];
116        [dateFormatter release];
117    }
118    else
119        dateString = NSLocalizedString(@"N/A", "Tracker last announce");
120   
121    NSString * baseString;
122    if (fStat.hasAnnounced && fStat.lastAnnounceTimedOut)
123        baseString = [NSLocalizedString(@"Announce timed out", "Tracker last announce") stringByAppendingFormat: @": %@", dateString];
124    else if (fStat.hasAnnounced && !fStat.lastAnnounceSucceeded)
125    {
126        baseString = NSLocalizedString(@"Announce error", "Tracker last announce");
127       
128        NSString * errorString = [NSString stringWithUTF8String: fStat.lastAnnounceResult];
129        if ([errorString isEqualToString: @""])
130            baseString = [baseString stringByAppendingFormat: @": %@", dateString];
131        else
132            baseString = [baseString stringByAppendingFormat: @": %@ - %@", errorString, dateString];
133    }
134    else
135    {
136        baseString = [NSLocalizedString(@"Last Announce", "Tracker last announce") stringByAppendingFormat: @": %@", dateString];
137        if (fStat.hasAnnounced && fStat.lastAnnounceSucceeded && fStat.lastAnnouncePeerCount > 0)
138        {
139            NSString * peerString;
140            if (fStat.lastAnnouncePeerCount == 1)
141                peerString = NSLocalizedString(@"got 1 peer", "Tracker last announce");
142            else
143                peerString = [NSString stringWithFormat: NSLocalizedString(@"got %d peers", "Tracker last announce"),
144                                        fStat.lastAnnouncePeerCount];
145            baseString = [baseString stringByAppendingFormat: @" (%@)", peerString];
146        }
147    }
148   
149    return baseString;
150}
151
152- (NSString *) nextAnnounceStatusString
153{
154    switch (fStat.announceState)
155    {
156        case TR_TRACKER_ACTIVE:
157            return [NSLocalizedString(@"Announce in progress", "Tracker next announce") stringByAppendingEllipsis];
158       
159        case TR_TRACKER_WAITING:
160        {
161            const NSTimeInterval nextAnnounceTimeLeft = fStat.nextAnnounceTime - [[NSDate date] timeIntervalSince1970];
162           
163            NSString *timeString;
164            if ([NSApp isOnYosemiteOrBetter]) {
165                static NSDateComponentsFormatter *formatter;
166                static dispatch_once_t onceToken;
167                dispatch_once(&onceToken, ^{
168                    formatter = [NSDateComponentsFormatter new];
169                    formatter.unitsStyle = NSDateComponentsFormatterUnitsStyleAbbreviated;
170                    formatter.zeroFormattingBehavior = NSDateComponentsFormatterZeroFormattingBehaviorDropLeading;
171                    formatter.collapsesLargestUnit = YES;
172                });
173               
174                timeString = [formatter stringFromTimeInterval: nextAnnounceTimeLeft];
175            }
176            else {
177                timeString = [NSString timeString: nextAnnounceTimeLeft includesTimeRemainingPhrase: NO showSeconds: YES];
178            }
179            return [NSString stringWithFormat: NSLocalizedString(@"Next announce in %@", "Tracker next announce"),
180                    timeString];
181        }
182        case TR_TRACKER_QUEUED:
183            return [NSLocalizedString(@"Announce is queued", "Tracker next announce") stringByAppendingEllipsis];
184       
185        case TR_TRACKER_INACTIVE:
186            return fStat.isBackup ? NSLocalizedString(@"Tracker will be used as a backup", "Tracker next announce")
187                                    : NSLocalizedString(@"Announce not scheduled", "Tracker next announce");
188       
189        default:
190            NSAssert1(NO, @"unknown announce state: %d", fStat.announceState);
191            return nil;
192    }
193}
194
195- (NSString *) lastScrapeStatusString
196{
197    NSString * dateString;
198    if (fStat.hasScraped)
199    {
200        NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
201        [dateFormatter setDateStyle: NSDateFormatterFullStyle];
202        [dateFormatter setTimeStyle: NSDateFormatterShortStyle];
203        [dateFormatter setDoesRelativeDateFormatting: YES];
204       
205        dateString = [dateFormatter stringFromDate: [NSDate dateWithTimeIntervalSince1970: fStat.lastScrapeTime]];
206        [dateFormatter release];
207    }
208    else
209        dateString = NSLocalizedString(@"N/A", "Tracker last scrape");
210   
211    NSString * baseString;
212    if (fStat.hasScraped && fStat.lastScrapeTimedOut)
213        baseString = [NSLocalizedString(@"Scrape timed out", "Tracker last scrape") stringByAppendingFormat: @": %@", dateString];
214    else if (fStat.hasScraped && !fStat.lastScrapeSucceeded)
215    {
216        baseString = NSLocalizedString(@"Scrape error", "Tracker last scrape");
217       
218        NSString * errorString = [NSString stringWithUTF8String: fStat.lastScrapeResult];
219        if ([errorString isEqualToString: @""])
220            baseString = [baseString stringByAppendingFormat: @": %@", dateString];
221        else
222            baseString = [baseString stringByAppendingFormat: @": %@ - %@", errorString, dateString];
223    }
224    else
225        baseString = [NSLocalizedString(@"Last Scrape", "Tracker last scrape") stringByAppendingFormat: @": %@", dateString];
226   
227    return baseString;
228}
229
230@end
Note: See TracBrowser for help on using the repository browser.