source: trunk/macosx/Torrent.m @ 9401

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

restore the check for remaining disk space

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