store subscriber directly in pEpUpdater

works_for_me
Volker Birk 3 years ago
parent e32232da37
commit 3e6554fdac

@ -74,7 +74,6 @@
/* Begin PBXFileReference section */
3527B2E024802F87007A6276 /* pEpNotifications.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = pEpNotifications.xcodeproj; path = pEpNotifications/pEpNotifications.xcodeproj; sourceTree = "<group>"; };
3527B2EC24806A18007A6276 /* pEpMacOSAdapterSubscriberProtocol.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = pEpMacOSAdapterSubscriberProtocol.h; sourceTree = "<group>"; };
35380AD4247BBB03004A08A6 /* downloadclient.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = downloadclient.xcodeproj; path = ../downloadclient/downloadclient/downloadclient.xcodeproj; sourceTree = "<group>"; };
35380AE6247BD13A004A08A6 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = SOURCE_ROOT; };
35380AE8247C784E004A08A6 /* pEpUpdater.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = pEpUpdater.h; sourceTree = "<group>"; };
@ -171,7 +170,6 @@
children = (
35380AE6247BD13A004A08A6 /* README.md */,
35DC18D5244DA19100FB2002 /* pEpMacOSAdapterProtocol.h */,
3527B2EC24806A18007A6276 /* pEpMacOSAdapterSubscriberProtocol.h */,
35DC18D6244DA19100FB2002 /* pEpMacOSAdapter.h */,
35DC18D7244DA19100FB2002 /* pEpMacOSAdapter.m */,
35380AE8247C784E004A08A6 /* pEpUpdater.h */,

@ -7,6 +7,7 @@
// This file is under GNU General Public License 3.0
//
/// make a type out of pEpMacOSAdapterSubscriberProtocol
@interface Subscriber : NSObject<pEpMacOSAdapterSubscriberProtocol>
@end
#pragma once
enum DNType { downloading, downloadArrived };
typedef void (^DownloadNotification)(enum DNType, NSString *);

@ -9,8 +9,7 @@
#import <Foundation/Foundation.h>
#import "pEpMacOSAdapterProtocol.h"
#import "pEpMacOSAdapterSubscriberProtocol.h"
/// This object implements the protocol which we have defined. It provides the actual behavior for the service. It is 'exported' by the service to make it available to the process hosting the service over an NSXPCConnection.
@interface pEpMacOSAdapter : NSObject <pEpMacOSAdapterProtocol, pEpMacOSAdapterSubscriberProtocol>
@interface pEpMacOSAdapter : NSObject <pEpMacOSAdapterProtocol>
@end

