source: trunk/macosx/Torrent.m @ 2251

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

fix the warning for remaining size coming up when it shouldn't

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