From a009a7f930f4f6b94ae75f1984ba8f0a40fb7545 Mon Sep 17 00:00:00 2001 From: SweetPPro Date: Thu, 3 Mar 2022 00:11:04 +0100 Subject: [PATCH] migrate macOS client to using tr_sessionLoadTorrents (#2686) * migrate macOS client to using tr_sessionLoadTorrents With this PR the macOS client now loads torrents at startup using `tr_sessionLoadTorrents` The history file is used in tandem to provide macOS specific additions --- macosx/Controller.mm | 39 +++++++++++++++++++++++++++---- macosx/Torrent.h | 2 +- macosx/Torrent.mm | 55 +++++++++----------------------------------- 3 files changed, 46 insertions(+), 50 deletions(-) diff --git a/macosx/Controller.mm b/macosx/Controller.mm index 37e5303ac..5beb8328d 100644 --- a/macosx/Controller.mm +++ b/macosx/Controller.mm @@ -252,6 +252,7 @@ static void removeKeRangerRansomware() @property(nonatomic, readonly) NSMutableArray* fTorrents; @property(nonatomic, readonly) NSMutableArray* fDisplayedTorrents; +@property(nonatomic, readonly) NSMutableDictionary* fTorrentHashes; @property(nonatomic, readonly) InfoWindowController* fInfoController; @property(nonatomic) MessageWindowController* fMessageController; @@ -514,6 +515,7 @@ static void removeKeRangerRansomware() _fTorrents = [[NSMutableArray alloc] init]; _fDisplayedTorrents = [[NSMutableArray alloc] init]; + _fTorrentHashes = [[NSMutableDictionary alloc] init]; _fInfoController = [[InfoWindowController alloc] init]; @@ -642,8 +644,33 @@ static void removeKeRangerRansomware() { NSLog(@"Could not IORegisterForSystemPower"); } + + auto* const session = self.fLib; //load previous transfers + tr_ctor* ctor = tr_ctorNew(session); + tr_ctorSetPaused(ctor, TR_FORCE, true); // paused by default; unpause below after checking state history + int n_torrents = 0; + tr_torrent** loaded_torrents = tr_sessionLoadTorrents(session, ctor, &n_torrents); + tr_ctorFree(ctor); + + // process the loaded torrents + for (int i = 0; i < n_torrents; ++i) + { + struct tr_torrent* tor = loaded_torrents[i]; + NSString* location; + if (tr_torrentGetDownloadDir(tor) != NULL) + { + location = @(tr_torrentGetDownloadDir(tor)); + } + Torrent* torrent = [[Torrent alloc] initWithTorrentStruct:tor location:location lib:self.fLib]; + [self.fTorrents addObject:torrent]; + self.fTorrentHashes[torrent.hashString] = torrent; + } + + + //update previous transfers state by recreating a torrent from history + //and comparing to torrents already loaded via tr_sessionLoadTorrents NSString* historyFile = [self.fConfigDirectory stringByAppendingPathComponent:TRANSFER_PLIST]; NSArray* history = [NSArray arrayWithContentsOfFile:historyFile]; if (!history) @@ -661,13 +688,14 @@ static void removeKeRangerRansomware() NSMutableArray* waitToStartTorrents = [NSMutableArray arrayWithCapacity:((history.count > 0 && !self.fPauseOnLaunch) ? history.count - 1 : 0)]; + Torrent *t = [[Torrent alloc] init]; for (NSDictionary* historyItem in history) { - Torrent* torrent; - if ((torrent = [[Torrent alloc] initWithHistory:historyItem lib:self.fLib forcePause:self.fPauseOnLaunch])) - { - [self.fTorrents addObject:torrent]; - + NSString *hash = historyItem[@"TorrentHash"]; + if ([self.fTorrentHashes.allKeys containsObject:hash]) { + Torrent *torrent = self.fTorrentHashes[hash]; + [t setResumeStatusForTorrent:torrent withHistory:historyItem forcePause:self.fPauseOnLaunch]; + NSNumber* waitToStart; if (!self.fPauseOnLaunch && (waitToStart = historyItem[@"WaitToStart"]) && waitToStart.boolValue) { @@ -2456,6 +2484,7 @@ static void removeKeRangerRansomware() for (Torrent* torrent in self.fTorrents) { [history addObject:torrent.history]; + self.fTorrentHashes[torrent.hashString] = torrent; } NSString* historyFile = [self.fConfigDirectory stringByAppendingPathComponent:TRANSFER_PLIST]; diff --git a/macosx/Torrent.h b/macosx/Torrent.h index 6cff6b666..58535a2f0 100644 --- a/macosx/Torrent.h +++ b/macosx/Torrent.h @@ -24,7 +24,7 @@ typedef NS_ENUM(unsigned int, TorrentDeterminationType) { lib:(tr_session*)lib; - (instancetype)initWithTorrentStruct:(tr_torrent*)torrentStruct location:(NSString*)location lib:(tr_session*)lib; - (instancetype)initWithMagnetAddress:(NSString*)address location:(NSString*)location lib:(tr_session*)lib; -- (instancetype)initWithHistory:(NSDictionary*)history lib:(tr_session*)lib forcePause:(BOOL)pause; +- (void)setResumeStatusForTorrent:(Torrent*)torrent withHistory:(NSDictionary*)history forcePause:(BOOL)pause; @property(nonatomic, readonly) NSDictionary* history; diff --git a/macosx/Torrent.mm b/macosx/Torrent.mm index b15d5d130..d53fe5a7e 100644 --- a/macosx/Torrent.mm +++ b/macosx/Torrent.mm @@ -195,58 +195,25 @@ bool trashDataFile(char const* filename, tr_error** error) return self; } -- (instancetype)initWithHistory:(NSDictionary*)history lib:(tr_session*)lib forcePause:(BOOL)pause +- (void)setResumeStatusForTorrent:(Torrent*)torrent withHistory:(NSDictionary*)history forcePause:(BOOL)pause { - self = [self initWithPath:history[@"InternalTorrentPath"] hash:history[@"TorrentHash"] torrentStruct:NULL magnetAddress:nil - lib:lib - groupValue:history[@"GroupValue"] - removeWhenFinishSeeding:history[@"RemoveWhenFinishSeeding"] - downloadFolder:history[@"DownloadFolder"] //upgrading from versions < 1.80 - legacyIncompleteFolder:[history[@"UseIncompleteFolder"] boolValue] //upgrading from versions < 1.80 - ? - history[@"IncompleteFolder"] : - nil]; - - if (self) + //start transfer + NSNumber* active; + if (!pause && (active = history[@"Active"]) && active.boolValue) { - //start transfer - NSNumber* active; - if (!pause && (active = history[@"Active"]) && active.boolValue) - { - _fStat = tr_torrentStat(_fHandle); - [self startTransferNoQueue]; - } - - //upgrading from versions < 1.60: get old stop ratio settings - NSNumber* ratioSetting; - if ((ratioSetting = history[@"RatioSetting"])) - { - switch (ratioSetting.intValue) - { - case NSControlStateValueOn: - self.ratioSetting = TR_RATIOLIMIT_SINGLE; - break; - case NSControlStateValueOff: - self.ratioSetting = TR_RATIOLIMIT_UNLIMITED; - break; - case NSControlStateValueMixed: - self.ratioSetting = TR_RATIOLIMIT_GLOBAL; - break; - } - } - NSNumber* ratioLimit; - if ((ratioLimit = history[@"RatioLimit"])) - { - self.ratioLimit = ratioLimit.floatValue; - } + [torrent startTransferNoQueue]; + } + + NSNumber* ratioLimit; + if ((ratioLimit = history[@"RatioLimit"])) + { + self.ratioLimit = ratioLimit.floatValue; } - return self; } - (NSDictionary*)history { return @{ - @"InternalTorrentPath" : self.torrentLocation, @"TorrentHash" : self.hashString, @"Active" : @(self.active), @"WaitToStart" : @(self.waitingToStart),