transmission/macosx/Torrent.m

1901 lines
61 KiB
Mathematica
Raw Normal View History

2007-09-16 01:02:06 +00:00
/******************************************************************************
* $Id$
*
2009-01-10 23:37:37 +00:00
* Copyright (c) 2006-2009 Transmission authors and contributors
2007-09-16 01:02:06 +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 "GroupsController.h"
#import "FileListNode.h"
2007-09-16 01:02:06 +00:00
#import "NSStringAdditions.h"
#import "TrackerNode.h"
#import "utils.h" //tr_httpIsValidURL
2007-09-16 01:02:06 +00:00
@interface Torrent (Private)
- (id) initWithPath: (NSString *) path hash: (NSString *) hashString torrentStruct: (tr_torrent *) torrentStruct lib: (tr_session *) lib
2007-09-16 01:02:06 +00:00
downloadFolder: (NSString *) downloadFolder
useIncompleteFolder: (NSNumber *) useIncompleteFolder incompleteFolder: (NSString *) incompleteFolder
2007-12-17 16:06:20 +00:00
waitToStart: (NSNumber *) waitToStart
groupValue: (NSNumber *) groupValue;
2007-09-16 01:02:06 +00:00
- (BOOL) shouldUseIncompleteFolderForName: (NSString *) name;
- (void) updateDownloadFolder;
- (void) createFileList;
- (void) insertPath: (NSMutableArray *) components forParent: (FileListNode *) parent fileSize: (uint64_t) size
index: (NSInteger) index flatList: (NSMutableArray *) flatFileList;
2007-09-16 01:02:06 +00:00
- (void) completenessChange: (NSNumber *) status;
- (void) ratioLimitHit;
2007-09-16 01:02:06 +00:00
- (void) quickPause;
- (void) endQuickPause;
- (NSString *) etaString;
2008-04-07 02:55:33 +00:00
- (void) updateAllTrackers: (NSMutableArray *) trackers;
- (void) setTimeMachineExclude: (BOOL) exclude forPath: (NSString *) path;
2007-09-16 01:02:06 +00:00
@end
void completenessChangeCallback(tr_torrent * torrent, tr_completeness status, void * torrentData)
{
[(Torrent *)torrentData performSelectorOnMainThread: @selector(completenessChange:)
withObject: [[NSNumber alloc] initWithInt: status] waitUntilDone: NO];
}
void ratioLimitHitCallback(tr_torrent * torrent, void * torrentData)
{
[(Torrent *)torrentData performSelectorOnMainThread: @selector(ratioLimitHit) withObject: nil waitUntilDone: NO];
}
int trashDataFile(const char * filename)
{
[Torrent trashFile: [NSString stringWithUTF8String: filename]];
return 0;
}
2007-09-16 01:02:06 +00:00
@implementation Torrent
- (id) initWithPath: (NSString *) path location: (NSString *) location deleteTorrentFile: (BOOL) torrentDelete
lib: (tr_session *) lib
2007-09-16 01:02:06 +00:00
{
self = [self initWithPath: path hash: nil torrentStruct: NULL lib: lib
2007-09-16 01:02:06 +00:00
downloadFolder: location
useIncompleteFolder: nil incompleteFolder: nil
waitToStart: nil groupValue: nil];
2007-09-16 01:02:06 +00:00
if (self)
{
if (torrentDelete && ![[self torrentLocation] isEqualToString: path])
[Torrent trashFile: path];
2007-09-16 01:02:06 +00:00
}
return self;
}
- (id) initWithTorrentStruct: (tr_torrent *) torrentStruct location: (NSString *) location lib: (tr_session *) lib
{
self = [self initWithPath: nil hash: nil torrentStruct: torrentStruct lib: lib
downloadFolder: location
useIncompleteFolder: nil incompleteFolder: nil
waitToStart: nil groupValue: nil];
return self;
}
- (id) initWithHistory: (NSDictionary *) history lib: (tr_session *) lib forcePause: (BOOL) pause
2007-09-16 01:02:06 +00:00
{
self = [self initWithPath: [history objectForKey: @"InternalTorrentPath"]
hash: [history objectForKey: @"TorrentHash"]
torrentStruct: NULL lib: lib
2007-09-16 01:02:06 +00:00
downloadFolder: [history objectForKey: @"DownloadFolder"]
useIncompleteFolder: [history objectForKey: @"UseIncompleteFolder"]
incompleteFolder: [history objectForKey: @"IncompleteFolder"]
waitToStart: [history objectForKey: @"WaitToStart"]
groupValue: [history objectForKey: @"GroupValue"]];
2007-09-16 01:02:06 +00:00
if (self)
{
//start transfer
NSNumber * active;
if (!pause && (active = [history objectForKey: @"Active"]) && [active boolValue])
2007-09-16 01:02:06 +00:00
{
fStat = tr_torrentStat(fHandle);
[self startTransfer];
}
2008-08-09 20:20:21 +00:00
//upgrading from versions < 1.30: get old added, activity, and done dates
NSDate * date;
if ((date = [history objectForKey: @"Date"]))
tr_torrentSetAddedDate(fHandle, [date timeIntervalSince1970]);
if ((date = [history objectForKey: @"DateActivity"]))
tr_torrentSetActivityDate(fHandle, [date timeIntervalSince1970]);
if ((date = [history objectForKey: @"DateCompleted"]))
tr_torrentSetDoneDate(fHandle, [date timeIntervalSince1970]);
//upgrading from versions < 1.60: get old stop ratio settings
NSNumber * ratioSetting;
if ((ratioSetting = [history objectForKey: @"RatioSetting"]))
{
switch ([ratioSetting intValue])
{
case NSOnState: [self setRatioSetting: TR_RATIOLIMIT_SINGLE]; break;
case NSOffState: [self setRatioSetting: TR_RATIOLIMIT_UNLIMITED]; break;
case NSMixedState: [self setRatioSetting: TR_RATIOLIMIT_GLOBAL]; break;
}
}
NSNumber * ratioLimit;
if ((ratioLimit = [history objectForKey: @"RatioLimit"]))
[self setRatioLimit: [ratioLimit floatValue]];
2007-09-16 01:02:06 +00:00
}
return self;
}
- (NSDictionary *) history
{
NSMutableDictionary * history = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[self torrentLocation], @"InternalTorrentPath",
2007-09-16 01:02:06 +00:00
[self hashString], @"TorrentHash",
fDownloadFolder, @"DownloadFolder",
[NSNumber numberWithBool: fUseIncompleteFolder], @"UseIncompleteFolder",
[NSNumber numberWithBool: [self isActive]], @"Active",
[NSNumber numberWithBool: fWaitToStart], @"WaitToStart",
[NSNumber numberWithInt: fGroupValue], @"GroupValue", nil];
2007-09-16 01:02:06 +00:00
if (fIncompleteFolder)
[history setObject: fIncompleteFolder forKey: @"IncompleteFolder"];
return history;
}
- (void) dealloc
{
2007-12-17 16:06:20 +00:00
[[NSNotificationCenter defaultCenter] removeObserver: self];
if (fFileStat)
tr_torrentFilesFree(fFileStat, [self fileCount]);
2007-09-16 01:02:06 +00:00
[fPreviousFinishedIndexes release];
[fPreviousFinishedIndexesDate release];
2008-02-16 19:32:22 +00:00
[fNameString release];
[fHashString release];
2007-09-16 01:02:06 +00:00
[fDownloadFolder release];
[fIncompleteFolder release];
[fIcon release];
[fFileList release];
[fFlatFileList release];
2007-09-16 01:02:06 +00:00
[fQuickPauseDict release];
[super dealloc];
}
2008-05-12 01:32:33 +00:00
- (NSString *) description
{
return [@"Torrent: " stringByAppendingString: [self name]];
}
- (void) closeRemoveTorrent
2007-09-16 01:02:06 +00:00
{
//allow the file to be index by Time Machine
[self setTimeMachineExclude: NO forPath: [[self downloadFolder] stringByAppendingPathComponent: [self name]]];
tr_torrentRemove(fHandle);
2007-09-16 01:02:06 +00:00
}
- (void) changeIncompleteDownloadFolder: (NSString *) folder
{
fUseIncompleteFolder = folder != nil;
2007-09-16 16:33:49 +00:00
[fIncompleteFolder release];
fIncompleteFolder = fUseIncompleteFolder ? [folder retain] : nil;
2007-09-16 01:02:06 +00:00
[self updateDownloadFolder];
}
- (void) changeDownloadFolder: (NSString *) folder
{
if (fDownloadFolder && [folder isEqualToString: fDownloadFolder])
return;
2007-09-16 16:33:49 +00:00
[fDownloadFolder release];
2007-09-16 01:02:06 +00:00
fDownloadFolder = [folder retain];
[self updateDownloadFolder];
}
- (NSString *) downloadFolder
{
return [NSString stringWithUTF8String: tr_torrentGetDownloadDir(fHandle)];
2007-09-16 01:02:06 +00:00
}
2008-11-02 01:07:01 +00:00
- (void) getAvailability: (int8_t *) tab size: (NSInteger) size
2007-09-16 01:02:06 +00:00
{
tr_torrentAvailability(fHandle, tab, size);
}
2008-11-02 01:07:01 +00:00
- (void) getAmountFinished: (float *) tab size: (NSInteger) size
2007-09-16 01:02:06 +00:00
{
tr_torrentAmountFinished(fHandle, tab, size);
}
- (NSIndexSet *) previousFinishedPieces
{
//if the torrent hasn't been seen in a bit, and therefore hasn't been refreshed, return nil
if (fPreviousFinishedIndexesDate && [fPreviousFinishedIndexesDate timeIntervalSinceNow] > -2.0)
return fPreviousFinishedIndexes;
else
return nil;
}
-(void) setPreviousFinishedPieces: (NSIndexSet *) indexes
{
[fPreviousFinishedIndexes release];
fPreviousFinishedIndexes = [indexes retain];
[fPreviousFinishedIndexesDate release];
fPreviousFinishedIndexesDate = indexes != nil ? [[NSDate alloc] init] : nil;
}
2007-09-16 01:02:06 +00:00
- (void) update
{
2008-01-14 04:50:19 +00:00
//get previous status values before update
BOOL wasChecking = NO, wasError = NO, wasStalled = NO;
if (fStat != NULL)
{
wasChecking = [self isChecking];
wasError = [self isError];
wasStalled = fStalled;
}
2008-01-14 04:50:19 +00:00
2007-09-16 01:02:06 +00:00
fStat = tr_torrentStat(fHandle);
2008-01-14 04:50:19 +00:00
//check if stalled (stored because based on time and needs to check if it was previously stalled)
2007-09-16 01:02:06 +00:00
fStalled = [self isActive] && [fDefaults boolForKey: @"CheckStalled"]
&& [self stalledMinutes] > [fDefaults integerForKey: @"StalledMinutes"];
2007-09-16 01:02:06 +00:00
//update queue for checking (from downloading to seeding), stalled, or error
2008-01-14 04:50:19 +00:00
if ((wasChecking && ![self isChecking]) || (wasStalled != fStalled) || (!wasError && [self isError] && [self isActive]))
2007-09-16 01:02:06 +00:00
[[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateQueue" object: self];
}
- (void) startTransfer
{
fWaitToStart = NO;
fFinishedSeeding = NO;
if (![self isActive] && [self alertForFolderAvailable] && [self alertForRemainingDiskSpace])
{
tr_torrentStart(fHandle);
[self update];
}
}
- (void) stopTransfer
{
fWaitToStart = NO;
if ([self isActive])
{
tr_torrentStop(fHandle);
[self update];
[[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateQueue" object: self];
}
}
- (void) sleep
{
if ((fResumeOnWake = [self isActive]))
tr_torrentStop(fHandle);
}
- (void) wakeUp
{
if (fResumeOnWake)
tr_torrentStart(fHandle);
}
- (void) manualAnnounce
{
tr_torrentManualUpdate(fHandle);
2007-09-16 01:02:06 +00:00
}
- (BOOL) canManualAnnounce
{
return tr_torrentCanManualUpdate(fHandle);
}
- (void) resetCache
{
2008-02-27 17:38:39 +00:00
tr_torrentVerify(fHandle);
2007-09-16 01:02:06 +00:00
[self update];
}
2008-11-02 01:07:01 +00:00
- (CGFloat) ratio
2007-09-16 01:02:06 +00:00
{
return fStat->ratio;
}
- (tr_ratiolimit) ratioSetting
2007-09-16 01:02:06 +00:00
{
return tr_torrentGetRatioMode(fHandle);
2007-09-16 01:02:06 +00:00
}
- (void) setRatioSetting: (tr_ratiolimit) setting
2007-09-16 01:02:06 +00:00
{
tr_torrentSetRatioMode(fHandle, setting);
2007-09-16 01:02:06 +00:00
}
2008-11-02 01:07:01 +00:00
- (CGFloat) ratioLimit
2007-09-16 01:02:06 +00:00
{
return tr_torrentGetRatioLimit(fHandle);
2007-09-16 01:02:06 +00:00
}
2008-11-02 01:07:01 +00:00
- (void) setRatioLimit: (CGFloat) limit
2007-09-16 01:02:06 +00:00
{
NSAssert(limit >= 0, @"Ratio cannot be negative");
tr_torrentSetRatioLimit(fHandle, limit);
2007-09-16 01:02:06 +00:00
}
- (BOOL) seedRatioSet
2007-09-16 01:02:06 +00:00
{
return tr_torrentGetSeedRatio(fHandle, NULL);
2007-09-16 01:02:06 +00:00
}
2008-11-02 01:07:01 +00:00
- (CGFloat) progressStopRatio
2008-06-24 04:00:39 +00:00
{
return fStat->percentRatio;
2007-09-16 01:02:06 +00:00
}
- (BOOL) usesSpeedLimit: (BOOL) upload
2007-09-16 01:02:06 +00:00
{
return tr_torrentUsesSpeedLimit(fHandle, upload ? TR_UP : TR_DOWN);
2007-09-16 01:02:06 +00:00
}
- (void) setUseSpeedLimit: (BOOL) use upload: (BOOL) upload
2007-09-16 01:02:06 +00:00
{
tr_torrentUseSpeedLimit(fHandle, upload ? TR_UP : TR_DOWN, use);
2007-09-16 01:02:06 +00:00
}
2008-11-02 01:07:01 +00:00
- (NSInteger) speedLimit: (BOOL) upload
2007-09-16 01:02:06 +00:00
{
return tr_torrentGetSpeedLimit(fHandle, upload ? TR_UP : TR_DOWN);
}
2008-11-02 01:07:01 +00:00
- (void) setSpeedLimit: (NSInteger) limit upload: (BOOL) upload
2007-09-16 01:02:06 +00:00
{
tr_torrentSetSpeedLimit(fHandle, upload ? TR_UP : TR_DOWN, limit);
}
- (BOOL) usesGlobalSpeedLimit
{
return tr_torrentUsesSessionLimits(fHandle);
}
- (void) setUseGlobalSpeedLimit: (BOOL) use
{
tr_torrentUseSessionLimits(fHandle, use);
}
- (void) setMaxPeerConnect: (uint16_t) count
{
2008-06-17 17:17:15 +00:00
NSAssert(count > 0, @"max peer count must be greater than 0");
tr_torrentSetPeerLimit(fHandle, count);
}
- (uint16_t) maxPeerConnect
{
2008-05-12 16:39:32 +00:00
return tr_torrentGetPeerLimit(fHandle);
}
2007-09-16 01:02:06 +00:00
- (void) setWaitToStart: (BOOL) wait
{
fWaitToStart = wait;
}
- (BOOL) waitingToStart
{
return fWaitToStart;
}
- (tr_priority_t) priority
{
return tr_torrentGetPriority(fHandle);
}
- (void) setPriority: (tr_priority_t) priority
{
return tr_torrentSetPriority(fHandle, priority);
}
#warning should be somewhere else?
+ (void) trashFile: (NSString *) path
2007-09-16 01:02:06 +00:00
{
//attempt to move to trash
if (![[NSWorkspace sharedWorkspace] performFileOperation: NSWorkspaceRecycleOperation
source: [path stringByDeletingLastPathComponent] destination: @""
files: [NSArray arrayWithObject: [path lastPathComponent]] tag: nil])
{
//if cannot trash, just delete it (will work if it's on a remote volume)
NSError * error;
if (![[NSFileManager defaultManager] removeItemAtPath: path error: &error])
NSLog(@"Could not trash %@: %@", path, [error localizedDescription]);
}
2007-09-16 01:02:06 +00:00
}
- (void) trashData
{
tr_torrentDeleteLocalData(fHandle, trashDataFile);
2007-09-16 01:02:06 +00:00
}
- (void) moveTorrentDataFileTo: (NSString *) folder
{
NSString * oldFolder = [self downloadFolder];
if (![oldFolder isEqualToString: folder] || ![fDownloadFolder isEqualToString: folder])
{
//check if moving inside itself
NSArray * oldComponents = [oldFolder pathComponents],
* newComponents = [folder pathComponents];
2008-11-02 01:07:01 +00:00
NSInteger count;
2007-09-16 01:02:06 +00:00
if ((count = [oldComponents count]) < [newComponents count]
&& [[newComponents objectAtIndex: count] isEqualToString: [self name]]
&& [oldComponents isEqualToArray:
[newComponents objectsAtIndexes: [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, count)]]])
{
NSAlert * alert = [[NSAlert alloc] init];
[alert setMessageText: NSLocalizedString(@"A folder cannot be moved to inside itself.",
"Move inside itself alert -> title")];
[alert setInformativeText: [NSString stringWithFormat:
NSLocalizedString(@"The move operation of \"%@\" cannot be done.",
"Move inside itself alert -> message"), [self name]]];
[alert addButtonWithTitle: NSLocalizedString(@"OK", "Move inside itself alert -> button")];
[alert runModal];
[alert release];
return;
}
[self quickPause];
//allow if file can be moved or does not exist
if ([[NSFileManager defaultManager] moveItemAtPath: [oldFolder stringByAppendingPathComponent: [self name]]
toPath: [folder stringByAppendingPathComponent: [self name]] error: NULL]
|| ![[NSFileManager defaultManager] fileExistsAtPath: [oldFolder stringByAppendingPathComponent: [self name]]])
2007-09-16 01:02:06 +00:00
{
//get rid of both incomplete folder and old download folder, even if move failed
fUseIncompleteFolder = NO;
if (fIncompleteFolder)
{
[fIncompleteFolder release];
fIncompleteFolder = nil;
}
[self changeDownloadFolder: folder];
2007-10-23 11:41:03 +00:00
[[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateStats" object: nil];
2007-09-16 01:02:06 +00:00
[self endQuickPause];
}
else
{
[self endQuickPause];
NSAlert * alert = [[NSAlert alloc] init];
[alert setMessageText: NSLocalizedString(@"There was an error moving the data file.", "Move error alert -> title")];
[alert setInformativeText: [NSString stringWithFormat:
NSLocalizedString(@"The move operation of \"%@\" cannot be done.",
"Move error alert -> message"), [self name]]];
[alert addButtonWithTitle: NSLocalizedString(@"OK", "Move error alert -> button")];
[alert runModal];
[alert release];
}
}
}
- (void) copyTorrentFileTo: (NSString *) path
{
[[NSFileManager defaultManager] copyItemAtPath: [self torrentLocation] toPath: path error: NULL];
2007-09-16 01:02:06 +00:00
}
- (BOOL) alertForRemainingDiskSpace
{
if ([self allDownloaded] || ![fDefaults boolForKey: @"WarningRemainingSpace"])
return YES;
2008-03-17 20:18:23 +00:00
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * downloadFolder = [self downloadFolder];
2008-03-17 20:18:23 +00:00
2007-09-16 01:02:06 +00:00
NSString * volumeName;
if ((volumeName = [[fileManager componentsToDisplayForPath: downloadFolder] objectAtIndex: 0]))
2007-09-16 01:02:06 +00:00
{
2008-12-26 05:57:51 +00:00
NSDictionary * systemAttributes = [fileManager attributesOfFileSystemForPath: downloadFolder error: NULL];
uint64_t remainingSpace = [[systemAttributes objectForKey: NSFileSystemFreeSize] unsignedLongLongValue];
2007-09-16 01:02:06 +00:00
2008-11-06 03:45:56 +00:00
//if the remaining space is greater than the size left, then there is enough space regardless of preallocation
if (remainingSpace < [self sizeLeft] && remainingSpace < tr_torrentGetBytesLeftToAllocate(fHandle))
{
NSAlert * alert = [[NSAlert alloc] init];
[alert setMessageText: [NSString stringWithFormat:
NSLocalizedString(@"Not enough remaining disk space to download \"%@\" completely.",
"Torrent disk space alert -> title"), [self name]]];
[alert setInformativeText: [NSString stringWithFormat: NSLocalizedString(@"The transfer will be paused."
" Clear up space on %@ or deselect files in the torrent inspector to continue.",
"Torrent disk space alert -> message"), volumeName]];
[alert addButtonWithTitle: NSLocalizedString(@"OK", "Torrent disk space alert -> button")];
[alert addButtonWithTitle: NSLocalizedString(@"Download Anyway", "Torrent disk space alert -> button")];
2008-12-26 05:57:51 +00:00
[alert setShowsSuppressionButton: YES];
[[alert suppressionButton] setTitle: NSLocalizedString(@"Do not check disk space again",
"Torrent disk space alert -> button")];
NSInteger result = [alert runModal];
2008-12-26 05:57:51 +00:00
if ([[alert suppressionButton] state] == NSOnState)
[fDefaults setBool: NO forKey: @"WarningRemainingSpace"];
[alert release];
return result != NSAlertFirstButtonReturn;
}
2007-09-16 01:02:06 +00:00
}
return YES;
}
- (BOOL) alertForFolderAvailable
{
#warning check for change from incomplete to download folder first
if (access(tr_torrentGetDownloadDir(fHandle), 0))
2007-09-16 01:02:06 +00:00
{
NSAlert * alert = [[NSAlert alloc] init];
[alert setMessageText: [NSString stringWithFormat:
NSLocalizedString(@"The folder for downloading \"%@\" cannot be used.",
"Folder cannot be used alert -> title"), [self name]]];
[alert setInformativeText: [NSString stringWithFormat:
NSLocalizedString(@"\"%@\" cannot be used. The transfer will be paused.",
"Folder cannot be used alert -> message"), [self downloadFolder]]];
[alert addButtonWithTitle: NSLocalizedString(@"OK", "Folder cannot be used alert -> button")];
[alert addButtonWithTitle: [NSLocalizedString(@"Choose New Location",
"Folder cannot be used alert -> location button") stringByAppendingEllipsis]];
if ([alert runModal] != NSAlertFirstButtonReturn)
{
NSOpenPanel * panel = [NSOpenPanel openPanel];
[panel setPrompt: NSLocalizedString(@"Select", "Folder cannot be used alert -> prompt")];
[panel setAllowsMultipleSelection: NO];
[panel setCanChooseFiles: NO];
[panel setCanChooseDirectories: YES];
[panel setCanCreateDirectories: YES];
[panel setMessage: [NSString stringWithFormat: NSLocalizedString(@"Select the download folder for \"%@\"",
"Folder cannot be used alert -> select destination folder"), [self name]]];
[[NSNotificationCenter defaultCenter] postNotificationName: @"MakeWindowKey" object: nil];
[panel beginSheetForDirectory: nil file: nil types: nil modalForWindow: [NSApp keyWindow] modalDelegate: self
didEndSelector: @selector(destinationChoiceClosed:returnCode:contextInfo:) contextInfo: nil];
}
[alert release];
return NO;
}
return YES;
}
2008-11-02 01:07:01 +00:00
- (void) destinationChoiceClosed: (NSOpenPanel *) openPanel returnCode: (NSInteger) code contextInfo: (void *) context
2007-09-16 01:02:06 +00:00
{
if (code != NSOKButton)
return;
[self changeDownloadFolder: [[openPanel filenames] objectAtIndex: 0]];
2007-09-16 01:02:06 +00:00
[self startTransfer];
[self update];
2007-10-23 11:41:03 +00:00
[[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateStats" object: nil];
2007-09-16 01:02:06 +00:00
}
- (BOOL) alertForMoveFolderAvailable
{
if (access([fDownloadFolder UTF8String], 0))
{
NSAlert * alert = [[NSAlert alloc] init];
[alert setMessageText: [NSString stringWithFormat:
NSLocalizedString(@"The folder for moving the completed \"%@\" cannot be used.",
"Move folder cannot be used alert -> title"), [self name]]];
[alert setInformativeText: [NSString stringWithFormat:
NSLocalizedString(@"\"%@\" cannot be used. The file will remain in its current location.",
"Move folder cannot be used alert -> message"), fDownloadFolder]];
[alert addButtonWithTitle: NSLocalizedString(@"OK", "Move folder cannot be used alert -> button")];
[alert runModal];
[alert release];
return NO;
}
return YES;
}
- (NSImage *) icon
{
if (!fIcon)
fIcon = [[[NSWorkspace sharedWorkspace] iconForFileType: [self isFolder] ? NSFileTypeForHFSTypeCode('fldr')
2007-09-16 01:02:06 +00:00
: [[self name] pathExtension]] retain];
return fIcon;
}
- (NSString *) name
{
2008-02-16 19:32:22 +00:00
return fNameString;
2007-09-16 01:02:06 +00:00
}
- (BOOL) isFolder
{
return fInfo->isMultifile;
}
2007-09-16 01:02:06 +00:00
- (uint64_t) size
{
return fInfo->totalSize;
}
- (uint64_t) sizeLeft
{
return fStat->leftUntilDone;
}
/*- (NSString *) trackerAddressAnnounce
2007-09-16 01:02:06 +00:00
{
#warning fix
return @"fix me!";
//return fStat->announceURL ? [NSString stringWithUTF8String: fStat->announceURL] : nil;
2007-09-16 01:02:06 +00:00
}
2008-02-22 01:36:30 +00:00
- (NSDate *) lastAnnounceTime
{
2008-11-02 01:07:01 +00:00
NSInteger date = fStat->lastAnnounceTime;
2008-02-22 01:36:30 +00:00
return date > 0 ? [NSDate dateWithTimeIntervalSince1970: date] : nil;
}
2008-11-02 01:07:01 +00:00
- (NSInteger) nextAnnounceTime
2008-02-22 01:36:30 +00:00
{
2008-11-02 01:07:01 +00:00
NSInteger date = fStat->nextAnnounceTime;
NSTimeInterval difference;
switch (date)
{
case 0:
return STAT_TIME_NONE;
case 1:
return STAT_TIME_NOW;
default:
difference = [[NSDate dateWithTimeIntervalSince1970: date] timeIntervalSinceNow];
2008-11-02 01:07:01 +00:00
return difference > 0 ? (NSInteger)difference : STAT_TIME_NONE;
}
2008-02-22 01:36:30 +00:00
}
- (NSString *) announceResponse
{
return [NSString stringWithUTF8String: fStat->announceResponse];
2008-02-22 01:36:30 +00:00
}
- (NSString *) trackerAddressScrape
{
#warning fix
return @"fix me!";
//return fStat->scrapeURL ? [NSString stringWithUTF8String: fStat->scrapeURL] : nil;
2008-02-22 01:36:30 +00:00
}
- (NSDate *) lastScrapeTime
{
2008-11-02 01:07:01 +00:00
NSInteger date = fStat->lastScrapeTime;
2008-02-22 01:36:30 +00:00
return date > 0 ? [NSDate dateWithTimeIntervalSince1970: date] : nil;
}
2008-11-02 01:07:01 +00:00
- (NSInteger) nextScrapeTime
2008-02-22 01:36:30 +00:00
{
2008-11-02 01:07:01 +00:00
NSInteger date = fStat->nextScrapeTime;
NSTimeInterval difference;
switch (date)
{
case 0:
return STAT_TIME_NONE;
case 1:
return STAT_TIME_NOW;
default:
difference = [[NSDate dateWithTimeIntervalSince1970: date] timeIntervalSinceNow];
2008-11-02 01:07:01 +00:00
return difference > 0 ? (NSInteger)difference : STAT_TIME_NONE;
}
2008-02-22 01:36:30 +00:00
}
2008-02-22 01:36:30 +00:00
- (NSString *) scrapeResponse
{
return [NSString stringWithUTF8String: fStat->scrapeResponse];
}*/
2008-02-22 01:36:30 +00:00
- (NSArray *) allTrackerStats
{
int count;
tr_tracker_stat * stats = tr_torrentTrackers(fHandle, &count);
2009-09-26 13:58:40 +00:00
NSMutableArray * trackers = [NSMutableArray arrayWithCapacity: count + stats[count-1].tier];
int prevTier = -1;
for (int i=0; i < count; ++i)
{
if (stats[i].tier != prevTier)
2008-04-24 06:42:45 +00:00
{
[trackers addObject: [NSNumber numberWithInteger: stats[i].tier]];
prevTier = stats[i].tier;
2008-04-24 06:42:45 +00:00
}
TrackerNode * tracker = [[TrackerNode alloc] initWithTrackerStat: &stats[i]];
[trackers addObject: tracker];
[tracker release];
}
tr_torrentTrackersFree(stats, count);
return trackers;
}
- (NSArray *) allTrackersFlat
2008-12-24 17:41:45 +00:00
{
NSMutableArray * allTrackers = [NSMutableArray arrayWithCapacity: fInfo->trackerCount];
for (NSInteger i = 0, tier = -1; i < fInfo->trackerCount; i++)
[allTrackers addObject: [NSString stringWithUTF8String: fInfo->trackers[i].announce]];
2008-12-24 17:41:45 +00:00
}
/*- (BOOL) updateAllTrackersForAdd: (NSMutableArray *) trackers
{
NSString * tracker = [trackers lastObject];
tracker = [tracker stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([tracker rangeOfString: @"://"].location == NSNotFound)
{
tracker = [@"http://" stringByAppendingString: tracker];
[trackers replaceObjectAtIndex: [trackers count]-1 withObject: tracker];
}
if (!tr_httpIsValidURL([tracker UTF8String]))
return NO;
[self updateAllTrackers: trackers];
return YES;
}
- (void) updateAllTrackersForRemove: (NSMutableArray *) trackers
{
[self updateAllTrackers: trackers];
}*/
2007-09-16 01:02:06 +00:00
- (NSString *) comment
{
2008-02-16 19:32:22 +00:00
return [NSString stringWithUTF8String: fInfo->comment];
2007-09-16 01:02:06 +00:00
}
- (NSString *) creator
{
2008-02-16 19:32:22 +00:00
return [NSString stringWithUTF8String: fInfo->creator];
2007-09-16 01:02:06 +00:00
}
- (NSDate *) dateCreated
{
2008-11-02 01:07:01 +00:00
NSInteger date = fInfo->dateCreated;
2007-09-16 01:02:06 +00:00
return date > 0 ? [NSDate dateWithTimeIntervalSince1970: date] : nil;
}
2008-11-02 01:07:01 +00:00
- (NSInteger) pieceSize
2007-09-16 01:02:06 +00:00
{
return fInfo->pieceSize;
}
2008-11-02 01:07:01 +00:00
- (NSInteger) pieceCount
2007-09-16 01:02:06 +00:00
{
return fInfo->pieceCount;
}
- (NSString *) hashString
{
return fHashString;
2007-09-16 01:02:06 +00:00
}
- (BOOL) privateTorrent
{
2007-10-15 21:52:51 +00:00
return fInfo->isPrivate;
2007-09-16 01:02:06 +00:00
}
- (NSString *) torrentLocation
{
return [NSString stringWithUTF8String: fInfo->torrent];
}
- (NSString *) dataLocation
{
return [[self downloadFolder] stringByAppendingPathComponent: [self name]];
}
2008-11-02 01:07:01 +00:00
- (CGFloat) progress
2007-09-16 01:02:06 +00:00
{
return fStat->percentComplete;
}
2008-11-02 01:07:01 +00:00
- (CGFloat) progressDone
2007-09-16 01:02:06 +00:00
{
return fStat->percentDone;
}
2008-11-02 01:07:01 +00:00
- (CGFloat) checkingProgress
{
return fStat->recheckProgress;
}
2008-11-02 01:07:01 +00:00
- (NSInteger) eta
2007-09-16 01:02:06 +00:00
{
return fStat->eta;
}
- (CGFloat) availableDesired
2007-10-15 18:44:39 +00:00
{
2009-09-06 21:19:19 +00:00
return (CGFloat)fStat->desiredAvailable / [self sizeLeft];
}
2007-09-16 01:02:06 +00:00
- (BOOL) isActive
{
return fStat->activity != TR_STATUS_STOPPED;
2007-09-16 01:02:06 +00:00
}
- (BOOL) isSeeding
{
return fStat->activity == TR_STATUS_SEED;
2007-09-16 01:02:06 +00:00
}
- (BOOL) isChecking
{
return fStat->activity == TR_STATUS_CHECK || fStat->activity == TR_STATUS_CHECK_WAIT;
2007-09-16 01:02:06 +00:00
}
- (BOOL) isCheckingWaiting
{
return fStat->activity == TR_STATUS_CHECK_WAIT;
}
2007-09-16 01:02:06 +00:00
- (BOOL) allDownloaded
{
2009-03-15 20:33:42 +00:00
return [self sizeLeft] == 0;
2007-09-16 01:02:06 +00:00
}
- (BOOL) isComplete
{
2007-10-15 18:20:39 +00:00
return [self progress] >= 1.0;
2007-09-16 01:02:06 +00:00
}
- (BOOL) isError
{
2009-08-05 01:55:37 +00:00
return fStat->error == TR_STAT_LOCAL_ERROR || fStat->error == TR_STAT_TRACKER_ERROR;
}
- (BOOL) isErrorOrWarning
{
return fStat->error != TR_STAT_OK;
2007-09-16 01:02:06 +00:00
}
- (NSString *) errorMessage
{
2009-08-05 01:55:37 +00:00
if (![self isErrorOrWarning])
2007-09-16 01:02:06 +00:00
return @"";
NSString * error;
if (!(error = [NSString stringWithUTF8String: fStat->errorString])
&& !(error = [NSString stringWithCString: fStat->errorString encoding: NSISOLatin1StringEncoding]))
error = [NSString stringWithFormat: @"(%@)", NSLocalizedString(@"unreadable error", "Torrent -> error string unreadable")];
2007-09-16 01:02:06 +00:00
return error;
}
- (NSArray *) peers
{
int totalPeers;
2007-09-20 20:24:33 +00:00
tr_peer_stat * peers = tr_torrentPeers(fHandle, &totalPeers);
2007-09-16 01:02:06 +00:00
NSMutableArray * peerDicts = [NSMutableArray arrayWithCapacity: totalPeers];
2007-09-16 01:02:06 +00:00
for (int i = 0; i < totalPeers; i++)
2007-09-16 01:02:06 +00:00
{
tr_peer_stat * peer = &peers[i];
NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithCapacity: 10];
2007-09-16 01:02:06 +00:00
[dict setObject: [NSNumber numberWithInt: peer->from] forKey: @"From"];
[dict setObject: [NSString stringWithUTF8String: peer->addr] forKey: @"IP"];
[dict setObject: [NSNumber numberWithInt: peer->port] forKey: @"Port"];
[dict setObject: [NSNumber numberWithFloat: peer->progress] forKey: @"Progress"];
[dict setObject: [NSNumber numberWithBool: peer->isSeed] forKey: @"Seed"];
[dict setObject: [NSNumber numberWithBool: peer->isEncrypted] forKey: @"Encryption"];
[dict setObject: [NSString stringWithUTF8String: peer->client] forKey: @"Client"];
[dict setObject: [NSString stringWithUTF8String: peer->flagStr] forKey: @"Flags"];
2007-09-16 01:02:06 +00:00
2008-01-10 00:52:02 +00:00
if (peer->isUploadingTo)
[dict setObject: [NSNumber numberWithFloat: peer->rateToPeer] forKey: @"UL To Rate"];
2008-01-10 00:52:02 +00:00
if (peer->isDownloadingFrom)
[dict setObject: [NSNumber numberWithFloat: peer->rateToClient] forKey: @"DL From Rate"];
2007-09-16 01:02:06 +00:00
[peerDicts addObject: dict];
2007-09-16 01:02:06 +00:00
}
tr_torrentPeersFree(peers, totalPeers);
return peerDicts;
2007-09-16 01:02:06 +00:00
}
- (NSUInteger) webSeedCount
{
return fInfo->webseedCount;
}
2008-06-08 18:02:16 +00:00
- (NSArray *) webSeeds
{
2008-11-02 01:07:01 +00:00
const NSInteger webSeedCount = fInfo->webseedCount;
2008-06-08 18:02:16 +00:00
NSMutableArray * webSeeds = [NSMutableArray arrayWithCapacity: webSeedCount];
float * dlSpeeds = tr_torrentWebSpeeds(fHandle);
2008-11-02 01:07:01 +00:00
for (NSInteger i = 0; i < webSeedCount; i++)
{
NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithCapacity: 2];
[dict setObject: [NSString stringWithUTF8String: fInfo->webseeds[i]] forKey: @"Address"];
if (dlSpeeds[i] != -1.0)
[dict setObject: [NSNumber numberWithFloat: dlSpeeds[i]] forKey: @"DL From Rate"];
2008-06-08 18:02:16 +00:00
[webSeeds addObject: dict];
}
tr_free(dlSpeeds);
2008-06-08 18:02:16 +00:00
return webSeeds;
}
2007-09-16 01:02:06 +00:00
- (NSString *) progressString
{
NSString * string;
if (![self allDownloaded])
{
2008-11-02 01:07:01 +00:00
CGFloat progress;
if ([self isFolder] && [fDefaults boolForKey: @"DisplayStatusProgressSelected"])
{
2008-04-07 13:43:26 +00:00
string = [NSString stringWithFormat: NSLocalizedString(@"%@ of %@ selected", "Torrent -> progress string"),
[NSString stringForFileSize: [self haveTotal]], [NSString stringForFileSize: [self totalSizeSelected]]];
progress = 100.0 * [self progressDone];
}
else
{
2008-04-07 13:43:26 +00:00
string = [NSString stringWithFormat: NSLocalizedString(@"%@ of %@", "Torrent -> progress string"),
[NSString stringForFileSize: [self haveTotal]], [NSString stringForFileSize: [self size]]];
progress = 100.0 * [self progress];
}
2008-04-07 13:43:26 +00:00
string = [NSString localizedStringWithFormat: @"%@ (%.2f%%)", string, progress];
}
2008-03-31 19:36:11 +00:00
else
{
2008-03-31 19:36:11 +00:00
NSString * downloadString;
if (![self isComplete]) //only multifile possible
{
if ([fDefaults boolForKey: @"DisplayStatusProgressSelected"])
downloadString = [NSString stringWithFormat: NSLocalizedString(@"%@ selected", "Torrent -> progress string"),
2008-04-07 13:43:26 +00:00
[NSString stringForFileSize: [self haveTotal]]];
2008-03-31 19:36:11 +00:00
else
2008-04-07 13:43:26 +00:00
{
downloadString = [NSString stringWithFormat: NSLocalizedString(@"%@ of %@", "Torrent -> progress string"),
[NSString stringForFileSize: [self haveTotal]], [NSString stringForFileSize: [self size]]];
downloadString = [NSString localizedStringWithFormat: @"%@ (%.2f%%)", downloadString, 100.0 * [self progress]];
}
2008-03-31 19:36:11 +00:00
}
else
2008-03-31 19:36:11 +00:00
downloadString = [NSString stringForFileSize: [self size]];
NSString * uploadString = [NSString stringWithFormat: NSLocalizedString(@"uploaded %@ (Ratio: %@)",
"Torrent -> progress string"), [NSString stringForFileSize: [self uploadedTotal]],
[NSString stringForRatio: [self ratio]]];
2008-04-07 14:09:28 +00:00
string = [downloadString stringByAppendingFormat: @", %@", uploadString];
}
//add time when downloading
if (fStat->activity == TR_STATUS_DOWNLOAD || ([self isSeeding] && [self seedRatioSet]))
string = [string stringByAppendingFormat: @" - %@", [self etaString]];
return string;
2007-09-16 01:02:06 +00:00
}
- (NSString *) statusString
{
NSString * string;
2009-08-05 01:55:37 +00:00
if ([self isErrorOrWarning])
{
2009-08-05 01:55:37 +00:00
switch (fStat->error)
{
case TR_STAT_LOCAL_ERROR: string = NSLocalizedString(@"Error", "Torrent -> status string"); break;
case TR_STAT_TRACKER_ERROR: string = NSLocalizedString(@"Tracker returned an error", "Torrent -> status string"); break;
case TR_STAT_TRACKER_WARNING: string = NSLocalizedString(@"Tracker returned a warning", "Torrent -> status string"); break;
default: NSAssert(NO, @"unknown error state");
}
NSString * errorString = [self errorMessage];
2008-02-22 15:39:20 +00:00
if (errorString && ![errorString isEqualToString: @""])
string = [string stringByAppendingFormat: @": %@", errorString];
}
else
{
switch (fStat->activity)
{
case TR_STATUS_STOPPED:
if (fWaitToStart)
{
string = ![self allDownloaded]
? [NSLocalizedString(@"Waiting to download", "Torrent -> status string") stringByAppendingEllipsis]
: [NSLocalizedString(@"Waiting to seed", "Torrent -> status string") stringByAppendingEllipsis];
}
else if (fFinishedSeeding)
string = NSLocalizedString(@"Seeding complete", "Torrent -> status string");
else
string = NSLocalizedString(@"Paused", "Torrent -> status string");
break;
case TR_STATUS_CHECK_WAIT:
string = [NSLocalizedString(@"Waiting to check existing data", "Torrent -> status string") stringByAppendingEllipsis];
break;
case TR_STATUS_CHECK:
2008-03-24 00:06:54 +00:00
string = [NSString localizedStringWithFormat: NSLocalizedString(@"Checking existing data (%.2f%%)",
"Torrent -> status string"), 100.0 * [self checkingProgress]];
break;
case TR_STATUS_DOWNLOAD:
if ([self totalPeersConnected] != 1)
string = [NSString stringWithFormat: NSLocalizedString(@"Downloading from %d of %d peers",
"Torrent -> status string"), [self peersSendingToUs], [self totalPeersConnected]];
else
string = [NSString stringWithFormat: NSLocalizedString(@"Downloading from %d of 1 peer",
"Torrent -> status string"), [self peersSendingToUs]];
2008-11-02 01:07:01 +00:00
NSInteger webSeedCount = fStat->webseedsSendingToUs;
if (webSeedCount > 0)
{
NSString * webSeedString;
if (webSeedCount == 1)
webSeedString = NSLocalizedString(@"web seed", "Torrent -> status string");
else
webSeedString = [NSString stringWithFormat: NSLocalizedString(@"%d web seeds", "Torrent -> status string"),
webSeedCount];
string = [string stringByAppendingFormat: @" + %@", webSeedString];
}
break;
case TR_STATUS_SEED:
if ([self totalPeersConnected] != 1)
string = [NSString stringWithFormat: NSLocalizedString(@"Seeding to %d of %d peers", "Torrent -> status string"),
[self peersGettingFromUs], [self totalPeersConnected]];
else
string = [NSString stringWithFormat: NSLocalizedString(@"Seeding to %d of 1 peer", "Torrent -> status string"),
[self peersGettingFromUs]];
}
if (fStalled)
2008-04-22 13:16:39 +00:00
string = [NSLocalizedString(@"Stalled", "Torrent -> status string") stringByAppendingFormat: @", %@", string];
}
//append even if error
if ([self isActive] && ![self isChecking])
{
if (fStat->activity == TR_STATUS_DOWNLOAD)
string = [string stringByAppendingFormat: @" - %@: %@, %@: %@",
NSLocalizedString(@"DL", "Torrent -> status string"), [NSString stringForSpeed: [self downloadRate]],
NSLocalizedString(@"UL", "Torrent -> status string"), [NSString stringForSpeed: [self uploadRate]]];
else
string = [string stringByAppendingFormat: @" - %@: %@",
NSLocalizedString(@"UL", "Torrent -> status string"), [NSString stringForSpeed: [self uploadRate]]];
}
return string;
2007-09-16 01:02:06 +00:00
}
- (NSString *) shortStatusString
{
NSString * string;
switch (fStat->activity)
{
case TR_STATUS_STOPPED:
if (fWaitToStart)
{
string = ![self allDownloaded]
? [NSLocalizedString(@"Waiting to download", "Torrent -> status string") stringByAppendingEllipsis]
: [NSLocalizedString(@"Waiting to seed", "Torrent -> status string") stringByAppendingEllipsis];
}
else if (fFinishedSeeding)
string = NSLocalizedString(@"Seeding complete", "Torrent -> status string");
else
string = NSLocalizedString(@"Paused", "Torrent -> status string");
break;
case TR_STATUS_CHECK_WAIT:
string = [NSLocalizedString(@"Waiting to check existing data", "Torrent -> status string") stringByAppendingEllipsis];
break;
case TR_STATUS_CHECK:
2008-03-24 00:06:54 +00:00
string = [NSString localizedStringWithFormat: NSLocalizedString(@"Checking existing data (%.2f%%)",
"Torrent -> status string"), 100.0 * [self checkingProgress]];
break;
2007-09-30 13:33:50 +00:00
case TR_STATUS_DOWNLOAD:
string = [NSString stringWithFormat: @"%@: %@, %@: %@",
NSLocalizedString(@"DL", "Torrent -> status string"), [NSString stringForSpeed: [self downloadRate]],
NSLocalizedString(@"UL", "Torrent -> status string"), [NSString stringForSpeed: [self uploadRate]]];
2007-09-30 13:33:50 +00:00
break;
case TR_STATUS_SEED:
string = [NSString stringWithFormat: @"%@: %@, %@: %@",
NSLocalizedString(@"Ratio", "Torrent -> status string"), [NSString stringForRatio: [self ratio]],
NSLocalizedString(@"UL", "Torrent -> status string"), [NSString stringForSpeed: [self uploadRate]]];
}
return string;
2007-09-16 01:02:06 +00:00
}
- (NSString *) remainingTimeString
{
if (fStat->activity == TR_STATUS_DOWNLOAD || ([self isSeeding] && [self seedRatioSet]))
return [self etaString];
else
2008-02-16 19:32:22 +00:00
return [self shortStatusString];
2007-09-16 01:02:06 +00:00
}
- (NSString *) stateString
{
switch (fStat->activity)
{
case TR_STATUS_STOPPED:
return NSLocalizedString(@"Paused", "Torrent -> status string");
case TR_STATUS_CHECK:
2008-03-24 00:06:54 +00:00
return [NSString localizedStringWithFormat: NSLocalizedString(@"Checking existing data (%.2f%%)",
"Torrent -> status string"), 100.0 * [self checkingProgress]];
case TR_STATUS_CHECK_WAIT:
return [NSLocalizedString(@"Waiting to check existing data", "Torrent -> status string") stringByAppendingEllipsis];
case TR_STATUS_DOWNLOAD:
return NSLocalizedString(@"Downloading", "Torrent -> status string");
case TR_STATUS_SEED:
return NSLocalizedString(@"Seeding", "Torrent -> status string");
}
}
2008-11-02 01:07:01 +00:00
- (NSInteger) totalPeersConnected
2007-09-16 01:02:06 +00:00
{
return fStat->peersConnected;
}
2008-11-02 01:07:01 +00:00
- (NSInteger) totalPeersTracker
2007-09-16 01:02:06 +00:00
{
return fStat->peersFrom[TR_PEER_FROM_TRACKER];
}
2008-11-02 01:07:01 +00:00
- (NSInteger) totalPeersIncoming
2007-09-16 01:02:06 +00:00
{
return fStat->peersFrom[TR_PEER_FROM_INCOMING];
}
2008-11-02 01:07:01 +00:00
- (NSInteger) totalPeersCache
2007-09-16 01:02:06 +00:00
{
return fStat->peersFrom[TR_PEER_FROM_CACHE];
}
2008-11-02 01:07:01 +00:00
- (NSInteger) totalPeersPex
2007-09-16 01:02:06 +00:00
{
return fStat->peersFrom[TR_PEER_FROM_PEX];
}
- (NSInteger) totalPeersDHT
{
return fStat->peersFrom[TR_PEER_FROM_DHT];
}
2008-11-02 01:07:01 +00:00
- (NSInteger) totalPeersKnown
2007-10-01 16:23:00 +00:00
{
return fStat->peersKnown;
}
2008-11-02 01:07:01 +00:00
- (NSInteger) peersSendingToUs
2007-09-16 01:02:06 +00:00
{
return fStat->peersSendingToUs;
}
2008-11-02 01:07:01 +00:00
- (NSInteger) peersGettingFromUs
2007-09-16 01:02:06 +00:00
{
return fStat->peersGettingFromUs;
}
2008-11-02 01:07:01 +00:00
- (CGFloat) downloadRate
2007-09-16 01:02:06 +00:00
{
return fStat->pieceDownloadSpeed;
2007-09-16 01:02:06 +00:00
}
2008-11-02 01:07:01 +00:00
- (CGFloat) uploadRate
2007-09-16 01:02:06 +00:00
{
return fStat->pieceUploadSpeed;
2007-09-16 01:02:06 +00:00
}
2008-11-02 01:07:01 +00:00
- (CGFloat) totalRate
2007-11-23 16:03:49 +00:00
{
return [self downloadRate] + [self uploadRate];
}
- (uint64_t) haveVerified
2007-09-16 01:02:06 +00:00
{
return fStat->haveValid;
}
- (uint64_t) haveTotal
{
return [self haveVerified] + fStat->haveUnchecked;
2007-09-16 01:02:06 +00:00
}
2008-02-27 19:34:55 +00:00
- (uint64_t) totalSizeSelected
{
return fStat->sizeWhenDone;
2008-02-27 19:34:55 +00:00
}
2007-09-26 18:53:11 +00:00
- (uint64_t) downloadedTotal
{
2007-09-26 18:53:11 +00:00
return fStat->downloadedEver;
}
2007-09-26 18:53:11 +00:00
- (uint64_t) uploadedTotal
2007-09-16 01:02:06 +00:00
{
2007-09-26 18:53:11 +00:00
return fStat->uploadedEver;
2007-09-16 01:02:06 +00:00
}
- (uint64_t) failedHash
2007-09-16 01:02:06 +00:00
{
return fStat->corruptEver;
2007-09-16 01:02:06 +00:00
}
2008-11-02 01:07:01 +00:00
- (CGFloat) swarmSpeed
2007-09-16 01:02:06 +00:00
{
return fStat->swarmSpeed;
2007-09-16 01:02:06 +00:00
}
2008-11-02 01:07:01 +00:00
- (NSInteger) groupValue
2007-12-17 16:06:20 +00:00
{
return fGroupValue;
}
2008-11-02 01:07:01 +00:00
- (void) setGroupValue: (NSInteger) goupValue
2007-12-17 16:06:20 +00:00
{
fGroupValue = goupValue;
}
2008-11-02 01:07:01 +00:00
- (NSInteger) groupOrderValue
{
return [[GroupsController groups] rowValueForIndex: fGroupValue];
}
- (void) checkGroupValueForRemoval: (NSNotification *) notification
2007-12-17 16:06:20 +00:00
{
if (fGroupValue != -1 && [[[notification userInfo] objectForKey: @"Index"] intValue] == fGroupValue)
2007-12-17 16:06:20 +00:00
fGroupValue = -1;
}
2007-09-16 01:02:06 +00:00
- (NSArray *) fileList
{
return fFileList;
}
2008-11-02 01:07:01 +00:00
- (NSInteger) fileCount
2007-09-16 01:02:06 +00:00
{
return fInfo->fileCount;
}
- (void) updateFileStat
{
if (fFileStat)
tr_torrentFilesFree(fFileStat, [self fileCount]);
2007-09-16 01:02:06 +00:00
fFileStat = tr_torrentFiles(fHandle, NULL);
2007-09-16 01:02:06 +00:00
}
2008-11-02 01:07:01 +00:00
- (CGFloat) fileProgress: (FileListNode *) node
2007-09-16 01:02:06 +00:00
{
if ([self isComplete])
return 1.0;
if (!fFileStat)
2007-09-16 01:02:06 +00:00
[self updateFileStat];
NSIndexSet * indexSet = [node indexes];
if ([indexSet count] == 1)
return fFileStat[[indexSet firstIndex]].progress;
uint64_t have = 0;
2008-11-02 01:07:01 +00:00
for (NSInteger index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index])
have += fFileStat[index].bytesCompleted;
NSAssert([node size], @"directory in torrent file has size 0");
2008-11-02 01:07:01 +00:00
return (CGFloat)have / [node size];
2007-09-16 01:02:06 +00:00
}
- (NSArray *) flatFileList
{
return fFlatFileList;
}
2008-11-02 01:07:01 +00:00
- (BOOL) canChangeDownloadCheckForFile: (NSInteger) index
2007-09-16 01:02:06 +00:00
{
if (!fFileStat)
2007-09-16 01:02:06 +00:00
[self updateFileStat];
return [self fileCount] > 1 && fFileStat[index].progress < 1.0;
2007-09-16 01:02:06 +00:00
}
- (BOOL) canChangeDownloadCheckForFiles: (NSIndexSet *) indexSet
{
if ([self fileCount] <= 1 || [self isComplete])
return NO;
if (!fFileStat)
2007-09-16 01:02:06 +00:00
[self updateFileStat];
2008-11-02 01:07:01 +00:00
for (NSInteger index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index])
if (fFileStat[index].progress < 1.0)
2007-09-16 01:02:06 +00:00
return YES;
return NO;
}
2008-11-02 01:07:01 +00:00
- (NSInteger) checkForFiles: (NSIndexSet *) indexSet
2007-09-16 01:02:06 +00:00
{
BOOL onState = NO, offState = NO;
2008-11-02 01:07:01 +00:00
for (NSInteger index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index])
2007-09-16 01:02:06 +00:00
{
if (tr_torrentGetFileDL(fHandle, index) || ![self canChangeDownloadCheckForFile: index])
onState = YES;
else
offState = YES;
if (onState && offState)
return NSMixedState;
}
return onState ? NSOnState : NSOffState;
}
2008-11-02 01:07:01 +00:00
- (void) setFileCheckState: (NSInteger) state forIndexes: (NSIndexSet *) indexSet
2007-09-16 01:02:06 +00:00
{
NSUInteger count = [indexSet count];
2008-03-22 18:22:10 +00:00
tr_file_index_t * files = malloc(count * sizeof(tr_file_index_t));
for (NSUInteger index = [indexSet firstIndex], i = 0; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index], i++)
2007-09-16 01:02:06 +00:00
files[i] = index;
tr_torrentSetFileDLs(fHandle, files, count, state != NSOffState);
free(files);
[self update];
2008-02-27 19:34:55 +00:00
[[NSNotificationCenter defaultCenter] postNotificationName: @"TorrentFileCheckChange" object: self];
2007-09-16 01:02:06 +00:00
}
- (void) setFilePriority: (tr_priority_t) priority forIndexes: (NSIndexSet *) indexSet
2007-09-16 01:02:06 +00:00
{
const NSUInteger count = [indexSet count];
2008-03-22 18:22:10 +00:00
tr_file_index_t * files = malloc(count * sizeof(tr_file_index_t));
for (NSUInteger index = [indexSet firstIndex], i = 0; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index], i++)
2007-09-16 01:02:06 +00:00
files[i] = index;
2007-09-16 01:02:06 +00:00
tr_torrentSetFilePriorities(fHandle, files, count, priority);
free(files);
}
- (BOOL) hasFilePriority: (tr_priority_t) priority forIndexes: (NSIndexSet *) indexSet
2007-09-16 01:02:06 +00:00
{
2008-11-02 01:07:01 +00:00
for (NSInteger index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index])
2007-09-16 01:02:06 +00:00
if (priority == tr_torrentGetFilePriority(fHandle, index) && [self canChangeDownloadCheckForFile: index])
return YES;
return NO;
}
- (NSSet *) filePrioritiesForIndexes: (NSIndexSet *) indexSet
{
BOOL low = NO, normal = NO, high = NO;
NSMutableSet * priorities = [NSMutableSet setWithCapacity: 3];
2008-11-02 01:07:01 +00:00
for (NSInteger index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index])
2007-09-16 01:02:06 +00:00
{
if (![self canChangeDownloadCheckForFile: index])
continue;
const tr_priority_t priority = tr_torrentGetFilePriority(fHandle, index);
2007-09-16 01:02:06 +00:00
if (priority == TR_PRI_LOW)
{
if (low)
continue;
low = YES;
}
else if (priority == TR_PRI_HIGH)
{
if (high)
continue;
high = YES;
}
else
{
if (normal)
continue;
normal = YES;
}
[priorities addObject: [NSNumber numberWithInteger: priority]];
2007-09-16 01:02:06 +00:00
if (low && normal && high)
break;
}
return priorities;
}
- (NSDate *) dateAdded
{
2009-02-17 04:00:53 +00:00
const time_t date = fStat->addedDate;
return [NSDate dateWithTimeIntervalSince1970: date];
2007-09-16 01:02:06 +00:00
}
- (NSDate *) dateCompleted
{
2009-02-17 04:00:53 +00:00
const time_t date = fStat->doneDate;
return date != 0 ? [NSDate dateWithTimeIntervalSince1970: date] : nil;
2007-09-16 01:02:06 +00:00
}
- (NSDate *) dateActivity
{
2009-02-17 04:00:53 +00:00
const time_t date = fStat->activityDate;
return date != 0 ? [NSDate dateWithTimeIntervalSince1970: date] : nil;
2007-09-16 01:02:06 +00:00
}
- (NSDate *) dateActivityOrAdd
{
NSDate * date = [self dateActivity];
return date ? date : [self dateAdded];
}
2008-11-02 01:07:01 +00:00
- (NSInteger) stalledMinutes
2007-09-16 01:02:06 +00:00
{
time_t start = fStat->startDate;
if (start == 0)
2007-09-16 01:02:06 +00:00
return -1;
NSDate * started = [NSDate dateWithTimeIntervalSince1970: start],
2007-09-16 01:02:06 +00:00
* activity = [self dateActivity];
NSDate * laterDate = activity ? [started laterDate: activity] : started;
2007-09-16 01:02:06 +00:00
return -1 * [laterDate timeIntervalSinceNow] / 60;
}
- (BOOL) isStalled
{
return fStalled;
}
- (NSInteger) stateSortKey
2007-09-16 01:02:06 +00:00
{
if (![self isActive]) //paused
return 0;
else if ([self isSeeding]) //seeding
return 1;
else //downloading
return 2;
2007-09-16 01:02:06 +00:00
}
- (tr_torrent *) torrentStruct
2007-09-16 01:02:06 +00:00
{
return fHandle;
2007-09-16 01:02:06 +00:00
}
- (NSURL *) previewItemURL
{
return [NSURL fileURLWithPath: [self dataLocation]];
}
2007-09-16 01:02:06 +00:00
@end
@implementation Torrent (Private)
- (id) initWithPath: (NSString *) path hash: (NSString *) hashString torrentStruct: (tr_torrent *) torrentStruct lib: (tr_session *) lib
2007-09-16 01:02:06 +00:00
downloadFolder: (NSString *) downloadFolder
useIncompleteFolder: (NSNumber *) useIncompleteFolder incompleteFolder: (NSString *) incompleteFolder
2007-12-17 16:06:20 +00:00
waitToStart: (NSNumber *) waitToStart
groupValue: (NSNumber *) groupValue
2007-09-16 01:02:06 +00:00
{
if (!(self = [super init]))
return nil;
fDefaults = [NSUserDefaults standardUserDefaults];
fDownloadFolder = downloadFolder ? downloadFolder : [fDefaults stringForKey: @"DownloadFolder"];
fDownloadFolder = [[fDownloadFolder stringByExpandingTildeInPath] retain];
fUseIncompleteFolder = useIncompleteFolder ? [useIncompleteFolder boolValue]
: [fDefaults boolForKey: @"UseIncompleteDownloadFolder"];
if (fUseIncompleteFolder)
{
fIncompleteFolder = incompleteFolder ? incompleteFolder : [fDefaults stringForKey: @"IncompleteDownloadFolder"];
fIncompleteFolder = [[fIncompleteFolder stringByExpandingTildeInPath] retain];
}
2008-05-26 23:23:07 +00:00
if (torrentStruct)
2007-09-16 01:02:06 +00:00
{
2008-05-26 23:23:07 +00:00
fHandle = torrentStruct;
fInfo = tr_torrentInfo(fHandle);
2008-05-26 23:23:07 +00:00
NSString * currentDownloadFolder = [self shouldUseIncompleteFolderForName: [NSString stringWithUTF8String: fInfo->name]]
? fIncompleteFolder : fDownloadFolder;
2008-05-26 23:23:07 +00:00
tr_torrentSetDownloadDir(fHandle, [currentDownloadFolder UTF8String]);
2007-09-16 01:02:06 +00:00
}
2008-05-26 23:23:07 +00:00
else
2007-09-16 01:02:06 +00:00
{
2008-05-26 23:23:07 +00:00
//set libtransmission settings for initialization
tr_ctor * ctor = tr_ctorNew(lib);
tr_ctorSetPaused(ctor, TR_FORCE, YES);
tr_ctorSetPeerLimit(ctor, TR_FALLBACK, [fDefaults integerForKey: @"PeersTorrent"]);
2009-08-05 02:13:45 +00:00
int result = TR_PARSE_ERR;
if (path)
2009-05-22 02:52:28 +00:00
result = tr_ctorSetMetainfoFromFile(ctor, [path UTF8String]);
//backup - shouldn't be needed after upgrade to 1.70
2009-08-05 02:13:45 +00:00
if (result != TR_PARSE_OK && hashString)
2009-05-22 12:11:45 +00:00
result = tr_ctorSetMetainfoFromHash(ctor, [hashString UTF8String]);
2008-05-26 23:23:07 +00:00
2009-08-05 02:13:45 +00:00
if (result == TR_PARSE_OK)
{
2009-05-22 02:52:28 +00:00
tr_info info;
result = tr_torrentParse(ctor, &info);
2009-08-05 02:13:45 +00:00
if (result == TR_PARSE_OK)
2009-05-22 02:52:28 +00:00
{
NSString * currentDownloadFolder = [self shouldUseIncompleteFolderForName: [NSString stringWithUTF8String: info.name]]
? fIncompleteFolder : fDownloadFolder;
tr_ctorSetDownloadDir(ctor, TR_FORCE, [currentDownloadFolder UTF8String]);
fHandle = tr_torrentNew(ctor, NULL);
}
2009-08-05 02:13:45 +00:00
if (result != TR_PARSE_ERR)
2009-05-22 02:52:28 +00:00
tr_metainfoFree(&info);
}
2008-05-26 23:23:07 +00:00
tr_ctorFree(ctor);
if (!fHandle)
{
[self release];
return nil;
}
fInfo = tr_torrentInfo(fHandle);
2007-09-16 01:02:06 +00:00
}
tr_torrentSetCompletenessCallback(fHandle, completenessChangeCallback, self);
tr_torrentSetRatioLimitHitCallback(fHandle, ratioLimitHitCallback, self);
2008-02-16 19:32:22 +00:00
fNameString = [[NSString alloc] initWithUTF8String: fInfo->name];
fHashString = [[NSString alloc] initWithUTF8String: fInfo->hashString];
2007-09-16 01:02:06 +00:00
fFinishedSeeding = NO;
2008-01-04 04:54:26 +00:00
fWaitToStart = waitToStart && [waitToStart boolValue];
fResumeOnWake = NO;
[self createFileList];
fGroupValue = groupValue ? [groupValue intValue] : [[GroupsController groups] groupIndexForTorrent: self];
2007-09-16 01:02:06 +00:00
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(checkGroupValueForRemoval:)
2007-12-17 16:06:20 +00:00
name: @"GroupValueRemoved" object: nil];
2007-09-16 01:02:06 +00:00
[self update];
//mark incomplete files to be ignored by Time Machine
[self setTimeMachineExclude: ![self allDownloaded] forPath: [[self downloadFolder] stringByAppendingPathComponent: [self name]]];
2007-09-16 01:02:06 +00:00
return self;
}
- (void) createFileList
{
if ([self isFolder])
2007-09-16 01:02:06 +00:00
{
2008-11-02 01:07:01 +00:00
NSInteger count = [self fileCount];
NSMutableArray * fileList = [[NSMutableArray alloc] initWithCapacity: count],
* flatFileList = [[NSMutableArray alloc] initWithCapacity: count];
2007-09-16 01:02:06 +00:00
2008-11-02 01:07:01 +00:00
for (NSInteger i = 0; i < count; i++)
2007-09-16 01:02:06 +00:00
{
tr_file * file = &fInfo->files[i];
NSMutableArray * pathComponents = [[[NSString stringWithUTF8String: file->name] pathComponents] mutableCopy];
NSString * path = [pathComponents objectAtIndex: 0];
NSString * name = [pathComponents objectAtIndex: 1];
[pathComponents removeObjectsAtIndexes: [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, 2)]];
if ([pathComponents count] > 0)
{
//determine if folder node already exists
2008-12-26 15:11:37 +00:00
FileListNode * node;
2008-12-26 07:25:17 +00:00
for (node in fileList)
if ([[node name] isEqualToString: name] && [node isFolder])
break;
if (!node)
{
node = [[FileListNode alloc] initWithFolderName: name path: path];
[fileList addObject: node];
[node release];
}
[node insertIndex: i withSize: file->length];
[self insertPath: pathComponents forParent: node fileSize: file->length index: i flatList: flatFileList];
}
else
{
FileListNode * node = [[FileListNode alloc] initWithFileName: name path: path size: file->length index: i];
[fileList addObject: node];
[flatFileList addObject: node];
[node release];
}
[pathComponents release];
2007-09-16 01:02:06 +00:00
}
fFileList = [[NSArray alloc] initWithArray: fileList];
[fileList release];
fFlatFileList = [[NSArray alloc] initWithArray: flatFileList];
[flatFileList release];
}
else
{
FileListNode * node = [[FileListNode alloc] initWithFileName: [self name] path: @"" size: [self size] index: 0];
fFileList = [[NSArray arrayWithObject: node] retain];
fFlatFileList = [fFileList copy];
[node release];
2007-09-16 01:02:06 +00:00
}
}
- (void) insertPath: (NSMutableArray *) components forParent: (FileListNode *) parent fileSize: (uint64_t) size
index: (NSInteger) index flatList: (NSMutableArray *) flatFileList
2007-09-16 01:02:06 +00:00
{
NSString * name = [components objectAtIndex: 0];
BOOL isFolder = [components count] > 1;
FileListNode * node = nil;
2007-09-16 01:02:06 +00:00
if (isFolder)
{
2008-12-26 07:25:17 +00:00
for (node in [parent children])
if ([[node name] isEqualToString: name] && [node isFolder])
2007-09-16 01:02:06 +00:00
break;
}
//create new folder or file if it doesn't already exist
if (!node)
2007-09-16 01:02:06 +00:00
{
if (isFolder)
node = [[FileListNode alloc] initWithFolderName: name path: [parent fullPath]];
2007-09-16 01:02:06 +00:00
else
{
node = [[FileListNode alloc] initWithFileName: name path: [parent fullPath] size: size index: index];
[flatFileList addObject: node];
}
[parent insertChild: node];
[node release];
2007-09-16 01:02:06 +00:00
}
if (isFolder)
{
[node insertIndex: index withSize: size];
2007-09-16 01:02:06 +00:00
[components removeObjectAtIndex: 0];
[self insertPath: components forParent: node fileSize: size index: index flatList: flatFileList];
2007-09-16 01:02:06 +00:00
}
}
- (BOOL) shouldUseIncompleteFolderForName: (NSString *) name
{
return fUseIncompleteFolder &&
![[NSFileManager defaultManager] fileExistsAtPath: [fDownloadFolder stringByAppendingPathComponent: name]];
}
- (void) updateDownloadFolder
{
//remove old Time Machine location
[self setTimeMachineExclude: NO forPath: [[self downloadFolder] stringByAppendingPathComponent: [self name]]];
2007-09-16 01:02:06 +00:00
NSString * folder = [self shouldUseIncompleteFolderForName: [self name]] ? fIncompleteFolder : fDownloadFolder;
tr_torrentSetDownloadDir(fHandle, [folder UTF8String]);
[self setTimeMachineExclude: ![self allDownloaded] forPath: [folder stringByAppendingPathComponent: [self name]]];
2007-09-16 01:02:06 +00:00
}
//status has been retained
- (void) completenessChange: (NSNumber *) status
{
fStat = tr_torrentStat(fHandle); //don't call update yet to avoid auto-stop
BOOL canMove;
switch ([status intValue])
{
case TR_SEED:
case TR_PARTIAL_SEED:
canMove = YES;
2008-02-16 19:32:22 +00:00
//move file from incomplete folder to download folder
if (fUseIncompleteFolder && ![[self downloadFolder] isEqualToString: fDownloadFolder]
&& (canMove = [self alertForMoveFolderAvailable]))
{
[self quickPause];
if ([[NSFileManager defaultManager] moveItemAtPath: [[self downloadFolder] stringByAppendingPathComponent: [self name]]
toPath: [fDownloadFolder stringByAppendingPathComponent: [self name]] error: NULL])
[self updateDownloadFolder];
else
canMove = NO;
[self endQuickPause];
}
if (!canMove)
{
fUseIncompleteFolder = NO;
[fDownloadFolder release];
fDownloadFolder = fIncompleteFolder;
fIncompleteFolder = nil;
}
//allow to be backed up by Time Machine
[self setTimeMachineExclude: NO forPath: [[self downloadFolder] stringByAppendingPathComponent: [self name]]];
[[NSNotificationCenter defaultCenter] postNotificationName: @"TorrentFinishedDownloading" object: self];
break;
case TR_LEECH:
//do not allow to be backed up by Time Machine
[self setTimeMachineExclude: YES forPath: [[self downloadFolder] stringByAppendingPathComponent: [self name]]];
[[NSNotificationCenter defaultCenter] postNotificationName: @"TorrentRestartedDownloading" object: self];
break;
}
[status release];
[self update];
}
- (void) ratioLimitHit
{
fStat = tr_torrentStat(fHandle);
[[NSNotificationCenter defaultCenter] postNotificationName: @"TorrentStoppedForRatio" object: self];
fFinishedSeeding = YES;
}
2007-09-16 01:02:06 +00:00
- (void) quickPause
{
if (fQuickPauseDict)
return;
fQuickPauseDict = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithInt: [self usesSpeedLimit: YES]], @"UploadUsesSpeedLimit",
2007-09-16 01:02:06 +00:00
[NSNumber numberWithInt: [self speedLimit: YES]], @"UploadSpeedLimit",
[NSNumber numberWithInt: [self usesSpeedLimit: NO]], @"DownloadUsesSpeedLimit",
2007-09-16 01:02:06 +00:00
[NSNumber numberWithInt: [self speedLimit: NO]], @"DownloadSpeedLimit", nil];
[self setUseSpeedLimit: YES upload: YES];
2007-09-16 01:02:06 +00:00
[self setSpeedLimit: 0 upload: YES];
[self setUseSpeedLimit: YES upload: NO];
2007-09-16 01:02:06 +00:00
[self setSpeedLimit: 0 upload: NO];
}
- (void) endQuickPause
{
if (!fQuickPauseDict)
return;
[self setUseSpeedLimit: [[fQuickPauseDict objectForKey: @"UploadUsesSpeedLimit"] intValue] upload: YES];
2007-09-16 01:02:06 +00:00
[self setSpeedLimit: [[fQuickPauseDict objectForKey: @"UploadSpeedLimit"] intValue] upload: YES];
[self setUseSpeedLimit: [[fQuickPauseDict objectForKey: @"DownloadUsesSpeedLimit"] intValue] upload: NO];
2007-09-16 01:02:06 +00:00
[self setSpeedLimit: [[fQuickPauseDict objectForKey: @"DownloadSpeedLimit"] intValue] upload: NO];
[fQuickPauseDict release];
fQuickPauseDict = nil;
}
- (NSString *) etaString
2008-04-07 02:55:33 +00:00
{
const NSInteger eta = [self eta];
2008-04-07 02:55:33 +00:00
switch (eta)
{
case TR_ETA_NOT_AVAIL:
2008-04-07 02:55:33 +00:00
case TR_ETA_UNKNOWN:
return NSLocalizedString(@"remaining time unknown", "Torrent -> eta string");
default:
return [NSString stringWithFormat: NSLocalizedString(@"%@ remaining", "Torrent -> eta string"),
[NSString timeString: eta showSeconds: YES maxFields: 2]];
2008-04-07 02:55:33 +00:00
}
}
- (void) updateAllTrackers: (NSMutableArray *) trackers
{
//get count
2008-11-02 01:07:01 +00:00
NSInteger count = 0;
2008-12-26 07:25:17 +00:00
for (id object in trackers)
if (![object isKindOfClass: [NSNumber class]])
count++;
//recreate the tracker structure
tr_tracker_info * trackerStructs = tr_new(tr_tracker_info, count);
2008-11-02 01:07:01 +00:00
NSInteger tier = 0, i = 0;
2008-12-26 07:25:17 +00:00
for (id object in trackers)
{
if (![object isKindOfClass: [NSNumber class]])
{
trackerStructs[i].tier = tier;
trackerStructs[i].announce = (char *)[object UTF8String];
i++;
}
else
tier++;
}
tr_torrentSetAnnounceList(fHandle, trackerStructs, count);
tr_free(trackerStructs);
}
- (void) setTimeMachineExclude: (BOOL) exclude forPath: (NSString *) path
{
2008-12-26 05:57:51 +00:00
CSBackupSetItemExcluded((CFURLRef)[NSURL fileURLWithPath: path], exclude, true);
}
2007-09-16 01:02:06 +00:00
@end