source: trunk/macosx/Torrent.m @ 10197

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

#2902 New tracker addition field obscured when trackers vertically fill inspector view

  • Property svn:keywords set to Date Rev Author Id
File size: 55.6 KB
Line 
1/******************************************************************************
2 * $Id: Torrent.m 10197 2010-02-14 04:48:12Z livings124 $
3 *
4 * Copyright (c) 2006-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 "Torrent.h"
26#import "GroupsController.h"
27#import "FileListNode.h"
28#import "NSStringAdditions.h"
29#import "TrackerNode.h"
30#import "utils.h" //tr_httpIsValidURL
31
32@interface Torrent (Private)
33
34- (id) initWithPath: (NSString *) path hash: (NSString *) hashString torrentStruct: (tr_torrent *) torrentStruct
35        magnetAddress: (NSString *) magnetAddress lib: (tr_session *) lib
36        waitToStart: (NSNumber *) waitToStart
37        groupValue: (NSNumber *) groupValue
38        downloadFolder: (NSString *) downloadFolder legacyIncompleteFolder: (NSString *) incompleteFolder;
39
40- (void) createFileList;
41- (void) insertPath: (NSMutableArray *) components forParent: (FileListNode *) parent fileSize: (uint64_t) size
42    index: (NSInteger) index flatList: (NSMutableArray *) flatFileList;
43
44- (void) completenessChange: (NSNumber *) status;
45- (void) ratioLimitHit;
46- (void) metadataRetrieved;
47
48- (NSString *) etaString;
49
50- (void) setTimeMachineExclude: (BOOL) exclude forPath: (NSString *) path;
51
52@end
53
54void completenessChangeCallback(tr_torrent * torrent, tr_completeness status, void * torrentData)
55{
56    [(Torrent *)torrentData performSelectorOnMainThread: @selector(completenessChange:)
57        withObject: [[NSNumber alloc] initWithInt: status] waitUntilDone: NO];
58}
59
60void ratioLimitHitCallback(tr_torrent * torrent, void * torrentData)
61{
62    [(Torrent *)torrentData performSelectorOnMainThread: @selector(ratioLimitHit) withObject: nil waitUntilDone: NO];
63}
64
65void metadataCallback(tr_torrent * torrent, void * torrentData)
66{
67    [(Torrent *)torrentData performSelectorOnMainThread: @selector(metadataRetrieved) withObject: nil waitUntilDone: NO];
68}
69
70int trashDataFile(const char * filename)
71{
72    if (filename != NULL)
73        [Torrent trashFile: [NSString stringWithUTF8String: filename]];
74    return 0;
75}
76
77@implementation Torrent
78
79- (id) initWithPath: (NSString *) path location: (NSString *) location deleteTorrentFile: (BOOL) torrentDelete
80        lib: (tr_session *) lib
81{
82    self = [self initWithPath: path hash: nil torrentStruct: NULL magnetAddress: nil lib: lib
83            waitToStart: nil groupValue: nil
84            downloadFolder: location legacyIncompleteFolder: nil];
85   
86    if (self)
87    {
88        if (torrentDelete && ![[self torrentLocation] isEqualToString: path])
89            [Torrent trashFile: path];
90    }
91    return self;
92}
93
94- (id) initWithTorrentStruct: (tr_torrent *) torrentStruct location: (NSString *) location lib: (tr_session *) lib
95{
96    self = [self initWithPath: nil hash: nil torrentStruct: torrentStruct magnetAddress: nil lib: lib
97            waitToStart: nil groupValue: nil
98            downloadFolder: location legacyIncompleteFolder: nil];
99   
100    return self;
101}
102
103#warning need location (and use it)?
104- (id) initWithMagnetAddress: (NSString *) address location: (NSString *) location lib: (tr_session *) lib
105{
106    self = [self initWithPath: nil hash: nil torrentStruct: nil magnetAddress: address
107            lib: lib waitToStart: nil groupValue: nil
108            downloadFolder: location legacyIncompleteFolder: nil];
109   
110    return self;
111}
112
113- (id) initWithHistory: (NSDictionary *) history lib: (tr_session *) lib forcePause: (BOOL) pause
114{
115    self = [self initWithPath: [history objectForKey: @"InternalTorrentPath"]
116                hash: [history objectForKey: @"TorrentHash"]
117                torrentStruct: NULL
118                magnetAddress: nil
119                lib: lib
120                waitToStart: [history objectForKey: @"WaitToStart"]
121                groupValue: [history objectForKey: @"GroupValue"]
122                downloadFolder: [history objectForKey: @"DownloadFolder"] //upgrading from versions < 1.80
123                legacyIncompleteFolder: [[history objectForKey: @"UseIncompleteFolder"] boolValue] //upgrading from versions < 1.80
124                                        ? [history objectForKey: @"IncompleteFolder"] : nil];
125   
126    if (self)
127    {
128        //start transfer
129        NSNumber * active;
130        if (!pause && (active = [history objectForKey: @"Active"]) && [active boolValue])
131        {
132            fStat = tr_torrentStat(fHandle);
133            [self startTransfer];
134        }
135       
136        //upgrading from versions < 1.30: get old added, activity, and done dates
137        NSDate * date;
138        if ((date = [history objectForKey: @"Date"]))
139            tr_torrentSetAddedDate(fHandle, [date timeIntervalSince1970]);
140        if ((date = [history objectForKey: @"DateActivity"]))
141            tr_torrentSetActivityDate(fHandle, [date timeIntervalSince1970]);
142        if ((date = [history objectForKey: @"DateCompleted"]))
143            tr_torrentSetDoneDate(fHandle, [date timeIntervalSince1970]);
144       
145        //upgrading from versions < 1.60: get old stop ratio settings
146        NSNumber * ratioSetting;
147        if ((ratioSetting = [history objectForKey: @"RatioSetting"]))
148        {
149            switch ([ratioSetting intValue])
150            {
151                case NSOnState: [self setRatioSetting: TR_RATIOLIMIT_SINGLE]; break;
152                case NSOffState: [self setRatioSetting: TR_RATIOLIMIT_UNLIMITED]; break;
153                case NSMixedState: [self setRatioSetting: TR_RATIOLIMIT_GLOBAL]; break;
154            }
155        }
156        NSNumber * ratioLimit;
157        if ((ratioLimit = [history objectForKey: @"RatioLimit"]))
158            [self setRatioLimit: [ratioLimit floatValue]];
159    }
160    return self;
161}
162
163- (NSDictionary *) history
164{
165    return [NSDictionary dictionaryWithObjectsAndKeys:
166                [self torrentLocation], @"InternalTorrentPath",
167                [self hashString], @"TorrentHash",
168                [NSNumber numberWithBool: [self isActive]], @"Active",
169                [NSNumber numberWithBool: fWaitToStart], @"WaitToStart",
170                [NSNumber numberWithInt: fGroupValue], @"GroupValue", nil];
171}
172
173- (void) dealloc
174{
175    [[NSNotificationCenter defaultCenter] removeObserver: self];
176   
177    if (fFileStat)
178        tr_torrentFilesFree(fFileStat, [self fileCount]);
179   
180    [fPreviousFinishedIndexes release];
181    [fPreviousFinishedIndexesDate release];
182   
183    [fHashString release];
184   
185    [fIcon release];
186   
187    [fFileList release];
188    [fFlatFileList release];
189   
190    [fTimeMachineExclude release];
191   
192    [super dealloc];
193}
194
195- (NSString *) description
196{
197    return [@"Torrent: " stringByAppendingString: [self name]];
198}
199
200- (id) copyWithZone: (NSZone *) zone
201{
202    return [self retain];
203}
204
205- (void) closeRemoveTorrent
206{
207    //allow the file to be index by Time Machine
208    if (fTimeMachineExclude)
209    {
210        [self setTimeMachineExclude: NO forPath: fTimeMachineExclude];
211        [fTimeMachineExclude release];
212        fTimeMachineExclude = nil;
213    }
214   
215    tr_torrentRemove(fHandle);
216}
217
218- (void) changeDownloadFolderBeforeUsing: (NSString *) folder
219{
220     tr_torrentSetDownloadDir(fHandle, [folder UTF8String]);
221     [self updateTimeMachineExclude];
222}
223
224- (NSString *) currentDirectory
225{
226    return [NSString stringWithUTF8String: tr_torrentGetCurrentDir(fHandle)];
227}
228
229- (void) getAvailability: (int8_t *) tab size: (NSInteger) size
230{
231    tr_torrentAvailability(fHandle, tab, size);
232}
233
234- (void) getAmountFinished: (float *) tab size: (NSInteger) size
235{
236    tr_torrentAmountFinished(fHandle, tab, size);
237}
238
239- (NSIndexSet *) previousFinishedPieces
240{
241    //if the torrent hasn't been seen in a bit, and therefore hasn't been refreshed, return nil
242    if (fPreviousFinishedIndexesDate && [fPreviousFinishedIndexesDate timeIntervalSinceNow] > -2.0)
243        return fPreviousFinishedIndexes;
244    else
245        return nil;
246}
247
248-(void) setPreviousFinishedPieces: (NSIndexSet *) indexes
249{
250    [fPreviousFinishedIndexes release];
251    fPreviousFinishedIndexes = [indexes retain];
252   
253    [fPreviousFinishedIndexesDate release];
254    fPreviousFinishedIndexesDate = indexes != nil ? [[NSDate alloc] init] : nil;
255}
256
257- (void) update
258{
259    //get previous status values before update
260    BOOL wasChecking = NO, wasError = NO, wasStalled = NO;
261    if (fStat != NULL)
262    {
263        wasChecking = [self isChecking];
264        wasError = [self isError];
265        wasStalled = fStalled;
266    }
267   
268    fStat = tr_torrentStat(fHandle);
269   
270    //check if stalled (stored because based on time and needs to check if it was previously stalled)
271    fStalled = [self isActive] && [fDefaults boolForKey: @"CheckStalled"]
272                && [self stalledMinutes] > [fDefaults integerForKey: @"StalledMinutes"];
273   
274    //update queue for checking (from downloading to seeding), stalled, or error
275    if ((wasChecking && ![self isChecking]) || (wasStalled != fStalled) || (!wasError && [self isError] && [self isActive]))
276        [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateQueue" object: self];
277   
278    //when the data first appears, update time machine exclusion
279    if (!fTimeMachineExclude)
280        [self updateTimeMachineExclude];
281}
282
283- (void) startTransfer
284{
285    fWaitToStart = NO;
286    fFinishedSeeding = NO;
287   
288    if (![self isActive] && [self alertForRemainingDiskSpace])
289    {
290        tr_torrentStart(fHandle);
291        [self update];
292    }
293}
294
295- (void) stopTransfer
296{
297    fWaitToStart = NO;
298   
299    if ([self isActive])
300    {
301        tr_torrentStop(fHandle);
302        [self update];
303       
304        [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateQueue" object: self];
305    }
306}
307
308- (void) sleep
309{
310    if ((fResumeOnWake = [self isActive]))
311        tr_torrentStop(fHandle);
312}
313
314- (void) wakeUp
315{
316    if (fResumeOnWake)
317        tr_torrentStart(fHandle);
318}
319
320- (void) manualAnnounce
321{
322    tr_torrentManualUpdate(fHandle);
323}
324
325- (BOOL) canManualAnnounce
326{
327    return tr_torrentCanManualUpdate(fHandle);
328}
329
330- (void) resetCache
331{
332    tr_torrentVerify(fHandle);
333    [self update];
334}
335
336- (BOOL) isMagnet
337{
338    return !tr_torrentHasMetadata(fHandle);
339}
340
341- (NSString *) magnetLink
342{
343    return [NSString stringWithUTF8String: tr_torrentGetMagnetLink(fHandle)];
344}
345
346- (CGFloat) ratio
347{
348    return fStat->ratio;
349}
350
351- (tr_ratiolimit) ratioSetting
352{
353    return tr_torrentGetRatioMode(fHandle);
354}
355
356- (void) setRatioSetting: (tr_ratiolimit) setting
357{
358    tr_torrentSetRatioMode(fHandle, setting);
359}
360
361- (CGFloat) ratioLimit
362{
363    return tr_torrentGetRatioLimit(fHandle);
364}
365
366- (void) setRatioLimit: (CGFloat) limit
367{
368    NSAssert(limit >= 0, @"Ratio cannot be negative");
369    tr_torrentSetRatioLimit(fHandle, limit);
370}
371
372- (BOOL) seedRatioSet
373{
374    return tr_torrentGetSeedRatio(fHandle, NULL);
375}
376
377- (CGFloat) progressStopRatio
378{
379    return fStat->percentRatio;
380}
381
382- (BOOL) usesSpeedLimit: (BOOL) upload
383{
384    return tr_torrentUsesSpeedLimit(fHandle, upload ? TR_UP : TR_DOWN);
385}
386
387- (void) setUseSpeedLimit: (BOOL) use upload: (BOOL) upload
388{
389    tr_torrentUseSpeedLimit(fHandle, upload ? TR_UP : TR_DOWN, use);
390}
391
392- (NSInteger) speedLimit: (BOOL) upload
393{
394    return tr_torrentGetSpeedLimit(fHandle, upload ? TR_UP : TR_DOWN);
395}
396
397- (void) setSpeedLimit: (NSInteger) limit upload: (BOOL) upload
398{
399    tr_torrentSetSpeedLimit(fHandle, upload ? TR_UP : TR_DOWN, limit);
400}
401
402- (BOOL) usesGlobalSpeedLimit
403{
404    return tr_torrentUsesSessionLimits(fHandle);
405}
406
407- (void) setUseGlobalSpeedLimit: (BOOL) use
408{
409    tr_torrentUseSessionLimits(fHandle, use);
410}
411
412- (void) setMaxPeerConnect: (uint16_t) count
413{
414    NSAssert(count > 0, @"max peer count must be greater than 0");
415   
416    tr_torrentSetPeerLimit(fHandle, count);
417}
418
419- (uint16_t) maxPeerConnect
420{
421    return tr_torrentGetPeerLimit(fHandle);
422}
423
424- (void) setWaitToStart: (BOOL) wait
425{
426    fWaitToStart = wait;
427}
428
429- (BOOL) waitingToStart
430{
431    return fWaitToStart;
432}
433
434- (tr_priority_t) priority
435{
436    return tr_torrentGetPriority(fHandle);
437}
438
439- (void) setPriority: (tr_priority_t) priority
440{
441    return tr_torrentSetPriority(fHandle, priority);
442}
443
444#warning when 10.6-only use recycleURLs:completionHandler:
445+ (void) trashFile: (NSString *) path
446{
447    //attempt to move to trash
448    if (![[NSWorkspace sharedWorkspace] performFileOperation: NSWorkspaceRecycleOperation
449        source: [path stringByDeletingLastPathComponent] destination: @""
450        files: [NSArray arrayWithObject: [path lastPathComponent]] tag: nil])
451    {
452        //if cannot trash, just delete it (will work if it's on a remote volume)
453        NSError * error;
454        if (![[NSFileManager defaultManager] removeItemAtPath: path error: &error])
455            NSLog(@"Could not trash %@: %@", path, [error localizedDescription]);
456    }
457}
458
459- (void) trashData
460{
461    tr_torrentDeleteLocalData(fHandle, trashDataFile);
462}
463
464- (void) moveTorrentDataFileTo: (NSString *) folder
465{
466    NSString * oldFolder = [self currentDirectory];
467    if ([oldFolder isEqualToString: folder])
468        return;
469   
470    //check if moving inside itself
471    NSArray * oldComponents = [oldFolder pathComponents],
472            * newComponents = [folder pathComponents];
473    const NSInteger oldCount = [oldComponents count];
474   
475    if (oldCount < [newComponents count] && [[newComponents objectAtIndex: oldCount] isEqualToString: [self name]]
476        && [folder hasPrefix: oldFolder])
477    {
478        NSAlert * alert = [[NSAlert alloc] init];
479        [alert setMessageText: NSLocalizedString(@"A folder cannot be moved to inside itself.",
480                                                    "Move inside itself alert -> title")];
481        [alert setInformativeText: [NSString stringWithFormat:
482                        NSLocalizedString(@"The move operation of \"%@\" cannot be done.",
483                                            "Move inside itself alert -> message"), [self name]]];
484        [alert addButtonWithTitle: NSLocalizedString(@"OK", "Move inside itself alert -> button")];
485       
486        [alert runModal];
487        [alert release];
488       
489        return;
490    }
491   
492    volatile int status;
493    tr_torrentSetLocation(fHandle, [folder UTF8String], YES, NULL, &status);
494   
495    while (status == TR_LOC_MOVING) //block while moving (for now)
496        [NSThread sleepForTimeInterval: 0.05];
497   
498    if (status == TR_LOC_DONE)
499        [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateStats" object: nil];
500    else
501    {
502        NSAlert * alert = [[NSAlert alloc] init];
503        [alert setMessageText: NSLocalizedString(@"There was an error moving the data file.", "Move error alert -> title")];
504        [alert setInformativeText: [NSString stringWithFormat:
505                NSLocalizedString(@"The move operation of \"%@\" cannot be done.", "Move error alert -> message"), [self name]]];
506        [alert addButtonWithTitle: NSLocalizedString(@"OK", "Move error alert -> button")];
507       
508        [alert runModal];
509        [alert release];
510    }
511   
512    [self updateTimeMachineExclude];
513}
514
515- (void) copyTorrentFileTo: (NSString *) path
516{
517    [[NSFileManager defaultManager] copyItemAtPath: [self torrentLocation] toPath: path error: NULL];
518}
519
520- (BOOL) alertForRemainingDiskSpace
521{
522    if ([self allDownloaded] || ![fDefaults boolForKey: @"WarningRemainingSpace"])
523        return YES;
524   
525    NSString * downloadFolder = [self currentDirectory], * volumeName;
526    if ((volumeName = [[[NSFileManager defaultManager] componentsToDisplayForPath: downloadFolder] objectAtIndex: 0]))
527    {
528        NSDictionary * systemAttributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath: downloadFolder error: NULL];
529        uint64_t remainingSpace = [[systemAttributes objectForKey: NSFileSystemFreeSize] unsignedLongLongValue];
530       
531        //if the remaining space is greater than the size left, then there is enough space regardless of preallocation
532        if (remainingSpace < [self sizeLeft] && remainingSpace < tr_torrentGetBytesLeftToAllocate(fHandle))
533        {
534            NSAlert * alert = [[NSAlert alloc] init];
535            [alert setMessageText: [NSString stringWithFormat:
536                                    NSLocalizedString(@"Not enough remaining disk space to download \"%@\" completely.",
537                                        "Torrent disk space alert -> title"), [self name]]];
538            [alert setInformativeText: [NSString stringWithFormat: NSLocalizedString(@"The transfer will be paused."
539                                        " Clear up space on %@ or deselect files in the torrent inspector to continue.",
540                                        "Torrent disk space alert -> message"), volumeName]];
541            [alert addButtonWithTitle: NSLocalizedString(@"OK", "Torrent disk space alert -> button")];
542            [alert addButtonWithTitle: NSLocalizedString(@"Download Anyway", "Torrent disk space alert -> button")];
543           
544            [alert setShowsSuppressionButton: YES];
545            [[alert suppressionButton] setTitle: NSLocalizedString(@"Do not check disk space again",
546                                                    "Torrent disk space alert -> button")];
547
548            const NSInteger result = [alert runModal];
549            if ([[alert suppressionButton] state] == NSOnState)
550                [fDefaults setBool: NO forKey: @"WarningRemainingSpace"];
551            [alert release];
552           
553            return result != NSAlertFirstButtonReturn;
554        }
555    }
556    return YES;
557}
558
559- (NSImage *) icon
560{
561    if ([self isMagnet])
562        return [NSImage imageNamed: @"Magnet.png"];
563   
564    #warning replace 'fldr' stuff with NSImageNameFolder on 10.6
565    if (!fIcon)
566        fIcon = [[[NSWorkspace sharedWorkspace] iconForFileType: [self isFolder] ? NSFileTypeForHFSTypeCode('fldr')
567                                                                                : [[self name] pathExtension]] retain];
568    return fIcon;
569}
570
571- (NSString *) name
572{
573    return fInfo->name != NULL ? [NSString stringWithUTF8String: fInfo->name] : fHashString;
574}
575
576- (BOOL) isFolder
577{
578    return fInfo->isMultifile;
579}
580
581- (uint64_t) size
582{
583    return fInfo->totalSize;
584}
585
586- (uint64_t) sizeLeft
587{
588    return fStat->leftUntilDone;
589}
590
591- (NSMutableArray *) allTrackerStats
592{
593    int count;
594    tr_tracker_stat * stats = tr_torrentTrackers(fHandle, &count);
595   
596    NSMutableArray * trackers = [NSMutableArray arrayWithCapacity: (count > 0 ? count + (stats[count-1].tier + 1) : 0)];
597   
598    int prevTier = -1;
599    for (int i=0; i < count; ++i)
600    {
601        if (stats[i].tier != prevTier)
602        {
603            [trackers addObject: [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInteger: stats[i].tier + 1], @"Tier",
604                                    [self name], @"Name", nil]];
605            prevTier = stats[i].tier;
606        }
607       
608        TrackerNode * tracker = [[TrackerNode alloc] initWithTrackerStat: &stats[i] torrent: self];
609        [trackers addObject: tracker];
610        [tracker release];
611    }
612   
613    tr_torrentTrackersFree(stats, count);
614    return trackers;
615}
616
617- (NSArray *) allTrackersFlat
618{
619    NSMutableArray * allTrackers = [NSMutableArray arrayWithCapacity: fInfo->trackerCount];
620   
621    for (NSInteger i=0; i < fInfo->trackerCount; i++)
622        [allTrackers addObject: [NSString stringWithUTF8String: fInfo->trackers[i].announce]];
623   
624    return allTrackers;
625}
626
627- (NSUInteger) numberOfTrackerTiers
628{
629    if (fInfo->trackerCount == 0)
630        return 0;
631   
632    return fInfo->trackers[fInfo->trackerCount-1].tier + 1;
633}
634
635- (BOOL) addTrackerToNewTier: (NSString *) tracker
636{
637    tracker = [tracker stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
638   
639    if ([tracker rangeOfString: @"://"].location == NSNotFound)
640        tracker = [@"http://" stringByAppendingString: tracker];
641   
642    //recreate the tracker structure
643    const int oldTrackerCount = fInfo->trackerCount;
644    tr_tracker_info * trackerStructs = tr_new(tr_tracker_info, oldTrackerCount+1);
645    for (NSUInteger i=0; i < oldTrackerCount; ++i)
646    {
647        trackerStructs[i] = fInfo->trackers[i];
648        trackerStructs[i].id = i;
649    }
650   
651    trackerStructs[oldTrackerCount].announce = (char *)[tracker UTF8String];
652    trackerStructs[oldTrackerCount].tier = trackerStructs[oldTrackerCount-1].tier + 1;
653    trackerStructs[oldTrackerCount].id = oldTrackerCount;
654   
655    const BOOL success = tr_torrentSetAnnounceList(fHandle, trackerStructs, oldTrackerCount+1);
656    tr_free(trackerStructs);
657   
658    return success;
659}
660
661- (void) removeTrackersWithIdentifiers: (NSIndexSet *) removeIdentifiers
662{
663    //recreate the tracker structure
664    tr_tracker_info * trackerStructs = tr_new(tr_tracker_info, fInfo->trackerCount);
665   
666    NSUInteger newCount = 0;
667    for (NSUInteger i = 0; i < fInfo->trackerCount; i++)
668    {
669        if (![removeIdentifiers containsIndex: fInfo->trackers[i].id])
670        {
671            trackerStructs[newCount] = fInfo->trackers[i];
672            trackerStructs[newCount].id = newCount++;
673        }
674    }
675   
676    const BOOL success = tr_torrentSetAnnounceList(fHandle, trackerStructs, newCount);
677    NSAssert(success, @"Removing tracker addresses failed");
678   
679    tr_free(trackerStructs);
680}
681
682- (NSString *) comment
683{
684    return fInfo->comment ? [NSString stringWithUTF8String: fInfo->comment] : @"";
685}
686
687- (NSString *) creator
688{
689    return fInfo->creator ? [NSString stringWithUTF8String: fInfo->creator] : @"";
690}
691
692- (NSDate *) dateCreated
693{
694    NSInteger date = fInfo->dateCreated;
695    return date > 0 ? [NSDate dateWithTimeIntervalSince1970: date] : nil;
696}
697
698- (NSInteger) pieceSize
699{
700    return fInfo->pieceSize;
701}
702
703- (NSInteger) pieceCount
704{
705    return fInfo->pieceCount;
706}
707
708- (NSString *) hashString
709{
710    return fHashString;
711}
712
713- (BOOL) privateTorrent
714{
715    return fInfo->isPrivate;
716}
717
718- (NSString *) torrentLocation
719{
720    return fInfo->torrent ? [NSString stringWithUTF8String: fInfo->torrent] : @"";
721}
722
723- (NSString *) dataLocation
724{
725    if ([self isMagnet])
726        return nil;
727   
728    if ([self isFolder])
729    {
730        NSString * dataLocation = [[self currentDirectory] stringByAppendingPathComponent: [self name]];
731       
732        if (![[NSFileManager defaultManager] fileExistsAtPath: dataLocation])
733            return nil;
734       
735        return dataLocation;
736    }
737    else
738    {
739        char * location = tr_torrentFindFile(fHandle, 0);
740        if (location == NULL)
741            return nil;
742       
743        NSString * dataLocation = [NSString stringWithUTF8String: location];
744        free(location);
745       
746        return dataLocation;
747    }
748}
749
750- (NSString *) fileLocation: (FileListNode *) node
751{
752    if ([node isFolder])
753    {
754        NSString * basePath = [[node path] stringByAppendingPathComponent: [node name]];
755        NSString * dataLocation = [[self currentDirectory] stringByAppendingPathComponent: basePath];
756       
757        if (![[NSFileManager defaultManager] fileExistsAtPath: dataLocation])
758            return nil;
759       
760        return dataLocation;
761    }
762    else
763    {
764        char * location = tr_torrentFindFile(fHandle, [[node indexes] firstIndex]);
765        if (location == NULL)
766            return nil;
767       
768        NSString * dataLocation = [NSString stringWithUTF8String: location];
769        free(location);
770       
771        return dataLocation;
772    }
773}
774
775- (CGFloat) progress
776{
777    return fStat->percentComplete;
778}
779
780- (CGFloat) progressDone
781{
782    return fStat->percentDone;
783}
784
785- (CGFloat) progressLeft
786{
787    if ([self size] == 0) //magnet links
788        return 0.0;
789   
790    return (CGFloat)[self sizeLeft] / [self size];
791}
792
793- (CGFloat) checkingProgress
794{
795    return fStat->recheckProgress;
796}
797
798- (NSInteger) eta
799{
800    return fStat->eta;
801}
802
803- (CGFloat) availableDesired
804{
805    return (CGFloat)fStat->desiredAvailable / [self sizeLeft];
806}
807
808- (BOOL) isActive
809{
810    return fStat->activity != TR_STATUS_STOPPED;
811}
812
813- (BOOL) isSeeding
814{
815    return fStat->activity == TR_STATUS_SEED;
816}
817
818- (BOOL) isChecking
819{
820    return fStat->activity == TR_STATUS_CHECK || fStat->activity == TR_STATUS_CHECK_WAIT;
821}
822
823- (BOOL) isCheckingWaiting
824{
825    return fStat->activity == TR_STATUS_CHECK_WAIT;
826}
827
828- (BOOL) allDownloaded
829{
830    return [self sizeLeft] == 0 && ![self isMagnet];
831}
832
833- (BOOL) isComplete
834{
835    return [self progress] >= 1.0;
836}
837
838- (BOOL) isError
839{
840    return fStat->error == TR_STAT_LOCAL_ERROR;
841}
842
843- (BOOL) isAnyErrorOrWarning
844{
845    return fStat->error != TR_STAT_OK;
846}
847
848- (NSString *) errorMessage
849{
850    if (![self isAnyErrorOrWarning])
851        return @"";
852   
853    NSString * error;
854    if (!(error = [NSString stringWithUTF8String: fStat->errorString])
855        && !(error = [NSString stringWithCString: fStat->errorString encoding: NSISOLatin1StringEncoding]))
856        error = [NSString stringWithFormat: @"(%@)", NSLocalizedString(@"unreadable error", "Torrent -> error string unreadable")];
857   
858    //libtransmission uses "Set Location", Mac client uses "Move data file to..." - very hacky!
859    error = [error stringByReplacingOccurrencesOfString: @"Set Location" withString: [@"Move Data File To" stringByAppendingEllipsis]];
860   
861    return error;
862}
863
864- (NSArray *) peers
865{
866    int totalPeers;
867    tr_peer_stat * peers = tr_torrentPeers(fHandle, &totalPeers);
868   
869    NSMutableArray * peerDicts = [NSMutableArray arrayWithCapacity: totalPeers];
870   
871    for (int i = 0; i < totalPeers; i++)
872    {
873        tr_peer_stat * peer = &peers[i];
874        NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithCapacity: 11];
875       
876        [dict setObject: [self name] forKey: @"Name"];
877        [dict setObject: [NSNumber numberWithInt: peer->from] forKey: @"From"];
878        [dict setObject: [NSString stringWithUTF8String: peer->addr] forKey: @"IP"];
879        [dict setObject: [NSNumber numberWithInt: peer->port] forKey: @"Port"];
880        [dict setObject: [NSNumber numberWithFloat: peer->progress] forKey: @"Progress"];
881        [dict setObject: [NSNumber numberWithBool: peer->isSeed] forKey: @"Seed"];
882        [dict setObject: [NSNumber numberWithBool: peer->isEncrypted] forKey: @"Encryption"];
883        [dict setObject: [NSString stringWithUTF8String: peer->client] forKey: @"Client"];
884        [dict setObject: [NSString stringWithUTF8String: peer->flagStr] forKey: @"Flags"];
885       
886        if (peer->isUploadingTo)
887            [dict setObject: [NSNumber numberWithFloat: peer->rateToPeer] forKey: @"UL To Rate"];
888        if (peer->isDownloadingFrom)
889            [dict setObject: [NSNumber numberWithFloat: peer->rateToClient] forKey: @"DL From Rate"];
890       
891        [peerDicts addObject: dict];
892    }
893   
894    tr_torrentPeersFree(peers, totalPeers);
895   
896    return peerDicts;
897}
898
899- (NSUInteger) webSeedCount
900{
901    return fInfo->webseedCount;
902}
903
904- (NSArray *) webSeeds
905{
906    NSMutableArray * webSeeds = [NSMutableArray arrayWithCapacity: fInfo->webseedCount];
907   
908    float * dlSpeeds = tr_torrentWebSpeeds(fHandle);
909   
910    for (NSInteger i = 0; i < fInfo->webseedCount; i++)
911    {
912        NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithCapacity: 3];
913       
914        [dict setObject: [self name] forKey: @"Name"];
915        [dict setObject: [NSString stringWithUTF8String: fInfo->webseeds[i]] forKey: @"Address"];
916       
917        if (dlSpeeds[i] != -1.0)
918            [dict setObject: [NSNumber numberWithFloat: dlSpeeds[i]] forKey: @"DL From Rate"];
919       
920        [webSeeds addObject: dict];
921    }
922   
923    tr_free(dlSpeeds);
924   
925    return webSeeds;
926}
927
928- (NSString *) progressString
929{
930    if ([self isMagnet])
931    {
932        NSString * progressString = fStat->metadataPercentComplete > 0.0
933                    ? [NSString localizedStringWithFormat: NSLocalizedString(@"%.2f%% of torrent metadata retrieved",
934                        "Torrent -> progress string"), 100.0 * fStat->metadataPercentComplete]
935                    : NSLocalizedString(@"torrent metadata needed", "Torrent -> progress string");
936       
937        return [NSString stringWithFormat: @"%@ - %@", NSLocalizedString(@"Magnetized transfer", "Torrent -> progress string"),
938                                            progressString];
939    }
940   
941    NSString * string;
942   
943    if (![self allDownloaded])
944    {
945        CGFloat progress;
946        if ([self isFolder] && [fDefaults boolForKey: @"DisplayStatusProgressSelected"])
947        {
948            string = [NSString stringWithFormat: NSLocalizedString(@"%@ of %@ selected", "Torrent -> progress string"),
949                        [NSString stringForFileSize: [self haveTotal]], [NSString stringForFileSize: [self totalSizeSelected]]];
950            progress = 100.0 * [self progressDone];
951        }
952        else
953        {
954            string = [NSString stringWithFormat: NSLocalizedString(@"%@ of %@", "Torrent -> progress string"),
955                        [NSString stringForFileSize: [self haveTotal]], [NSString stringForFileSize: [self size]]];
956            progress = 100.0 * [self progress];
957        }
958       
959        string = [NSString localizedStringWithFormat: @"%@ (%.2f%%)", string, progress];
960    }
961    else
962    {
963        NSString * downloadString;
964        if (![self isComplete]) //only multifile possible
965        {
966            if ([fDefaults boolForKey: @"DisplayStatusProgressSelected"])
967                downloadString = [NSString stringWithFormat: NSLocalizedString(@"%@ selected", "Torrent -> progress string"),
968                                    [NSString stringForFileSize: [self haveTotal]]];
969            else
970            {
971                downloadString = [NSString stringWithFormat: NSLocalizedString(@"%@ of %@", "Torrent -> progress string"),
972                                    [NSString stringForFileSize: [self haveTotal]], [NSString stringForFileSize: [self size]]];
973               
974                downloadString = [NSString localizedStringWithFormat: @"%@ (%.2f%%)", downloadString, 100.0 * [self progress]];
975            }
976        }
977        else
978            downloadString = [NSString stringForFileSize: [self size]];
979       
980        NSString * uploadString = [NSString stringWithFormat: NSLocalizedString(@"uploaded %@ (Ratio: %@)",
981                                    "Torrent -> progress string"), [NSString stringForFileSize: [self uploadedTotal]],
982                                    [NSString stringForRatio: [self ratio]]];
983       
984        string = [downloadString stringByAppendingFormat: @", %@", uploadString];
985    }
986   
987    //add time when downloading
988    if (fStat->activity == TR_STATUS_DOWNLOAD || ([self isSeeding] && [self seedRatioSet]))
989        string = [string stringByAppendingFormat: @" - %@", [self etaString]];
990   
991    return string;
992}
993
994- (NSString *) statusString
995{
996    NSString * string;
997   
998    if ([self isAnyErrorOrWarning])
999    {
1000        switch (fStat->error)
1001        {
1002            case TR_STAT_LOCAL_ERROR: string = NSLocalizedString(@"Error", "Torrent -> status string"); break;
1003            case TR_STAT_TRACKER_ERROR: string = NSLocalizedString(@"Tracker returned an error", "Torrent -> status string"); break;
1004            case TR_STAT_TRACKER_WARNING: string = NSLocalizedString(@"Tracker returned a warning", "Torrent -> status string"); break;
1005            default: NSAssert(NO, @"unknown error state");
1006        }
1007       
1008        NSString * errorString = [self errorMessage];
1009        if (errorString && ![errorString isEqualToString: @""])
1010            string = [string stringByAppendingFormat: @": %@", errorString];
1011    }
1012    else
1013    {
1014        switch (fStat->activity)
1015        {
1016            case TR_STATUS_STOPPED:
1017                if (fWaitToStart)
1018                {
1019                    string = ![self allDownloaded]
1020                            ? [NSLocalizedString(@"Waiting to download", "Torrent -> status string") stringByAppendingEllipsis]
1021                            : [NSLocalizedString(@"Waiting to seed", "Torrent -> status string") stringByAppendingEllipsis];
1022                }
1023                else if (fFinishedSeeding)
1024                    string = NSLocalizedString(@"Seeding complete", "Torrent -> status string");
1025                else
1026                    string = NSLocalizedString(@"Paused", "Torrent -> status string");
1027                break;
1028
1029            case TR_STATUS_CHECK_WAIT:
1030                string = [NSLocalizedString(@"Waiting to check existing data", "Torrent -> status string") stringByAppendingEllipsis];
1031                break;
1032
1033            case TR_STATUS_CHECK:
1034                string = [NSString localizedStringWithFormat: @"%@ (%.2f%%)",
1035                            NSLocalizedString(@"Checking existing data", "Torrent -> status string"), 100.0 * [self checkingProgress]];
1036                break;
1037
1038            case TR_STATUS_DOWNLOAD:
1039                if ([self totalPeersConnected] != 1)
1040                    string = [NSString stringWithFormat: NSLocalizedString(@"Downloading from %d of %d peers",
1041                                                    "Torrent -> status string"), [self peersSendingToUs], [self totalPeersConnected]];
1042                else
1043                    string = [NSString stringWithFormat: NSLocalizedString(@"Downloading from %d of 1 peer",
1044                                                    "Torrent -> status string"), [self peersSendingToUs]];
1045               
1046                const NSInteger webSeedCount = fStat->webseedsSendingToUs;
1047                if (webSeedCount > 0)
1048                {
1049                    NSString * webSeedString;
1050                    if (webSeedCount == 1)
1051                        webSeedString = NSLocalizedString(@"web seed", "Torrent -> status string");
1052                    else
1053                        webSeedString = [NSString stringWithFormat: NSLocalizedString(@"%d web seeds", "Torrent -> status string"),
1054                                                                    webSeedCount];
1055                   
1056                    string = [string stringByAppendingFormat: @" + %@", webSeedString];
1057                }
1058               
1059                break;
1060
1061            case TR_STATUS_SEED:
1062                if ([self totalPeersConnected] != 1)
1063                    string = [NSString stringWithFormat: NSLocalizedString(@"Seeding to %d of %d peers", "Torrent -> status string"),
1064                                                    [self peersGettingFromUs], [self totalPeersConnected]];
1065                else
1066                    string = [NSString stringWithFormat: NSLocalizedString(@"Seeding to %d of 1 peer", "Torrent -> status string"),
1067                                                    [self peersGettingFromUs]];
1068        }
1069       
1070        if (fStalled)
1071            string = [NSLocalizedString(@"Stalled", "Torrent -> status string") stringByAppendingFormat: @", %@", string];
1072    }
1073   
1074    //append even if error
1075    if ([self isActive] && ![self isChecking])
1076    {
1077        if (fStat->activity == TR_STATUS_DOWNLOAD)
1078            string = [string stringByAppendingFormat: @" - %@: %@, %@: %@",
1079                        NSLocalizedString(@"DL", "Torrent -> status string"), [NSString stringForSpeed: [self downloadRate]],
1080                        NSLocalizedString(@"UL", "Torrent -> status string"), [NSString stringForSpeed: [self uploadRate]]];
1081        else
1082            string = [string stringByAppendingFormat: @" - %@: %@",
1083                        NSLocalizedString(@"UL", "Torrent -> status string"), [NSString stringForSpeed: [self uploadRate]]];
1084    }
1085   
1086    return string;
1087}
1088
1089- (NSString *) shortStatusString
1090{
1091    NSString * string;
1092   
1093    switch (fStat->activity)
1094    {
1095        case TR_STATUS_STOPPED:
1096            if (fWaitToStart)
1097            {
1098                string = ![self allDownloaded]
1099                        ? [NSLocalizedString(@"Waiting to download", "Torrent -> status string") stringByAppendingEllipsis]
1100                        : [NSLocalizedString(@"Waiting to seed", "Torrent -> status string") stringByAppendingEllipsis];
1101            }
1102            else if (fFinishedSeeding)
1103                string = NSLocalizedString(@"Seeding complete", "Torrent -> status string");
1104            else
1105                string = NSLocalizedString(@"Paused", "Torrent -> status string");
1106            break;
1107
1108        case TR_STATUS_CHECK_WAIT:
1109            string = [NSLocalizedString(@"Waiting to check existing data", "Torrent -> status string") stringByAppendingEllipsis];
1110            break;
1111
1112        case TR_STATUS_CHECK:
1113            string = [NSString localizedStringWithFormat: @"%@ (%.2f%%)",
1114                        NSLocalizedString(@"Checking existing data", "Torrent -> status string"), 100.0 * [self checkingProgress]];
1115            break;
1116       
1117        case TR_STATUS_DOWNLOAD:
1118            string = [NSString stringWithFormat: @"%@: %@, %@: %@",
1119                            NSLocalizedString(@"DL", "Torrent -> status string"), [NSString stringForSpeed: [self downloadRate]],
1120                            NSLocalizedString(@"UL", "Torrent -> status string"), [NSString stringForSpeed: [self uploadRate]]];
1121            break;
1122       
1123        case TR_STATUS_SEED:
1124            string = [NSString stringWithFormat: @"%@: %@, %@: %@",
1125                            NSLocalizedString(@"Ratio", "Torrent -> status string"), [NSString stringForRatio: [self ratio]],
1126                            NSLocalizedString(@"UL", "Torrent -> status string"), [NSString stringForSpeed: [self uploadRate]]];
1127    }
1128   
1129    return string;
1130}
1131
1132- (NSString *) remainingTimeString
1133{
1134    if (fStat->activity == TR_STATUS_DOWNLOAD || ([self isSeeding] && [self seedRatioSet]))
1135        return [self etaString];
1136    else
1137        return [self shortStatusString];
1138}
1139
1140- (NSString *) stateString
1141{
1142    switch (fStat->activity)
1143    {
1144        case TR_STATUS_STOPPED:
1145            return NSLocalizedString(@"Paused", "Torrent -> status string");
1146
1147        case TR_STATUS_CHECK:
1148            return [NSString localizedStringWithFormat: @"%@ (%.2f%%)",
1149                    NSLocalizedString(@"Checking existing data", "Torrent -> status string"), 100.0 * [self checkingProgress]];
1150       
1151        case TR_STATUS_CHECK_WAIT:
1152            return [NSLocalizedString(@"Waiting to check existing data", "Torrent -> status string") stringByAppendingEllipsis];
1153
1154        case TR_STATUS_DOWNLOAD:
1155            return NSLocalizedString(@"Downloading", "Torrent -> status string");
1156
1157        case TR_STATUS_SEED:
1158            return NSLocalizedString(@"Seeding", "Torrent -> status string");
1159    }
1160}
1161
1162- (NSInteger) totalPeersConnected
1163{
1164    return fStat->peersConnected;
1165}
1166
1167- (NSInteger) totalPeersTracker
1168{
1169    return fStat->peersFrom[TR_PEER_FROM_TRACKER];
1170}
1171
1172- (NSInteger) totalPeersIncoming
1173{
1174    return fStat->peersFrom[TR_PEER_FROM_INCOMING];
1175}
1176
1177- (NSInteger) totalPeersCache
1178{
1179    return fStat->peersFrom[TR_PEER_FROM_RESUME];
1180}
1181
1182- (NSInteger) totalPeersPex
1183{
1184    return fStat->peersFrom[TR_PEER_FROM_PEX];
1185}
1186
1187- (NSInteger) totalPeersDHT
1188{
1189    return fStat->peersFrom[TR_PEER_FROM_DHT];
1190}
1191
1192- (NSInteger) totalPeersLTEP
1193{
1194    return fStat->peersFrom[TR_PEER_FROM_LTEP];
1195}
1196
1197- (NSInteger) totalPeersKnown
1198{
1199    return fStat->peersKnown;
1200}
1201
1202- (NSInteger) peersSendingToUs
1203{
1204    return fStat->peersSendingToUs;
1205}
1206
1207- (NSInteger) peersGettingFromUs
1208{
1209    return fStat->peersGettingFromUs;
1210}
1211
1212- (CGFloat) downloadRate
1213{
1214    return fStat->pieceDownloadSpeed;
1215}
1216
1217- (CGFloat) uploadRate
1218{
1219    return fStat->pieceUploadSpeed;
1220}
1221
1222- (CGFloat) totalRate
1223{
1224    return [self downloadRate] + [self uploadRate];
1225}
1226
1227- (uint64_t) haveVerified
1228{
1229    return fStat->haveValid;
1230}
1231
1232- (uint64_t) haveTotal
1233{
1234    return [self haveVerified] + fStat->haveUnchecked;
1235}
1236
1237- (uint64_t) totalSizeSelected
1238{
1239    return fStat->sizeWhenDone;
1240}
1241
1242- (uint64_t) downloadedTotal
1243{
1244    return fStat->downloadedEver;
1245}
1246
1247- (uint64_t) uploadedTotal
1248{
1249    return fStat->uploadedEver;
1250}
1251
1252- (uint64_t) failedHash
1253{
1254    return fStat->corruptEver;
1255}
1256
1257- (NSInteger) groupValue
1258{
1259    return fGroupValue;
1260}
1261
1262- (void) setGroupValue: (NSInteger) goupValue
1263{
1264    fGroupValue = goupValue;
1265}
1266
1267- (NSInteger) groupOrderValue
1268{
1269    return [[GroupsController groups] rowValueForIndex: fGroupValue];
1270}
1271
1272- (void) checkGroupValueForRemoval: (NSNotification *) notification
1273{
1274    if (fGroupValue != -1 && [[[notification userInfo] objectForKey: @"Index"] integerValue] == fGroupValue)
1275        fGroupValue = -1;
1276}
1277
1278- (NSArray *) fileList
1279{
1280    return fFileList;
1281}
1282
1283- (NSArray *) flatFileList
1284{
1285    return fFlatFileList;
1286}
1287
1288- (NSInteger) fileCount
1289{
1290    return fInfo->fileCount;
1291}
1292
1293- (void) updateFileStat
1294{
1295    if (fFileStat)
1296        tr_torrentFilesFree(fFileStat, [self fileCount]);
1297   
1298    fFileStat = tr_torrentFiles(fHandle, NULL);
1299}
1300
1301- (CGFloat) fileProgress: (FileListNode *) node
1302{
1303    if ([self fileCount] == 1 || [self isComplete])
1304        return [self progress];
1305   
1306    if (!fFileStat)
1307        [self updateFileStat];
1308   
1309    NSIndexSet * indexSet = [node indexes];
1310   
1311    if ([indexSet count] == 1)
1312        return fFileStat[[indexSet firstIndex]].progress;
1313   
1314    uint64_t have = 0;
1315    for (NSInteger index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index])
1316        have += fFileStat[index].bytesCompleted;
1317   
1318    NSAssert([node size], @"directory in torrent file has size 0");
1319    return (CGFloat)have / [node size];
1320}
1321
1322- (BOOL) canChangeDownloadCheckForFile: (NSUInteger) index
1323{
1324    NSAssert2(index < [self fileCount], @"Index %d is greater than file count %d", index, [self fileCount]);
1325   
1326    if ([self fileCount] == 1 || [self isComplete])
1327        return NO;
1328   
1329    if (!fFileStat)
1330        [self updateFileStat];
1331   
1332    return fFileStat[index].progress < 1.0;
1333}
1334
1335- (BOOL) canChangeDownloadCheckForFiles: (NSIndexSet *) indexSet
1336{
1337    if ([self fileCount] == 1 || [self isComplete])
1338        return NO;
1339   
1340    if (!fFileStat)
1341        [self updateFileStat];
1342   
1343    for (NSUInteger index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index])
1344        if (fFileStat[index].progress < 1.0)
1345            return YES;
1346    return NO;
1347}
1348
1349- (NSInteger) checkForFiles: (NSIndexSet *) indexSet
1350{
1351    NSAssert(indexSet != nil, @"indexSet must not be nil");
1352   
1353    BOOL onState = NO, offState = NO;
1354    for (NSUInteger index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index])
1355    {
1356        if (!fInfo->files[index].dnd || ![self canChangeDownloadCheckForFile: index])
1357            onState = YES;
1358        else
1359            offState = YES;
1360       
1361        if (onState && offState)
1362            return NSMixedState;
1363    }
1364    return onState ? NSOnState : NSOffState;
1365}
1366
1367- (void) setFileCheckState: (NSInteger) state forIndexes: (NSIndexSet *) indexSet
1368{
1369    NSUInteger count = [indexSet count];
1370    tr_file_index_t * files = malloc(count * sizeof(tr_file_index_t));
1371    for (NSUInteger index = [indexSet firstIndex], i = 0; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index], i++)
1372        files[i] = index;
1373   
1374    tr_torrentSetFileDLs(fHandle, files, count, state != NSOffState);
1375    free(files);
1376   
1377    [self update];
1378    [[NSNotificationCenter defaultCenter] postNotificationName: @"TorrentFileCheckChange" object: self];
1379}
1380
1381- (void) setFilePriority: (tr_priority_t) priority forIndexes: (NSIndexSet *) indexSet
1382{
1383    const NSUInteger count = [indexSet count];
1384    tr_file_index_t * files = tr_malloc(count * sizeof(tr_file_index_t));
1385    for (NSUInteger index = [indexSet firstIndex], i = 0; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index], i++)
1386        files[i] = index;
1387   
1388    tr_torrentSetFilePriorities(fHandle, files, count, priority);
1389    tr_free(files);
1390}
1391
1392- (BOOL) hasFilePriority: (tr_priority_t) priority forIndexes: (NSIndexSet *) indexSet
1393{
1394    for (NSUInteger index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index])
1395        if (priority == fInfo->files[index].priority && [self canChangeDownloadCheckForFile: index])
1396            return YES;
1397    return NO;
1398}
1399
1400- (NSSet *) filePrioritiesForIndexes: (NSIndexSet *) indexSet
1401{
1402    BOOL low = NO, normal = NO, high = NO;
1403    NSMutableSet * priorities = [NSMutableSet setWithCapacity: MIN([indexSet count], 3)];
1404   
1405    for (NSUInteger index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index])
1406    {
1407        if (![self canChangeDownloadCheckForFile: index])
1408            continue;
1409       
1410        const tr_priority_t priority = fInfo->files[index].priority;
1411        switch (priority)
1412        {
1413            case TR_PRI_LOW:
1414                if (low)
1415                    continue;
1416                low = YES;
1417                break;
1418            case TR_PRI_NORMAL:
1419                if (normal)
1420                    continue;
1421                normal = YES;
1422                break;
1423            case TR_PRI_HIGH:
1424                if (high)
1425                    continue;
1426                high = YES;
1427                break;
1428            default:
1429                NSAssert2(NO, @"Unknown priority %d for file index %d", priority, index);
1430        }
1431       
1432        [priorities addObject: [NSNumber numberWithInteger: priority]];
1433        if (low && normal && high)
1434            break;
1435    }
1436    return priorities;
1437}
1438
1439- (NSDate *) dateAdded
1440{
1441    const time_t date = fStat->addedDate;
1442    return [NSDate dateWithTimeIntervalSince1970: date];
1443}
1444
1445- (NSDate *) dateCompleted
1446{
1447    const time_t date = fStat->doneDate;
1448    return date != 0 ? [NSDate dateWithTimeIntervalSince1970: date] : nil;
1449}
1450
1451- (NSDate *) dateActivity
1452{
1453    const time_t date = fStat->activityDate;
1454    return date != 0 ? [NSDate dateWithTimeIntervalSince1970: date] : nil;
1455}
1456
1457- (NSDate *) dateActivityOrAdd
1458{
1459    NSDate * date = [self dateActivity];
1460    return date ? date : [self dateAdded];
1461}
1462
1463- (NSInteger) stalledMinutes
1464{
1465    const time_t start = fStat->startDate;
1466    if (start == 0)
1467        return -1;
1468   
1469    NSDate * started = [NSDate dateWithTimeIntervalSince1970: start],
1470            * activity = [self dateActivity];
1471   
1472    NSDate * laterDate = activity ? [started laterDate: activity] : started;
1473   
1474    return ABS([laterDate timeIntervalSinceNow]) / 60;
1475}
1476
1477- (BOOL) isStalled
1478{
1479    return fStalled;
1480}
1481
1482- (void) updateTimeMachineExclude
1483{
1484    NSString * currentLocation = ![self allDownloaded] ? [self dataLocation] : nil;
1485   
1486    //return if the locations are the same
1487    if (fTimeMachineExclude && currentLocation && [fTimeMachineExclude isEqualToString: currentLocation])
1488        return;
1489   
1490    //remove old location...
1491    if (fTimeMachineExclude)
1492    {
1493        [self setTimeMachineExclude: NO forPath: fTimeMachineExclude];
1494        [fTimeMachineExclude release];
1495        fTimeMachineExclude = nil;
1496    }
1497   
1498    //...set new location
1499    if (currentLocation)
1500    {
1501        [self setTimeMachineExclude: YES forPath: currentLocation];
1502        fTimeMachineExclude = [currentLocation retain];
1503    }
1504}
1505
1506- (NSInteger) stateSortKey
1507{
1508    if (![self isActive]) //paused
1509    {
1510        if (fWaitToStart)
1511            return 1;
1512        else
1513            return 0;
1514    }
1515    else if ([self isSeeding]) //seeding
1516        return 10;
1517    else //downloading
1518        return 20;
1519}
1520
1521- (NSString *) trackerSortKey
1522{
1523    int count;
1524    tr_tracker_stat * stats = tr_torrentTrackers(fHandle, &count);
1525   
1526    NSString * best = nil;
1527   
1528    for (int i=0; i < count; ++i)
1529    {
1530        NSString * tracker = [NSString stringWithUTF8String: stats[i].host];
1531        if (!best || [tracker localizedCaseInsensitiveCompare: best] == NSOrderedAscending)
1532            best = tracker;
1533    }
1534   
1535    tr_torrentTrackersFree(stats, count);
1536    return best;
1537}
1538
1539- (tr_torrent *) torrentStruct
1540{
1541    return fHandle;
1542}
1543
1544- (NSURL *) previewItemURL
1545{
1546    NSString * location = [self dataLocation];
1547    return location ? [NSURL fileURLWithPath: location] : nil;
1548}
1549
1550@end
1551
1552@implementation Torrent (Private)
1553
1554- (id) initWithPath: (NSString *) path hash: (NSString *) hashString torrentStruct: (tr_torrent *) torrentStruct
1555        magnetAddress: (NSString *) magnetAddress lib: (tr_session *) lib
1556        waitToStart: (NSNumber *) waitToStart
1557        groupValue: (NSNumber *) groupValue
1558        downloadFolder: (NSString *) downloadFolder legacyIncompleteFolder: (NSString *) incompleteFolder
1559{
1560    if (!(self = [super init]))
1561        return nil;
1562   
1563    fDefaults = [NSUserDefaults standardUserDefaults];
1564   
1565    if (torrentStruct)
1566        fHandle = torrentStruct;
1567    else
1568    {
1569        //set libtransmission settings for initialization
1570        tr_ctor * ctor = tr_ctorNew(lib);
1571       
1572        tr_ctorSetPaused(ctor, TR_FORCE, YES);
1573        if (downloadFolder)
1574            tr_ctorSetDownloadDir(ctor, TR_FORCE, [downloadFolder UTF8String]);
1575        if (incompleteFolder)
1576            tr_ctorSetIncompleteDir(ctor, [incompleteFolder UTF8String]);
1577       
1578        tr_parse_result result = TR_PARSE_ERR;
1579        if (path)
1580            result = tr_ctorSetMetainfoFromFile(ctor, [path UTF8String]);
1581       
1582        if (result != TR_PARSE_OK && magnetAddress)
1583            result = tr_ctorSetMetainfoFromMagnetLink(ctor, [magnetAddress UTF8String]);
1584       
1585        //backup - shouldn't be needed after upgrade to 1.70
1586        if (result != TR_PARSE_OK && hashString)
1587            result = tr_ctorSetMetainfoFromHash(ctor, [hashString UTF8String]);
1588       
1589        if (result == TR_PARSE_OK)
1590            fHandle = tr_torrentNew(ctor, NULL);
1591       
1592        tr_ctorFree(ctor);
1593       
1594        if (!fHandle)
1595        {
1596            [self release];
1597            return nil;
1598        }
1599    }
1600
1601    fInfo = tr_torrentInfo(fHandle);
1602   
1603    tr_torrentSetCompletenessCallback(fHandle, completenessChangeCallback, self);
1604    tr_torrentSetRatioLimitHitCallback(fHandle, ratioLimitHitCallback, self);
1605    tr_torrentSetMetadataCallback(fHandle, metadataCallback, self);
1606   
1607    fHashString = [[NSString alloc] initWithUTF8String: fInfo->hashString];
1608       
1609    fFinishedSeeding = NO;
1610   
1611    fWaitToStart = waitToStart && [waitToStart boolValue];
1612    fResumeOnWake = NO;
1613   
1614    //don't do after this point - it messes with auto-group functionality
1615    if (![self isMagnet])
1616        [self createFileList];
1617       
1618    fGroupValue = groupValue ? [groupValue intValue] : [[GroupsController groups] groupIndexForTorrent: self];
1619   
1620    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(checkGroupValueForRemoval:)
1621        name: @"GroupValueRemoved" object: nil];
1622   
1623    fTimeMachineExclude = nil;
1624    [self update];
1625   
1626    return self;
1627}
1628
1629- (void) createFileList
1630{
1631    NSAssert(![self isMagnet], @"Cannot create a file list until the torrent is demagnetized");
1632   
1633    if ([self isFolder])
1634    {
1635        NSInteger count = [self fileCount];
1636        NSMutableArray * fileList = [NSMutableArray arrayWithCapacity: count],
1637                    * flatFileList = [NSMutableArray arrayWithCapacity: count];
1638       
1639        for (NSInteger i = 0; i < count; i++)
1640        {
1641            tr_file * file = &fInfo->files[i];
1642           
1643            NSString * fullPath = [NSString stringWithUTF8String: file->name];
1644            NSArray * pathComponents = [fullPath pathComponents];
1645            NSAssert1([pathComponents count] >= 2, @"Not enough components in path %@", fullPath);
1646           
1647            NSString * path = [pathComponents objectAtIndex: 0];
1648            NSString * name = [pathComponents objectAtIndex: 1];
1649           
1650            if ([pathComponents count] > 2)
1651            {
1652                //determine if folder node already exists
1653                FileListNode * node;
1654                for (node in fileList)
1655                    if ([[node name] isEqualToString: name] && [node isFolder])
1656                        break;
1657               
1658                if (!node)
1659                {
1660                    node = [[FileListNode alloc] initWithFolderName: name path: path];
1661                    [fileList addObject: node];
1662                    [node release];
1663                }
1664               
1665                NSMutableArray * trimmedComponents = [NSMutableArray arrayWithArray: [pathComponents subarrayWithRange:
1666                                                        NSMakeRange(2, [pathComponents count]-2)]];
1667               
1668                [node insertIndex: i withSize: file->length];
1669                [self insertPath: trimmedComponents forParent: node fileSize: file->length index: i flatList: flatFileList];
1670            }
1671            else
1672            {
1673                FileListNode * node = [[FileListNode alloc] initWithFileName: name path: path size: file->length index: i];
1674                [fileList addObject: node];
1675                [flatFileList addObject: node];
1676                [node release];
1677            }
1678        }
1679       
1680        fFileList = [[NSArray alloc] initWithArray: fileList];
1681        fFlatFileList = [[NSArray alloc] initWithArray: flatFileList];
1682    }
1683    else
1684    {
1685        FileListNode * node = [[FileListNode alloc] initWithFileName: [self name] path: @"" size: [self size] index: 0];
1686        fFileList = [[NSArray arrayWithObject: node] retain];
1687        fFlatFileList = [fFileList retain];
1688        [node release];
1689    }
1690}
1691
1692- (void) insertPath: (NSMutableArray *) components forParent: (FileListNode *) parent fileSize: (uint64_t) size
1693    index: (NSInteger) index flatList: (NSMutableArray *) flatFileList
1694{
1695    NSString * name = [components objectAtIndex: 0];
1696    const BOOL isFolder = [components count] > 1;
1697   
1698    FileListNode * node = nil;
1699    if (isFolder)
1700    {
1701        for (node in [parent children])
1702            if ([[node name] isEqualToString: name] && [node isFolder])
1703                break;
1704    }
1705   
1706    //create new folder or file if it doesn't already exist
1707    if (!node)
1708    {
1709        NSString * path = [[parent path] stringByAppendingPathComponent: [parent name]];
1710        if (isFolder)
1711            node = [[FileListNode alloc] initWithFolderName: name path: path];
1712        else
1713        {
1714            node = [[FileListNode alloc] initWithFileName: name path: path size: size index: index];
1715            [flatFileList addObject: node];
1716        }
1717       
1718        [parent insertChild: node];
1719        [node release];
1720    }
1721   
1722    if (isFolder)
1723    {
1724        [node insertIndex: index withSize: size];
1725       
1726        [components removeObjectAtIndex: 0];
1727        [self insertPath: components forParent: node fileSize: size index: index flatList: flatFileList];
1728    }
1729}
1730
1731//status has been retained
1732- (void) completenessChange: (NSNumber *) status
1733{
1734    fStat = tr_torrentStat(fHandle); //don't call update yet to avoid auto-stop
1735   
1736    switch ([status intValue])
1737    {
1738        case TR_SEED:
1739        case TR_PARTIAL_SEED:
1740            [[NSNotificationCenter defaultCenter] postNotificationName: @"TorrentFinishedDownloading" object: self];
1741            break;
1742       
1743        case TR_LEECH:
1744            [[NSNotificationCenter defaultCenter] postNotificationName: @"TorrentRestartedDownloading" object: self];
1745            break;
1746    }
1747    [status release];
1748   
1749    [self update];
1750    [self updateTimeMachineExclude];
1751}
1752
1753- (void) ratioLimitHit
1754{
1755    fStat = tr_torrentStat(fHandle);
1756   
1757    [[NSNotificationCenter defaultCenter] postNotificationName: @"TorrentStoppedForRatio" object: self];
1758   
1759    fFinishedSeeding = YES;
1760}
1761
1762- (void) metadataRetrieved
1763{
1764    fStat = tr_torrentStat(fHandle);
1765   
1766    [self createFileList];
1767   
1768    [[NSNotificationCenter defaultCenter] postNotificationName: @"ResetInspector" object: self];
1769}
1770
1771- (NSString *) etaString
1772{
1773    const NSInteger eta = [self eta];
1774    switch (eta)
1775    {
1776        case TR_ETA_NOT_AVAIL:
1777        case TR_ETA_UNKNOWN:
1778            return NSLocalizedString(@"remaining time unknown", "Torrent -> eta string");
1779        default:
1780            return [NSString stringWithFormat: NSLocalizedString(@"%@ remaining", "Torrent -> eta string"),
1781                        [NSString timeString: eta showSeconds: YES maxFields: 2]];
1782    }
1783}
1784
1785- (void) setTimeMachineExclude: (BOOL) exclude forPath: (NSString *) path
1786{
1787    CSBackupSetItemExcluded((CFURLRef)[NSURL fileURLWithPath: path], exclude, true);
1788}
1789
1790@end
Note: See TracBrowser for help on using the repository browser.