source: trunk/macosx/Torrent.m @ 9468

Last change on this file since 9468 was 9468, checked in by livings124, 13 years ago

200 microseconds for real

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