source: trunk/macosx/Torrent.m @ 2266

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

a little code cleanup

  • Property svn:keywords set to Date Rev Author Id
File size: 60.3 KB
Line 
1/******************************************************************************
2 * $Id: Torrent.m 2266 2007-07-01 13:27:26Z livings124 $
3 *
4 * Copyright (c) 2006-2007 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#import "Torrent.h"
26#import "StringAdditions.h"
27
28#define BAR_HEIGHT 12.0
29
30#define MAX_PIECES 324
31#define BLANK_PIECE -99
32
33static int static_lastid = 0;
34
35@interface Torrent (Private)
36
37- (id) initWithHash: (NSString *) hashString path: (NSString *) path lib: (tr_handle_t *) lib
38        publicTorrent: (NSNumber *) publicTorrent
39        downloadFolder: (NSString *) downloadFolder
40        useIncompleteFolder: (NSNumber *) useIncompleteFolder incompleteFolder: (NSString *) incompleteFolder
41        dateAdded: (NSDate *) dateAdded dateCompleted: (NSDate *) dateCompleted
42        dateActivity: (NSDate *) dateActivity
43        ratioSetting: (NSNumber *) ratioSetting ratioLimit: (NSNumber *) ratioLimit
44        limitSpeedCustom: (NSNumber *) limitCustom
45        checkUpload: (NSNumber *) checkUpload uploadLimit: (NSNumber *) uploadLimit
46        checkDownload: (NSNumber *) checkDownload downloadLimit: (NSNumber *) downloadLimit
47                pex: (NSNumber *) pex
48        waitToStart: (NSNumber *) waitToStart orderValue: (NSNumber *) orderValue
49        filesShouldDownload: (NSArray *) filesShouldDownload filePriorities: (NSArray *) filePriorities;
50- (void) historyFilePriorities: (NSMutableArray *) history forItems: (NSArray *) items;
51
52- (BOOL) shouldUseIncompleteFolderForName: (NSString *) name;
53- (void) updateDownloadFolder;
54
55- (void) createFileListShouldDownload: (NSArray *) filesShouldDownload priorities: (NSArray *) filePriorities;
56- (void) insertPath: (NSMutableArray *) components forSiblings: (NSMutableArray *) siblings
57            withParent: (NSMutableDictionary *) parent previousPath: (NSString *) previousPath
58            flatList: (NSMutableArray *) flatList fileSize: (uint64_t) size index: (int) index priority: (int) priority;
59- (NSImage *) advancedBar;
60- (void) trashFile: (NSString *) path;
61
62@end
63
64@implementation Torrent
65
66// Used to optimize drawing. They contain packed RGBA pixels for every color needed.
67#define BE OSSwapBigToHostConstInt32
68
69static uint32_t kRed   = BE(0xFF6450FF), //255, 100, 80
70                kBlue = BE(0x50A0FFFF), //80, 160, 255
71                kBlue2 = BE(0x1E46B4FF), //30, 70, 180
72                kGray  = BE(0x969696FF), //150, 150, 150
73                kGreen1 = BE(0x99FFCCFF), //153, 255, 204
74                kGreen2 = BE(0x66FF99FF), //102, 255, 153
75                kGreen3 = BE(0x00FF66FF), //0, 255, 102
76                kWhite = BE(0xFFFFFFFF); //255, 255, 255
77
78- (id) initWithPath: (NSString *) path location: (NSString *) location forceDeleteTorrent: (BOOL) delete lib: (tr_handle_t *) lib
79{
80    self = [self initWithHash: nil path: path lib: lib
81            publicTorrent: delete ? [NSNumber numberWithBool: NO] : nil
82            downloadFolder: location
83            useIncompleteFolder: nil incompleteFolder: nil
84            dateAdded: nil dateCompleted: nil
85            dateActivity: nil
86            ratioSetting: nil ratioLimit: nil
87            limitSpeedCustom: nil
88            checkUpload: nil uploadLimit: nil
89            checkDownload: nil downloadLimit: nil
90                        pex: nil
91            waitToStart: nil orderValue: nil
92            filesShouldDownload: nil filePriorities: nil];
93   
94    if (self)
95    {
96        if (!fPublicTorrent)
97            [self trashFile: path];
98    }
99    return self;
100}
101
102- (id) initWithHistory: (NSDictionary *) history lib: (tr_handle_t *) lib
103{
104    self = [self initWithHash: [history objectForKey: @"TorrentHash"]
105                path: [history objectForKey: @"TorrentPath"] lib: lib
106                publicTorrent: [history objectForKey: @"PublicCopy"]
107                downloadFolder: [history objectForKey: @"DownloadFolder"]
108                useIncompleteFolder: [history objectForKey: @"UseIncompleteFolder"]
109                incompleteFolder: [history objectForKey: @"IncompleteFolder"]
110                dateAdded: [history objectForKey: @"Date"]
111                                dateCompleted: [history objectForKey: @"DateCompleted"]
112                dateActivity: [history objectForKey: @"DateActivity"]
113                ratioSetting: [history objectForKey: @"RatioSetting"]
114                ratioLimit: [history objectForKey: @"RatioLimit"]
115                limitSpeedCustom: [history objectForKey: @"LimitSpeedCustom"]
116                checkUpload: [history objectForKey: @"CheckUpload"]
117                uploadLimit: [history objectForKey: @"UploadLimit"]
118                checkDownload: [history objectForKey: @"CheckDownload"]
119                downloadLimit: [history objectForKey: @"DownloadLimit"]
120                                pex: [history objectForKey: @"Pex"]
121                waitToStart: [history objectForKey: @"WaitToStart"]
122                orderValue: [history objectForKey: @"OrderValue"]
123                filesShouldDownload: [history objectForKey: @"FilesShouldDownload"]
124                filePriorities: [history objectForKey: @"FilePriorities"]];
125   
126    if (self)
127    {
128        //start transfer
129        NSNumber * active;
130        if ((active = [history objectForKey: @"Active"]) && [active boolValue])
131        {
132            fStat = tr_torrentStat(fHandle);
133            [self startTransfer];
134        }
135    }
136    return self;
137}
138
139- (NSDictionary *) history
140{
141    NSMutableDictionary * history = [NSMutableDictionary dictionaryWithObjectsAndKeys:
142                    [NSNumber numberWithBool: fPublicTorrent], @"PublicCopy",
143                    [self hashString], @"TorrentHash",
144                    fDownloadFolder, @"DownloadFolder",
145                    [NSNumber numberWithBool: fUseIncompleteFolder], @"UseIncompleteFolder",
146                    [NSNumber numberWithBool: [self isActive]], @"Active",
147                    fDateAdded, @"Date",
148                    [NSNumber numberWithInt: fRatioSetting], @"RatioSetting",
149                    [NSNumber numberWithFloat: fRatioLimit], @"RatioLimit",
150                    [NSNumber numberWithInt: fCheckUpload], @"CheckUpload",
151                    [NSNumber numberWithInt: fUploadLimit], @"UploadLimit",
152                    [NSNumber numberWithInt: fCheckDownload], @"CheckDownload",
153                    [NSNumber numberWithInt: fDownloadLimit], @"DownloadLimit",
154                    [NSNumber numberWithBool: fWaitToStart], @"WaitToStart",
155                    [self orderValue], @"OrderValue",
156                    nil];
157   
158    if (fIncompleteFolder)
159        [history setObject: fIncompleteFolder forKey: @"IncompleteFolder"];
160   
161    //set file should download
162    int fileCount = [self fileCount];
163    NSMutableArray * filesShouldDownload = [NSMutableArray arrayWithCapacity: fileCount];
164   
165    tr_priority_t * priorities = tr_torrentGetFilePriorities(fHandle);
166    int i;
167    for (i = 0; i < fileCount; i++)
168        [filesShouldDownload addObject: [NSNumber numberWithBool: priorities[i] != TR_PRI_DND]];
169    free(priorities);
170    [history setObject: filesShouldDownload forKey: @"FilesShouldDownload"];
171   
172    //set file priorities
173    NSMutableArray * filePriorities = [NSMutableArray arrayWithCapacity: fileCount];
174    [self historyFilePriorities: filePriorities forItems: fFileList];
175    [history setObject: filePriorities forKey: @"FilePriorities"];
176
177    if (fPublicTorrent)
178        [history setObject: [self publicTorrentLocation] forKey: @"TorrentPath"];
179       
180        if (![self privateTorrent])
181                [history setObject: [NSNumber numberWithBool: fPex] forKey: @"Pex"];
182       
183        if (fDateCompleted)
184                [history setObject: fDateCompleted forKey: @"DateCompleted"];
185   
186    NSDate * dateActivity = [self dateActivity];
187    if (dateActivity)
188                [history setObject: dateActivity forKey: @"DateActivity"];
189       
190    return history;
191}
192
193- (void) dealloc
194{
195    #warning cleanup
196    if (fHandle)
197    {
198        if (fDownloadFolder)
199            [fDownloadFolder release];
200        if (fIncompleteFolder)
201            [fIncompleteFolder release];
202       
203        if (fPublicTorrentLocation)
204            [fPublicTorrentLocation release];
205       
206        [fDateAdded release];
207                if (fDateCompleted)
208                        [fDateCompleted release];
209        if (fDateActivity)
210                        [fDateActivity release];
211       
212        if (fAnnounceDate)
213            [fAnnounceDate release];
214       
215        [fIcon release];
216        [fIconFlipped release];
217        [fIconSmall release];
218       
219        [fProgressString release];
220        [fStatusString release];
221        [fShortStatusString release];
222        [fRemainingTimeString release];
223       
224        [fFileList release];
225        [fFlatFileList release];
226       
227        [fBitmap release];
228        free(fPieces);
229    }
230    [super dealloc];
231}
232
233- (void) closeTorrent
234{
235    tr_torrentClose(fHandle);
236}
237
238- (void) closeRemoveTorrent
239{
240    tr_torrentRemoveSaved(fHandle);
241    [self closeTorrent];
242}
243
244- (void) changeIncompleteDownloadFolder: (NSString *) folder
245{
246    fUseIncompleteFolder = folder != nil;
247    if (fIncompleteFolder)
248    {
249        [fIncompleteFolder release];
250        fIncompleteFolder = nil;
251    }
252   
253    if (folder)
254        fIncompleteFolder = [folder retain];
255   
256    [self updateDownloadFolder];
257}
258
259- (void) changeDownloadFolder: (NSString *) folder
260{
261    if (fDownloadFolder)
262        [fDownloadFolder release];
263    fDownloadFolder = [folder retain];
264   
265    [self updateDownloadFolder];
266}
267
268- (NSString *) downloadFolder
269{
270    return [NSString stringWithUTF8String: tr_torrentGetFolder(fHandle)];
271}
272
273- (void) getAvailability: (int8_t *) tab size: (int) size
274{
275    tr_torrentAvailability(fHandle, tab, size);
276}
277
278- (void) getAmountFinished: (float *) tab size: (int) size
279{
280    tr_torrentAmountFinished(fHandle, tab, size);
281}
282
283- (void) update
284{
285    fStat = tr_torrentStat(fHandle);
286   
287    //notification when downloading finished
288    if (tr_getComplete(fHandle) || tr_getDone(fHandle))
289    {
290        BOOL canMove = YES;
291       
292        //move file from incomplete folder to download folder
293        if (fUseIncompleteFolder && ![[self downloadFolder] isEqualToString: fDownloadFolder]
294            && (canMove = [self alertForMoveFolderAvailable]))
295        {
296            //pause without actually stopping
297            tr_setUseCustomUpload(fHandle, 1);
298            tr_setUploadLimit(fHandle, 0);
299           
300            tr_setUseCustomDownload(fHandle, 1);
301            tr_setDownloadLimit(fHandle, 0);
302           
303            if ([[NSFileManager defaultManager] movePath: [[self downloadFolder] stringByAppendingPathComponent: [self name]]
304                                    toPath: [fDownloadFolder stringByAppendingPathComponent: [self name]] handler: nil])
305                [self updateDownloadFolder];
306           
307            [self updateSpeedSetting];
308        }
309       
310        if (!canMove)
311        {
312            fUseIncompleteFolder = NO;
313           
314            [fDownloadFolder release];
315            fDownloadFolder = fIncompleteFolder;
316            fIncompleteFolder = nil;
317        }
318               
319                if (fDateCompleted)
320                        [fDateCompleted release];
321                fDateCompleted = [[NSDate alloc] init];
322       
323        fStat = tr_torrentStat(fHandle);
324        [[NSNotificationCenter defaultCenter] postNotificationName: @"TorrentFinishedDownloading" object: self];
325    }
326    else if (tr_getIncomplete(fHandle))
327        [[NSNotificationCenter defaultCenter] postNotificationName: @"TorrentRestartedDownloading" object: self];
328    else;
329   
330    //check to stop for ratio
331    float stopRatio;
332    if ([self isSeeding] && (stopRatio = [self actualStopRatio]) != INVALID && [self ratio] >= stopRatio)
333    {
334        [self stopTransfer];
335        fStat = tr_torrentStat(fHandle);
336       
337        fFinishedSeeding = YES;
338       
339        [self setRatioSetting: NSOffState];
340        [[NSNotificationCenter defaultCenter] postNotificationName: @"TorrentStoppedForRatio" object: self];
341    }
342       
343        NSMutableString * progressString = [NSMutableString stringWithString: @""],
344                                        * remainingTimeString = [NSMutableString stringWithString: @""],
345                                        * statusString = [NSMutableString string],
346                                        * shortStatusString = [NSMutableString string];
347
348    if (![self allDownloaded])
349        [progressString appendFormat: NSLocalizedString(@"%@ of %@ (%.2f%%)", "Torrent -> progress string"),
350                            [NSString stringForFileSize: [self downloadedValid]],
351                            [NSString stringForFileSize: [self size]], 100.0 * [self progress]];
352    else if ([self progress] < 1.0)
353        [progressString appendFormat: NSLocalizedString(@"%@ of %@ (%.2f%%), uploaded %@ (Ratio: %@)",
354                "Torrent -> progress string"),
355                [NSString stringForFileSize: [self downloadedValid]], [NSString stringForFileSize: [self size]],
356                100.0 * [self progress], [NSString stringForFileSize: [self uploadedTotal]],
357                [NSString stringForRatio: [self ratio]]];
358    else
359        [progressString appendFormat: NSLocalizedString(@"%@, uploaded %@ (Ratio: %@)", "Torrent -> progress string"),
360                [NSString stringForFileSize: [self size]], [NSString stringForFileSize: [self uploadedTotal]],
361                [NSString stringForRatio: [self ratio]]];
362
363    BOOL wasChecking = fChecking;
364    fChecking = NO;
365    switch (fStat->status)
366    {
367        NSString * tempString;
368       
369        case TR_STATUS_STOPPED:
370            if (fWaitToStart)
371            {
372                tempString = ![self allDownloaded]
373                        ? [NSLocalizedString(@"Waiting to download", "Torrent -> status string") stringByAppendingEllipsis]
374                        : [NSLocalizedString(@"Waiting to seed", "Torrent -> status string") stringByAppendingEllipsis];
375            }
376            else if (fFinishedSeeding)
377                tempString = NSLocalizedString(@"Seeding complete", "Torrent -> status string");
378            else
379                tempString = NSLocalizedString(@"Paused", "Torrent -> status string");
380           
381            [statusString setString: tempString];
382            [shortStatusString setString: tempString];
383           
384            break;
385
386        case TR_STATUS_CHECK_WAIT:
387            tempString = [NSLocalizedString(@"Waiting to check existing data", "Torrent -> status string")
388                            stringByAppendingEllipsis];
389           
390            [statusString setString: tempString];
391            [shortStatusString setString: tempString];
392            [remainingTimeString setString: tempString];
393           
394            fChecking = YES;
395           
396            break;
397
398        case TR_STATUS_CHECK:
399            tempString = [NSLocalizedString(@"Checking existing data", "Torrent -> status string") stringByAppendingEllipsis];
400           
401            [statusString setString: tempString];
402            [shortStatusString setString: tempString];
403            [remainingTimeString setString: tempString];
404           
405            fChecking = YES;
406           
407            break;
408
409        case TR_STATUS_DOWNLOAD:
410            [statusString setString: @""];
411            if ([self totalPeers] != 1)
412                [statusString appendFormat: NSLocalizedString(@"Downloading from %d of %d peers",
413                                                "Torrent -> status string"), [self peersUploading], [self totalPeers]];
414            else
415                [statusString appendFormat: NSLocalizedString(@"Downloading from %d of 1 peer",
416                                                "Torrent -> status string"), [self peersUploading]];
417           
418            int eta = [self eta];
419            if (eta < 0)
420            {
421                [remainingTimeString setString: NSLocalizedString(@"Unknown", "Torrent -> remaining time")];
422                [progressString appendString: NSLocalizedString(@" - remaining time unknown", "Torrent -> progress string")];
423            }
424            else
425            {
426                if (eta < 60)
427                    [remainingTimeString appendFormat: NSLocalizedString(@"%d sec", "Torrent -> remaining time"), eta];
428                else if (eta < 3600) //60 * 60
429                    [remainingTimeString appendFormat: NSLocalizedString(@"%d min %d sec", "Torrent -> remaining time"),
430                                                            eta / 60, eta % 60];
431                else if (eta < 86400) //24 * 60 * 60
432                    [remainingTimeString appendFormat: NSLocalizedString(@"%d hr %d min", "Torrent -> remaining time"),
433                                                            eta / 3600, (eta / 60) % 60];
434                else
435                {
436                                        int days = eta / 86400;
437                    if (days > 1)
438                        [remainingTimeString appendFormat: NSLocalizedString(@"%d days %d hr", "Torrent -> remaining time"),
439                                                                days, (eta / 3600) % 24];
440                    else
441                        [remainingTimeString appendFormat: NSLocalizedString(@"1 day %d hr", "Torrent -> remaining time"),
442                                                                (eta / 3600) % 24];
443                }
444               
445                [progressString appendFormat: NSLocalizedString(@" - %@ remaining", "Torrent -> progress string"),
446                                                                    remainingTimeString];
447            }
448           
449            break;
450
451        case TR_STATUS_SEED:
452        case TR_STATUS_DONE:
453            [statusString setString: @""];
454            if ([self totalPeers] != 1)
455                [statusString appendFormat: NSLocalizedString(@"Seeding to %d of %d peers", "Torrent -> status string"),
456                                                [self peersDownloading], [self totalPeers]];
457            else
458                [statusString appendFormat: NSLocalizedString(@"Seeding to %d of 1 peer", "Torrent -> status string"),
459                                                [self peersDownloading]];
460           
461            break;
462
463        case TR_STATUS_STOPPING:
464            tempString = [NSLocalizedString(@"Stopping", "Torrent -> status string") stringByAppendingEllipsis];
465       
466            [statusString setString: tempString];
467            [shortStatusString setString: tempString];
468           
469            break;
470    }
471   
472    //check for error
473    BOOL wasError = fError;
474    fError = [self isError];
475   
476    //check if stalled
477    BOOL wasStalled = fStalled;
478    fStalled = [self isActive] && [fDefaults boolForKey: @"CheckStalled"]
479                && [fDefaults integerForKey: @"StalledMinutes"] < [self stalledMinutes];
480   
481    //create strings for error or stalled
482    if (fError)
483        [statusString setString: [NSLocalizedString(@"Error: ", "Torrent -> status string")
484                                    stringByAppendingString: [self errorMessage]]];
485    else if (fStalled)
486        [statusString setString: [NSLocalizedString(@"Stalled, ", "Torrent -> status string")
487                                    stringByAppendingString: statusString]];
488    else;
489   
490    //update queue for checking (from downloading to seeding), stalled, or error
491    if ((wasChecking && !fChecking) || (!wasStalled && fStalled) || (!wasError && fError && [self isActive]))
492        [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateQueue" object: self];
493
494    if ([self isActive] && ![self isChecking])
495    {
496        NSString * stringToAppend = @"";
497        if (![self allDownloaded])
498        {
499            stringToAppend = [NSString stringWithFormat: NSLocalizedString(@"DL: %@, ", "Torrent -> status string"),
500                                [NSString stringForSpeed: [self downloadRate]]];
501            [shortStatusString setString: @""];
502        }
503        else
504        {
505            NSString * ratioString = [NSString stringForRatio: [self ratio]];
506       
507            [shortStatusString setString: [NSString stringWithFormat: NSLocalizedString(@"Ratio: %@, ",
508                                            "Torrent -> status string"), ratioString]];
509            [remainingTimeString setString: [NSLocalizedString(@"Ratio: ", "Torrent -> status string")
510                                                stringByAppendingString: ratioString]];
511        }
512       
513        stringToAppend = [stringToAppend stringByAppendingString: [NSLocalizedString(@"UL: ", "Torrent -> status string")
514                                            stringByAppendingString: [NSString stringForSpeed: [self uploadRate]]]];
515
516        [statusString appendFormat: @" - %@", stringToAppend];
517        [shortStatusString appendString: stringToAppend];
518    }
519       
520        [fProgressString setString: progressString];
521        [fStatusString setString: statusString];
522        [fShortStatusString setString: shortStatusString];
523        [fRemainingTimeString setString: remainingTimeString];
524}
525
526- (NSDictionary *) infoForCurrentView
527{
528    NSMutableDictionary * info = [NSMutableDictionary dictionaryWithObjectsAndKeys:
529                                    [self name], @"Name",
530                                    [NSNumber numberWithBool: [self isSeeding]], @"Seeding",
531                                    [NSNumber numberWithFloat: [self progress]], @"Progress",
532                                    [NSNumber numberWithFloat: (float)fStat->left/[self size]], @"Left",
533                                    [NSNumber numberWithBool: [self isActive]], @"Active",
534                                    [NSNumber numberWithBool: [self isError]], @"Error", nil];
535   
536    if ([self isSeeding])
537        [info setObject: [NSNumber numberWithFloat: [self progressStopRatio]] forKey: @"ProgressStopRatio"];
538   
539    if (![fDefaults boolForKey: @"SmallView"])
540    {
541        [info setObject: fIconFlipped forKey: @"Icon"];
542        [info setObject: [self progressString] forKey: @"ProgressString"];
543        [info setObject: [self statusString] forKey: @"StatusString"];
544    }
545    else
546    {
547        [info setObject: fIconSmall forKey: @"Icon"];
548        [info setObject: [self remainingTimeString] forKey: @"RemainingTimeString"];
549        [info setObject: [self shortStatusString] forKey: @"ShortStatusString"];
550    }
551   
552    if ([fDefaults boolForKey: @"UseAdvancedBar"])
553        [info setObject: [self advancedBar] forKey: @"AdvancedBar"];
554   
555    return info;
556}
557
558- (void) startTransfer
559{
560    fWaitToStart = NO;
561    fFinishedSeeding = NO;
562   
563    if (![self isActive] && [self alertForFolderAvailable] && [self alertForRemainingDiskSpace])
564    {
565        tr_torrentStart(fHandle);
566        [self update];
567    }
568}
569
570- (void) stopTransfer
571{
572    fError = NO;
573    fWaitToStart = NO;
574   
575    if ([self isActive])
576    {
577        tr_torrentStop(fHandle);
578        [self update];
579
580        [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateQueue" object: self];
581    }
582}
583
584- (void) sleep
585{
586    if ((fResumeOnWake = [self isActive]))
587        tr_torrentStop(fHandle);
588}
589
590- (void) wakeUp
591{
592    if (fResumeOnWake)
593        tr_torrentStart(fHandle);
594}
595
596- (void) announce
597{
598    if (![self isActive])
599        return;
600   
601    tr_manualUpdate(fHandle);
602   
603    if (fAnnounceDate)
604        [fAnnounceDate release];
605    fAnnounceDate = [[NSDate date] retain];
606}
607
608- (NSDate *) announceDate
609{
610    return fAnnounceDate;
611}
612
613- (void) resetCache
614{
615    tr_torrentRecheck(fHandle);
616}
617
618- (float) ratio
619{
620    return fStat->ratio;
621}
622
623- (int) ratioSetting
624{
625    return fRatioSetting;
626}
627
628- (void) setRatioSetting: (int) setting
629{
630    fRatioSetting = setting;
631}
632
633- (float) ratioLimit
634{
635    return fRatioLimit;
636}
637
638- (void) setRatioLimit: (float) limit
639{
640    if (limit >= 0)
641        fRatioLimit = limit;
642}
643
644- (float) actualStopRatio
645{
646    if (fRatioSetting == NSOnState)
647        return fRatioLimit;
648    else if (fRatioSetting == NSMixedState && [fDefaults boolForKey: @"RatioCheck"])
649        return [fDefaults floatForKey: @"RatioLimit"];
650    else
651        return INVALID;
652}
653
654- (float) progressStopRatio
655{
656    float stopRatio, ratio;
657    if ((stopRatio = [self actualStopRatio]) == INVALID || (ratio = [self ratio]) >= stopRatio)
658        return 1.0;
659    else if (ratio > 0 && stopRatio > 0)
660        return ratio / stopRatio;
661    else
662        return 0;
663}
664
665- (int) checkUpload
666{
667    return fCheckUpload;
668}
669
670- (void) setCheckUpload: (int) setting
671{
672    fCheckUpload = setting;
673    [self updateSpeedSetting];
674}
675
676- (int) uploadLimit
677{
678    return fUploadLimit;
679}
680
681- (void) setUploadLimit: (int) limit
682{
683    fUploadLimit = limit;
684    [self updateSpeedSetting];
685}
686
687- (int) checkDownload
688{
689    return fCheckDownload;
690}
691
692- (void) setCheckDownload: (int) setting
693{
694    fCheckDownload = setting;
695    [self updateSpeedSetting];
696}
697
698- (int) downloadLimit
699{
700    return fDownloadLimit;
701}
702
703- (void) setDownloadLimit: (int) limit
704{
705    fDownloadLimit = limit;
706    [self updateSpeedSetting];
707}
708
709- (void) updateSpeedSetting
710{
711    tr_setUseCustomUpload(fHandle, fCheckUpload != NSMixedState);
712    tr_setUploadLimit(fHandle, fCheckUpload == NSOnState ? fUploadLimit : -1);
713   
714    tr_setUseCustomDownload(fHandle, fCheckDownload != NSMixedState);
715    tr_setDownloadLimit(fHandle, fCheckDownload == NSOnState ? fDownloadLimit : -1);
716}
717
718- (void) setWaitToStart: (BOOL) wait
719{
720    fWaitToStart = wait;
721}
722
723- (BOOL) waitingToStart
724{
725    return fWaitToStart;
726}
727
728- (void) revealData
729{
730    [[NSWorkspace sharedWorkspace] selectFile: [self dataLocation] inFileViewerRootedAtPath: nil];
731}
732
733- (void) revealPublicTorrent
734{
735    if (fPublicTorrent)
736        [[NSWorkspace sharedWorkspace] selectFile: fPublicTorrentLocation inFileViewerRootedAtPath: nil];
737}
738
739- (void) trashData
740{
741    [self trashFile: [self dataLocation]];
742}
743
744- (void) trashTorrent
745{
746    if (fPublicTorrent)
747        [self trashFile: [self publicTorrentLocation]];
748}
749
750- (void) moveTorrentDataFileTo: (NSString *) folder
751{
752    NSString * oldFolder = [self downloadFolder];
753    if (![oldFolder isEqualToString: folder] || ![fDownloadFolder isEqualToString: folder])
754    {
755        //check if moving inside itself
756        NSArray * oldComponents = [oldFolder pathComponents],
757                * newComponents = [folder pathComponents];
758        int count;
759       
760        if ((count = [oldComponents count]) < [newComponents count]
761                && [[newComponents objectAtIndex: count] isEqualToString: [self name]]
762                && [oldComponents isEqualToArray:
763                        [newComponents objectsAtIndexes: [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, count)]]])
764        {
765            NSAlert * alert = [[NSAlert alloc] init];
766            [alert setMessageText: NSLocalizedString(@"A folder cannot be moved to inside itself.",
767                                                        "Move inside itself alert -> title")];
768            [alert setInformativeText: [NSString stringWithFormat:
769                            NSLocalizedString(@"The move operation of \"%@\" cannot be done.",
770                                                "Move inside itself alert -> message"), [self name]]];
771            [alert addButtonWithTitle: NSLocalizedString(@"OK", "Move inside itself alert -> button")];
772           
773            [alert runModal];
774            [alert release];
775           
776            return;
777        }
778       
779        //pause without actually stopping
780        tr_setDownloadLimit(fHandle, 0);
781        tr_setUploadLimit(fHandle, 0);
782       
783        if ([[NSFileManager defaultManager] movePath: [oldFolder stringByAppendingPathComponent: [self name]]
784                            toPath: [folder stringByAppendingPathComponent: [self name]] handler: nil])
785        {
786            //get rid of both incomplete folder and old download folder, even if move failed
787            fUseIncompleteFolder = NO;
788            if (fIncompleteFolder)
789            {
790                [fIncompleteFolder release];
791                fIncompleteFolder = nil;
792            }
793            [self changeDownloadFolder: folder];
794           
795            [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateInfoSettings" object: nil];
796           
797            [self updateSpeedSetting];
798        }
799        else
800        {
801            [self updateSpeedSetting]; //restart before showing the alert
802       
803            NSAlert * alert = [[NSAlert alloc] init];
804            [alert setMessageText: NSLocalizedString(@"There was an error moving the data file.", "Move error alert -> title")];
805            [alert setInformativeText: [NSString stringWithFormat:
806                            NSLocalizedString(@"The move operation of \"%@\" cannot be done.",
807                                                "Move error alert -> message"), [self name]]];
808            [alert addButtonWithTitle: NSLocalizedString(@"OK", "Move error alert -> button")];
809           
810            [alert runModal];
811            [alert release];
812        }
813    }
814}
815
816- (void) copyTorrentFileTo: (NSString *) path
817{
818    [[NSFileManager defaultManager] copyPath: [self torrentLocation] toPath: path handler: nil];
819}
820
821- (BOOL) alertForRemainingDiskSpace
822{
823    if ([self allDownloaded] || ![fDefaults boolForKey: @"WarningRemainingSpace"])
824        return YES;
825   
826    NSString * volumeName;
827    if ((volumeName = [[[NSFileManager defaultManager] componentsToDisplayForPath: [self downloadFolder]] objectAtIndex: 0]))
828    {
829        NSDictionary * fsAttributes = [[NSFileManager defaultManager] fileSystemAttributesAtPath: [self downloadFolder]];
830        uint64_t remainingSpace = [[fsAttributes objectForKey: NSFileSystemFreeSize] unsignedLongLongValue];
831       
832        if (remainingSpace <= fStat->left)
833        {
834            NSAlert * alert = [[NSAlert alloc] init];
835            [alert setMessageText: [NSString stringWithFormat:
836                                    NSLocalizedString(@"Not enough remaining disk space to download \"%@\" completely.",
837                                        "Torrent file disk space alert -> title"), [self name]]];
838            [alert setInformativeText: [NSString stringWithFormat:
839                            NSLocalizedString(@"The transfer will be paused. Clear up space on \"%@\" to continue.",
840                                                "Torrent file disk space alert -> message"), volumeName]];
841            [alert addButtonWithTitle: NSLocalizedString(@"OK", "Torrent file disk space alert -> button")];
842            [alert addButtonWithTitle: NSLocalizedString(@"Download Anyway", "Torrent file disk space alert -> button")];
843            [alert addButtonWithTitle: NSLocalizedString(@"Always Download", "Torrent file disk space alert -> button")];
844           
845            int result = [alert runModal];
846            [alert release];
847           
848            if (result == NSAlertThirdButtonReturn)
849                [fDefaults setBool: NO forKey: @"WarningRemainingSpace"];
850           
851            return result != NSAlertFirstButtonReturn;
852        }
853    }
854    return YES;
855}
856
857- (BOOL) alertForFolderAvailable
858{
859    if (access(tr_torrentGetFolder(fHandle), 0))
860    {
861        NSAlert * alert = [[NSAlert alloc] init];
862        [alert setMessageText: [NSString stringWithFormat:
863                                NSLocalizedString(@"The folder for downloading \"%@\" cannot be found.",
864                                    "Folder cannot be found alert -> title"), [self name]]];
865        [alert setInformativeText: [NSString stringWithFormat:
866                        NSLocalizedString(@"\"%@\" cannot be found. The transfer will be paused.",
867                                            "Folder cannot be found alert -> message"), [self downloadFolder]]];
868        [alert addButtonWithTitle: NSLocalizedString(@"OK", "Folder cannot be found alert -> button")];
869        [alert addButtonWithTitle: [NSLocalizedString(@"Choose New Location",
870                                    "Folder cannot be found alert -> location button") stringByAppendingEllipsis]];
871       
872        if ([alert runModal] != NSAlertFirstButtonReturn)
873        {
874            NSOpenPanel * panel = [NSOpenPanel openPanel];
875           
876            [panel setPrompt: NSLocalizedString(@"Select", "Folder cannot be found alert -> prompt")];
877            [panel setAllowsMultipleSelection: NO];
878            [panel setCanChooseFiles: NO];
879            [panel setCanChooseDirectories: YES];
880            [panel setCanCreateDirectories: YES];
881
882            [panel setMessage: [NSString stringWithFormat: NSLocalizedString(@"Select the download folder for \"%@\"",
883                                "Folder cannot be found alert -> select destination folder"), [self name]]];
884           
885            [[NSNotificationCenter defaultCenter] postNotificationName: @"MakeWindowKey" object: nil];
886            [panel beginSheetForDirectory: nil file: nil types: nil modalForWindow: [NSApp keyWindow] modalDelegate: self
887                    didEndSelector: @selector(destinationChoiceClosed:returnCode:contextInfo:) contextInfo: nil];
888        }
889       
890        [alert release];
891       
892        return NO;
893    }
894   
895    return YES;
896}
897
898- (void) destinationChoiceClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) context
899{
900    if (code != NSOKButton)
901        return;
902   
903    NSString * folder = [[openPanel filenames] objectAtIndex: 0];
904    if (fUseIncompleteFolder)
905        [self changeDownloadFolder: folder];
906    else
907        [self changeDownloadFolder: folder];
908   
909    [self startTransfer];
910    [self update];
911   
912    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateInfoSettings" object: nil];
913}
914
915- (BOOL) alertForMoveFolderAvailable
916{
917    if (access([fDownloadFolder UTF8String], 0))
918    {
919        NSAlert * alert = [[NSAlert alloc] init];
920        [alert setMessageText: [NSString stringWithFormat:
921                                NSLocalizedString(@"The folder for moving the completed \"%@\" cannot be found.",
922                                    "Move folder cannot be found alert -> title"), [self name]]];
923        [alert setInformativeText: [NSString stringWithFormat:
924                                NSLocalizedString(@"\"%@\" cannot be found. The file will remain in its current location.",
925                                    "Move folder cannot be found alert -> message"), fDownloadFolder]];
926        [alert addButtonWithTitle: NSLocalizedString(@"OK", "Move folder cannot be found alert -> button")];
927       
928        [alert runModal];
929        [alert release];
930       
931        return NO;
932    }
933   
934    return YES;
935}
936
937- (NSImage *) icon
938{
939    return fIcon;
940}
941
942- (NSImage *) iconFlipped
943{
944    return fIconFlipped;
945}
946
947- (NSImage *) iconSmall
948{
949    return fIconSmall;
950}
951
952- (NSString *) name
953{
954    return [NSString stringWithUTF8String: fInfo->name];
955}
956
957- (uint64_t) size
958{
959    return fInfo->totalSize;
960}
961
962- (NSString *) trackerAddress
963{
964    return [NSString stringWithFormat: @"http://%s:%d", fStat->tracker->address, fStat->tracker->port];
965}
966
967- (NSString *) trackerAddressAnnounce
968{
969    return [NSString stringWithUTF8String: fStat->tracker->announce];
970}
971
972- (NSString *) comment
973{
974    return [NSString stringWithUTF8String: fInfo->comment];
975}
976
977- (NSString *) creator
978{
979    return [NSString stringWithUTF8String: fInfo->creator];
980}
981
982- (NSDate *) dateCreated
983{
984    int date = fInfo->dateCreated;
985    return date > 0 ? [NSDate dateWithTimeIntervalSince1970: date] : nil;
986}
987
988- (int) pieceSize
989{
990    return fInfo->pieceSize;
991}
992
993- (int) pieceCount
994{
995    return fInfo->pieceCount;
996}
997
998- (NSString *) hashString
999{
1000    return [NSString stringWithUTF8String: fInfo->hashString];
1001}
1002
1003- (BOOL) privateTorrent
1004{
1005    return TR_FLAG_PRIVATE & fInfo->flags;
1006}
1007
1008- (NSString *) torrentLocation
1009{
1010    return [NSString stringWithUTF8String: fInfo->torrent];
1011}
1012
1013- (NSString *) publicTorrentLocation
1014{
1015    return fPublicTorrentLocation;
1016}
1017
1018- (NSString *) dataLocation
1019{
1020    return [[self downloadFolder] stringByAppendingPathComponent: [self name]];
1021}
1022
1023- (BOOL) publicTorrent
1024{
1025    return fPublicTorrent;
1026}
1027
1028- (NSString *) stateString
1029{
1030    switch( fStat->status )
1031    {
1032        case TR_STATUS_STOPPED:
1033            return NSLocalizedString(@"Paused", "Torrent -> status string");
1034            break;
1035
1036        case TR_STATUS_CHECK:
1037            return [NSLocalizedString(@"Checking existing data", "Torrent -> status string") stringByAppendingEllipsis];
1038            break;
1039
1040        case TR_STATUS_DOWNLOAD:
1041            return NSLocalizedString(@"Downloading", "Torrent -> status string");
1042            break;
1043
1044        case TR_STATUS_SEED:
1045        case TR_STATUS_DONE:
1046            return NSLocalizedString(@"Seeding", "Torrent -> status string");
1047            break;
1048
1049        case TR_STATUS_STOPPING:
1050            return [NSLocalizedString(@"Stopping", "Torrent -> status string") stringByAppendingEllipsis];
1051            break;
1052       
1053        default:
1054            return NSLocalizedString(@"N/A", "Torrent -> status string");
1055    }
1056}
1057
1058- (float) progress
1059{
1060    return fStat->percentComplete;
1061}
1062
1063- (float) progressDone
1064{
1065    return fStat->percentDone;
1066}
1067
1068- (int) eta
1069{
1070    return fStat->eta;
1071}
1072
1073- (BOOL) isActive
1074{
1075    return fStat->status & TR_STATUS_ACTIVE;
1076}
1077
1078- (BOOL) isSeeding
1079{
1080    return fStat->status == TR_STATUS_SEED || fStat->status == TR_STATUS_DONE;
1081}
1082
1083- (BOOL) isPaused
1084{
1085    return fStat->status == TR_STATUS_STOPPED;
1086}
1087
1088- (BOOL) isChecking
1089{
1090    return fStat->status == TR_STATUS_CHECK || fStat->status == TR_STATUS_CHECK_WAIT;
1091}
1092
1093- (BOOL) allDownloaded
1094{
1095    return fStat->cpStatus != TR_CP_INCOMPLETE;
1096}
1097
1098- (BOOL) isError
1099{
1100    return fStat->error != 0;
1101}
1102
1103- (NSString *) errorMessage
1104{
1105    if (![self isError])
1106        return @"";
1107   
1108    NSString * error;
1109    if (!(error = [NSString stringWithUTF8String: fStat->errorString])
1110        && !(error = [NSString stringWithCString: fStat->errorString encoding: NSISOLatin1StringEncoding]))
1111        error = NSLocalizedString(@"(unreadable error)", "Torrent -> error string unreadable");
1112   
1113    return error;
1114}
1115
1116- (NSArray *) peers
1117{
1118    int totalPeers, i;
1119    tr_peer_stat_t * peers = tr_torrentPeers(fHandle, & totalPeers);
1120   
1121    NSMutableArray * peerDics = [NSMutableArray arrayWithCapacity: totalPeers];
1122    NSMutableDictionary * dic;
1123   
1124    tr_peer_stat_t * peer;
1125    for (i = 0; i < totalPeers; i++)
1126    {
1127        peer = &peers[i];
1128       
1129        dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:
1130            [NSNumber numberWithBool: peer->isConnected], @"Connected",
1131            [NSNumber numberWithInt: peer->from], @"From",
1132            [NSString stringWithCString: (char *) peer->addr encoding: NSUTF8StringEncoding], @"IP",
1133            [NSNumber numberWithInt: peer->port], @"Port", nil];
1134       
1135        if (peer->isConnected)
1136        {
1137            [dic setObject: [NSNumber numberWithFloat: peer->progress] forKey: @"Progress"];
1138           
1139            if (peer->isDownloading)
1140                [dic setObject: [NSNumber numberWithFloat: peer->uploadToRate] forKey: @"UL To Rate"];
1141            if (peer->isUploading)
1142                [dic setObject: [NSNumber numberWithFloat: peer->downloadFromRate] forKey: @"DL From Rate"];
1143           
1144            [dic setObject: [NSString stringWithCString: (char *) peer->client encoding: NSUTF8StringEncoding] forKey: @"Client"];
1145        }
1146        else
1147            [dic setObject: @"" forKey: @"Client"];
1148       
1149        [peerDics addObject: dic];
1150    }
1151   
1152    tr_torrentPeersFree(peers, totalPeers);
1153   
1154    return peerDics;
1155}
1156
1157- (NSString *) progressString
1158{
1159    return fProgressString;
1160}
1161
1162- (NSString *) statusString
1163{
1164    return fStatusString;
1165}
1166
1167- (NSString *) shortStatusString
1168{
1169    return fShortStatusString;
1170}
1171
1172- (NSString *) remainingTimeString
1173{
1174    return fRemainingTimeString;
1175}
1176
1177- (int) seeders
1178{
1179    return fStat->seeders;
1180}
1181
1182- (int) leechers
1183{
1184    return fStat->leechers;
1185}
1186
1187- (int) completedFromTracker
1188{
1189    return fStat->completedFromTracker;
1190}
1191
1192- (int) totalPeers
1193{
1194    return fStat->peersTotal;
1195}
1196
1197- (int) totalPeersTracker
1198{
1199    return fStat->peersFrom[TR_PEER_FROM_TRACKER];
1200}
1201
1202- (int) totalPeersIncoming
1203{
1204    return fStat->peersFrom[TR_PEER_FROM_INCOMING];
1205}
1206
1207- (int) totalPeersCache
1208{
1209    return fStat->peersFrom[TR_PEER_FROM_CACHE];
1210}
1211
1212- (int) totalPeersPex
1213{
1214    return fStat->peersFrom[TR_PEER_FROM_PEX];
1215}
1216
1217//peers uploading to you
1218- (int) peersUploading
1219{
1220    return fStat->peersUploading;
1221}
1222
1223//peers downloading from you
1224- (int) peersDownloading
1225{
1226    return fStat->peersDownloading;
1227}
1228
1229- (float) downloadRate
1230{
1231    return fStat->rateDownload;
1232}
1233
1234- (float) uploadRate
1235{
1236    return fStat->rateUpload;
1237}
1238
1239- (uint64_t) downloadedValid
1240{
1241    return fStat->downloadedValid;
1242}
1243
1244- (uint64_t) downloadedTotal
1245{
1246    return fStat->downloaded;
1247}
1248
1249- (uint64_t) uploadedTotal
1250{
1251    return fStat->uploaded;
1252}
1253
1254- (float) swarmSpeed
1255{
1256    return fStat->swarmspeed;
1257}
1258
1259- (BOOL) pex
1260{
1261        return fPex;
1262}
1263
1264- (void) setPex: (BOOL) setting
1265{
1266        if (![self privateTorrent])
1267        {
1268                fPex = setting;
1269                tr_torrentDisablePex(fHandle, !setting);
1270        }
1271}
1272
1273- (NSNumber *) orderValue
1274{
1275    return [NSNumber numberWithInt: fOrderValue];
1276}
1277
1278- (void) setOrderValue: (int) orderValue
1279{
1280    fOrderValue = orderValue;
1281}
1282
1283- (NSArray *) fileList
1284{
1285    return fFileList;
1286}
1287
1288- (int) fileCount
1289{
1290    return fInfo->fileCount;
1291}
1292
1293- (float) fileProgress: (int) index
1294{
1295    return tr_torrentFileCompletion(fHandle, index);
1296}
1297
1298- (int) checkForFiles: (NSIndexSet *) indexSet
1299{
1300    BOOL onState = NO, offState = NO;
1301    int index;
1302    for (index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index])
1303    {
1304        if (tr_torrentGetFilePriority(fHandle, index) != TR_PRI_DND
1305                || ![self canChangeDownloadCheckForFiles: [NSIndexSet indexSetWithIndex: index]])
1306            onState = YES;
1307        else
1308            offState = YES;
1309       
1310        if (onState == offState)
1311            return NSMixedState;
1312    }
1313    return onState ? NSOnState : NSOffState;
1314}
1315
1316- (BOOL) canChangeDownloadCheckForFiles: (NSIndexSet *) indexSet
1317{
1318    if ([self fileCount] <= 1)
1319        return NO;
1320   
1321    int index;
1322    for (index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index])
1323        if ([self fileProgress: index] < 1.0)
1324            return YES;
1325    return NO;
1326}
1327
1328- (void) setFileCheckState: (int) state forIndexes: (NSIndexSet *) indexSet
1329{
1330    int index;
1331    for (index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index])
1332    {
1333        tr_priority_t actualPriority;
1334        if (state == NSOnState)
1335        {
1336            int priority = [[[fFlatFileList objectAtIndex: index] objectForKey: @"Priority"] intValue];
1337            if (priority == PRIORITY_HIGH)
1338                actualPriority = TR_PRI_HIGH;
1339            else if (priority == PRIORITY_LOW)
1340                actualPriority = TR_PRI_LOW;
1341            else
1342                actualPriority = TR_PRI_NORMAL;
1343        }
1344        else
1345            actualPriority = TR_PRI_DND;
1346       
1347        tr_torrentSetFilePriority(fHandle, index, actualPriority);
1348    }
1349   
1350    [self update];
1351    if ([self isPaused])
1352        [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateQueue" object: self]; //for paused torrents
1353}
1354
1355- (void) setFilePriority: (int) priority forIndexes: (NSIndexSet *) indexSet
1356{
1357    NSNumber * priorityValue = [NSNumber numberWithInt: priority];
1358   
1359    int index;
1360    for (index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index])
1361    {
1362        [[fFlatFileList objectAtIndex: index] setObject: priorityValue forKey: @"Priority"];
1363       
1364        if ([self checkForFiles: [NSIndexSet indexSetWithIndex: index]] == NSOnState)
1365        {
1366            tr_priority_t actualPriority;
1367            if (priority == PRIORITY_HIGH)
1368                actualPriority = TR_PRI_HIGH;
1369            else if (priority == PRIORITY_LOW)
1370                actualPriority = TR_PRI_LOW;
1371            else
1372                actualPriority = TR_PRI_NORMAL;
1373            tr_torrentSetFilePriority(fHandle, index, actualPriority);
1374        }
1375    }
1376}
1377
1378- (BOOL) hasFilePriority: (int) priority forIndexes: (NSIndexSet *) indexSet
1379{
1380    int index;
1381    for (index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index])
1382        if (priority == [[[fFlatFileList objectAtIndex: index] objectForKey: @"Priority"] intValue]
1383                && [self canChangeDownloadCheckForFiles: [NSIndexSet indexSetWithIndex: index]])
1384            return YES;
1385    return NO;
1386}
1387
1388- (NSDate *) dateAdded
1389{
1390    return fDateAdded;
1391}
1392
1393- (NSDate *) dateCompleted
1394{
1395    return fDateCompleted;
1396}
1397
1398- (NSDate *) dateActivity
1399{
1400    uint64_t date = fStat->activityDate / 1000;
1401    return date != 0 ? [NSDate dateWithTimeIntervalSince1970: date] : fDateActivity;
1402}
1403
1404- (int) stalledMinutes
1405{
1406    uint64_t start;
1407    if ((start = fStat->startDate) == 0)
1408        return -1;
1409   
1410    NSDate * started = [NSDate dateWithTimeIntervalSince1970: start / 1000],
1411            * activity = [self dateActivity];
1412    if (!activity || [started compare: activity] == NSOrderedDescending)
1413        return -1 * [started timeIntervalSinceNow] / 60;
1414    else
1415        return -1 * [activity timeIntervalSinceNow] / 60;
1416}
1417
1418- (BOOL) isStalled
1419{
1420    return fStalled;
1421}
1422
1423- (NSNumber *) stateSortKey
1424{
1425    if (![self isActive])
1426        return [NSNumber numberWithInt: 0];
1427    else if ([self isSeeding])
1428        return [NSNumber numberWithInt: 1];
1429    else
1430        return [NSNumber numberWithInt: 2];
1431}
1432
1433- (NSNumber *) progressSortKey
1434{
1435    float progress;
1436    if ((progress = [self progress]) >= 1.0)
1437       progress += [self progressStopRatio];
1438   
1439    return [NSNumber numberWithFloat: progress];
1440}
1441
1442- (NSNumber *) ratioSortKey
1443{
1444    return [NSNumber numberWithFloat: [self ratio]];
1445}
1446
1447- (int) torrentID
1448{
1449    return fID;
1450}
1451
1452- (const tr_info_t *) torrentInfo
1453{
1454    return fInfo;
1455}
1456
1457- (tr_stat_t *) torrentStat
1458{
1459    return fStat;
1460}
1461
1462@end
1463
1464@implementation Torrent (Private)
1465
1466//if a hash is given, attempt to load that; otherwise, attempt to open file at path
1467- (id) initWithHash: (NSString *) hashString path: (NSString *) path lib: (tr_handle_t *) lib
1468        publicTorrent: (NSNumber *) publicTorrent
1469        downloadFolder: (NSString *) downloadFolder
1470        useIncompleteFolder: (NSNumber *) useIncompleteFolder incompleteFolder: (NSString *) incompleteFolder
1471        dateAdded: (NSDate *) dateAdded dateCompleted: (NSDate *) dateCompleted
1472        dateActivity: (NSDate *) dateActivity
1473        ratioSetting: (NSNumber *) ratioSetting ratioLimit: (NSNumber *) ratioLimit
1474        limitSpeedCustom: (NSNumber *) limitCustom
1475        checkUpload: (NSNumber *) checkUpload uploadLimit: (NSNumber *) uploadLimit
1476        checkDownload: (NSNumber *) checkDownload downloadLimit: (NSNumber *) downloadLimit
1477                pex: (NSNumber *) pex
1478        waitToStart: (NSNumber *) waitToStart orderValue: (NSNumber *) orderValue
1479        filesShouldDownload: (NSArray *) filesShouldDownload filePriorities: (NSArray *) filePriorities;
1480{
1481    if (!(self = [super init]))
1482        return nil;
1483   
1484    static_lastid++;
1485    fID = static_lastid;
1486   
1487    fLib = lib;
1488    fDefaults = [NSUserDefaults standardUserDefaults];
1489
1490    fPublicTorrent = path && (publicTorrent ? [publicTorrent boolValue] : ![fDefaults boolForKey: @"DeleteOriginalTorrent"]);
1491    if (fPublicTorrent)
1492        fPublicTorrentLocation = [path retain];
1493   
1494    fDownloadFolder = downloadFolder ? downloadFolder : [fDefaults stringForKey: @"DownloadFolder"];
1495    fDownloadFolder = [[fDownloadFolder stringByExpandingTildeInPath] retain];
1496   
1497    fUseIncompleteFolder = useIncompleteFolder ? [useIncompleteFolder boolValue]
1498                                : [fDefaults boolForKey: @"UseIncompleteDownloadFolder"];
1499    if (fUseIncompleteFolder)
1500    {
1501        fIncompleteFolder = incompleteFolder ? incompleteFolder : [fDefaults stringForKey: @"IncompleteDownloadFolder"];
1502        fIncompleteFolder = [[fIncompleteFolder stringByExpandingTildeInPath] retain];
1503    }
1504   
1505    NSString * currentDownloadFolder;
1506    tr_info_t info;
1507    int error;
1508    if (hashString)
1509    {
1510        if (tr_torrentParseHash(fLib, [hashString UTF8String], NULL, &info) == TR_OK)
1511        {
1512            currentDownloadFolder = [self shouldUseIncompleteFolderForName: [NSString stringWithUTF8String: info.name]]
1513                                        ? fIncompleteFolder : fDownloadFolder;
1514            fHandle = tr_torrentInitSaved(fLib, [hashString UTF8String], [currentDownloadFolder UTF8String],
1515                                            TR_FLAG_SAVE | TR_FLAG_PAUSED, &error);
1516        }
1517        tr_metainfoFree(&info);
1518    }
1519    if (!fHandle && path)
1520    {
1521        if (tr_torrentParse(fLib, [path UTF8String], NULL, &info) == TR_OK)
1522        {
1523            currentDownloadFolder = [self shouldUseIncompleteFolderForName: [NSString stringWithUTF8String: info.name]]
1524                                        ? fIncompleteFolder : fDownloadFolder;
1525            fHandle = tr_torrentInit(fLib, [path UTF8String], [currentDownloadFolder UTF8String],
1526                                        TR_FLAG_SAVE | TR_FLAG_PAUSED, &error);
1527        }
1528        tr_metainfoFree(&info);
1529    }
1530    if (!fHandle)
1531    {
1532        [self release];
1533        return nil;
1534    }
1535   
1536    fInfo = tr_torrentInfo(fHandle);
1537   
1538    NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
1539    [nc addObserver: self selector: @selector(updateSpeedSetting:)
1540                name: @"UpdateSpeedSetting" object: nil];
1541
1542    fDateAdded = dateAdded ? [dateAdded retain] : [[NSDate alloc] init];
1543        if (dateCompleted)
1544                fDateCompleted = [dateCompleted retain];
1545    if (dateActivity)
1546                fDateActivity = [dateActivity retain];
1547       
1548    fRatioSetting = ratioSetting ? [ratioSetting intValue] : NSMixedState;
1549    fRatioLimit = ratioLimit ? [ratioLimit floatValue] : [fDefaults floatForKey: @"RatioLimit"];
1550    fFinishedSeeding = NO;
1551   
1552    fCheckUpload = checkUpload ? [checkUpload intValue] : NSMixedState;
1553    fUploadLimit = uploadLimit ? [uploadLimit intValue] : [fDefaults integerForKey: @"UploadLimit"];
1554    fCheckDownload = checkDownload ? [checkDownload intValue] : NSMixedState;
1555    fDownloadLimit = downloadLimit ? [downloadLimit intValue] : [fDefaults integerForKey: @"DownloadLimit"];
1556    [self updateSpeedSetting];
1557       
1558        if ([self privateTorrent])
1559                fPex = NO;
1560        else
1561                fPex = pex ? [pex boolValue] : YES;
1562        tr_torrentDisablePex(fHandle, !fPex);
1563   
1564    fWaitToStart = waitToStart ? [waitToStart boolValue] : [fDefaults boolForKey: @"AutoStartDownload"];
1565    fOrderValue = orderValue ? [orderValue intValue] : tr_torrentCount(fLib) - 1;
1566    fError = NO;
1567   
1568    fIcon = [[[NSWorkspace sharedWorkspace] iconForFileType: fInfo->multifile ? NSFileTypeForHFSTypeCode('fldr')
1569                                                : [[self name] pathExtension]] retain];
1570   
1571    fIconFlipped = [fIcon copy];
1572    [fIconFlipped setFlipped: YES];
1573   
1574    fIconSmall = [fIconFlipped copy];
1575    [fIconSmall setScalesWhenResized: YES];
1576    [fIconSmall setSize: NSMakeSize(16.0, 16.0)];
1577
1578    fProgressString = [[NSMutableString alloc] initWithCapacity: 50];
1579    fStatusString = [[NSMutableString alloc] initWithCapacity: 75];
1580    fShortStatusString = [[NSMutableString alloc] initWithCapacity: 30];
1581    fRemainingTimeString = [[NSMutableString alloc] initWithCapacity: 30];
1582   
1583    [self createFileListShouldDownload: filesShouldDownload priorities: filePriorities];
1584   
1585    //set up advanced bar
1586    fBitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: nil
1587        pixelsWide: MAX_PIECES pixelsHigh: BAR_HEIGHT bitsPerSample: 8 samplesPerPixel: 4 hasAlpha: YES
1588        isPlanar: NO colorSpaceName: NSCalibratedRGBColorSpace bytesPerRow: 0 bitsPerPixel: 0];
1589   
1590    fPieces = malloc(MAX_PIECES);
1591    int i;
1592    for (i = 0; i < MAX_PIECES; i++)
1593        fPieces[i] = BLANK_PIECE;
1594   
1595    [self update];
1596    return self;
1597}
1598
1599- (void) createFileListShouldDownload: (NSArray *) filesShouldDownload priorities: (NSArray *) filePriorities
1600{
1601    int count = [self fileCount], i;
1602    tr_file_t * file;
1603    NSMutableArray * pathComponents;
1604    NSString * path;
1605    int priority;
1606    tr_priority_t actualPriority;
1607   
1608    NSMutableArray * fileList = [[NSMutableArray alloc] init],
1609                    * flatFileList = [[NSMutableArray alloc] initWithCapacity: count];
1610   
1611    for (i = 0; i < count; i++)
1612    {
1613        file = &fInfo->files[i];
1614       
1615        pathComponents = [[[NSString stringWithUTF8String: file->name] pathComponents] mutableCopy];
1616        if (fInfo->multifile)
1617        {
1618            path = [pathComponents objectAtIndex: 0];
1619            [pathComponents removeObjectAtIndex: 0];
1620        }
1621        else
1622            path = @"";
1623       
1624        priority = filePriorities ? [[filePriorities objectAtIndex: i] intValue] : PRIORITY_NORMAL;
1625        [self insertPath: pathComponents forSiblings: fileList withParent: nil previousPath: path
1626                flatList: flatFileList fileSize: file->length index: i priority: priority];
1627        [pathComponents autorelease];
1628       
1629        if (!filesShouldDownload || [[filesShouldDownload objectAtIndex: i] boolValue])
1630        {
1631            if (priority == PRIORITY_HIGH)
1632                actualPriority = TR_PRI_HIGH;
1633            else if (priority == PRIORITY_LOW)
1634                actualPriority = TR_PRI_LOW;
1635            else
1636                actualPriority = TR_PRI_NORMAL;
1637        }
1638        else
1639            actualPriority = TR_PRI_DND;
1640       
1641        tr_torrentSetFilePriority(fHandle, i, actualPriority);
1642    }
1643   
1644    fFileList = [[NSArray alloc] initWithArray: fileList];
1645    [fileList release];
1646    fFlatFileList = [[NSArray alloc] initWithArray: flatFileList];
1647    [flatFileList release];
1648}
1649
1650- (void) insertPath: (NSMutableArray *) components forSiblings: (NSMutableArray *) siblings
1651        withParent: (NSMutableDictionary *) parent previousPath: (NSString *) previousPath
1652        flatList: (NSMutableArray *) flatList fileSize: (uint64_t) size index: (int) index priority: (int) priority
1653{
1654    NSString * name = [components objectAtIndex: 0];
1655    BOOL isFolder = [components count] > 1;
1656   
1657    NSMutableDictionary * dict = nil;
1658    if (isFolder)
1659    {
1660        NSEnumerator * enumerator = [siblings objectEnumerator];
1661        while ((dict = [enumerator nextObject]))
1662            if ([[dict objectForKey: @"Name"] isEqualToString: name] && [[dict objectForKey: @"IsFolder"] boolValue])
1663                break;
1664    }
1665   
1666    NSString * currentPath = [previousPath stringByAppendingPathComponent: name];
1667   
1668    //create new folder or item if it doesn't already exist
1669    if (!dict)
1670    {
1671        dict = [NSMutableDictionary dictionaryWithObjectsAndKeys: name, @"Name",
1672                [NSNumber numberWithBool: isFolder], @"IsFolder", currentPath, @"Path", nil];
1673        [siblings addObject: dict];
1674       
1675        if (isFolder)
1676        {
1677            [dict setObject: [NSMutableArray array] forKey: @"Children"];
1678            [dict setObject: [NSMutableIndexSet indexSetWithIndex: index] forKey: @"Indexes"];
1679        }
1680        else
1681        {
1682            [dict setObject: [NSIndexSet indexSetWithIndex: index] forKey: @"Indexes"];
1683            [dict setObject: [NSNumber numberWithUnsignedLongLong: size] forKey: @"Size"];
1684            [dict setObject: [[NSWorkspace sharedWorkspace] iconForFileType: [name pathExtension]] forKey: @"Icon"];
1685            [dict setObject: [NSNumber numberWithInt: priority] forKey: @"Priority"];
1686           
1687            [flatList addObject: dict];
1688        }
1689       
1690        if (parent)
1691            [dict setObject: parent forKey: @"Parent"];
1692    }
1693    else
1694        [[dict objectForKey: @"Indexes"] addIndex: index];
1695   
1696    if (isFolder)
1697    {
1698        [components removeObjectAtIndex: 0];
1699        [self insertPath: components forSiblings: [dict objectForKey: @"Children"]
1700            withParent: dict previousPath: currentPath flatList: flatList fileSize: size index: index priority: priority];
1701    }
1702}
1703
1704- (void) historyFilePriorities: (NSMutableArray *) history forItems: (NSArray *) items
1705{
1706    NSEnumerator * enumerator = [items objectEnumerator];
1707    NSDictionary * item;
1708    while ((item = [enumerator nextObject]))
1709    {
1710        if (![[item objectForKey: @"IsFolder"] boolValue])
1711            [history addObject: [item objectForKey: @"Priority"]];
1712        else
1713            [self historyFilePriorities: history forItems: [item objectForKey: @"Children"]];
1714    }
1715}
1716
1717- (BOOL) shouldUseIncompleteFolderForName: (NSString *) name
1718{
1719    return fUseIncompleteFolder &&
1720        ![[NSFileManager defaultManager] fileExistsAtPath: [fDownloadFolder stringByAppendingPathComponent: name]];
1721}
1722
1723- (void) updateDownloadFolder
1724{
1725    NSString * folder = [self shouldUseIncompleteFolderForName: [self name]] ? fIncompleteFolder : fDownloadFolder;
1726    tr_torrentSetFolder(fHandle, [folder UTF8String]);
1727}
1728
1729#warning move?
1730- (NSImage *) advancedBar
1731{
1732    uint32_t * p;
1733    uint8_t * bitmapData = [fBitmap bitmapData];
1734    int bytesPerRow = [fBitmap bytesPerRow];
1735   
1736    int pieceCount = [self pieceCount];
1737    int8_t * piecesAvailablity = malloc(pieceCount);
1738    [self getAvailability: piecesAvailablity size: pieceCount];
1739   
1740    //lines 2 to 14: blue, green, or gray depending on piece availability
1741    int i, h, index = 0;
1742    float increment = (float)pieceCount / (float)MAX_PIECES, indexValue = 0;
1743    uint32_t color;
1744    BOOL change;
1745    for (i = 0; i < MAX_PIECES; i++)
1746    {
1747        change = NO;
1748        if (piecesAvailablity[index] < 0)
1749        {
1750            if (fPieces[i] != -1)
1751            {
1752                color = kBlue;
1753                fPieces[i] = -1;
1754                change = YES;
1755            }
1756        }
1757        else if (piecesAvailablity[index] == 0)
1758        {
1759            if (fPieces[i] != 0)
1760            {
1761                color = kGray;
1762                fPieces[i] = 0;
1763                change = YES;
1764            }
1765        }
1766        else if (piecesAvailablity[index] <= 4)
1767        {
1768            if (fPieces[i] != 1)
1769            {
1770                color = kGreen1;
1771                fPieces[i] = 1;
1772                change = YES;
1773            }
1774        }
1775        else if (piecesAvailablity[index] <= 8)
1776        {
1777            if (fPieces[i] != 2)
1778            {
1779                color = kGreen2;
1780                fPieces[i] = 2;
1781                change = YES;
1782            }
1783        }
1784        else
1785        {
1786            if (fPieces[i] != 3)
1787            {
1788                color = kGreen3;
1789                fPieces[i] = 3;
1790                change = YES;
1791            }
1792        }
1793       
1794        if (change)
1795        {
1796            //point to pixel (i, 2) and draw "vertically"
1797            p = (uint32_t *)(bitmapData + 2 * bytesPerRow) + i;
1798            for (h = 2; h < BAR_HEIGHT; h++)
1799            {
1800                p[0] = color;
1801                p = (uint32_t *)((uint8_t *)p + bytesPerRow);
1802            }
1803        }
1804       
1805        indexValue += increment;
1806        index = (int)indexValue;
1807    }
1808   
1809    //determine percentage finished and available
1810    int have = rintf((float)MAX_PIECES * [self progress]), avail;
1811    if ([self progress] >= 1.0 || ![self isActive] || [self totalPeers] <= 0)
1812        avail = 0;
1813    else
1814    {
1815        float * piecesFinished = malloc(pieceCount * sizeof(float));
1816        [self getAmountFinished: piecesFinished size: pieceCount];
1817       
1818        float available = 0;
1819        for (i = 0; i < pieceCount; i++)
1820            if (piecesAvailablity[i] > 0)
1821                available += 1.0 - piecesFinished[i];
1822       
1823        avail = rintf((float)MAX_PIECES * available / (float)pieceCount);
1824        if (have + avail > MAX_PIECES) //case if both end in .5 and all pieces are available
1825            avail--;
1826       
1827        free(piecesFinished);
1828    }
1829   
1830    free(piecesAvailablity);
1831   
1832    //first two lines: dark blue to show progression, green to show available
1833    p = (uint32_t *)bitmapData;
1834    for (i = 0; i < have; i++)
1835    {
1836        p[i] = kBlue2;
1837        p[i + bytesPerRow / 4] = kBlue2;
1838    }
1839    for (; i < avail + have; i++)
1840    {
1841        p[i] = kGreen3;
1842        p[i + bytesPerRow / 4] = kGreen3;
1843    }
1844    for (; i < MAX_PIECES; i++)
1845    {
1846        p[i] = kWhite;
1847        p[i + bytesPerRow / 4] = kWhite;
1848    }
1849   
1850    //actually draw image
1851    NSImage * bar = [[NSImage alloc] initWithSize: [fBitmap size]];
1852    [bar addRepresentation: fBitmap];
1853    [bar setScalesWhenResized: YES];
1854   
1855    return [bar autorelease];
1856}
1857
1858- (void) trashFile: (NSString *) path
1859{
1860    //attempt to move to trash
1861    if (![[NSWorkspace sharedWorkspace] performFileOperation: NSWorkspaceRecycleOperation
1862            source: [path stringByDeletingLastPathComponent] destination: @""
1863            files: [NSArray arrayWithObject: [path lastPathComponent]] tag: nil])
1864    {
1865        //if cannot trash, just delete it (will work if it is on a remote volume)
1866        if (![[NSFileManager defaultManager] removeFileAtPath: path handler: nil])
1867            NSLog(@"Could not trash %@", path);
1868    }
1869}
1870
1871@end
Note: See TracBrowser for help on using the repository browser.