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:
parent
60ef1abadf
commit
a009a7f930
|
@ -252,6 +252,7 @@ static void removeKeRangerRansomware()
|
||||||
|
|
||||||
@property(nonatomic, readonly) NSMutableArray* fTorrents;
|
@property(nonatomic, readonly) NSMutableArray* fTorrents;
|
||||||
@property(nonatomic, readonly) NSMutableArray* fDisplayedTorrents;
|
@property(nonatomic, readonly) NSMutableArray* fDisplayedTorrents;
|
||||||
|
@property(nonatomic, readonly) NSMutableDictionary* fTorrentHashes;
|
||||||
|
|
||||||
@property(nonatomic, readonly) InfoWindowController* fInfoController;
|
@property(nonatomic, readonly) InfoWindowController* fInfoController;
|
||||||
@property(nonatomic) MessageWindowController* fMessageController;
|
@property(nonatomic) MessageWindowController* fMessageController;
|
||||||
|
@ -514,6 +515,7 @@ static void removeKeRangerRansomware()
|
||||||
|
|
||||||
_fTorrents = [[NSMutableArray alloc] init];
|
_fTorrents = [[NSMutableArray alloc] init];
|
||||||
_fDisplayedTorrents = [[NSMutableArray alloc] init];
|
_fDisplayedTorrents = [[NSMutableArray alloc] init];
|
||||||
|
_fTorrentHashes = [[NSMutableDictionary alloc] init];
|
||||||
|
|
||||||
_fInfoController = [[InfoWindowController alloc] init];
|
_fInfoController = [[InfoWindowController alloc] init];
|
||||||
|
|
||||||
|
@ -642,8 +644,33 @@ static void removeKeRangerRansomware()
|
||||||
{
|
{
|
||||||
NSLog(@"Could not IORegisterForSystemPower");
|
NSLog(@"Could not IORegisterForSystemPower");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto* const session = self.fLib;
|
||||||
|
|
||||||
//load previous transfers
|
//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];
|
NSString* historyFile = [self.fConfigDirectory stringByAppendingPathComponent:TRANSFER_PLIST];
|
||||||
NSArray* history = [NSArray arrayWithContentsOfFile:historyFile];
|
NSArray* history = [NSArray arrayWithContentsOfFile:historyFile];
|
||||||
if (!history)
|
if (!history)
|
||||||
|
@ -661,13 +688,14 @@ static void removeKeRangerRansomware()
|
||||||
NSMutableArray* waitToStartTorrents = [NSMutableArray
|
NSMutableArray* waitToStartTorrents = [NSMutableArray
|
||||||
arrayWithCapacity:((history.count > 0 && !self.fPauseOnLaunch) ? history.count - 1 : 0)];
|
arrayWithCapacity:((history.count > 0 && !self.fPauseOnLaunch) ? history.count - 1 : 0)];
|
||||||
|
|
||||||
|
Torrent *t = [[Torrent alloc] init];
|
||||||
for (NSDictionary* historyItem in history)
|
for (NSDictionary* historyItem in history)
|
||||||
{
|
{
|
||||||
Torrent* torrent;
|
NSString *hash = historyItem[@"TorrentHash"];
|
||||||
if ((torrent = [[Torrent alloc] initWithHistory:historyItem lib:self.fLib forcePause:self.fPauseOnLaunch]))
|
if ([self.fTorrentHashes.allKeys containsObject:hash]) {
|
||||||
{
|
Torrent *torrent = self.fTorrentHashes[hash];
|
||||||
[self.fTorrents addObject:torrent];
|
[t setResumeStatusForTorrent:torrent withHistory:historyItem forcePause:self.fPauseOnLaunch];
|
||||||
|
|
||||||
NSNumber* waitToStart;
|
NSNumber* waitToStart;
|
||||||
if (!self.fPauseOnLaunch && (waitToStart = historyItem[@"WaitToStart"]) && waitToStart.boolValue)
|
if (!self.fPauseOnLaunch && (waitToStart = historyItem[@"WaitToStart"]) && waitToStart.boolValue)
|
||||||
{
|
{
|
||||||
|
@ -2456,6 +2484,7 @@ static void removeKeRangerRansomware()
|
||||||
for (Torrent* torrent in self.fTorrents)
|
for (Torrent* torrent in self.fTorrents)
|
||||||
{
|
{
|
||||||
[history addObject:torrent.history];
|
[history addObject:torrent.history];
|
||||||
|
self.fTorrentHashes[torrent.hashString] = torrent;
|
||||||
}
|
}
|
||||||
|
|
||||||
NSString* historyFile = [self.fConfigDirectory stringByAppendingPathComponent:TRANSFER_PLIST];
|
NSString* historyFile = [self.fConfigDirectory stringByAppendingPathComponent:TRANSFER_PLIST];
|
||||||
|
|
|
@ -24,7 +24,7 @@ typedef NS_ENUM(unsigned int, TorrentDeterminationType) {
|
||||||
lib:(tr_session*)lib;
|
lib:(tr_session*)lib;
|
||||||
- (instancetype)initWithTorrentStruct:(tr_torrent*)torrentStruct location:(NSString*)location 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)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;
|
@property(nonatomic, readonly) NSDictionary* history;
|
||||||
|
|
||||||
|
|
|
@ -195,58 +195,25 @@ bool trashDataFile(char const* filename, tr_error** error)
|
||||||
return self;
|
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
|
//start transfer
|
||||||
lib:lib
|
NSNumber* active;
|
||||||
groupValue:history[@"GroupValue"]
|
if (!pause && (active = history[@"Active"]) && active.boolValue)
|
||||||
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
|
[torrent startTransferNoQueue];
|
||||||
NSNumber* active;
|
}
|
||||||
if (!pause && (active = history[@"Active"]) && active.boolValue)
|
|
||||||
{
|
NSNumber* ratioLimit;
|
||||||
_fStat = tr_torrentStat(_fHandle);
|
if ((ratioLimit = history[@"RatioLimit"]))
|
||||||
[self startTransferNoQueue];
|
{
|
||||||
}
|
self.ratioLimit = ratioLimit.floatValue;
|
||||||
|
|
||||||
//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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return self;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSDictionary*)history
|
- (NSDictionary*)history
|
||||||
{
|
{
|
||||||
return @{
|
return @{
|
||||||
@"InternalTorrentPath" : self.torrentLocation,
|
|
||||||
@"TorrentHash" : self.hashString,
|
@"TorrentHash" : self.hashString,
|
||||||
@"Active" : @(self.active),
|
@"Active" : @(self.active),
|
||||||
@"WaitToStart" : @(self.waitingToStart),
|
@"WaitToStart" : @(self.waitingToStart),
|
||||||
|
|
Loading…
Reference in New Issue