From cb82ea8e26f80f9df7c77411c58c5325498cd6df Mon Sep 17 00:00:00 2001 From: Mitchell Livingston Date: Fri, 29 Jun 2007 01:01:55 +0000 Subject: [PATCH] don't crash when restoring torrents on launch, and allow removing then re-adding the same torrent --- macosx/Controller.m | 12 +++++++- macosx/Torrent.h | 3 ++ macosx/Torrent.m | 70 +++++++++++++++++++++++++++++---------------- 3 files changed, 60 insertions(+), 25 deletions(-) diff --git a/macosx/Controller.m b/macosx/Controller.m index 1649c766f..54f497874 100644 --- a/macosx/Controller.m +++ b/macosx/Controller.m @@ -311,6 +311,7 @@ static void sleepCallBack(void * controller, io_service_t y, natural_t messageTy { [fTorrents addObject: torrent]; [torrent release]; + NSLog(@"%u", (long)[torrent retainCount]); } [history release]; @@ -545,9 +546,17 @@ static void sleepCallBack(void * controller, io_service_t y, natural_t messageTy //save history and stop running torrents [self updateTorrentHistory]; #warning check if all torrents are fully released + //Torrent * torrent = [fTorrents objectAtIndex: 0]; + //NSLog(@"%d", (long)[torrent retainCount]); [fDisplayedTorrents removeAllObjects]; - [fTorrents removeAllObjects]; + enumerator = [fTorrents objectEnumerator]; + Torrent * torrent; + while ((torrent = [enumerator nextObject])) + [torrent endTorrent]; + + [fTorrents removeAllObjects]; + //NSLog(@"%d", (long)[torrent retainCount]); //disable NAT traversal tr_natTraversalEnable(fLib, 0); @@ -1118,6 +1127,7 @@ static void sleepCallBack(void * controller, io_service_t y, natural_t messageTy [fTorrents removeObject: torrent]; [fDisplayedTorrents removeObject: torrent]; + [torrent endTorrent]; } [torrents release]; diff --git a/macosx/Torrent.h b/macosx/Torrent.h index 7bf14864a..2e2a4425e 100644 --- a/macosx/Torrent.h +++ b/macosx/Torrent.h @@ -39,6 +39,7 @@ tr_stat_t * fStat; int fID; + BOOL fInitialized; BOOL fResumeOnWake; NSDate * fDateAdded, * fDateCompleted, * fAnnounceDate, @@ -75,6 +76,8 @@ - (NSDictionary *) history; +- (void) endTorrent; + - (void) changeIncompleteDownloadFolder: (NSString *) folder; - (void) changeDownloadFolder: (NSString *) folder; - (NSString *) downloadFolder; diff --git a/macosx/Torrent.m b/macosx/Torrent.m index dcec6d60f..481e6dc6a 100644 --- a/macosx/Torrent.m +++ b/macosx/Torrent.m @@ -49,7 +49,7 @@ static int static_lastid = 0; filesShouldDownload: (NSArray *) filesShouldDownload filePriorities: (NSArray *) filePriorities; - (void) historyFilePriorities: (NSMutableArray *) history forItems: (NSArray *) items; -- (BOOL) shouldUseIncompleteFolder; +- (BOOL) shouldUseIncompleteFolderForName: (NSString *) name; - (void) updateDownloadFolder; - (void) createFileListShouldDownload: (NSArray *) filesShouldDownload priorities: (NSArray *) filePriorities; @@ -126,16 +126,8 @@ static uint32_t kRed = BE(0xFF6450FF), //255, 100, 80 if (self) { //start transfer - BOOL start = YES; NSNumber * active; - NSString * paused; - if ((active = [history objectForKey: @"Active"])) - start = [active boolValue]; - else if ((paused = [history objectForKey: @"Paused"])) //old way of storing active status - start = [paused isEqualToString: @"NO"]; - else; - - if (start) + if ((active = [history objectForKey: @"Active"]) && [active boolValue]) { fStat = tr_torrentStat(fHandle); [self startTransfer]; @@ -196,11 +188,24 @@ static uint32_t kRed = BE(0xFF6450FF), //255, 100, 80 return history; } -- (void) dealloc +#warning need for now :( +- (void) endTorrent { - if (fHandle) + if (fInitialized) { tr_torrentClose(fHandle); + fInitialized = NO; + } +} + +- (void) dealloc +{ + NSLog(@"Released!"); + NSLog(@"%d", (long)[self retainCount]); + if (fHandle) + { + if (fInitialized) + tr_torrentClose(fHandle); if (fDownloadFolder) [fDownloadFolder release]; @@ -610,7 +615,6 @@ static uint32_t kRed = BE(0xFF6450FF), //255, 100, 80 - (void) resetCache { - #warning look over tr_torrentRecheck(fHandle); } @@ -1481,9 +1485,11 @@ static uint32_t kRed = BE(0xFF6450FF), //255, 100, 80 waitToStart: (NSNumber *) waitToStart orderValue: (NSNumber *) orderValue filesShouldDownload: (NSArray *) filesShouldDownload filePriorities: (NSArray *) filePriorities; { + fInitialized = NO; if (!(self = [super init])) return nil; + NSLog(@"%d", (long)[self retainCount]); static_lastid++; fID = static_lastid; @@ -1504,27 +1510,43 @@ static uint32_t kRed = BE(0xFF6450FF), //255, 100, 80 fIncompleteFolder = incompleteFolder ? incompleteFolder : [fDefaults stringForKey: @"IncompleteDownloadFolder"]; fIncompleteFolder = [[fIncompleteFolder stringByExpandingTildeInPath] retain]; } - NSString * currentDownloadFolder = [self shouldUseIncompleteFolder] ? fIncompleteFolder : fDownloadFolder; - #warning give error for duplicate? + #warning duplicate warning? + NSString * currentDownloadFolder; + tr_info_t info; int error; if (hashString) - fHandle = tr_torrentInitSaved(fLib, [hashString UTF8String], [currentDownloadFolder UTF8String], TR_FLAG_SAVE, & error); - + { + if (tr_torrentParseHash(fLib, [hashString UTF8String], NULL, &info) == TR_OK) + { + currentDownloadFolder = [self shouldUseIncompleteFolderForName: [NSString stringWithUTF8String: info.name]] + ? fIncompleteFolder : fDownloadFolder; + fHandle = tr_torrentInitSaved(fLib, [hashString UTF8String], [currentDownloadFolder UTF8String], TR_FLAG_SAVE, &error); + } + tr_metainfoFree(&info); + } if (!fHandle && path) - fHandle = tr_torrentInit(fLib, [path UTF8String], [currentDownloadFolder UTF8String], TR_FLAG_SAVE, & error); - + { + if (tr_torrentParse(fLib, [path UTF8String], NULL, &info) == TR_OK) + { + currentDownloadFolder = [self shouldUseIncompleteFolderForName: [NSString stringWithUTF8String: info.name]] + ? fIncompleteFolder : fDownloadFolder; + fHandle = tr_torrentInit(fLib, [path UTF8String], [currentDownloadFolder UTF8String], TR_FLAG_SAVE, &error); + } + tr_metainfoFree(&info); + } if (!fHandle) { [self release]; return nil; } + fInitialized = YES; + + fInfo = tr_torrentInfo(fHandle); NSNotificationCenter * nc = [NSNotificationCenter defaultCenter]; [nc addObserver: self selector: @selector(updateSpeedSetting:) name: @"UpdateSpeedSetting" object: nil]; - - fInfo = tr_torrentInfo(fHandle); fDateAdded = dateAdded ? [dateAdded retain] : [[NSDate alloc] init]; if (dateCompleted) @@ -1702,15 +1724,15 @@ static uint32_t kRed = BE(0xFF6450FF), //255, 100, 80 } } -- (BOOL) shouldUseIncompleteFolder +- (BOOL) shouldUseIncompleteFolderForName: (NSString *) name { return fUseIncompleteFolder && - ![[NSFileManager defaultManager] fileExistsAtPath: [fDownloadFolder stringByAppendingPathComponent: [self name]]]; + ![[NSFileManager defaultManager] fileExistsAtPath: [fDownloadFolder stringByAppendingPathComponent: name]]; } - (void) updateDownloadFolder { - NSString * folder = [self shouldUseIncompleteFolder] ? fIncompleteFolder : fDownloadFolder; + NSString * folder = [self shouldUseIncompleteFolderForName: [self name]] ? fIncompleteFolder : fDownloadFolder; tr_torrentSetFolder(fHandle, [folder UTF8String]); }