source: trunk/macosx/Torrent.m @ 3680

Last change on this file since 3680 was 3680, checked in by livings124, 15 years ago

don't check if backup status should be set if torrent is not active

  • Property svn:keywords set to Date Rev Author Id
File size: 53.5 KB
Line 
1/******************************************************************************
2 * $Id: Torrent.m 3680 2007-11-01 15:53:39Z livings124 $
3 *
4 * Copyright (c) 2006-2007 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 "NSApplicationAdditions.h"
27#import "NSStringAdditions.h"
28
29static int static_lastid = 0;
30
31@interface Torrent (Private)
32
33- (id) initWithHash: (NSString *) hashString path: (NSString *) path lib: (tr_handle *) lib
34        publicTorrent: (NSNumber *) publicTorrent
35        downloadFolder: (NSString *) downloadFolder
36        useIncompleteFolder: (NSNumber *) useIncompleteFolder incompleteFolder: (NSString *) incompleteFolder
37        dateAdded: (NSDate *) dateAdded dateCompleted: (NSDate *) dateCompleted
38        dateActivity: (NSDate *) dateActivity
39        ratioSetting: (NSNumber *) ratioSetting ratioLimit: (NSNumber *) ratioLimit
40        waitToStart: (NSNumber *) waitToStart orderValue: (NSNumber *) orderValue;
41
42- (BOOL) shouldUseIncompleteFolderForName: (NSString *) name;
43- (void) updateDownloadFolder;
44
45- (void) createFileList;
46- (void) insertPath: (NSMutableArray *) components forSiblings: (NSMutableArray *) siblings previousPath: (NSString *) previousPath
47            fileSize: (uint64_t) size index: (int) index;
48
49- (void) completenessChange: (NSNumber *) status;
50
51- (void) quickPause;
52- (void) endQuickPause;
53
54- (void) trashFile: (NSString *) path;
55
56@end
57
58void completenessChangeCallback(tr_torrent * torrent, cp_status_t status, void * torrentData)
59{
60    [(Torrent *)torrentData performSelectorOnMainThread: @selector(completenessChange:)
61                withObject: [[NSNumber alloc] initWithInt: status] waitUntilDone: NO];
62}
63
64@implementation Torrent
65
66- (id) initWithPath: (NSString *) path location: (NSString *) location deleteTorrentFile: (torrentFileState) torrentDelete
67        lib: (tr_handle *) lib
68{
69    self = [self initWithHash: nil path: path lib: lib
70            publicTorrent: torrentDelete != TORRENT_FILE_DEFAULT
71                            ? [NSNumber numberWithBool: torrentDelete == TORRENT_FILE_SAVE] : nil
72            downloadFolder: location
73            useIncompleteFolder: nil incompleteFolder: nil
74            dateAdded: nil dateCompleted: nil
75            dateActivity: nil
76            ratioSetting: nil ratioLimit: nil
77            waitToStart: nil orderValue: nil];
78   
79    if (self)
80    {
81        if (!fPublicTorrent)
82            [self trashFile: path];
83    }
84    return self;
85}
86
87- (id) initWithHistory: (NSDictionary *) history lib: (tr_handle *) lib
88{
89    self = [self initWithHash: [history objectForKey: @"TorrentHash"]
90                path: [history objectForKey: @"TorrentPath"] lib: lib
91                publicTorrent: [history objectForKey: @"PublicCopy"]
92                downloadFolder: [history objectForKey: @"DownloadFolder"]
93                useIncompleteFolder: [history objectForKey: @"UseIncompleteFolder"]
94                incompleteFolder: [history objectForKey: @"IncompleteFolder"]
95                dateAdded: [history objectForKey: @"Date"]
96                                dateCompleted: [history objectForKey: @"DateCompleted"]
97                dateActivity: [history objectForKey: @"DateActivity"]
98                ratioSetting: [history objectForKey: @"RatioSetting"]
99                ratioLimit: [history objectForKey: @"RatioLimit"]
100                waitToStart: [history objectForKey: @"WaitToStart"]
101                orderValue: [history objectForKey: @"OrderValue"]];
102   
103    if (self)
104    {
105        //start transfer
106        NSNumber * active;
107        if ((active = [history objectForKey: @"Active"]) && [active boolValue])
108        {
109            fStat = tr_torrentStat(fHandle);
110            [self startTransfer];
111        }
112    }
113    return self;
114}
115
116- (NSDictionary *) history
117{
118    NSMutableDictionary * history = [NSMutableDictionary dictionaryWithObjectsAndKeys:
119                    [NSNumber numberWithBool: fPublicTorrent], @"PublicCopy",
120                    [self hashString], @"TorrentHash",
121                    fDownloadFolder, @"DownloadFolder",
122                    [NSNumber numberWithBool: fUseIncompleteFolder], @"UseIncompleteFolder",
123                    [NSNumber numberWithBool: [self isActive]], @"Active",
124                    fDateAdded, @"Date",
125                    [NSNumber numberWithInt: fRatioSetting], @"RatioSetting",
126                    [NSNumber numberWithFloat: fRatioLimit], @"RatioLimit",
127                    [NSNumber numberWithBool: fWaitToStart], @"WaitToStart",
128                    [self orderValue], @"OrderValue", nil];
129   
130    if (fIncompleteFolder)
131        [history setObject: fIncompleteFolder forKey: @"IncompleteFolder"];
132
133    if (fPublicTorrent)
134        [history setObject: [self publicTorrentLocation] forKey: @"TorrentPath"];
135       
136    if (fDateCompleted)
137                [history setObject: fDateCompleted forKey: @"DateCompleted"];
138   
139    NSDate * dateActivity = [self dateActivity];
140    if (dateActivity)
141                [history setObject: dateActivity forKey: @"DateActivity"];
142       
143    return history;
144}
145
146- (void) dealloc
147{
148    if (fileStat)
149        tr_torrentFilesFree(fileStat, [self fileCount]);
150   
151    [fDownloadFolder release];
152    [fIncompleteFolder release];
153   
154    [fPublicTorrentLocation release];
155   
156    [fDateAdded release];
157    [fDateCompleted release];
158    [fDateActivity release];
159   
160    [fIcon release];
161   
162    [fFileList release];
163    [fFileMenu release];
164   
165    [fOrderValue release];
166   
167    [fQuickPauseDict release];
168   
169    [super dealloc];
170}
171
172- (void) closeTorrent
173{
174    tr_torrentClearStatusCallback(fHandle);
175    tr_torrentClose(fHandle);
176}
177
178- (void) closeRemoveTorrent
179{
180    tr_torrentRemoveSaved(fHandle);
181    [self closeTorrent];
182}
183
184- (void) changeIncompleteDownloadFolder: (NSString *) folder
185{
186    fUseIncompleteFolder = folder != nil;
187   
188    [fIncompleteFolder release];
189    fIncompleteFolder = fUseIncompleteFolder ? [folder retain] : nil;
190   
191    [self updateDownloadFolder];
192}
193
194- (void) changeDownloadFolder: (NSString *) folder
195{
196    [fDownloadFolder release];
197    fDownloadFolder = [folder retain];
198   
199    [self updateDownloadFolder];
200}
201
202- (NSString *) downloadFolder
203{
204    return [NSString stringWithUTF8String: tr_torrentGetFolder(fHandle)];
205}
206
207- (void) getAvailability: (int8_t *) tab size: (int) size
208{
209    tr_torrentAvailability(fHandle, tab, size);
210}
211
212- (void) getAmountFinished: (float *) tab size: (int) size
213{
214    tr_torrentAmountFinished(fHandle, tab, size);
215}
216
217- (void) update
218{
219    fStat = tr_torrentStat(fHandle);
220   
221    #warning find a better way
222    //check if the file is created for Time Machine
223    if (fNeedSetTimeMachine && [self isActive])
224    {
225        NSURL *url = [NSURL fileURLWithPath: [[self downloadFolder] stringByAppendingPathComponent: [self name]]];
226        fNeedSetTimeMachine = CSBackupSetItemExcluded((CFURLRef)url, ![self allDownloaded], false) != noErr;
227    }
228   
229    //check to stop for ratio
230    float stopRatio;
231    if ([self isSeeding] && (stopRatio = [self actualStopRatio]) != INVALID && [self ratio] >= stopRatio)
232    {
233        [self stopTransfer];
234        fStat = tr_torrentStat(fHandle);
235       
236        fFinishedSeeding = YES;
237       
238        [self setRatioSetting: NSOffState];
239        [[NSNotificationCenter defaultCenter] postNotificationName: @"TorrentStoppedForRatio" object: self];
240    }
241   
242    //check if checking data
243    BOOL wasChecking = fChecking;
244    fChecking = fStat->status == TR_STATUS_CHECK || fStat->status == TR_STATUS_CHECK_WAIT;
245   
246    //check for error
247    BOOL wasError = fError;
248    fError = [self isError];
249   
250    //check if stalled
251    BOOL wasStalled = fStalled;
252    fStalled = [self isActive] && [fDefaults boolForKey: @"CheckStalled"]
253                && [fDefaults integerForKey: @"StalledMinutes"] < [self stalledMinutes];
254   
255    //update queue for checking (from downloading to seeding), stalled, or error
256    if ((wasChecking && !fChecking) || (!wasStalled && fStalled) || (!wasError && fError && [self isActive]))
257        [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateQueue" object: self];
258}
259
260- (void) startTransfer
261{
262    fWaitToStart = NO;
263    fFinishedSeeding = NO;
264   
265    if (![self isActive] && [self alertForFolderAvailable] && [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_manualUpdate(fHandle);
300}
301
302- (BOOL) canManualAnnounce
303{
304    return tr_torrentCanManualUpdate(fHandle);
305}
306
307- (void) resetCache
308{
309    tr_torrentRecheck(fHandle);
310    [self update];
311}
312
313- (float) ratio
314{
315    return fStat->ratio;
316}
317
318- (int) ratioSetting
319{
320    return fRatioSetting;
321}
322
323- (void) setRatioSetting: (int) setting
324{
325    fRatioSetting = setting;
326}
327
328- (float) ratioLimit
329{
330    return fRatioLimit;
331}
332
333- (void) setRatioLimit: (float) limit
334{
335    if (limit >= 0)
336        fRatioLimit = limit;
337}
338
339- (float) actualStopRatio
340{
341    if (fRatioSetting == NSOnState)
342        return fRatioLimit;
343    else if (fRatioSetting == NSMixedState && [fDefaults boolForKey: @"RatioCheck"])
344        return [fDefaults floatForKey: @"RatioLimit"];
345    else
346        return INVALID;
347}
348
349- (float) progressStopRatio
350{
351    float stopRatio, ratio;
352    if ((stopRatio = [self actualStopRatio]) == INVALID || (ratio = [self ratio]) >= stopRatio)
353        return 1.0;
354    else if (ratio > 0 && stopRatio > 0)
355        return ratio / stopRatio;
356    else
357        return 0;
358}
359
360- (tr_speedlimit) speedMode: (BOOL) upload
361{
362    return tr_torrentGetSpeedMode(fHandle, upload ? TR_UP : TR_DOWN);
363}
364
365- (void) setSpeedMode: (tr_speedlimit) mode upload: (BOOL) upload
366{
367    tr_torrentSetSpeedMode(fHandle, upload ? TR_UP : TR_DOWN, mode);
368}
369
370- (int) speedLimit: (BOOL) upload
371{
372    return tr_torrentGetSpeedLimit(fHandle, upload ? TR_UP : TR_DOWN);
373}
374
375- (void) setSpeedLimit: (int) limit upload: (BOOL) upload
376{
377    tr_torrentSetSpeedLimit(fHandle, upload ? TR_UP : TR_DOWN, limit);
378}
379
380- (void) setWaitToStart: (BOOL) wait
381{
382    fWaitToStart = wait;
383}
384
385- (BOOL) waitingToStart
386{
387    return fWaitToStart;
388}
389
390- (void) revealData
391{
392    [[NSWorkspace sharedWorkspace] selectFile: [self dataLocation] inFileViewerRootedAtPath: nil];
393}
394
395- (void) revealPublicTorrent
396{
397    if (fPublicTorrent)
398        [[NSWorkspace sharedWorkspace] selectFile: fPublicTorrentLocation inFileViewerRootedAtPath: nil];
399}
400
401- (void) trashData
402{
403    [self trashFile: [self dataLocation]];
404}
405
406- (void) trashTorrent
407{
408    if (fPublicTorrent)
409        [self trashFile: [self publicTorrentLocation]];
410}
411
412- (void) moveTorrentDataFileTo: (NSString *) folder
413{
414    NSString * oldFolder = [self downloadFolder];
415    if (![oldFolder isEqualToString: folder] || ![fDownloadFolder isEqualToString: folder])
416    {
417        //check if moving inside itself
418        NSArray * oldComponents = [oldFolder pathComponents],
419                * newComponents = [folder pathComponents];
420        int count;
421       
422        if ((count = [oldComponents count]) < [newComponents count]
423                && [[newComponents objectAtIndex: count] isEqualToString: [self name]]
424                && [oldComponents isEqualToArray:
425                        [newComponents objectsAtIndexes: [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, count)]]])
426        {
427            NSAlert * alert = [[NSAlert alloc] init];
428            [alert setMessageText: NSLocalizedString(@"A folder cannot be moved to inside itself.",
429                                                        "Move inside itself alert -> title")];
430            [alert setInformativeText: [NSString stringWithFormat:
431                            NSLocalizedString(@"The move operation of \"%@\" cannot be done.",
432                                                "Move inside itself alert -> message"), [self name]]];
433            [alert addButtonWithTitle: NSLocalizedString(@"OK", "Move inside itself alert -> button")];
434           
435            [alert runModal];
436            [alert release];
437           
438            return;
439        }
440       
441        [self quickPause];
442       
443        if ([[NSFileManager defaultManager] movePath: [oldFolder stringByAppendingPathComponent: [self name]]
444                            toPath: [folder stringByAppendingPathComponent: [self name]] handler: nil])
445        {
446            //get rid of both incomplete folder and old download folder, even if move failed
447            fUseIncompleteFolder = NO;
448            if (fIncompleteFolder)
449            {
450                [fIncompleteFolder release];
451                fIncompleteFolder = nil;
452            }
453            [self changeDownloadFolder: folder];
454           
455            [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateStats" object: nil];
456           
457            [self endQuickPause];
458        }
459        else
460        {
461            [self endQuickPause];
462       
463            NSAlert * alert = [[NSAlert alloc] init];
464            [alert setMessageText: NSLocalizedString(@"There was an error moving the data file.", "Move error alert -> title")];
465            [alert setInformativeText: [NSString stringWithFormat:
466                            NSLocalizedString(@"The move operation of \"%@\" cannot be done.",
467                                                "Move error alert -> message"), [self name]]];
468            [alert addButtonWithTitle: NSLocalizedString(@"OK", "Move error alert -> button")];
469           
470            [alert runModal];
471            [alert release];
472        }
473    }
474}
475
476- (void) copyTorrentFileTo: (NSString *) path
477{
478    [[NSFileManager defaultManager] copyPath: [self torrentLocation] toPath: path handler: nil];
479}
480
481- (BOOL) alertForRemainingDiskSpace
482{
483    if ([self allDownloaded] || ![fDefaults boolForKey: @"WarningRemainingSpace"])
484        return YES;
485   
486    NSString * volumeName;
487    if ((volumeName = [[[NSFileManager defaultManager] componentsToDisplayForPath: [self downloadFolder]] objectAtIndex: 0]))
488    {
489        NSDictionary * systemAttributes = [[NSFileManager defaultManager] fileSystemAttributesAtPath: [self downloadFolder]];
490        uint64_t remainingSpace = [[systemAttributes objectForKey: NSFileSystemFreeSize] unsignedLongLongValue];
491       
492        if (remainingSpace <= [self sizeLeft])
493        {
494            NSAlert * alert = [[NSAlert alloc] init];
495            [alert setMessageText: [NSString stringWithFormat:
496                                    NSLocalizedString(@"Not enough remaining disk space to download \"%@\" completely.",
497                                        "Torrent file disk space alert -> title"), [self name]]];
498            [alert setInformativeText: [NSString stringWithFormat: NSLocalizedString(@"The transfer will be paused."
499                                        " Clear up space on %@ or deselect files in the torrent inspector to continue.",
500                                        "Torrent file disk space alert -> message"), volumeName]];
501            [alert addButtonWithTitle: NSLocalizedString(@"OK", "Torrent file disk space alert -> button")];
502            [alert addButtonWithTitle: NSLocalizedString(@"Download Anyway", "Torrent file disk space alert -> button")];
503           
504            #warning factor in choice with suppression
505            BOOL onLeopard = [NSApp isOnLeopardOrBetter];
506            if (onLeopard)
507                [alert setShowsSuppressionButton: YES];
508            else
509                [alert addButtonWithTitle: NSLocalizedString(@"Always Download", "Torrent file disk space alert -> button")];
510
511            NSInteger result = [alert runModal];
512            if ((onLeopard ? [[alert suppressionButton] state] == NSOnState : result == NSAlertThirdButtonReturn))
513                [fDefaults setBool: NO forKey: @"WarningRemainingSpace"];
514            [alert release];
515           
516            return result != NSAlertFirstButtonReturn;
517        }
518    }
519    return YES;
520}
521
522- (BOOL) alertForFolderAvailable
523{
524    #warning check for change from incomplete to download folder first
525    if (access(tr_torrentGetFolder(fHandle), 0))
526    {
527        NSAlert * alert = [[NSAlert alloc] init];
528        [alert setMessageText: [NSString stringWithFormat:
529                                NSLocalizedString(@"The folder for downloading \"%@\" cannot be used.",
530                                    "Folder cannot be used alert -> title"), [self name]]];
531        [alert setInformativeText: [NSString stringWithFormat:
532                        NSLocalizedString(@"\"%@\" cannot be used. The transfer will be paused.",
533                                            "Folder cannot be used alert -> message"), [self downloadFolder]]];
534        [alert addButtonWithTitle: NSLocalizedString(@"OK", "Folder cannot be used alert -> button")];
535        [alert addButtonWithTitle: [NSLocalizedString(@"Choose New Location",
536                                    "Folder cannot be used alert -> location button") stringByAppendingEllipsis]];
537       
538        if ([alert runModal] != NSAlertFirstButtonReturn)
539        {
540            NSOpenPanel * panel = [NSOpenPanel openPanel];
541           
542            [panel setPrompt: NSLocalizedString(@"Select", "Folder cannot be used alert -> prompt")];
543            [panel setAllowsMultipleSelection: NO];
544            [panel setCanChooseFiles: NO];
545            [panel setCanChooseDirectories: YES];
546            [panel setCanCreateDirectories: YES];
547
548            [panel setMessage: [NSString stringWithFormat: NSLocalizedString(@"Select the download folder for \"%@\"",
549                                "Folder cannot be used alert -> select destination folder"), [self name]]];
550           
551            [[NSNotificationCenter defaultCenter] postNotificationName: @"MakeWindowKey" object: nil];
552            [panel beginSheetForDirectory: nil file: nil types: nil modalForWindow: [NSApp keyWindow] modalDelegate: self
553                    didEndSelector: @selector(destinationChoiceClosed:returnCode:contextInfo:) contextInfo: nil];
554        }
555       
556        [alert release];
557       
558        return NO;
559    }
560    return YES;
561}
562
563- (void) destinationChoiceClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) context
564{
565    if (code != NSOKButton)
566        return;
567   
568    [self changeDownloadFolder: [[openPanel filenames] objectAtIndex: 0]];
569   
570    [self startTransfer];
571    [self update];
572   
573    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateStats" object: nil];
574}
575
576- (BOOL) alertForMoveFolderAvailable
577{
578    if (access([fDownloadFolder UTF8String], 0))
579    {
580        NSAlert * alert = [[NSAlert alloc] init];
581        [alert setMessageText: [NSString stringWithFormat:
582                                NSLocalizedString(@"The folder for moving the completed \"%@\" cannot be used.",
583                                    "Move folder cannot be used alert -> title"), [self name]]];
584        [alert setInformativeText: [NSString stringWithFormat:
585                                NSLocalizedString(@"\"%@\" cannot be used. The file will remain in its current location.",
586                                    "Move folder cannot be used alert -> message"), fDownloadFolder]];
587        [alert addButtonWithTitle: NSLocalizedString(@"OK", "Move folder cannot be used alert -> button")];
588       
589        [alert runModal];
590        [alert release];
591       
592        return NO;
593    }
594   
595    return YES;
596}
597
598- (NSImage *) icon
599{
600    if (!fIcon)
601    {
602        fIcon = [[[NSWorkspace sharedWorkspace] iconForFileType: fInfo->isMultifile ? NSFileTypeForHFSTypeCode('fldr')
603                                                : [[self name] pathExtension]] retain];
604        [fIcon setFlipped: YES];
605    }
606    return fIcon;
607}
608
609- (NSString *) name
610{
611    return [NSString stringWithUTF8String: fInfo->name];
612}
613
614- (uint64_t) size
615{
616    return fInfo->totalSize;
617}
618
619- (uint64_t) sizeLeft
620{
621    return fStat->leftUntilDone;
622}
623
624- (NSString *) trackerAddress
625{
626    return [NSString stringWithFormat: @"http://%s:%d", fStat->tracker->address, fStat->tracker->port];
627}
628
629- (NSString *) trackerAddressAnnounce
630{
631    return [NSString stringWithUTF8String: fStat->tracker->announce];
632}
633
634- (NSArray *) allTrackers
635{
636    NSMutableArray * trackers = [NSMutableArray arrayWithCapacity: fInfo->trackerTiers], * subTrackers;
637   
638    int i, j;
639    for (i = 0; i < fInfo->trackerTiers; i++)
640    {
641        subTrackers = [NSMutableArray arrayWithCapacity: fInfo->trackerList[i].count];
642        for (j = 0; j < fInfo->trackerList[i].count; j++)
643            [subTrackers addObject: [NSString stringWithFormat: @"http://%s:%d",
644                fInfo->trackerList[i].list[j].address, fInfo->trackerList[i].list[j].port]];
645       
646        [trackers addObject: subTrackers];
647    }
648   
649    return trackers;
650}
651
652- (NSString *) comment
653{
654    return [NSString stringWithUTF8String: fInfo->comment];
655}
656
657- (NSString *) creator
658{
659    return [NSString stringWithUTF8String: fInfo->creator];
660}
661
662- (NSDate *) dateCreated
663{
664    int date = fInfo->dateCreated;
665    return date > 0 ? [NSDate dateWithTimeIntervalSince1970: date] : nil;
666}
667
668- (int) pieceSize
669{
670    return fInfo->pieceSize;
671}
672
673- (int) pieceCount
674{
675    return fInfo->pieceCount;
676}
677
678- (NSString *) hashString
679{
680    return [NSString stringWithUTF8String: fInfo->hashString];
681}
682
683- (BOOL) privateTorrent
684{
685    return fInfo->isPrivate;
686}
687
688- (NSString *) torrentLocation
689{
690    return [NSString stringWithUTF8String: fInfo->torrent];
691}
692
693- (NSString *) publicTorrentLocation
694{
695    return fPublicTorrentLocation;
696}
697
698- (NSString *) dataLocation
699{
700    return [[self downloadFolder] stringByAppendingPathComponent: [self name]];
701}
702
703- (BOOL) publicTorrent
704{
705    return fPublicTorrent;
706}
707
708- (float) progress
709{
710    return fStat->percentComplete;
711    //return (float)[self haveTotal] / [self size];
712}
713
714- (float) progressDone
715{
716    return fStat->percentDone;
717    /*uint64_t have = [self haveTotal];
718    return (float)have / (have + [self sizeLeft]);*/
719}
720
721- (float) progressLeft
722{
723    //NSLog(@"left %f",(float)fStat->leftUntilDone / [self size]);
724    return (float)[self sizeLeft] / [self size];
725}
726
727- (int) eta
728{
729    return fStat->eta;
730}
731
732- (NSString * ) etaString
733{
734    int eta = [self eta];
735    if (eta < 0)
736        return @"";
737   
738    if (eta < 60)
739        return [NSString stringWithFormat: NSLocalizedString(@"%d sec", "Torrent -> remaining time"), eta];
740    else if (eta < 3600) //60 * 60
741        return [NSString stringWithFormat: NSLocalizedString(@"%d min %d sec", "Torrent -> remaining time"),
742                                                eta / 60, eta % 60];
743    else if (eta < 86400) //24 * 60 * 60
744        return [NSString stringWithFormat: NSLocalizedString(@"%d hr %d min", "Torrent -> remaining time"),
745                                                eta / 3600, (eta / 60) % 60];
746    else
747    {
748        int days = eta / 86400, hours = (eta / 3600) % 24;
749        if (days > 1)
750            return [NSString stringWithFormat: NSLocalizedString(@"%d days %d hr", "Torrent -> remaining time"), days, hours];
751        else
752            return [NSString stringWithFormat: NSLocalizedString(@"1 day %d hr", "Torrent -> remaining time"), hours];
753    }
754}
755
756- (float) notAvailableDesired
757{
758    //NSLog(@"not available %f", (float)(fStat->desiredSize - fStat->desiredAvailable) / [self size]);
759    return (float)(fStat->desiredSize - fStat->desiredAvailable) / [self size];
760}
761
762- (BOOL) isActive
763{
764    return fStat->status != TR_STATUS_STOPPED;
765}
766
767- (BOOL) isSeeding
768{
769    return fStat->status == TR_STATUS_SEED || fStat->status == TR_STATUS_DONE;
770}
771
772- (BOOL) isChecking
773{
774    return fStat->status == TR_STATUS_CHECK || fStat->status == TR_STATUS_CHECK_WAIT;
775}
776
777- (BOOL) allDownloaded
778{
779    return [self progressDone] >= 1.0;
780}
781
782- (BOOL) isComplete
783{
784    return [self progress] >= 1.0;
785}
786
787- (BOOL) isError
788{
789    return fStat->error != 0;
790}
791
792- (NSString *) errorMessage
793{
794    if (![self isError])
795        return @"";
796   
797    NSString * error;
798    if (!(error = [NSString stringWithUTF8String: fStat->errorString])
799        && !(error = [NSString stringWithCString: fStat->errorString encoding: NSISOLatin1StringEncoding]))
800        error = NSLocalizedString(@"(unreadable error)", "Torrent -> error string unreadable");
801   
802    return error;
803}
804
805- (NSArray *) peers
806{
807    int totalPeers, i;
808    tr_peer_stat * peers = tr_torrentPeers(fHandle, &totalPeers);
809   
810    NSMutableArray * peerDics = [NSMutableArray arrayWithCapacity: totalPeers];
811    NSMutableDictionary * dic;
812   
813    tr_peer_stat * peer;
814    for (i = 0; i < totalPeers; i++)
815    {
816        peer = &peers[i];
817       
818        dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:
819            [NSNumber numberWithInt: peer->from], @"From",
820            [NSString stringWithCString: (char *)peer->addr encoding: NSUTF8StringEncoding], @"IP",
821            [NSNumber numberWithInt: peer->port], @"Port",
822            [NSNumber numberWithFloat: peer->progress], @"Progress",
823            [NSNumber numberWithBool: peer->isEncrypted], @"Encryption",
824            [NSString stringWithCString: (char *)peer->client encoding: NSUTF8StringEncoding], @"Client", nil];
825       
826        if (peer->isDownloading)
827            [dic setObject: [NSNumber numberWithFloat: peer->uploadToRate] forKey: @"UL To Rate"];
828        if (peer->isUploading)
829            [dic setObject: [NSNumber numberWithFloat: peer->downloadFromRate] forKey: @"DL From Rate"];
830       
831        [peerDics addObject: dic];
832    }
833   
834    tr_torrentPeersFree(peers, totalPeers);
835   
836    return peerDics;
837}
838
839- (NSString *) progressString
840{
841    NSString * string;
842   
843    if (![self allDownloaded])
844    {
845        if ([fDefaults boolForKey: @"DisplayStatusProgressSelected"])
846        {
847            uint64_t have = [self haveTotal];
848            string = [NSString stringWithFormat: NSLocalizedString(@"%@ of %@ selected (%.2f%%)", "Torrent -> progress string"),
849                            [NSString stringForFileSize: have], [NSString stringForFileSize: have + [self sizeLeft]],
850                            100.0 * [self progressDone]];
851        }
852        else
853            string = [NSString stringWithFormat: NSLocalizedString(@"%@ of %@ (%.2f%%)", "Torrent -> progress string"),
854                            [NSString stringForFileSize: [self haveTotal]],
855                            [NSString stringForFileSize: [self size]], 100.0 * [self progress]];
856    }
857    else if (![self isComplete])
858    {
859        if ([fDefaults boolForKey: @"DisplayStatusProgressSelected"])
860            string = [NSString stringWithFormat: NSLocalizedString(@"%@ selected, uploaded %@ (Ratio: %@)",
861                "Torrent -> progress string"), [NSString stringForFileSize: [self haveTotal]],
862                [NSString stringForFileSize: [self uploadedTotal]], [NSString stringForRatio: [self ratio]]];
863        else
864            string = [NSString stringWithFormat: NSLocalizedString(@"%@ of %@ (%.2f%%), uploaded %@ (Ratio: %@)",
865                "Torrent -> progress string"), [NSString stringForFileSize: [self haveTotal]],
866                [NSString stringForFileSize: [self size]], 100.0 * [self progress],
867                [NSString stringForFileSize: [self uploadedTotal]], [NSString stringForRatio: [self ratio]]];
868    }
869    else
870        string = [NSString stringWithFormat: NSLocalizedString(@"%@, uploaded %@ (Ratio: %@)", "Torrent -> progress string"),
871                [NSString stringForFileSize: [self size]], [NSString stringForFileSize: [self uploadedTotal]],
872                [NSString stringForRatio: [self ratio]]];
873   
874    //add time when downloading
875    if (fStat->status == TR_STATUS_DOWNLOAD)
876    {
877        string = [self eta] >= 0
878            ? [string stringByAppendingFormat: NSLocalizedString(@" - %@ remaining", "Torrent -> progress string"), [self etaString]]
879            : [string stringByAppendingString: NSLocalizedString(@" - remaining time unknown", "Torrent -> progress string")];
880    }
881   
882    return string;
883}
884
885- (NSString *) statusString
886{
887    NSString * string;
888   
889    if ([self isError])
890    {
891        NSString * errorString = [self errorMessage];
892        if (!errorString || [errorString isEqualToString: @""])
893            string = NSLocalizedString(@"Error", "Torrent -> status string");
894        else
895            string = [NSLocalizedString(@"Error: ", "Torrent -> status string") stringByAppendingString: errorString];
896    }
897    else
898    {
899        switch (fStat->status)
900        {
901            case TR_STATUS_STOPPED:
902                if (fWaitToStart)
903                {
904                    string = ![self allDownloaded]
905                            ? [NSLocalizedString(@"Waiting to download", "Torrent -> status string") stringByAppendingEllipsis]
906                            : [NSLocalizedString(@"Waiting to seed", "Torrent -> status string") stringByAppendingEllipsis];
907                }
908                else if (fFinishedSeeding)
909                    string = NSLocalizedString(@"Seeding complete", "Torrent -> status string");
910                else
911                    string = NSLocalizedString(@"Paused", "Torrent -> status string");
912                break;
913
914            case TR_STATUS_CHECK_WAIT:
915                string = [NSLocalizedString(@"Waiting to check existing data", "Torrent -> status string") stringByAppendingEllipsis];
916                break;
917
918            case TR_STATUS_CHECK:
919                string = [NSString stringWithFormat: NSLocalizedString(@"Checking existing data (%.2f%%)",
920                                        "Torrent -> status string"), 100.0 * fStat->recheckProgress];
921                break;
922
923            case TR_STATUS_DOWNLOAD:
924                if ([self totalPeersConnected] != 1)
925                    string = [NSString stringWithFormat: NSLocalizedString(@"Downloading from %d of %d peers",
926                                                    "Torrent -> status string"), [self peersSendingToUs], [self totalPeersConnected]];
927                else
928                    string = [NSString stringWithFormat: NSLocalizedString(@"Downloading from %d of 1 peer",
929                                                    "Torrent -> status string"), [self peersSendingToUs]];
930                break;
931
932            case TR_STATUS_SEED:
933            case TR_STATUS_DONE:
934                if ([self totalPeersConnected] != 1)
935                    string = [NSString stringWithFormat: NSLocalizedString(@"Seeding to %d of %d peers", "Torrent -> status string"),
936                                                    [self peersGettingFromUs], [self totalPeersConnected]];
937                else
938                    string = [NSString stringWithFormat: NSLocalizedString(@"Seeding to %d of 1 peer", "Torrent -> status string"),
939                                                    [self peersGettingFromUs]];
940                break;
941           
942            default:
943                string = @"";
944        }
945       
946        if (fStalled)
947            string = [NSLocalizedString(@"Stalled, ", "Torrent -> status string") stringByAppendingString: string];
948    }
949   
950    //append even if error
951    if ([self isActive] && ![self isChecking])
952    {
953        if (fStat->status == TR_STATUS_DOWNLOAD)
954            string = [string stringByAppendingFormat: NSLocalizedString(@" - DL: %@, UL: %@", "Torrent -> status string"),
955                    [NSString stringForSpeed: [self downloadRate]], [NSString stringForSpeed: [self uploadRate]]];
956        else
957            string = [string stringByAppendingFormat: NSLocalizedString(@" - UL: %@", "Torrent -> status string"),
958                        [NSString stringForSpeed: [self uploadRate]]];
959    }
960   
961    return string;
962}
963
964- (NSString *) shortStatusString
965{
966    NSString * string;
967   
968    switch (fStat->status)
969    {
970        case TR_STATUS_STOPPED:
971            if (fWaitToStart)
972            {
973                string = ![self allDownloaded]
974                        ? [NSLocalizedString(@"Waiting to download", "Torrent -> status string") stringByAppendingEllipsis]
975                        : [NSLocalizedString(@"Waiting to seed", "Torrent -> status string") stringByAppendingEllipsis];
976            }
977            else if (fFinishedSeeding)
978                string = NSLocalizedString(@"Seeding complete", "Torrent -> status string");
979            else
980                string = NSLocalizedString(@"Paused", "Torrent -> status string");
981            break;
982
983        case TR_STATUS_CHECK_WAIT:
984            string = [NSLocalizedString(@"Waiting to check existing data", "Torrent -> status string") stringByAppendingEllipsis];
985            break;
986
987        case TR_STATUS_CHECK:
988            string = [NSString stringWithFormat: NSLocalizedString(@"Checking existing data (%.2f%%)",
989                                    "Torrent -> status string"), 100.0 * fStat->recheckProgress];
990            break;
991       
992        case TR_STATUS_DOWNLOAD:
993            string = [NSString stringWithFormat: NSLocalizedString(@"DL: %@, UL: %@", "Torrent -> status string"),
994                            [NSString stringForSpeed: [self downloadRate]], [NSString stringForSpeed: [self uploadRate]]];
995            break;
996       
997        case TR_STATUS_SEED:
998        case TR_STATUS_DONE:
999            string = [NSString stringWithFormat: NSLocalizedString(@"Ratio: %@, UL: %@", "Torrent -> status string"),
1000                            [NSString stringForRatio: [self ratio]], [NSString stringForSpeed: [self uploadRate]]];
1001            break;
1002       
1003        default:
1004            string = @"";
1005    }
1006   
1007    return string;
1008}
1009
1010- (NSString *) remainingTimeString
1011{
1012    switch (fStat->status)
1013    {
1014        case TR_STATUS_DOWNLOAD:
1015            return [self eta] >= 0 ? [self etaString] : NSLocalizedString(@"Unknown", "Torrent -> remaining time");
1016       
1017        case TR_STATUS_SEED:
1018        case TR_STATUS_DONE:
1019            return [NSLocalizedString(@"Ratio: ", "Torrent -> status string") stringByAppendingString:
1020                                                                            [NSString stringForRatio: [self ratio]]];
1021       
1022        default:
1023            return [self shortStatusString];
1024    }
1025}
1026
1027- (NSString *) stateString
1028{
1029    switch (fStat->status)
1030    {
1031        case TR_STATUS_STOPPED:
1032            return NSLocalizedString(@"Paused", "Torrent -> status string");
1033
1034        case TR_STATUS_CHECK:
1035            return [NSString stringWithFormat: NSLocalizedString(@"Checking existing data (%.2f%%)",
1036                                    "Torrent -> status string"), 100.0 * fStat->recheckProgress];
1037       
1038        case TR_STATUS_CHECK_WAIT:
1039            return [NSLocalizedString(@"Waiting to check existing data", "Torrent -> status string") stringByAppendingEllipsis];
1040
1041        case TR_STATUS_DOWNLOAD:
1042            return NSLocalizedString(@"Downloading", "Torrent -> status string");
1043
1044        case TR_STATUS_SEED:
1045        case TR_STATUS_DONE:
1046            return NSLocalizedString(@"Seeding", "Torrent -> status string");
1047       
1048        default:
1049            return NSLocalizedString(@"N/A", "Torrent -> status string");
1050    }
1051}
1052
1053- (int) seeders
1054{
1055    return fStat->seeders;
1056}
1057
1058- (int) leechers
1059{
1060    return fStat->leechers;
1061}
1062
1063- (int) completedFromTracker
1064{
1065    return fStat->completedFromTracker;
1066}
1067
1068- (int) totalPeersConnected
1069{
1070    return fStat->peersConnected;
1071}
1072
1073- (int) totalPeersTracker
1074{
1075    return fStat->peersFrom[TR_PEER_FROM_TRACKER];
1076}
1077
1078- (int) totalPeersIncoming
1079{
1080    return fStat->peersFrom[TR_PEER_FROM_INCOMING];
1081}
1082
1083- (int) totalPeersCache
1084{
1085    return fStat->peersFrom[TR_PEER_FROM_CACHE];
1086}
1087
1088- (int) totalPeersPex
1089{
1090    return fStat->peersFrom[TR_PEER_FROM_PEX];
1091}
1092
1093- (int) totalPeersKnown
1094{
1095    return fStat->peersKnown;
1096}
1097
1098- (int) peersSendingToUs
1099{
1100    return fStat->peersSendingToUs;
1101}
1102
1103- (int) peersGettingFromUs
1104{
1105    return fStat->peersGettingFromUs;
1106}
1107
1108- (float) downloadRate
1109{
1110    return fStat->rateDownload;
1111}
1112
1113- (float) uploadRate
1114{
1115    return fStat->rateUpload;
1116}
1117
1118- (uint64_t) haveVerified
1119{
1120    return fStat->haveValid;
1121}
1122
1123- (uint64_t) haveTotal
1124{
1125    return [self haveVerified] + fStat->haveUnchecked;
1126}
1127
1128- (uint64_t) downloadedTotal
1129{
1130    return fStat->downloadedEver;
1131}
1132
1133- (uint64_t) uploadedTotal
1134{
1135    return fStat->uploadedEver;
1136}
1137
1138- (uint64_t) failedHash
1139{
1140    return fStat->corruptEver;
1141}
1142
1143- (float) swarmSpeed
1144{
1145    return fStat->swarmspeed;
1146}
1147
1148- (BOOL) pex
1149{
1150        return tr_torrentIsPexEnabled(fHandle);
1151}
1152
1153- (void) setPex: (BOOL) enable
1154{
1155        tr_torrentDisablePex(fHandle, !enable);
1156}
1157
1158- (NSNumber *) orderValue
1159{
1160    return fOrderValue;
1161}
1162
1163- (void) setOrderValue: (int) orderValue
1164{
1165    [fOrderValue release];
1166    fOrderValue = [[NSNumber alloc] initWithInt: orderValue];
1167}
1168
1169- (NSArray *) fileList
1170{
1171    return fFileList;
1172}
1173
1174- (int) fileCount
1175{
1176    return fInfo->fileCount;
1177}
1178
1179- (void) updateFileStat
1180{
1181    if (fileStat)
1182        tr_torrentFilesFree(fileStat, [self fileCount]);
1183   
1184    int count;
1185    fileStat = tr_torrentFiles(fHandle, &count);
1186}
1187
1188- (float) fileProgress: (int) index
1189{
1190    if (!fileStat)
1191        [self updateFileStat];
1192       
1193    return fileStat[index].progress;
1194}
1195
1196- (BOOL) canChangeDownloadCheckForFile: (int) index
1197{
1198    if (!fileStat)
1199        [self updateFileStat];
1200   
1201    return [self fileCount] > 1 && fileStat[index].progress < 1.0;
1202}
1203
1204- (BOOL) canChangeDownloadCheckForFiles: (NSIndexSet *) indexSet
1205{
1206    if ([self fileCount] <= 1 || [self isComplete])
1207        return NO;
1208   
1209    if (!fileStat)
1210        [self updateFileStat];
1211   
1212    int index;
1213    for (index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index])
1214        if (fileStat[index].progress < 1.0)
1215            return YES;
1216    return NO;
1217}
1218
1219- (int) checkForFiles: (NSIndexSet *) indexSet
1220{
1221    BOOL onState = NO, offState = NO;
1222    int index;
1223    for (index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index])
1224    {
1225        if (tr_torrentGetFileDL(fHandle, index) || ![self canChangeDownloadCheckForFile: index])
1226            onState = YES;
1227        else
1228            offState = YES;
1229       
1230        if (onState && offState)
1231            return NSMixedState;
1232    }
1233    return onState ? NSOnState : NSOffState;
1234}
1235
1236- (void) setFileCheckState: (int) state forIndexes: (NSIndexSet *) indexSet
1237{
1238    int count = [indexSet count], i = 0, index;
1239    int * files = malloc(count * sizeof(int));
1240    for (index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index])
1241    {
1242        files[i] = index;
1243        i++;
1244    }
1245   
1246    tr_torrentSetFileDLs(fHandle, files, count, state != NSOffState);
1247    free(files);
1248   
1249    [self update];
1250}
1251
1252- (void) setFilePriority: (int) priority forIndexes: (NSIndexSet *) indexSet
1253{
1254    int count = [indexSet count], i = 0, index;
1255    int * files = malloc(count * sizeof(int));
1256    for (index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index])
1257    {
1258        files[i] = index;
1259        i++;
1260    }
1261   
1262    tr_torrentSetFilePriorities(fHandle, files, count, priority);
1263    free(files);
1264}
1265
1266- (BOOL) hasFilePriority: (int) priority forIndexes: (NSIndexSet *) indexSet
1267{
1268    int index;
1269    for (index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index])
1270        if (priority == tr_torrentGetFilePriority(fHandle, index) && [self canChangeDownloadCheckForFile: index])
1271            return YES;
1272    return NO;
1273}
1274
1275- (NSSet *) filePrioritiesForIndexes: (NSIndexSet *) indexSet
1276{
1277    BOOL low = NO, normal = NO, high = NO;
1278    NSMutableSet * priorities = [NSMutableSet setWithCapacity: 3];
1279   
1280    int index, priority;
1281    for (index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index])
1282    {
1283        if (![self canChangeDownloadCheckForFile: index])
1284            continue;
1285       
1286        priority = tr_torrentGetFilePriority(fHandle, index);
1287        if (priority == TR_PRI_LOW)
1288        {
1289            if (low)
1290                continue;
1291            low = YES;
1292        }
1293        else if (priority == TR_PRI_HIGH)
1294        {
1295            if (high)
1296                continue;
1297            high = YES;
1298        }
1299        else
1300        {
1301            if (normal)
1302                continue;
1303            normal = YES;
1304        }
1305       
1306        [priorities addObject: [NSNumber numberWithInt: priority]];
1307        if (low && normal && high)
1308            break;
1309    }
1310    return priorities;
1311}
1312
1313- (NSMenu *) fileMenu
1314{
1315    if (!fFileMenu)
1316    {
1317        fFileMenu = [[NSMenu alloc] initWithTitle: [@"TorrentMenu:" stringByAppendingString: [self name]]];
1318        [fFileMenu setAutoenablesItems: NO];
1319    }
1320    return fFileMenu;
1321}
1322
1323- (NSDate *) dateAdded
1324{
1325    return fDateAdded;
1326}
1327
1328- (NSDate *) dateCompleted
1329{
1330    return fDateCompleted;
1331}
1332
1333- (NSDate *) dateActivity
1334{
1335    uint64_t date = fStat->activityDate;
1336    return date != 0 ? [NSDate dateWithTimeIntervalSince1970: date / 1000] : fDateActivity;
1337}
1338
1339- (int) stalledMinutes
1340{
1341    uint64_t start;
1342    if ((start = fStat->startDate) == 0)
1343        return -1;
1344   
1345    NSDate * started = [NSDate dateWithTimeIntervalSince1970: start / 1000],
1346            * activity = [self dateActivity];
1347   
1348    NSDate * laterDate = (!activity || [started compare: activity] == NSOrderedDescending) ? started : activity;
1349    return -1 * [laterDate timeIntervalSinceNow] / 60;
1350}
1351
1352- (BOOL) isStalled
1353{
1354    return fStalled;
1355}
1356
1357- (NSNumber *) stateSortKey
1358{
1359    if (![self isActive])
1360        return [NSNumber numberWithInt: 0];
1361    else if ([self isSeeding])
1362        return [NSNumber numberWithInt: 1];
1363    else
1364        return [NSNumber numberWithInt: 2];
1365}
1366
1367- (NSNumber *) progressSortKey
1368{
1369    return [NSNumber numberWithFloat: [self progress]];
1370}
1371
1372- (NSNumber *) ratioSortKey
1373{
1374    return [NSNumber numberWithFloat: [self ratio]];
1375}
1376
1377- (NSNumber *) ratioProgressSortKey
1378{
1379    return [NSNumber numberWithFloat: [self progressStopRatio]];
1380}
1381
1382- (int) torrentID
1383{
1384    return fID;
1385}
1386
1387- (const tr_info *) torrentInfo
1388{
1389    return fInfo;
1390}
1391
1392- (const tr_stat *) torrentStat
1393{
1394    return fStat;
1395}
1396
1397@end
1398
1399@implementation Torrent (Private)
1400
1401//if a hash is given, attempt to load that; otherwise, attempt to open file at path
1402- (id) initWithHash: (NSString *) hashString path: (NSString *) path lib: (tr_handle *) lib
1403        publicTorrent: (NSNumber *) publicTorrent
1404        downloadFolder: (NSString *) downloadFolder
1405        useIncompleteFolder: (NSNumber *) useIncompleteFolder incompleteFolder: (NSString *) incompleteFolder
1406        dateAdded: (NSDate *) dateAdded dateCompleted: (NSDate *) dateCompleted
1407        dateActivity: (NSDate *) dateActivity
1408        ratioSetting: (NSNumber *) ratioSetting ratioLimit: (NSNumber *) ratioLimit
1409        waitToStart: (NSNumber *) waitToStart orderValue: (NSNumber *) orderValue;
1410{
1411    if (!(self = [super init]))
1412        return nil;
1413   
1414    static_lastid++;
1415    fID = static_lastid;
1416   
1417    fLib = lib;
1418    fDefaults = [NSUserDefaults standardUserDefaults];
1419
1420    fPublicTorrent = path && (publicTorrent ? [publicTorrent boolValue] : ![fDefaults boolForKey: @"DeleteOriginalTorrent"]);
1421    if (fPublicTorrent)
1422        fPublicTorrentLocation = [path retain];
1423   
1424    fDownloadFolder = downloadFolder ? downloadFolder : [fDefaults stringForKey: @"DownloadFolder"];
1425    fDownloadFolder = [[fDownloadFolder stringByExpandingTildeInPath] retain];
1426   
1427    fUseIncompleteFolder = useIncompleteFolder ? [useIncompleteFolder boolValue]
1428                                : [fDefaults boolForKey: @"UseIncompleteDownloadFolder"];
1429    if (fUseIncompleteFolder)
1430    {
1431        fIncompleteFolder = incompleteFolder ? incompleteFolder : [fDefaults stringForKey: @"IncompleteDownloadFolder"];
1432        fIncompleteFolder = [[fIncompleteFolder stringByExpandingTildeInPath] retain];
1433    }
1434   
1435    NSString * currentDownloadFolder;
1436    tr_info info;
1437    int error;
1438    if (hashString)
1439    {
1440        if (tr_torrentParseHash(fLib, [hashString UTF8String], NULL, &info) == TR_OK)
1441        {
1442            currentDownloadFolder = [self shouldUseIncompleteFolderForName: [NSString stringWithUTF8String: info.name]]
1443                                        ? fIncompleteFolder : fDownloadFolder;
1444            fHandle = tr_torrentInitSaved(fLib, [hashString UTF8String], [currentDownloadFolder UTF8String], YES, &error);
1445        }
1446        tr_metainfoFree(&info);
1447    }
1448    if (!fHandle && path)
1449    {
1450        if (tr_torrentParse(fLib, [path UTF8String], NULL, &info) == TR_OK)
1451        {
1452            currentDownloadFolder = [self shouldUseIncompleteFolderForName: [NSString stringWithUTF8String: info.name]]
1453                                        ? fIncompleteFolder : fDownloadFolder;
1454            fHandle = tr_torrentInit(fLib, [path UTF8String], [currentDownloadFolder UTF8String], YES, &error);
1455        }
1456        tr_metainfoFree(&info);
1457    }
1458    if (!fHandle)
1459    {
1460        [self release];
1461        return nil;
1462    }
1463   
1464    tr_torrentSetStatusCallback(fHandle, completenessChangeCallback, self);
1465   
1466    fInfo = tr_torrentInfo(fHandle);
1467
1468    fDateAdded = dateAdded ? [dateAdded retain] : [[NSDate alloc] init];
1469        if (dateCompleted)
1470                fDateCompleted = [dateCompleted retain];
1471    if (dateActivity)
1472                fDateActivity = [dateActivity retain];
1473       
1474    fRatioSetting = ratioSetting ? [ratioSetting intValue] : NSMixedState;
1475    fRatioLimit = ratioLimit ? [ratioLimit floatValue] : [fDefaults floatForKey: @"RatioLimit"];
1476    fFinishedSeeding = NO;
1477   
1478    fWaitToStart = waitToStart ? [waitToStart boolValue] : [fDefaults boolForKey: @"AutoStartDownload"];
1479   
1480    [self setOrderValue: orderValue ? [orderValue intValue] : tr_torrentCount(fLib) - 1];
1481    fError = NO;
1482   
1483    [self createFileList];
1484   
1485    [self update];
1486   
1487    //mark incomplete files to be ignored by Time Machine
1488    if ([NSApp isOnLeopardOrBetter])
1489    {
1490        NSURL *url = [NSURL fileURLWithPath: [[self downloadFolder] stringByAppendingPathComponent: [self name]]];
1491        fNeedSetTimeMachine = CSBackupSetItemExcluded((CFURLRef)url, ![self allDownloaded], false) != noErr;
1492    }
1493    else
1494        fNeedSetTimeMachine = NO;
1495   
1496    return self;
1497}
1498
1499- (void) createFileList
1500{
1501    int count = [self fileCount], i;
1502    tr_file * file;
1503    NSMutableArray * pathComponents;
1504    NSString * path;
1505   
1506    NSMutableArray * fileList = [[NSMutableArray alloc] initWithCapacity: count];
1507   
1508    for (i = 0; i < count; i++)
1509    {
1510        file = &fInfo->files[i];
1511       
1512        pathComponents = [[[NSString stringWithUTF8String: file->name] pathComponents] mutableCopy];
1513        if (fInfo->isMultifile)
1514        {
1515            path = [pathComponents objectAtIndex: 0];
1516            [pathComponents removeObjectAtIndex: 0];
1517        }
1518        else
1519            path = @"";
1520       
1521        [self insertPath: pathComponents forSiblings: fileList previousPath: path fileSize: file->length index: i];
1522        [pathComponents release];
1523    }
1524   
1525    fFileList = [[NSArray alloc] initWithArray: fileList];
1526    [fileList release];
1527}
1528
1529- (void) insertPath: (NSMutableArray *) components forSiblings: (NSMutableArray *) siblings previousPath: (NSString *) previousPath
1530            fileSize: (uint64_t) size index: (int) index
1531{
1532    NSString * name = [components objectAtIndex: 0];
1533    BOOL isFolder = [components count] > 1;
1534   
1535    NSMutableDictionary * dict = nil;
1536    if (isFolder)
1537    {
1538        NSEnumerator * enumerator = [siblings objectEnumerator];
1539        while ((dict = [enumerator nextObject]))
1540            if ([[dict objectForKey: @"Name"] isEqualToString: name] && [[dict objectForKey: @"IsFolder"] boolValue])
1541                break;
1542    }
1543   
1544    NSString * currentPath = [previousPath stringByAppendingPathComponent: name];
1545   
1546    //create new folder or item if it doesn't already exist
1547    if (!dict)
1548    {
1549        dict = [NSMutableDictionary dictionaryWithObjectsAndKeys: name, @"Name",
1550                [NSNumber numberWithBool: isFolder], @"IsFolder", currentPath, @"Path", nil];
1551        [siblings addObject: dict];
1552       
1553        if (isFolder)
1554        {
1555            [dict setObject: [NSMutableArray array] forKey: @"Children"];
1556            [dict setObject: [NSMutableIndexSet indexSetWithIndex: index] forKey: @"Indexes"];
1557        }
1558        else
1559        {
1560            [dict setObject: [NSIndexSet indexSetWithIndex: index] forKey: @"Indexes"];
1561            [dict setObject: [NSNumber numberWithUnsignedLongLong: size] forKey: @"Size"];
1562           
1563            NSImage * icon = [[NSWorkspace sharedWorkspace] iconForFileType: [name pathExtension]];
1564            [icon setFlipped: YES];
1565            [dict setObject: icon forKey: @"Icon"];
1566        }
1567    }
1568    else
1569        [[dict objectForKey: @"Indexes"] addIndex: index];
1570   
1571    if (isFolder)
1572    {
1573        [components removeObjectAtIndex: 0];
1574        [self insertPath: components forSiblings: [dict objectForKey: @"Children"] previousPath: currentPath fileSize: size
1575                index: index];
1576    }
1577}
1578
1579- (BOOL) shouldUseIncompleteFolderForName: (NSString *) name
1580{
1581    return fUseIncompleteFolder &&
1582        ![[NSFileManager defaultManager] fileExistsAtPath: [fDownloadFolder stringByAppendingPathComponent: name]];
1583}
1584
1585- (void) updateDownloadFolder
1586{
1587    NSString * folder = [self shouldUseIncompleteFolderForName: [self name]] ? fIncompleteFolder : fDownloadFolder;
1588    tr_torrentSetFolder(fHandle, [folder UTF8String]);
1589}
1590
1591//status has been retained
1592- (void) completenessChange: (NSNumber *) status
1593{
1594    [self update];
1595   
1596    BOOL canMove;
1597    switch ([status intValue])
1598    {
1599        case TR_CP_DONE:
1600        case TR_CP_COMPLETE:
1601            canMove = YES;
1602       
1603            //move file from incomplete folder to download folder
1604            if (fUseIncompleteFolder && ![[self downloadFolder] isEqualToString: fDownloadFolder]
1605                && (canMove = [self alertForMoveFolderAvailable]))
1606            {
1607                [self quickPause];
1608               
1609                if ([[NSFileManager defaultManager] movePath: [[self downloadFolder] stringByAppendingPathComponent: [self name]]
1610                                        toPath: [fDownloadFolder stringByAppendingPathComponent: [self name]] handler: nil])
1611                    [self updateDownloadFolder];
1612                else
1613                    canMove = NO;
1614               
1615                [self endQuickPause];
1616            }
1617           
1618            if (!canMove)
1619            {
1620                fUseIncompleteFolder = NO;
1621               
1622                [fDownloadFolder release];
1623                fDownloadFolder = fIncompleteFolder;
1624                fIncompleteFolder = nil;
1625            }
1626           
1627            [fDateCompleted release];
1628            fDateCompleted = [[NSDate alloc] init];
1629           
1630            //allow to be backed up by Time Machine
1631            if ([NSApp isOnLeopardOrBetter])
1632            {
1633                NSURL *url = [NSURL fileURLWithPath: [[self downloadFolder] stringByAppendingPathComponent: [self name]]];
1634                CSBackupSetItemExcluded((CFURLRef)url, false, false);
1635            }
1636           
1637            fStat = tr_torrentStat(fHandle);
1638            [[NSNotificationCenter defaultCenter] postNotificationName: @"TorrentFinishedDownloading" object: self];
1639            break;
1640       
1641        case TR_CP_INCOMPLETE:
1642            //do not allow to be backed up by Time Machine
1643            if ([NSApp isOnLeopardOrBetter])
1644            {
1645                NSURL *url = [NSURL fileURLWithPath: [[self downloadFolder] stringByAppendingPathComponent: [self name]]];
1646                CSBackupSetItemExcluded((CFURLRef)url, true, false);
1647            }
1648           
1649            [[NSNotificationCenter defaultCenter] postNotificationName: @"TorrentRestartedDownloading" object: self];
1650            break;
1651    }
1652    [status release];
1653}
1654
1655- (void) quickPause
1656{
1657    if (fQuickPauseDict)
1658        return;
1659
1660    fQuickPauseDict = [[NSDictionary alloc] initWithObjectsAndKeys:
1661                    [NSNumber numberWithInt: [self speedMode: YES]], @"UploadSpeedMode",
1662                    [NSNumber numberWithInt: [self speedLimit: YES]], @"UploadSpeedLimit",
1663                    [NSNumber numberWithInt: [self speedMode: NO]], @"DownloadSpeedMode",
1664                    [NSNumber numberWithInt: [self speedLimit: NO]], @"DownloadSpeedLimit", nil];
1665   
1666    [self setSpeedMode: TR_SPEEDLIMIT_SINGLE upload: YES];
1667    [self setSpeedLimit: 0 upload: YES];
1668    [self setSpeedMode: TR_SPEEDLIMIT_SINGLE upload: NO];
1669    [self setSpeedLimit: 0 upload: NO];
1670}
1671
1672- (void) endQuickPause
1673{
1674    if (!fQuickPauseDict)
1675        return;
1676   
1677    [self setSpeedMode: [[fQuickPauseDict objectForKey: @"UploadSpeedMode"] intValue] upload: YES];
1678    [self setSpeedLimit: [[fQuickPauseDict objectForKey: @"UploadSpeedLimit"] intValue] upload: YES];
1679    [self setSpeedMode: [[fQuickPauseDict objectForKey: @"DownloadSpeedMode"] intValue] upload: NO];
1680    [self setSpeedLimit: [[fQuickPauseDict objectForKey: @"DownloadSpeedLimit"] intValue] upload: NO];
1681   
1682    [fQuickPauseDict release];
1683    fQuickPauseDict = nil;
1684}
1685
1686- (void) trashFile: (NSString *) path
1687{
1688    //attempt to move to trash
1689    if (![[NSWorkspace sharedWorkspace] performFileOperation: NSWorkspaceRecycleOperation
1690            source: [path stringByDeletingLastPathComponent] destination: @""
1691            files: [NSArray arrayWithObject: [path lastPathComponent]] tag: nil])
1692    {
1693        //if cannot trash, just delete it (will work if it is on a remote volume)
1694        if (![[NSFileManager defaultManager] removeFileAtPath: path handler: nil])
1695            NSLog(@"Could not trash %@", path);
1696    }
1697}
1698
1699@end
Note: See TracBrowser for help on using the repository browser.