2006-03-23 12:39:39 +00:00
|
|
|
/******************************************************************************
|
2006-05-29 21:21:23 +00:00
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006 Transmission authors and contributors
|
2006-03-23 12:39:39 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#import "Torrent.h"
|
|
|
|
#import "StringAdditions.h"
|
|
|
|
|
|
|
|
@interface Torrent (Private)
|
|
|
|
|
2006-06-15 02:07:50 +00:00
|
|
|
- (id) initWithHash: (NSString *) hashString path: (NSString *) path lib: (tr_handle_t *) lib
|
2006-06-15 05:30:08 +00:00
|
|
|
privateTorrent: (NSNumber *) privateTorrent publicTorrent: (NSNumber *) publicTorrent
|
2006-06-15 02:07:50 +00:00
|
|
|
date: (NSDate *) date stopRatioSetting: (NSNumber *) stopRatioSetting
|
|
|
|
ratioLimit: (NSNumber *) ratioLimit;
|
2006-06-09 19:53:35 +00:00
|
|
|
|
2006-06-14 03:00:27 +00:00
|
|
|
- (void) trashFile: (NSString *) path;
|
2006-03-23 12:39:39 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
@implementation Torrent
|
|
|
|
|
2006-05-25 12:22:19 +00:00
|
|
|
- (id) initWithPath: (NSString *) path lib: (tr_handle_t *) lib
|
2006-03-23 12:39:39 +00:00
|
|
|
{
|
2006-06-15 06:24:03 +00:00
|
|
|
self = [self initWithHash: nil path: path lib: lib
|
|
|
|
privateTorrent: nil publicTorrent: nil
|
|
|
|
date: nil stopRatioSetting: nil ratioLimit: nil];
|
|
|
|
|
|
|
|
if (self)
|
2006-06-15 02:07:50 +00:00
|
|
|
{
|
|
|
|
if (!fPublicTorrent)
|
|
|
|
[self trashFile: path];
|
|
|
|
}
|
2006-06-15 05:30:08 +00:00
|
|
|
return self;
|
2006-05-25 12:22:19 +00:00
|
|
|
}
|
2006-03-23 12:39:39 +00:00
|
|
|
|
2006-05-25 12:22:19 +00:00
|
|
|
- (id) initWithHistory: (NSDictionary *) history lib: (tr_handle_t *) lib
|
|
|
|
{
|
2006-06-15 06:24:03 +00:00
|
|
|
self = [self initWithHash: [history objectForKey: @"TorrentHash"]
|
2006-06-15 05:30:08 +00:00
|
|
|
path: [history objectForKey: @"TorrentPath"] lib: lib
|
|
|
|
privateTorrent: [history objectForKey: @"PrivateCopy"]
|
|
|
|
publicTorrent: [history objectForKey: @"PublicCopy"]
|
|
|
|
date: [history objectForKey: @"Date"]
|
2006-06-15 05:03:32 +00:00
|
|
|
stopRatioSetting: [history objectForKey: @"StopRatioSetting"]
|
2006-06-15 06:24:03 +00:00
|
|
|
ratioLimit: [history objectForKey: @"RatioLimit"]];
|
|
|
|
|
|
|
|
if (self)
|
2006-03-23 12:39:39 +00:00
|
|
|
{
|
2006-05-25 12:22:19 +00:00
|
|
|
NSString * downloadFolder;
|
|
|
|
if (!(downloadFolder = [history objectForKey: @"DownloadFolder"]))
|
|
|
|
downloadFolder = [[fDefaults stringForKey: @"DownloadFolder"]
|
|
|
|
stringByExpandingTildeInPath];
|
2006-06-06 18:05:57 +00:00
|
|
|
[self setDownloadFolder: downloadFolder];
|
2006-05-25 12:22:19 +00:00
|
|
|
|
|
|
|
NSString * paused;
|
|
|
|
if (!(paused = [history objectForKey: @"Paused"]) || [paused isEqualToString: @"NO"])
|
|
|
|
[self start];
|
2006-03-23 12:39:39 +00:00
|
|
|
}
|
2006-06-15 05:30:08 +00:00
|
|
|
return self;
|
2006-03-23 12:39:39 +00:00
|
|
|
}
|
|
|
|
|
2006-05-25 12:22:19 +00:00
|
|
|
- (NSDictionary *) history
|
2006-04-25 16:31:19 +00:00
|
|
|
{
|
2006-06-09 19:53:35 +00:00
|
|
|
NSMutableDictionary * history = [NSMutableDictionary dictionaryWithObjectsAndKeys:
|
|
|
|
[NSNumber numberWithBool: fPrivateTorrent], @"PrivateCopy",
|
2006-06-15 02:07:50 +00:00
|
|
|
[NSNumber numberWithBool: fPublicTorrent], @"PublicCopy",
|
2006-06-09 19:53:35 +00:00
|
|
|
[self downloadFolder], @"DownloadFolder",
|
|
|
|
[self isActive] ? @"NO" : @"YES", @"Paused",
|
|
|
|
[self date], @"Date",
|
|
|
|
[NSNumber numberWithInt: fStopRatioSetting], @"StopRatioSetting",
|
|
|
|
[NSNumber numberWithFloat: fRatioLimit], @"RatioLimit", nil];
|
|
|
|
|
|
|
|
if (fPrivateTorrent)
|
|
|
|
[history setObject: [self hashString] forKey: @"TorrentHash"];
|
2006-06-15 02:07:50 +00:00
|
|
|
|
|
|
|
if (fPublicTorrent)
|
2006-06-15 02:35:40 +00:00
|
|
|
[history setObject: [self publicTorrentLocation] forKey: @"TorrentPath"];
|
2006-06-09 19:53:35 +00:00
|
|
|
|
|
|
|
return history;
|
2006-04-25 16:31:19 +00:00
|
|
|
}
|
|
|
|
|
2006-03-23 12:39:39 +00:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
if( fHandle )
|
|
|
|
{
|
|
|
|
tr_torrentClose( fLib, fHandle );
|
2006-05-25 12:22:19 +00:00
|
|
|
|
2006-06-15 02:07:50 +00:00
|
|
|
if (fPublicTorrentLocation)
|
|
|
|
[fPublicTorrentLocation release];
|
|
|
|
|
2006-04-25 16:31:19 +00:00
|
|
|
[fDate release];
|
2006-03-23 12:39:39 +00:00
|
|
|
[fIcon release];
|
2006-06-06 18:05:57 +00:00
|
|
|
[fIconFlipped release];
|
|
|
|
[fProgressString release];
|
2006-03-23 12:39:39 +00:00
|
|
|
[fStatusString release];
|
|
|
|
}
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2006-06-06 18:05:57 +00:00
|
|
|
- (void) setDownloadFolder: (NSString *) path
|
2006-03-23 12:39:39 +00:00
|
|
|
{
|
|
|
|
tr_torrentSetFolder( fHandle, [path UTF8String] );
|
|
|
|
}
|
|
|
|
|
2006-06-06 18:05:57 +00:00
|
|
|
- (NSString *) downloadFolder
|
2006-03-23 12:39:39 +00:00
|
|
|
{
|
|
|
|
return [NSString stringWithUTF8String: tr_torrentGetFolder( fHandle )];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) getAvailability: (int8_t *) tab size: (int) size
|
|
|
|
{
|
|
|
|
tr_torrentAvailability( fHandle, tab, size );
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) update
|
|
|
|
{
|
|
|
|
fStat = tr_torrentStat( fHandle );
|
2006-05-25 12:22:19 +00:00
|
|
|
|
2006-06-15 02:07:50 +00:00
|
|
|
if ([self isSeeding]
|
|
|
|
&& (fStopRatioSetting == RATIO_CHECK && [self ratio] >= fRatioLimit)
|
|
|
|
|| (fStopRatioSetting == RATIO_GLOBAL && [fDefaults boolForKey: @"RatioCheck"]
|
|
|
|
&& [self ratio] >= [fDefaults floatForKey: @"RatioLimit"]))
|
|
|
|
{
|
|
|
|
[self stop];
|
|
|
|
[self setStopRatioSetting: RATIO_NO_CHECK];
|
|
|
|
fFinishedSeeding = YES;
|
|
|
|
|
|
|
|
fStat = tr_torrentStat( fHandle );
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:
|
|
|
|
@"TorrentRatioChanged" object: self];
|
|
|
|
}
|
|
|
|
|
2006-06-06 18:05:57 +00:00
|
|
|
[fProgressString setString: @""];
|
|
|
|
if ([self progress] < 1.0)
|
2006-06-09 19:53:35 +00:00
|
|
|
[fProgressString appendFormat: @"%@ of %@ (%.2f%%)", [NSString stringForFileSize:
|
2006-06-16 00:35:11 +00:00
|
|
|
[self downloadedValid]], [NSString stringForFileSize: [self size]], 100.0 * [self progress]];
|
2006-06-06 18:05:57 +00:00
|
|
|
else
|
|
|
|
[fProgressString appendFormat: @"%@, uploaded %@ (ratio: %@)", [NSString stringForFileSize:
|
2006-06-14 03:00:27 +00:00
|
|
|
[self size]], [NSString stringForFileSize: [self uploadedTotal]],
|
|
|
|
[NSString stringForRatioWithDownload: [self downloadedTotal] upload: [self uploadedTotal]]];
|
2006-03-23 12:39:39 +00:00
|
|
|
|
|
|
|
switch( fStat->status )
|
|
|
|
{
|
|
|
|
case TR_STATUS_PAUSE:
|
2006-06-12 23:36:25 +00:00
|
|
|
[fStatusString setString: fFinishedSeeding ? @"Seeding Complete" : @"Paused"];
|
2006-03-23 12:39:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_STATUS_CHECK:
|
2006-06-14 20:08:51 +00:00
|
|
|
[fStatusString setString: [@"Checking existing files" stringByAppendingEllipsis]];
|
2006-03-23 12:39:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_STATUS_DOWNLOAD:
|
2006-06-06 18:05:57 +00:00
|
|
|
[fStatusString setString: @""];
|
|
|
|
[fStatusString appendFormat:
|
|
|
|
@"Downloading from %d of %d peer%s",
|
|
|
|
[self peersUploading], [self totalPeers],
|
|
|
|
([self totalPeers] == 1) ? "" : "s"];
|
|
|
|
|
|
|
|
int eta = fStat->eta;
|
|
|
|
if (eta < 0)
|
|
|
|
[fProgressString appendString: @" - remaining time unknown"];
|
2006-03-23 12:39:39 +00:00
|
|
|
else
|
|
|
|
{
|
2006-06-06 18:05:57 +00:00
|
|
|
if (eta < 60)
|
|
|
|
[fProgressString appendFormat: @" - %d sec remaining", eta];
|
|
|
|
else if (eta < 3600)
|
|
|
|
[fProgressString appendFormat: @" - %d min %02d sec remaining",
|
|
|
|
eta / 60, eta % 60];
|
|
|
|
else
|
|
|
|
[fProgressString appendFormat: @" - %d hr %02d min remaining",
|
|
|
|
eta / 3600, (eta / 60) % 60];
|
2006-03-23 12:39:39 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_STATUS_SEED:
|
2006-06-06 18:05:57 +00:00
|
|
|
[fStatusString setString: @""];
|
2006-03-23 12:39:39 +00:00
|
|
|
[fStatusString appendFormat:
|
2006-06-06 18:05:57 +00:00
|
|
|
@"Seeding to %d of %d peer%s",
|
|
|
|
[self peersDownloading], [self totalPeers],
|
|
|
|
([self totalPeers] == 1) ? "" : "s"];
|
2006-03-23 12:39:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_STATUS_STOPPING:
|
2006-06-14 20:08:51 +00:00
|
|
|
[fStatusString setString: [@"Stopping" stringByAppendingEllipsis]];
|
2006-03-23 12:39:39 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-06-06 18:05:57 +00:00
|
|
|
|
2006-04-08 15:56:26 +00:00
|
|
|
if( fStat->error & TR_ETRACKER )
|
2006-03-23 12:39:39 +00:00
|
|
|
{
|
2006-06-06 18:05:57 +00:00
|
|
|
[fStatusString setString: [@"Error: " stringByAppendingString:
|
2006-04-22 16:44:56 +00:00
|
|
|
[NSString stringWithUTF8String: fStat->trackerError]]];
|
2006-03-23 12:39:39 +00:00
|
|
|
}
|
|
|
|
|
2006-06-06 18:05:57 +00:00
|
|
|
if ([self isActive])
|
2006-03-23 12:39:39 +00:00
|
|
|
{
|
2006-06-07 14:42:54 +00:00
|
|
|
[fStatusString appendString: @" - "];
|
2006-06-06 18:05:57 +00:00
|
|
|
if ([self progress] < 1.0)
|
2006-06-07 14:42:54 +00:00
|
|
|
[fStatusString appendFormat: @"DL: %@, ", [NSString stringForSpeed: [self downloadRate]]];
|
2006-06-06 18:05:57 +00:00
|
|
|
[fStatusString appendString: [@"UL: " stringByAppendingString:
|
|
|
|
[NSString stringForSpeed: [self uploadRate]]]];
|
2006-03-23 12:39:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) start
|
|
|
|
{
|
|
|
|
if( fStat->status & TR_STATUS_INACTIVE )
|
|
|
|
{
|
|
|
|
tr_torrentStart( fHandle );
|
2006-06-12 23:36:25 +00:00
|
|
|
fFinishedSeeding = NO;
|
2006-03-23 12:39:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) stop
|
|
|
|
{
|
|
|
|
if( fStat->status & TR_STATUS_ACTIVE )
|
|
|
|
{
|
|
|
|
tr_torrentStop( fHandle );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-09 19:53:35 +00:00
|
|
|
- (void) removeForever
|
|
|
|
{
|
|
|
|
if (fInfo->flags & TR_FSAVEPRIVATE)
|
|
|
|
tr_torrentRemoveSaved(fHandle);
|
|
|
|
}
|
|
|
|
|
2006-03-23 12:39:39 +00:00
|
|
|
- (void) sleep
|
|
|
|
{
|
2006-04-22 16:44:56 +00:00
|
|
|
if( ( fResumeOnWake = ( fStat->status & TR_STATUS_ACTIVE ) ) )
|
2006-03-23 12:39:39 +00:00
|
|
|
{
|
|
|
|
[self stop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) wakeUp
|
|
|
|
{
|
|
|
|
if( fResumeOnWake )
|
|
|
|
{
|
|
|
|
[self start];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-25 12:22:19 +00:00
|
|
|
- (float) ratio
|
|
|
|
{
|
2006-06-14 03:00:27 +00:00
|
|
|
float downloaded = [self downloadedTotal];
|
|
|
|
return downloaded > 0 ? (float)[self uploadedTotal] / downloaded : -1;
|
2006-05-25 12:22:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (int) stopRatioSetting
|
|
|
|
{
|
|
|
|
return fStopRatioSetting;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setStopRatioSetting: (int) setting
|
|
|
|
{
|
|
|
|
fStopRatioSetting = setting;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (float) ratioLimit
|
|
|
|
{
|
|
|
|
return fRatioLimit;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setRatioLimit: (float) limit
|
|
|
|
{
|
|
|
|
if (limit >= 0)
|
|
|
|
fRatioLimit = limit;
|
|
|
|
}
|
|
|
|
|
2006-03-23 12:39:39 +00:00
|
|
|
- (void) reveal
|
|
|
|
{
|
2006-06-06 18:05:57 +00:00
|
|
|
[[NSWorkspace sharedWorkspace] selectFile: [self dataLocation]
|
2006-03-23 12:39:39 +00:00
|
|
|
inFileViewerRootedAtPath: nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) trashData
|
|
|
|
{
|
2006-06-14 03:00:27 +00:00
|
|
|
[self trashFile: [self dataLocation]];
|
2006-03-23 12:39:39 +00:00
|
|
|
}
|
|
|
|
|
2006-06-15 02:07:50 +00:00
|
|
|
- (void) trashTorrent
|
|
|
|
{
|
|
|
|
if (fPublicTorrent)
|
|
|
|
[self trashFile: [self publicTorrentLocation]];
|
|
|
|
}
|
|
|
|
|
2006-03-23 12:39:39 +00:00
|
|
|
- (NSImage *) icon
|
|
|
|
{
|
|
|
|
return fIcon;
|
|
|
|
}
|
|
|
|
|
2006-06-06 18:05:57 +00:00
|
|
|
- (NSImage *) iconFlipped
|
2006-03-23 12:39:39 +00:00
|
|
|
{
|
2006-06-06 18:05:57 +00:00
|
|
|
return fIconFlipped;
|
2006-03-23 12:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) name
|
|
|
|
{
|
|
|
|
return [NSString stringWithUTF8String: fInfo->name];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (uint64_t) size
|
|
|
|
{
|
|
|
|
return fInfo->totalSize;
|
|
|
|
}
|
|
|
|
|
2006-03-27 15:09:29 +00:00
|
|
|
- (NSString *) tracker
|
|
|
|
{
|
|
|
|
return [NSString stringWithFormat: @"%s:%d",
|
|
|
|
fInfo->trackerAddress, fInfo->trackerPort];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) announce
|
|
|
|
{
|
|
|
|
return [NSString stringWithUTF8String: fInfo->trackerAnnounce];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int) pieceSize
|
|
|
|
{
|
|
|
|
return fInfo->pieceSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int) pieceCount
|
|
|
|
{
|
|
|
|
return fInfo->pieceCount;
|
|
|
|
}
|
|
|
|
|
2006-06-06 18:05:57 +00:00
|
|
|
- (NSString *) hashString
|
2006-03-27 15:09:29 +00:00
|
|
|
{
|
2006-06-09 19:53:35 +00:00
|
|
|
return [NSString stringWithUTF8String: fInfo->hashString];
|
2006-03-27 15:09:29 +00:00
|
|
|
}
|
|
|
|
|
2006-06-06 18:05:57 +00:00
|
|
|
- (NSString *) torrentLocation
|
|
|
|
{
|
2006-06-09 19:53:35 +00:00
|
|
|
return [NSString stringWithUTF8String: fInfo->torrent];
|
2006-06-06 18:05:57 +00:00
|
|
|
}
|
|
|
|
|
2006-06-15 02:07:50 +00:00
|
|
|
- (NSString *) publicTorrentLocation
|
|
|
|
{
|
|
|
|
return fPublicTorrentLocation;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) publicTorrent
|
|
|
|
{
|
|
|
|
return fPublicTorrent;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) privateTorrent
|
|
|
|
{
|
|
|
|
return fPrivateTorrent;
|
|
|
|
}
|
|
|
|
|
2006-06-06 18:05:57 +00:00
|
|
|
- (NSString *) dataLocation
|
|
|
|
{
|
|
|
|
return [[self downloadFolder] stringByAppendingPathComponent: [self name]];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) state
|
|
|
|
{
|
|
|
|
switch( fStat->status )
|
|
|
|
{
|
|
|
|
case TR_STATUS_PAUSE:
|
|
|
|
return @"Paused";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_STATUS_CHECK:
|
2006-06-14 20:08:51 +00:00
|
|
|
return [@"Checking existing files" stringByAppendingEllipsis];
|
2006-06-06 18:05:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_STATUS_DOWNLOAD:
|
|
|
|
return @"Downloading";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_STATUS_SEED:
|
|
|
|
return @"Seeding";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_STATUS_STOPPING:
|
2006-06-14 20:08:51 +00:00
|
|
|
return [@"Stopping" stringByAppendingEllipsis];
|
2006-06-06 18:05:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return @"N/A";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-23 12:39:39 +00:00
|
|
|
- (float) progress
|
|
|
|
{
|
|
|
|
return fStat->progress;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isActive
|
|
|
|
{
|
|
|
|
return ( fStat->status & TR_STATUS_ACTIVE );
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isSeeding
|
|
|
|
{
|
|
|
|
return ( fStat->status == TR_STATUS_SEED );
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isPaused
|
|
|
|
{
|
|
|
|
return ( fStat->status == TR_STATUS_PAUSE );
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) justFinished
|
|
|
|
{
|
|
|
|
return tr_getFinished( fHandle );
|
|
|
|
}
|
|
|
|
|
2006-06-06 18:05:57 +00:00
|
|
|
- (NSString *) progressString
|
|
|
|
{
|
|
|
|
return fProgressString;
|
|
|
|
}
|
|
|
|
|
2006-03-23 12:39:39 +00:00
|
|
|
- (NSString *) statusString
|
|
|
|
{
|
|
|
|
return fStatusString;
|
|
|
|
}
|
|
|
|
|
2006-06-06 18:05:57 +00:00
|
|
|
- (int) seeders
|
2006-03-23 12:39:39 +00:00
|
|
|
{
|
2006-06-06 18:05:57 +00:00
|
|
|
return fStat->seeders;
|
2006-03-23 12:39:39 +00:00
|
|
|
}
|
|
|
|
|
2006-06-06 18:05:57 +00:00
|
|
|
- (int) leechers
|
2006-03-23 12:39:39 +00:00
|
|
|
{
|
2006-06-06 18:05:57 +00:00
|
|
|
return fStat->leechers;
|
2006-03-23 12:39:39 +00:00
|
|
|
}
|
|
|
|
|
2006-06-06 18:05:57 +00:00
|
|
|
- (int) totalPeers
|
2006-03-23 12:39:39 +00:00
|
|
|
{
|
2006-06-06 18:05:57 +00:00
|
|
|
return fStat->peersTotal;
|
2006-03-23 12:39:39 +00:00
|
|
|
}
|
|
|
|
|
2006-06-06 18:05:57 +00:00
|
|
|
//peers uploading to you
|
|
|
|
- (int) peersUploading
|
2006-03-27 15:09:29 +00:00
|
|
|
{
|
2006-06-06 18:05:57 +00:00
|
|
|
return fStat->peersUploading;
|
2006-03-27 15:09:29 +00:00
|
|
|
}
|
|
|
|
|
2006-06-06 18:05:57 +00:00
|
|
|
//peers downloading from you
|
|
|
|
- (int) peersDownloading
|
2006-03-27 15:09:29 +00:00
|
|
|
{
|
2006-06-06 18:05:57 +00:00
|
|
|
return fStat->peersDownloading;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (float) downloadRate
|
|
|
|
{
|
|
|
|
return fStat->rateDownload;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (float) uploadRate
|
|
|
|
{
|
|
|
|
return fStat->rateUpload;
|
2006-03-27 15:09:29 +00:00
|
|
|
}
|
|
|
|
|
2006-06-14 03:00:27 +00:00
|
|
|
- (float) downloadedValid
|
|
|
|
{
|
|
|
|
return [self progress] * [self size];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (uint64_t) downloadedTotal
|
2006-03-27 15:09:29 +00:00
|
|
|
{
|
|
|
|
return fStat->downloaded;
|
|
|
|
}
|
|
|
|
|
2006-06-14 03:00:27 +00:00
|
|
|
- (uint64_t) uploadedTotal
|
2006-03-27 15:09:29 +00:00
|
|
|
{
|
|
|
|
return fStat->uploaded;
|
|
|
|
}
|
|
|
|
|
2006-06-06 18:05:57 +00:00
|
|
|
- (NSArray *) fileList
|
|
|
|
{
|
|
|
|
int count = fInfo->fileCount, i;
|
|
|
|
NSMutableArray * files = [NSMutableArray arrayWithCapacity: count];
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
[files addObject: [[self downloadFolder] stringByAppendingPathComponent:
|
|
|
|
[NSString stringWithUTF8String: fInfo->files[i].name]]];
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
2006-04-25 16:31:19 +00:00
|
|
|
- (NSDate *) date
|
|
|
|
{
|
|
|
|
return fDate;
|
|
|
|
}
|
|
|
|
|
2006-05-25 12:22:19 +00:00
|
|
|
- (NSNumber *) stateSortKey
|
|
|
|
{
|
|
|
|
if (fStat->status & TR_STATUS_INACTIVE)
|
|
|
|
return [NSNumber numberWithInt: 0];
|
|
|
|
else if (fStat->status == TR_STATUS_SEED)
|
|
|
|
return [NSNumber numberWithInt: 1];
|
|
|
|
else
|
|
|
|
return [NSNumber numberWithInt: 2];
|
|
|
|
}
|
|
|
|
|
2006-06-06 18:05:57 +00:00
|
|
|
- (NSNumber *) progressSortKey
|
|
|
|
{
|
|
|
|
return [NSNumber numberWithFloat: [self progress]];
|
|
|
|
}
|
|
|
|
|
2006-03-23 12:39:39 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
@implementation Torrent (Private)
|
|
|
|
|
2006-06-15 05:30:08 +00:00
|
|
|
//if a hash is given, attempt to load that; otherwise, attempt to open file at path
|
2006-06-15 02:07:50 +00:00
|
|
|
- (id) initWithHash: (NSString *) hashString path: (NSString *) path lib: (tr_handle_t *) lib
|
2006-06-15 05:30:08 +00:00
|
|
|
privateTorrent: (NSNumber *) privateTorrent publicTorrent: (NSNumber *) publicTorrent
|
|
|
|
date: (NSDate *) date stopRatioSetting: (NSNumber *) stopRatioSetting
|
|
|
|
ratioLimit: (NSNumber *) ratioLimit
|
2006-06-09 19:53:35 +00:00
|
|
|
{
|
|
|
|
if (!(self = [super init]))
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
fLib = lib;
|
2006-06-15 05:30:08 +00:00
|
|
|
fDefaults = [NSUserDefaults standardUserDefaults];
|
|
|
|
|
|
|
|
fPrivateTorrent = privateTorrent ? [privateTorrent boolValue] : [fDefaults boolForKey: @"SavePrivateTorrent"];
|
|
|
|
fPublicTorrent = !fPrivateTorrent || (publicTorrent ? [publicTorrent boolValue]
|
|
|
|
: ![fDefaults boolForKey: @"DeleteOriginalTorrent"]);
|
2006-06-09 19:53:35 +00:00
|
|
|
|
|
|
|
int error;
|
2006-06-15 05:30:08 +00:00
|
|
|
if (fPrivateTorrent && hashString)
|
2006-06-15 05:03:32 +00:00
|
|
|
fHandle = tr_torrentInitSaved(fLib, [hashString UTF8String], TR_FSAVEPRIVATE, & error);
|
2006-06-15 22:07:05 +00:00
|
|
|
|
|
|
|
if (!fHandle && path)
|
2006-06-15 05:03:32 +00:00
|
|
|
fHandle = tr_torrentInit(fLib, [path UTF8String], fPrivateTorrent ? TR_FSAVEPRIVATE : 0, & error);
|
2006-06-15 05:30:08 +00:00
|
|
|
|
2006-06-15 05:03:32 +00:00
|
|
|
if (!fHandle)
|
2006-06-09 19:53:35 +00:00
|
|
|
{
|
|
|
|
[self release];
|
|
|
|
return nil;
|
|
|
|
}
|
2006-06-08 04:44:45 +00:00
|
|
|
|
2006-06-09 19:53:35 +00:00
|
|
|
fInfo = tr_torrentInfo( fHandle );
|
2006-05-25 12:22:19 +00:00
|
|
|
|
2006-06-15 05:30:08 +00:00
|
|
|
if (fPublicTorrent)
|
2006-06-15 02:35:40 +00:00
|
|
|
fPublicTorrentLocation = [path retain];
|
|
|
|
|
2006-05-25 12:22:19 +00:00
|
|
|
fDate = date ? [date retain] : [[NSDate alloc] init];
|
2006-06-12 23:36:25 +00:00
|
|
|
|
2006-05-25 12:22:19 +00:00
|
|
|
fStopRatioSetting = stopRatioSetting ? [stopRatioSetting intValue] : -1;
|
|
|
|
fRatioLimit = ratioLimit ? [ratioLimit floatValue] : [fDefaults floatForKey: @"RatioLimit"];
|
2006-06-12 23:36:25 +00:00
|
|
|
fFinishedSeeding = NO;
|
2006-05-25 12:22:19 +00:00
|
|
|
|
|
|
|
NSString * fileType = ( fInfo->fileCount > 1 ) ?
|
|
|
|
NSFileTypeForHFSTypeCode('fldr') : [[self name] pathExtension];
|
|
|
|
fIcon = [[NSWorkspace sharedWorkspace] iconForFileType: fileType];
|
|
|
|
[fIcon retain];
|
2006-06-06 18:05:57 +00:00
|
|
|
|
|
|
|
fIconFlipped = [fIcon copy];
|
|
|
|
[fIconFlipped setFlipped: YES];
|
2006-05-25 12:22:19 +00:00
|
|
|
|
2006-06-06 18:05:57 +00:00
|
|
|
fProgressString = [[NSMutableString alloc] initWithCapacity: 50];
|
|
|
|
fStatusString = [[NSMutableString alloc] initWithCapacity: 75];
|
2006-05-25 12:22:19 +00:00
|
|
|
|
|
|
|
[self update];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2006-06-14 03:00:27 +00:00
|
|
|
- (void) trashFile: (NSString *) path
|
2006-03-23 12:39:39 +00:00
|
|
|
{
|
2006-03-28 06:26:13 +00:00
|
|
|
if( ![[NSWorkspace sharedWorkspace] performFileOperation:
|
|
|
|
NSWorkspaceRecycleOperation source:
|
|
|
|
[path stringByDeletingLastPathComponent]
|
|
|
|
destination: @""
|
|
|
|
files: [NSArray arrayWithObject: [path lastPathComponent]]
|
|
|
|
tag: nil] )
|
2006-03-23 12:39:39 +00:00
|
|
|
{
|
2006-03-28 06:26:13 +00:00
|
|
|
/* We can't move it to the trash, let's try just to delete it
|
|
|
|
(will work if it is on a remote volume) */
|
2006-06-15 06:24:03 +00:00
|
|
|
if (![[NSFileManager defaultManager] removeFileAtPath: path handler: nil])
|
|
|
|
NSLog([@"Could not trash " stringByAppendingString: path]);
|
2006-03-23 12:39:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|