source: trunk/macosx/Torrent.m @ 2347

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

use file status instead of file progress for canChangeDownloadCheckForFiles:

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