@ -9,32 +9,30 @@
#import "pEpMacOSAdapter.h"
#import "pEpUpdater.h"
#include "Subscriber.h"
static pEpUpdater* _updater = nil;
@implementation pEpMacOSAdapter
void (^_downloadArrived)(NSString *) = nil;
// begin pEpMacOSAdapterProtocol
- (void)subscribeForUpdate:(void (^)(NSString *))downloadArrived
- (void)subscribeForUpdate:(DownloadNotification)downloadNotification
{
if (downloadArrived) {
if (downloadNotification) {
if (!_updater) {
_updater = [[pEpUpdater alloc] initWithSubscriber:(Subscriber*)self];
_updater = [[pEpUpdater alloc] initWithSubscriber:downloadNotification];
[_updater scheduleUpdates];
}
else {
[_updater setNewSubscriber:(Subscriber*)self];
[_updater setNewSubscriber:downloadNotification];
}
_downloadArrived = downloadArrived;
}
}
- (void)unsubscribeForUpdate
{
_downloadArrived = nil;
[_updater setNewSubscriber :nil];
}
- (void)updateNow
@ -46,15 +44,4 @@ void (^_downloadArrived)(NSString *) = nil;
// end pEpMacOSAdapterProtocol
// begin pEpMacOSAdapterSubscriberProtocol
- (void)notifyDownloadArrived:(nonnull NSString *)download
{
if (_downloadArrived) {
_downloadArrived(download);
}
}
// end pEpMacOSAdapterSubscriberProtocol
@end

@ -8,6 +8,7 @@
//
#import <Foundation/Foundation.h>
#include "Subscriber.h"
/// This protocol is providing the XPC interface to the User Interface program pEpNotifications
@protocol pEpMacOSAdapterProtocol
@ -15,9 +16,10 @@
/**
subscribe to the published notifications about downloads arriving
- Parameter downloading: block to call back when a download is going to happen
- Parameter downloadArrived: block to call back when a download arrived
*/
- (void)subscribeForUpdate:(void (^)(NSString *))downloadArrived;
- (void)subscribeForUpdate:(DownloadNotification)downloadNotification;
/// unsubscribe from receiving notifications about downloads arriving
- (void)unsubscribeForUpdate;

@ -1,14 +0,0 @@
//
// pEpMacOSAdapterNotifyProtocol.h
// pEpMacOSAdapter
//
// Created by Volker Birk on 28.05.20.
// Copyright © 2020 p≡p foundation. All rights reserved.
// This file is under GNU General Public License 3.0
//
@protocol pEpMacOSAdapterSubscriberProtocol
- (void)notifyDownloadArrived:(nonnull NSString *)download;
@end

@ -8,8 +8,7 @@
//
#import <Foundation/Foundation.h>
#import "pEpMacOSAdapterSubscriberProtocol.h"
#import "Subscriber.h"
#include "Subscriber.h"
/// object encapsulating the update client functionality of downloadclient
@interface pEpUpdater : NSObject
@ -23,10 +22,10 @@
/// initializes the pEpUpdater
///
/// - parameter subscriber: object fulfilling the `pEpMacOSAdapterSubscriberProtocol`.
- (id)initWithSubscriber:(Subscriber*)subscriber;
- (id)initWithSubscriber:(DownloadNotification)subscriber;
/// switches the subscriber
- (void)setNewSubscriber:(Subscriber*)subscriber;
- (void)setNewSubscriber:(DownloadNotification)subscriber;
/// schedules updates each 2 hours
- (void)scheduleUpdates;

@ -16,9 +16,9 @@ static const double CYCLE = 7200.0; // 7200 seconds = 2 hours
@implementation pEpUpdater
__weak Subscriber* _subscriber = nil;
DownloadNotification _subscriber = nil;
- (id)initWithSubscriber:(Subscriber*)subscriber
- (id)initWithSubscriber:(DownloadNotification)subscriber
{
self = [super init];
self.cycle = CYCLE;
@ -27,7 +27,7 @@ __weak Subscriber* _subscriber = nil;
return self;
}
- (void)setNewSubscriber:(Subscriber*)subscriber
- (void)setNewSubscriber:(DownloadNotification)subscriber
{
_subscriber = subscriber;
}
@ -84,7 +84,13 @@ __weak Subscriber* _subscriber = nil;
NSString* tmp = localFileManager.temporaryDirectory.path;
[localFileManager changeCurrentDirectoryPath:tmp];
std::string _filename = pEp::UpdateClient::update(p, keyfile);
std::string _filename;
pEp::notifyRead_t notifyRead = nullptr;
if (_subscriber) {
}
_filename = pEp::UpdateClient::update(p, keyfile, notifyRead);
NSString* filename = [NSString stringWithUTF8String:_filename.c_str()];
NSString* download = [NSString stringWithFormat:@"%@/%@", tmp, filename];
[localFileManager changeCurrentDirectoryPath:cwd];
@ -92,7 +98,7 @@ __weak Subscriber* _subscriber = nil;
NSLog(@"pEpMacOSAdapter: download arrived %@", download);
if (_subscriber) {
[_subscriber notifyDownloadArrived:download];
_subscriber(downloadArrived, download);
}
}
catch (std::exception& e) {

@ -10,10 +10,12 @@
import Cocoa
import SwiftUI
typealias DownloadArrived = (_: NSString) -> Void
@objc enum DNType : Int { case downloading, downloadArrived }
typealias DownloadNotification = (_: DNType, NSString) -> Void
@objc(pEpMacOSAdapterProtocol) protocol pEpMacOSAdapterProtocol {
func subscribeForUpdate(downloadArrived: @escaping DownloadArrived)
func subscribeForUpdate(downloadNotification: @escaping DownloadNotification)
func unsubscribeForUpdate()
func updateNow()
}
@ -27,9 +29,14 @@ class AppDelegate: NSObject, NSApplicationDelegate {
var connection: NSXPCConnection!
var proxy: pEpMacOSAdapterProtocol!
func notifyDownloadArrived(download: NSString)
func notifyDownload(notification: DNType, download: NSString)
{
NSLog("pEpNotifications: notifyDownloadArrived(%@)", download)
switch notification {
case .downloading:
NSLog("pEpNotifications: downloading")
case .downloadArrived:
NSLog("pEpNotifications: download arrived(%@)", download)
}
}
@objc func updateNow() {
@ -58,7 +65,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
// subscribe
proxy = connection.remoteObjectProxy as? pEpMacOSAdapterProtocol
proxy?.subscribeForUpdate(downloadArrived: notifyDownloadArrived)
proxy?.subscribeForUpdate(downloadNotification: notifyDownload)
}
else {
NSLog("pEpNotifications: %@", "cannot connect to pEp.foundation.pEpMacOSAdapter")

Loading…
Cancel
Save