Browse Source

...

works_for_me
Volker Birk 2 years ago
parent
commit
f7372ef7c4
1 changed files with 35 additions and 20 deletions
  1. +35
    -20
      pEpNotifications/pEpNotifications/AppDelegate.swift

+ 35
- 20
pEpNotifications/pEpNotifications/AppDelegate.swift View File

@ -10,14 +10,14 @@
import Cocoa
import SwiftUI
@objc enum DNType : Int { case downloading, downloadArrived, noDownloadAvailable }
enum DNType : Int { case ready = 0, downloading, downloadArrived, noDownloadAvailable }
@objc(pEpNotificationProtocol) protocol pEpNotificationProtocol {
func notifyDownload(type: DNType, name: NSString, filename: NSString)
func notifyDownload(_ type: Int, withName: NSString, withFilename: NSString)
}
@objc(pEpMacOSAdapterProtocol) protocol pEpMacOSAdapterProtocol {
func subscribeForUpdate()
func subscribeForUpdate(_ endpoint: NSXPCListenerEndpoint?)
func unsubscribeForUpdate()
func updateNow()
}
@ -25,13 +25,13 @@ import SwiftUI
@objc class pEpNotification : NSObject, pEpNotificationProtocol {
var delegate: pEpNotificationProtocol!
func notifyDownload(type: DNType, name: NSString, filename: NSString) {
delegate?.notifyDownload(type: type, name: name, filename: filename)
func notifyDownload(_ type: Int, withName: NSString, withFilename: NSString) {
delegate?.notifyDownload(type, withName: withName, withFilename: withFilename)
}
}
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDelegate, pEpNotificationProtocol {
class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDelegate, NSXPCListenerDelegate, pEpNotificationProtocol {
@IBOutlet weak var _updateNow: NSMenuItem!
var statusBarItem: NSStatusItem!
@ -39,18 +39,20 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
var connection: NSXPCConnection!
var service: pEpMacOSAdapterProtocol!
var nc = NSUserNotificationCenter.default
var clientListener: NSXPCListener!
var receiver: pEpNotification!
func notifyDownload(type: DNType, name: NSString, filename: NSString)
func notifyDownload(_ type: Int, withName: NSString, withFilename: NSString)
{
switch type {
let _type = DNType.init(rawValue: type)
switch _type {
case .downloading:
NSLog("pEpNotifications: downloading")
statusText.title = "Downloading…"
case .downloadArrived:
NSLog("pEpNotifications: download for %@ arrived %@", name, filename)
let _name : String = name as String
NSLog("pEpNotifications: download for %@ arrived %@", withName, withFilename)
let _name = withName
statusText.title = String(format: NSLocalizedString("New version of %@ available", comment: ""), _name)
let un = NSUserNotification()
un.title = NSLocalizedString("Update available", comment: "")
@ -67,12 +69,17 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
case .noDownloadAvailable:
NSLog("pEpNotifications: no download available")
statusText.title = "The software is up to date."
case .ready:
NSLog("pEpNotifications: ready.")
statusText.title = NSLocalizedString("Connected.", comment: "")
case .none:
break;
}
}
@objc func updateNow() {
service.updateNow()
NSLog("pEpNotifications: %@", "update fired")
}
@objc func userNotificationCenter(_ : NSUserNotificationCenter, didActivate: NSUserNotification) {
@ -85,6 +92,14 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
NSLog("%@", err.localizedDescription)
statusText.title = NSLocalizedString("Connection failed", comment: "")
}
@objc func shouldAcceptNewConnection(newConnection: NSXPCConnection) {
newConnection.exportedInterface = NSXPCInterface.init(with: pEpNotificationProtocol.self)
let obj = pEpNotification()
obj.delegate = self
newConnection.exportedObject = obj;
newConnection.resume()
}
func applicationDidFinishLaunching(_ aNotification: Notification) {
let statusBar = NSStatusBar.system
@ -103,19 +118,19 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
if connection != nil {
connection.remoteObjectInterface = NSXPCInterface.init(with: pEpMacOSAdapterProtocol.self)
connection.resume()
// subscribe
statusText.title = NSLocalizedString("Connecting…", comment: "")
service = connection.remoteObjectProxyWithErrorHandler(proxyErrorHandler) as? pEpMacOSAdapterProtocol
// init client callback service
receiver = pEpNotification()
receiver.delegate = self
connection.exportedObject = receiver
connection.exportedInterface = NSXPCInterface.init(with: pEpNotificationProtocol.self)
service.subscribeForUpdate()
clientListener = NSXPCListener.init(machServiceName: "foundation.pEp.updates")
clientListener.delegate = self
clientListener.resume()
statusText.title = NSLocalizedString("Connected.", comment: "")
// subscribe
service.subscribeForUpdate(clientListener.endpoint)
}
else {
NSLog("pEpNotifications: %@", "cannot connect to pEp.foundation.adapter.macOS")


Loading…
Cancel
Save