source: trunk/macosx/Torrent.m @ 5814

Last change on this file since 5814 was 5814, checked in by livings124, 14 years ago

fix ordering error

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