source: trunk/macosx/Torrent.m @ 14016

Last change on this file since 14016 was 14016, checked in by livings124, 9 years ago

#5286 Files renamed in Mac Inspector are not re-sorted

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