source: trunk/macosx/TrackerNode.m @ 10190

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

add the ability to remove trackers when multiple torrents are selected

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