2006-03-23 12:39:39 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* Copyright (c) 2005-2006 Transmission authors and contributors
|
|
|
|
*
|
|
|
|
* 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"
|
2006-04-22 16:44:56 +00:00
|
|
|
#import "Utils.h"
|
2006-03-23 12:39:39 +00:00
|
|
|
|
|
|
|
@interface Torrent (Private)
|
|
|
|
|
|
|
|
- (void) trashPath: (NSString *) path;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
@implementation Torrent
|
|
|
|
|
2006-04-25 16:31:19 +00:00
|
|
|
- (id) initWithPath: (NSString *) path lib: (tr_handle_t *) lib date: (NSDate *) date
|
2006-03-23 12:39:39 +00:00
|
|
|
{
|
|
|
|
fLib = lib;
|
|
|
|
|
|
|
|
int error;
|
|
|
|
fHandle = tr_torrentInit( fLib, [path UTF8String], &error );
|
|
|
|
if( !fHandle )
|
|
|
|
{
|
|
|
|
[self release];
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2006-04-25 16:31:19 +00:00
|
|
|
fDate = [date retain];
|
2006-03-23 12:39:39 +00:00
|
|
|
fInfo = tr_torrentInfo( fHandle );
|
|
|
|
|
|
|
|
NSString * fileType = ( fInfo->fileCount > 1 ) ?
|
|
|
|
NSFileTypeForHFSTypeCode('fldr') : [[self name] pathExtension];
|
|
|
|
fIcon = [[NSWorkspace sharedWorkspace] iconForFileType: fileType];
|
|
|
|
[fIcon setFlipped: YES];
|
|
|
|
[fIcon retain];
|
2006-03-27 15:09:29 +00:00
|
|
|
fIconNonFlipped = [[NSWorkspace sharedWorkspace] iconForFileType: fileType];
|
|
|
|
[fIconNonFlipped retain];
|
2006-03-23 12:39:39 +00:00
|
|
|
|
|
|
|
fStatusString = [[NSMutableString alloc] initWithCapacity: 50];
|
|
|
|
fInfoString = [[NSMutableString alloc] initWithCapacity: 50];
|
|
|
|
fDownloadString = [[NSMutableString alloc] initWithCapacity: 10];
|
|
|
|
fUploadString = [[NSMutableString alloc] initWithCapacity: 10];
|
|
|
|
|
|
|
|
[self update];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2006-04-25 16:31:19 +00:00
|
|
|
- (id) initWithPath: (NSString *) path lib: (tr_handle_t *) lib
|
|
|
|
{
|
|
|
|
return [self initWithPath: path lib: lib date: [NSDate date]];
|
|
|
|
}
|
|
|
|
|
2006-03-23 12:39:39 +00:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
if( fHandle )
|
|
|
|
{
|
|
|
|
tr_torrentClose( fLib, fHandle );
|
2006-04-25 16:31:19 +00:00
|
|
|
[fDate release];
|
2006-03-23 12:39:39 +00:00
|
|
|
[fIcon release];
|
2006-04-25 16:31:19 +00:00
|
|
|
[fIconNonFlipped release];
|
2006-03-23 12:39:39 +00:00
|
|
|
[fStatusString release];
|
|
|
|
[fInfoString release];
|
|
|
|
[fDownloadString release];
|
|
|
|
[fUploadString release];
|
|
|
|
}
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setFolder: (NSString *) path
|
|
|
|
{
|
|
|
|
tr_torrentSetFolder( fHandle, [path UTF8String] );
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) getFolder
|
|
|
|
{
|
|
|
|
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 );
|
|
|
|
|
|
|
|
[fStatusString setString: @""];
|
|
|
|
[fInfoString setString: @""];
|
|
|
|
|
|
|
|
switch( fStat->status )
|
|
|
|
{
|
|
|
|
case TR_STATUS_PAUSE:
|
|
|
|
[fStatusString appendFormat: @"Paused (%.2f %%)",
|
|
|
|
100 * fStat->progress];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_STATUS_CHECK:
|
|
|
|
[fStatusString appendFormat:
|
|
|
|
@"Checking existing files (%.2f %%)",
|
|
|
|
100 * fStat->progress];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_STATUS_DOWNLOAD:
|
|
|
|
if( fStat->eta < 0 )
|
|
|
|
{
|
|
|
|
[fStatusString appendFormat:
|
|
|
|
@"Finishing in --:--:-- (%.2f %%)",
|
|
|
|
100 * fStat->progress];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[fStatusString appendFormat:
|
|
|
|
@"Finishing in %02d:%02d:%02d (%.2f %%)",
|
|
|
|
fStat->eta / 3600, ( fStat->eta / 60 ) % 60,
|
|
|
|
fStat->eta % 60, 100 * fStat->progress];
|
|
|
|
}
|
|
|
|
[fInfoString appendFormat:
|
|
|
|
@"Downloading from %d of %d peer%s",
|
|
|
|
fStat->peersUploading, fStat->peersTotal,
|
|
|
|
( fStat->peersTotal == 1 ) ? "" : "s"];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_STATUS_SEED:
|
|
|
|
[fStatusString appendFormat:
|
|
|
|
@"Seeding, uploading to %d of %d peer%s",
|
|
|
|
fStat->peersDownloading, fStat->peersTotal,
|
|
|
|
( fStat->peersTotal == 1 ) ? "" : "s"];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_STATUS_STOPPING:
|
2006-04-22 16:44:56 +00:00
|
|
|
[fStatusString setString: [@"Stopping"
|
|
|
|
stringByAppendingString: NS_ELLIPSIS]];
|
2006-03-23 12:39:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-04-08 15:56:26 +00:00
|
|
|
if( fStat->error & TR_ETRACKER )
|
2006-03-23 12:39:39 +00:00
|
|
|
{
|
2006-04-22 16:44:56 +00:00
|
|
|
[fInfoString setString: [@"Error: " stringByAppendingString:
|
|
|
|
[NSString stringWithUTF8String: fStat->trackerError]]];
|
2006-03-23 12:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( fStat->progress == 1.0 )
|
|
|
|
{
|
2006-04-22 16:44:56 +00:00
|
|
|
[fDownloadString setString: [@"Ratio: " stringByAppendingString:
|
|
|
|
[NSString stringForRatio: fStat->downloaded
|
|
|
|
upload: fStat->uploaded]]];
|
2006-03-23 12:39:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-04-22 16:44:56 +00:00
|
|
|
[fDownloadString setString: [@"DL: " stringByAppendingString:
|
|
|
|
[NSString stringForSpeed: fStat->rateDownload]]];
|
2006-03-23 12:39:39 +00:00
|
|
|
}
|
2006-04-22 16:44:56 +00:00
|
|
|
[fUploadString setString: [@"UL: " stringByAppendingString:
|
|
|
|
[NSString stringForSpeed: fStat->rateUpload]]];
|
2006-03-23 12:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) start
|
|
|
|
{
|
|
|
|
if( fStat->status & TR_STATUS_INACTIVE )
|
|
|
|
{
|
|
|
|
tr_torrentStart( fHandle );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) stop
|
|
|
|
{
|
|
|
|
if( fStat->status & TR_STATUS_ACTIVE )
|
|
|
|
{
|
|
|
|
tr_torrentStop( fHandle );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) reveal
|
|
|
|
{
|
2006-04-22 16:54:20 +00:00
|
|
|
[[NSWorkspace sharedWorkspace] selectFile: [[self getFolder]
|
|
|
|
stringByAppendingPathComponent: [self name]]
|
2006-03-23 12:39:39 +00:00
|
|
|
inFileViewerRootedAtPath: nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) trashTorrent
|
|
|
|
{
|
|
|
|
[self trashPath: [self path]];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) trashData
|
|
|
|
{
|
2006-04-22 16:54:20 +00:00
|
|
|
[self trashPath: [[self getFolder]
|
|
|
|
stringByAppendingPathComponent: [self name]]];
|
2006-03-23 12:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSImage *) icon
|
|
|
|
{
|
|
|
|
return fIcon;
|
|
|
|
}
|
|
|
|
|
2006-03-27 15:09:29 +00:00
|
|
|
- (NSImage *) iconNonFlipped
|
|
|
|
{
|
|
|
|
return fIconNonFlipped;
|
|
|
|
}
|
|
|
|
|
2006-03-23 12:39:39 +00:00
|
|
|
- (NSString *) path
|
|
|
|
{
|
|
|
|
return [NSString stringWithUTF8String: fInfo->torrent];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (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;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) hash1
|
|
|
|
{
|
|
|
|
NSMutableString * string = [NSMutableString
|
|
|
|
stringWithCapacity: SHA_DIGEST_LENGTH];
|
|
|
|
int i;
|
|
|
|
for( i = 0; i < SHA_DIGEST_LENGTH / 2; i++ )
|
|
|
|
{
|
|
|
|
[string appendFormat: @"%02x", fInfo->hash[i]];
|
|
|
|
}
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
- (NSString *) hash2
|
|
|
|
{
|
|
|
|
NSMutableString * string = [NSMutableString
|
|
|
|
stringWithCapacity: SHA_DIGEST_LENGTH];
|
|
|
|
int i;
|
|
|
|
for( i = SHA_DIGEST_LENGTH / 2; i < SHA_DIGEST_LENGTH; i++ )
|
|
|
|
{
|
|
|
|
[string appendFormat: @"%02x", fInfo->hash[i]];
|
|
|
|
}
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) statusString
|
|
|
|
{
|
|
|
|
return fStatusString;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) infoString
|
|
|
|
{
|
|
|
|
return fInfoString;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) downloadString
|
|
|
|
{
|
|
|
|
return fDownloadString;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) uploadString
|
|
|
|
{
|
|
|
|
return fUploadString;
|
|
|
|
}
|
|
|
|
|
2006-03-27 15:09:29 +00:00
|
|
|
- (int) seeders
|
|
|
|
{
|
|
|
|
return fStat->seeders;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int) leechers
|
|
|
|
{
|
|
|
|
return fStat->leechers;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (uint64_t) downloaded
|
|
|
|
{
|
|
|
|
return fStat->downloaded;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (uint64_t) uploaded
|
|
|
|
{
|
|
|
|
return fStat->uploaded;
|
|
|
|
}
|
|
|
|
|
2006-04-25 16:31:19 +00:00
|
|
|
- (NSDate *) date
|
|
|
|
{
|
|
|
|
return fDate;
|
|
|
|
}
|
|
|
|
|
2006-03-23 12:39:39 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
@implementation Torrent (Private)
|
|
|
|
|
|
|
|
- (void) trashPath: (NSString *) path
|
|
|
|
{
|
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) */
|
|
|
|
if( ![[NSFileManager defaultManager]
|
|
|
|
removeFileAtPath: path handler: nil] )
|
|
|
|
{
|
2006-04-22 16:44:56 +00:00
|
|
|
NSLog( [@"Could not trash " stringByAppendingString: path] );
|
2006-03-28 06:26:13 +00:00
|
|
|
}
|
2006-03-23 12:39:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|