source: trunk/macosx/Torrent.m @ 2225

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

hopefully fix a nasty memory leak and a bunch of redundant code

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