//
|
|
// pEpUpdater.mm
|
|
// pEpMacOSAdapter
|
|
//
|
|
// Created by Volker Birk on 26.05.20.
|
|
// Copyright © 2020 p≡p foundation. All rights reserved.
|
|
// This file is under GNU General Public License 3.0
|
|
//
|
|
|
|
#import "pEpUpdater.h"
|
|
|
|
#include "../../downloadclient/downloadclient.hh"
|
|
|
|
const double CYCLE = 7200.0; // 7200 seconds = 2 hours
|
|
NSString* CONFIG_PATH = @"/Library/Application Support/pEp/Updater";
|
|
|
|
@implementation pEpUpdater
|
|
|
|
- (id)init
|
|
{
|
|
self = [super init];
|
|
if (self) {
|
|
self.configPath = CONFIG_PATH;
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void)scheduleUpdates
|
|
{
|
|
if (!self.timer) {
|
|
NSLog(@"schedule updates");
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
self.timer = [NSTimer scheduledTimerWithTimeInterval:CYCLE
|
|
target:self
|
|
selector:@selector(updateAll:)
|
|
userInfo:nil
|
|
repeats:YES];
|
|
});
|
|
}
|
|
else {
|
|
NSLog(@"timer already there");
|
|
}
|
|
}
|
|
|
|
- (void)stopUpdates
|
|
{
|
|
if (self.timer) {
|
|
NSLog(@"stop auto updates");
|
|
[self.timer invalidate];
|
|
self.timer = nil;
|
|
}
|
|
}
|
|
|
|
- (void)updateAll:(NSTimer*)timer
|
|
{
|
|
NSLog(@"update all registered products");
|
|
|
|
NSFileManager *localFileManager=[NSFileManager new];
|
|
NSDirectoryEnumerator *dirEnum = [localFileManager enumeratorAtPath:_configPath];
|
|
|
|
NSString *configFile;
|
|
while (dirEnum && (configFile = [dirEnum nextObject])) {
|
|
if ([[configFile pathExtension] isEqualToString: @"plist"]) {
|
|
NSError *err = nil;
|
|
[self updateWithFile:[NSString stringWithFormat:@"%@/%@", _configPath, configFile] error:&err];
|
|
}
|
|
}
|
|
}
|
|
|
|
- (void)updateWithFile:(NSString*)configFile error:(NSError **)err
|
|
{
|
|
NSLog(@"update product with config file %@", configFile);
|
|
|
|
NSInputStream *is = [NSInputStream inputStreamWithFileAtPath:configFile];
|
|
[is open];
|
|
NSDictionary* plist = [NSPropertyListSerialization
|
|
propertyListWithStream:is options:NSPropertyListMutableContainers
|
|
format:nil error:err];
|
|
[is close];
|
|
|
|
if (plist) {
|
|
NSString* name = [plist objectForKey:@"name"];
|
|
NSString* url = [plist objectForKey:@"url"];
|
|
if (name && url) {
|
|
[self update:name usingUrl:url];
|
|
}
|
|
}
|
|
else if (err) {
|
|
NSLog(@"%@ %@ %@ %@", (*err).localizedDescription,
|
|
(*err).localizedFailureReason, (*err).localizedRecoveryOptions,
|
|
(*err).localizedRecoverySuggestion);
|
|
}
|
|
}
|
|
|
|
- (void)update:(NSString*)name usingUrl:(NSString*)url
|
|
{
|
|
NSLog(@"update %@ using %@", name, url);
|
|
|
|
pEp::UpdateClient::product p = { name.UTF8String, url.UTF8String };
|
|
|
|
std::string keyfile = _configPath.UTF8String;
|
|
keyfile += std::string("/");
|
|
keyfile += p.name;
|
|
keyfile += std::string(".der");
|
|
|
|
NSLog(@"using key from %s", keyfile.c_str());
|
|
|
|
NSString* cwd = [NSFileManager defaultManager].currentDirectoryPath;
|
|
NSString* dir = @"/tmp";
|
|
|
|
[[NSFileManager defaultManager] changeCurrentDirectoryPath:dir];
|
|
std::string _filename;
|
|
|
|
pEp::notifyRead_t notifyRead = [=]()->void{
|
|
NSLog(@"downloading: %@", name);
|
|
if (_subscriber) {
|
|
[_subscriber notifyDownload:(int)downloading withName:name withFilename:@""];
|
|
}
|
|
};
|
|
|
|
try {
|
|
_filename = pEp::UpdateClient::update(p, keyfile, notifyRead);
|
|
|
|
if (_filename.length()) {
|
|
NSString* filename = [NSString stringWithUTF8String:_filename.c_str()];
|
|
NSString* download = [NSString stringWithFormat:@"%@/%@", dir, filename];
|
|
[[NSFileManager defaultManager] changeCurrentDirectoryPath:cwd];
|
|
|
|
NSLog(@"download arrived %@", download);
|
|
if (_subscriber) {
|
|
[_subscriber notifyDownload:(int)downloadArrived withName:name withFilename:download];
|
|
}
|
|
}
|
|
else {
|
|
NSLog(@"no download available for %@", name);
|
|
if (_subscriber) {
|
|
[_subscriber notifyDownload:(int)noDownloadAvailable withName:name withFilename:@""];
|
|
}
|
|
}
|
|
}
|
|
catch (std::exception& e) {
|
|
NSLog(@"pEp::UpdateClient::update({\"%s\", \"%s\"}, \"%s\"): %s",
|
|
p.name.c_str(), p.url.c_str(), keyfile.c_str(), e.what());
|
|
}
|
|
}
|
|
|
|
@end
|