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
This commit is contained in:
SweetPPro 2022-03-03 00:11:04 +01:00 committed by GitHub
parent 60ef1abadf
commit a009a7f930
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 50 deletions

View File

@ -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];

View File

@ -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;

View File

@ -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),