|
|
@ -11,16 +11,6 @@ import Cocoa |
|
|
|
import SwiftUI |
|
|
|
import PEPUpdatesXPCService |
|
|
|
|
|
|
|
enum DNType : Int { case ready = 0, downloading, downloadArrived, noDownloadAvailable } |
|
|
|
|
|
|
|
@objc(PEPMacOSAdapterProtocol) protocol PEPMacOSAdapterProtocol { |
|
|
|
func subscribeForUpdate(_ endpoint: NSXPCListenerEndpoint?) |
|
|
|
func unsubscribeForUpdate() |
|
|
|
func updateNow() |
|
|
|
func scheduleUpdates() |
|
|
|
func stopUpdates() |
|
|
|
} |
|
|
|
|
|
|
|
@NSApplicationMain |
|
|
|
class AppDelegate: NSObject { |
|
|
|
|
|
|
@ -35,10 +25,9 @@ class AppDelegate: NSObject { |
|
|
|
// MARK: - Properties |
|
|
|
|
|
|
|
private var statusBarItem: NSStatusItem? = nil |
|
|
|
private lazy var updateDownloadService = PEPUpdateDownloadApiClientService() |
|
|
|
private lazy var updateDownloadClientService = PEPUpdateDownloadApiClientService() |
|
|
|
private lazy var notificationService = PEPNotificationXPCService(delegate: self) |
|
|
|
private var nc = NSUserNotificationCenter.default |
|
|
|
private lazy var clientListener: NSXPCListener = NSXPCListener.anonymous() |
|
|
|
private var receiver: PEPNotification? |
|
|
|
private lazy var downloadStateNotifier = DownloadStateNotifier(at: statusText) |
|
|
|
@available(OSX 10.14, *) |
|
|
|
private lazy var downloadNotificationManager = DownloadNotificationManager() |
|
|
@ -49,11 +38,11 @@ class AppDelegate: NSObject { |
|
|
|
if sender.state == NSControl.StateValue.on { |
|
|
|
sender.state = NSControl.StateValue.off |
|
|
|
UserDefaults.standard.set(false, forKey: "ScheduleUpdates") |
|
|
|
updateDownloadService.stopUpdates() |
|
|
|
updateDownloadClientService.stopUpdates() |
|
|
|
} else { |
|
|
|
sender.state = NSControl.StateValue.on |
|
|
|
UserDefaults.standard.set(true, forKey: "ScheduleUpdates") |
|
|
|
updateDownloadService.scheduleUpdates() |
|
|
|
updateDownloadClientService.scheduleUpdates() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -86,7 +75,7 @@ class AppDelegate: NSObject { |
|
|
|
nc.removeDeliveredNotification(notification) |
|
|
|
} |
|
|
|
|
|
|
|
NSLog("pEpNotifications: installNow clicked for %@", product.name) |
|
|
|
NSLog("pEpUpdates: installNow clicked for %@", product.name) |
|
|
|
NSWorkspace.shared.openFile(product.filename) |
|
|
|
sender.representedObject = nil |
|
|
|
downloadStateNotifier.notify(.Connected) |
|
|
@ -99,7 +88,7 @@ extension AppDelegate { |
|
|
|
@objc |
|
|
|
private func updateNow() { |
|
|
|
nc.removeAllDeliveredNotifications() |
|
|
|
updateDownloadService.updateNow() |
|
|
|
updateDownloadClientService.updateNow() |
|
|
|
} |
|
|
|
|
|
|
|
private func setupAppDefaults() { |
|
|
@ -114,22 +103,21 @@ extension AppDelegate { |
|
|
|
} |
|
|
|
|
|
|
|
private func initUpdateDownloadXPCConnection() { |
|
|
|
updateDownloadService.start() |
|
|
|
updateDownloadClientService.start() |
|
|
|
} |
|
|
|
|
|
|
|
private func initClientService() { |
|
|
|
clientListener.delegate = self |
|
|
|
clientListener.resume() |
|
|
|
private func initUpdateDownloadNotificationService() { |
|
|
|
notificationService.start() |
|
|
|
} |
|
|
|
|
|
|
|
private func setupServiceSubscription() { |
|
|
|
updateDownloadService.subscribe(forUpdate:clientListener.endpoint) |
|
|
|
updateDownloadClientService.subscribe(forUpdate:notificationService.notificationEndpoint) |
|
|
|
if UserDefaults.standard.bool(forKey: "ScheduleUpdates") { |
|
|
|
updateDownloadService.scheduleUpdates() |
|
|
|
updateDownloadClientService.scheduleUpdates() |
|
|
|
scheduleUpdatesMenuItem.state = NSControl.StateValue.on |
|
|
|
} |
|
|
|
else { |
|
|
|
updateDownloadService.stopUpdates() |
|
|
|
updateDownloadClientService.stopUpdates() |
|
|
|
scheduleUpdatesMenuItem.state = NSControl.StateValue.off |
|
|
|
} |
|
|
|
} |
|
|
@ -187,62 +175,57 @@ extension AppDelegate { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// MARK: - PEPNotificationProtocol |
|
|
|
// MARK: - PEPNotificationXPCServiceDelegate |
|
|
|
|
|
|
|
extension AppDelegate: PEPNotificationProtocol { |
|
|
|
|
|
|
|
func notifyDownload(_ type: Int, withName: NSString, withFilename: NSString) { |
|
|
|
let _type = DNType.init(rawValue: type) |
|
|
|
switch _type { |
|
|
|
case .downloading: |
|
|
|
NSLog("pEpNotifications: downloading") |
|
|
|
downloadStateNotifier.notify(.Downloading(String(withName))) |
|
|
|
DispatchQueue.main.async { [weak self] in |
|
|
|
guard let me = self else { |
|
|
|
return |
|
|
|
} |
|
|
|
me.installMenuExtra() |
|
|
|
} |
|
|
|
|
|
|
|
case .downloadArrived: |
|
|
|
var product = Product(name: withName as String, filename: withFilename as String); |
|
|
|
|
|
|
|
if #available(OSX 10.14, *) { |
|
|
|
downloadNotificationManager.scheduleDownloadNotification(with: product) |
|
|
|
} else { |
|
|
|
/* |
|
|
|
UNUserNotificationCenter availability is 10.14+, so for older os |
|
|
|
versions we have to keep the deprecated NSUserNotification. |
|
|
|
We will be able to delete this code after pEp drops support for |
|
|
|
10.13 os version. |
|
|
|
*/ |
|
|
|
removeDeliveredNotifications(with: withName) |
|
|
|
let un = NSUserNotification() |
|
|
|
un.title = NSLocalizedString("Update available", comment: "") |
|
|
|
un.informativeText = String(format: NSLocalizedString("A new update for %@ is ready to be installed.", comment: ""), withName) |
|
|
|
un.actionButtonTitle = NSLocalizedString("Install", comment: "") |
|
|
|
un.hasActionButton = true |
|
|
|
un.otherButtonTitle = NSLocalizedString("Not now", comment: "") |
|
|
|
un.soundName = NSUserNotificationDefaultSoundName |
|
|
|
un.setValue(true, forKey: "_showsButtons") |
|
|
|
un.userInfo = ["name": withName, "filename": withFilename] |
|
|
|
nc.deliver(un) |
|
|
|
product.notification = un |
|
|
|
extension AppDelegate: PEPNotificationXPCServiceDelegate { |
|
|
|
func pEpNotificationIsDownloading(withName name: String) { |
|
|
|
NSLog("pEpUpdates: downloading") |
|
|
|
downloadStateNotifier.notify(.Downloading(String(name))) |
|
|
|
DispatchQueue.main.async { [weak self] in |
|
|
|
guard let me = self else { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
downloadStateNotifier.notify(.NewVersionAvailable(String(withName)), with: product) |
|
|
|
|
|
|
|
case .noDownloadAvailable: |
|
|
|
NSLog("pEpNotifications: no download available") |
|
|
|
downloadStateNotifier.notify(.UpToDate) |
|
|
|
|
|
|
|
case .ready: |
|
|
|
NSLog("pEpNotifications: ready.") |
|
|
|
downloadStateNotifier.notify(.Connected) |
|
|
|
|
|
|
|
case .none: |
|
|
|
break |
|
|
|
me.installMenuExtra() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func pEpNotificationDownloadDidArrive(withName name: String, filename: String) { |
|
|
|
var product = Product(name: name as String, filename: filename as String); |
|
|
|
|
|
|
|
if #available(OSX 10.14, *) { |
|
|
|
downloadNotificationManager.scheduleDownloadNotification(with: product) |
|
|
|
} else { |
|
|
|
/* |
|
|
|
UNUserNotificationCenter availability is 10.14+, so for older os |
|
|
|
versions we have to keep the deprecated NSUserNotification. |
|
|
|
We will be able to delete this code after pEp drops support for |
|
|
|
10.13 os version. |
|
|
|
*/ |
|
|
|
removeDeliveredNotifications(with: name as NSString) |
|
|
|
let un = NSUserNotification() |
|
|
|
un.title = NSLocalizedString("Update available", comment: "") |
|
|
|
un.informativeText = String(format: NSLocalizedString("A new update for %@ is ready to be installed.", comment: ""), name) |
|
|
|
un.actionButtonTitle = NSLocalizedString("Install", comment: "") |
|
|
|
un.hasActionButton = true |
|
|
|
un.otherButtonTitle = NSLocalizedString("Not now", comment: "") |
|
|
|
un.soundName = NSUserNotificationDefaultSoundName |
|
|
|
un.setValue(true, forKey: "_showsButtons") |
|
|
|
un.userInfo = ["name": name, "filename": filename] |
|
|
|
nc.deliver(un) |
|
|
|
product.notification = un |
|
|
|
} |
|
|
|
|
|
|
|
downloadStateNotifier.notify(.NewVersionAvailable(String(name)), with: product) |
|
|
|
} |
|
|
|
|
|
|
|
func pEpNotificationNoDownloadAvailable() { |
|
|
|
NSLog("pEpUpdates: no download available") |
|
|
|
downloadStateNotifier.notify(.UpToDate) |
|
|
|
} |
|
|
|
|
|
|
|
func pEpNotificationReady() { |
|
|
|
NSLog("pEpUpdates: ready.") |
|
|
|
downloadStateNotifier.notify(.Connected) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -259,19 +242,20 @@ extension AppDelegate: NSApplicationDelegate { |
|
|
|
initUpdateDownloadXPCConnection() |
|
|
|
|
|
|
|
// init client callback service |
|
|
|
initClientService() |
|
|
|
initUpdateDownloadNotificationService() |
|
|
|
|
|
|
|
// subscribe and schedule updates |
|
|
|
setupServiceSubscription() |
|
|
|
|
|
|
|
updateDownloadService.updateNow() |
|
|
|
updateDownloadClientService.updateNow() |
|
|
|
|
|
|
|
initNotificationCenter() |
|
|
|
} |
|
|
|
|
|
|
|
func applicationWillTerminate(_ aNotification: Notification) { |
|
|
|
updateDownloadService.unsubscribeForUpdate() |
|
|
|
updateDownloadService.stop() |
|
|
|
updateDownloadClientService.unsubscribeForUpdate() |
|
|
|
updateDownloadClientService.stop() |
|
|
|
notificationService.stop() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -287,26 +271,12 @@ extension AppDelegate: NSUserNotificationCenterDelegate { |
|
|
|
|
|
|
|
uninstallMenuExtra() |
|
|
|
let filename : String = didActivate.userInfo?["filename"] as! String |
|
|
|
NSLog("pEpNotifications: actionButtonClicked for %@", filename) |
|
|
|
NSLog("pEpUpdates: actionButtonClicked for %@", filename) |
|
|
|
NSWorkspace.shared.openFile(filename) |
|
|
|
downloadStateNotifier.notify(.Connected) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// MARK: - NSXPCListenerDelegate |
|
|
|
|
|
|
|
extension AppDelegate: NSXPCListenerDelegate { |
|
|
|
@objc func listener(_ listener: NSXPCListener, shouldAcceptNewConnection newConnection: NSXPCConnection) -> Bool { |
|
|
|
newConnection.exportedInterface = NSXPCInterface.init(with: PEPNotificationProtocol.self) |
|
|
|
let obj = PEPNotification() |
|
|
|
obj.delegate = self |
|
|
|
newConnection.exportedObject = obj |
|
|
|
newConnection.resume() |
|
|
|
|
|
|
|
return true |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// MARK: - DownloadNotificationManagerDelegate |
|
|
|
|
|
|
|
extension AppDelegate: DownloadNotificationManagerDelegate { |
|
|
@ -315,7 +285,7 @@ extension AppDelegate: DownloadNotificationManagerDelegate { |
|
|
|
uninstallMenuExtra() |
|
|
|
} |
|
|
|
|
|
|
|
NSLog("pEpNotifications: actionButtonClicked for %@", product.filename) |
|
|
|
NSLog("pEpUpdates: actionButtonClicked for %@", product.filename) |
|
|
|
NSWorkspace.shared.openFile(product.filename) |
|
|
|
downloadStateNotifier.notify(.Connected) |
|
|
|
} |
|
|
|