Browse Source

export subscription target

works_for_me
Volker Birk 2 years ago
parent
commit
71088af4f7
5 changed files with 36 additions and 12 deletions
  1. +4
    -1
      pEpMacOSAdapter/main.m
  2. +3
    -0
      pEpMacOSAdapter/pEpMacOSAdapter.h
  3. +2
    -1
      pEpMacOSAdapter/pEpMacOSAdapter.m
  4. +1
    -1
      pEpMacOSAdapter/pEpMacOSAdapterProtocol.h
  5. +26
    -9
      pEpNotifications/pEpNotifications/AppDelegate.swift

+ 4
- 1
pEpMacOSAdapter/main.m View File

@ -29,9 +29,12 @@
// First, set the interface that the exported object implements.
newConnection.exportedInterface = [NSXPCInterface interfaceWithProtocol:@protocol(pEpMacOSAdapterProtocol)];
// Next, set the object that the connection exports. All messages sent on the connection to this service will be sent to the exported object to handle. The connection retains the exported object.
// Next, set the object that the connection exports. All messages sent on
// the connection to this service will be sent to the exported object to
// handle. The connection retains the exported object.
pEpMacOSAdapter *exportedObject = [pEpMacOSAdapter new];
newConnection.exportedObject = exportedObject;
exportedObject.connection = newConnection;
if (!newConnection.exportedInterface || !newConnection.exportedObject) {
NSLog(@"failed to allocate object and interface");


+ 3
- 0
pEpMacOSAdapter/pEpMacOSAdapter.h View File

@ -12,4 +12,7 @@
/// 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>
@property (assign) NSXPCConnection* connection;
@end

+ 2
- 1
pEpMacOSAdapter/pEpMacOSAdapter.m View File

@ -16,10 +16,11 @@ static pEpUpdater* _updater = nil;
// begin pEpMacOSAdapterProtocol
- (void)subscribeForUpdate:(pEpNotification*)downloadNotification
- (void)subscribeForUpdate
{
NSLog(@"subscribeForUpdate called");
pEpNotification* downloadNotification = [_connection exportedObject];
if (downloadNotification) {
if (!_updater) {
_updater = [[pEpUpdater alloc] initWithSubscriber:downloadNotification];


+ 1
- 1
pEpMacOSAdapter/pEpMacOSAdapterProtocol.h View File

@ -33,7 +33,7 @@ typedef enum { downloading, downloadArrived, noDownloadAvailable } DNType;
- 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:(pEpNotification*)downloadNotification;
- (void)subscribeForUpdate;
/// unsubscribe from receiving notifications about downloads arriving
- (void)unsubscribeForUpdate;


+ 26
- 9
pEpNotifications/pEpNotifications/AppDelegate.swift View File

@ -11,16 +11,27 @@ import Cocoa
import SwiftUI
@objc enum DNType : Int { case downloading, downloadArrived, noDownloadAvailable }
typealias DownloadNotification = (_: DNType, NSString, NSString) -> Void
@objc(pEpNotificationProtocol) protocol pEpNotificationProtocol {
func notifyDownload(type: DNType, name: NSString, filename: NSString)
}
@objc(pEpMacOSAdapterProtocol) protocol pEpMacOSAdapterProtocol {
func subscribeForUpdate(downloadNotification: @escaping DownloadNotification)
func subscribeForUpdate()
func unsubscribeForUpdate()
func updateNow()
}
@objc class pEpNotification : NSObject, pEpNotificationProtocol {
var delegate: pEpNotificationProtocol!
func notifyDownload(type: DNType, name: NSString, filename: NSString) {
delegate?.notifyDownload(type: type, name: name, filename: filename)
}
}
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDelegate {
class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDelegate, pEpNotificationProtocol {
@IBOutlet weak var _updateNow: NSMenuItem!
var statusBarItem: NSStatusItem!
@ -28,16 +39,17 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
var connection: NSXPCConnection!
var service: pEpMacOSAdapterProtocol!
var nc = NSUserNotificationCenter.default
var receiver: pEpNotification!
func notifyDownload(notification: DNType, name: NSString, download: NSString)
func notifyDownload(type: DNType, name: NSString, filename: NSString)
{
switch notification {
switch type {
case .downloading:
NSLog("pEpNotifications: downloading")
statusText.title = "Downloading…"
case .downloadArrived:
NSLog("pEpNotifications: download for %@ arrived %@", name, download)
NSLog("pEpNotifications: download for %@ arrived %@", name, filename)
let _name : String = name as String
statusText.title = String(format: NSLocalizedString("New version of %@ available", comment: ""), _name)
let un = NSUserNotification()
@ -74,7 +86,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
statusText.title = NSLocalizedString("Connection failed", comment: "")
}
internal func applicationDidFinishLaunching(_ aNotification: Notification) {
func applicationDidFinishLaunching(_ aNotification: Notification) {
let statusBar = NSStatusBar.system
// configure Menu Item Extra
@ -97,7 +109,12 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
statusText.title = NSLocalizedString("Connecting…", comment: "")
service = connection.remoteObjectProxyWithErrorHandler(proxyErrorHandler) as? pEpMacOSAdapterProtocol
service.subscribeForUpdate(downloadNotification: notifyDownload)
receiver = pEpNotification()
receiver.delegate = self
connection.exportedObject = receiver
connection.exportedInterface = NSXPCInterface.init(with: pEpNotificationProtocol.self)
service.subscribeForUpdate()
statusText.title = NSLocalizedString("Connected.", comment: "")
}
else {
@ -108,7 +125,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
nc.delegate = self
}
internal func applicationWillTerminate(_ aNotification: Notification) {
func applicationWillTerminate(_ aNotification: Notification) {
service.unsubscribeForUpdate()
connection?.invalidate()
}


Loading…
Cancel
Save