source: trunk/macosx/Torrent.m @ 2363

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

get the mac os build building again

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