source: trunk/macosx/Torrent.m @ 9667

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

#2625 Add a menu item to copy a magnet link to the clipboard

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