source: trunk/macosx/Torrent.m @ 6048

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

handle all settings changed by rpc (previously download location was missing)

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