Browse Source

remove notification with install

works_for_me
Volker Birk 2 years ago
parent
commit
03775a77aa
5 changed files with 65 additions and 55 deletions
  1. +58
    -51
      pEpNotifications/pEpNotifications/AppDelegate.swift
  2. +4
    -4
      pEpNotifications/pEpNotifications/Base.lproj/Main.storyboard
  3. BIN
      pEpNotifications/pEpNotifications/de.lproj/Localizable.strings
  4. +2
    -0
      pEpNotifications/pEpNotifications/de.lproj/Main.strings
  5. +1
    -0
      pEpNotifications/pEpNotifications/en.lproj/Main.strings

+ 58
- 51
pEpNotifications/pEpNotifications/AppDelegate.swift View File

@ -20,6 +20,8 @@ enum DNType : Int { case ready = 0, downloading, downloadArrived, noDownloadAvai
func subscribeForUpdate(_ endpoint: NSXPCListenerEndpoint?)
func unsubscribeForUpdate()
func updateNow()
func scheduleUpdates()
func stopUpdates()
}
@objc class pEpNotification : NSObject, pEpNotificationProtocol {
@ -34,9 +36,9 @@ enum DNType : Int { case ready = 0, downloading, downloadArrived, noDownloadAvai
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDelegate, NSXPCListenerDelegate, pEpNotificationProtocol {
@IBOutlet weak var pEpMenu: NSMenu!
@IBOutlet weak var _updateNow: NSMenuItem!
@IBOutlet weak var statusText: NSMenuItem!
@IBOutlet weak var _hideMenu: NSMenuItem!
@IBOutlet weak var _updateNow: NSMenuItem!
@IBOutlet weak var _scheduleUpdates: NSMenuItem!
var statusBarItem: NSStatusItem!
var connection: NSXPCConnection!
@ -45,24 +47,47 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
var clientListener: NSXPCListener!
var receiver: pEpNotification!
@IBAction func hideMenu(_ sender: NSMenuItem) {
func setDownloadState(_ text: String, _ product: Dictionary<String, Any>? = nil) {
if statusBarItem == nil {
statusBarItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
statusBarItem.button?.title = "p≡p"
statusBarItem.menu = NSApp.menu?.item(at: 0)?.submenu
_updateNow.action = #selector(updateNow)
}
if product == nil {
statusText.title = NSLocalizedString(text, comment: "")
statusText.isEnabled = false
}
else {
let name = product?["name"] as! String
statusText.title = NSString.localizedStringWithFormat(NSLocalizedString(text, comment: "") as NSString, name) as String
statusText.isEnabled = true
}
statusText.representedObject = product
}
@IBAction func scheduleUpdates(_ sender: NSMenuItem) {
if sender.state == NSControl.StateValue.on {
sender.state = NSControl.StateValue.off
UserDefaults.standard.set(false, forKey: "HideWithoutUpdate")
UserDefaults.standard.set(false, forKey: "ScheduleUpdates")
service.stopUpdates()
}
else {
sender.state = NSControl.StateValue.on
UserDefaults.standard.set(true, forKey: "HideWithoutUpdate")
if statusText.representedObject == nil {
NSStatusBar.system.removeStatusItem(statusBarItem)
}
UserDefaults.standard.set(true, forKey: "ScheduleUpdates")
service.scheduleUpdates()
}
}
@IBAction func installNow(_ sender: NSMenuItem) {
let product : Dictionary<String, String> = sender.representedObject as! Dictionary<String, String>
NSLog("pEpNotifications: installNow clicked for %@", product["name"]!)
NSWorkspace.shared.openFile(product["filename"]!)
let product = sender.representedObject as! Dictionary<String, Any>
let un = product["notification"] as! NSUserNotification
nc.removeDeliveredNotification(un)
NSLog("pEpNotifications: installNow clicked for %@", product["name"] as! String)
NSWorkspace.shared.openFile(product["filename"] as! String)
sender.representedObject = nil
setDownloadState("Connected.")
}
func notifyDownload(_ type: Int, withName: NSString, withFilename: NSString)
@ -71,17 +96,12 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
switch _type {
case .downloading:
NSLog("pEpNotifications: downloading")
statusText.title = "Downloading…"
statusText.isEnabled = false
setDownloadState("Downloading…")
case .downloadArrived:
NSLog("pEpNotifications: download for %@ arrived %@", withName, withFilename)
let _name = withName
statusText.title = String(format: NSLocalizedString("New version of %@ available", comment: ""), _name)
statusText.isEnabled = true
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.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: "")
@ -90,19 +110,17 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
un.additionalActions = [NSUserNotificationAction(identifier: "install", title: installTitle)]
un.setValue(true, forKey: "_showsButtons")
un.userInfo = ["name": withName, "filename": withFilename]
statusText.representedObject = ["name": withName, "filename": withFilename]
nc.deliver(un)
setDownloadState("New version of %@ available", ["name": withName, "filename": withFilename, "notification": un])
case .noDownloadAvailable:
NSLog("pEpNotifications: no download available")
statusText.title = "The software is up to date."
statusText.isEnabled = false
setDownloadState("The software is up to date.")
case .ready:
NSLog("pEpNotifications: ready.")
statusText.title = NSLocalizedString("Connected.", comment: "")
statusText.isEnabled = false
setDownloadState("Connected.")
case .none:
break;
}
@ -118,13 +136,13 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
let filename : String = didActivate.userInfo?["filename"] as! String;
NSLog("pEpNotifications: actionButtonClicked for %@", filename)
NSWorkspace.shared.openFile(filename)
setDownloadState("Connected.")
}
}
func proxyErrorHandler(err: Error) -> Void {
NSLog("%@", err.localizedDescription)
statusText.title = NSLocalizedString("Connection failed", comment: "")
statusText.isEnabled = false
setDownloadState("Connection failed")
}
@objc func listener(_ listener: NSXPCListener, shouldAcceptNewConnection newConnection: NSXPCConnection) -> Bool {
@ -137,38 +155,18 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
}
func applicationDidFinishLaunching(_ aNotification: Notification) {
let statusBar = NSStatusBar.system
// preference defaults
let appDefaults = ["HideWithoutUpdate": true]
let appDefaults = ["ScheduleUpdates": true]
UserDefaults.standard.register(defaults: appDefaults)
// configure Menu Item Extra
statusBarItem = statusBar.statusItem(withLength: NSStatusItem.squareLength)
statusBarItem.button?.title = "p≡p"
statusBarItem.menu = NSApp.menu?.item(at: 0)?.submenu
_updateNow.action = #selector(updateNow)
// initialize statusText
if UserDefaults.standard.bool(forKey: "HideWithoutUpdate") {
_hideMenu.state = NSControl.StateValue.on
}
else {
_hideMenu.state = NSControl.StateValue.off
}
setDownloadState("Connecting…")
// connect to XPC service
connection = NSXPCConnection.init(machServiceName: "foundation.pEp.adapter.macOS")
if connection != nil {
connection.remoteObjectInterface = NSXPCInterface.init(with: pEpMacOSAdapterProtocol.self)
connection.resume()
statusText.title = NSLocalizedString("Connecting…", comment: "")
statusText.isEnabled = false
service = connection.remoteObjectProxyWithErrorHandler(proxyErrorHandler) as? pEpMacOSAdapterProtocol
// init client callback service
@ -177,9 +175,18 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
clientListener.delegate = self
clientListener.resume()
// subscribe
// subscribe and schedule updates
service.subscribeForUpdate(clientListener.endpoint)
if UserDefaults.standard.bool(forKey: "ScheduleUpdates") {
service.scheduleUpdates()
_scheduleUpdates.state = NSControl.StateValue.on
}
else {
service.stopUpdates()
_scheduleUpdates.state = NSControl.StateValue.off
}
service.updateNow()
}
else {
NSLog("pEpNotifications: %@", "cannot connect to pEp.foundation.adapter.macOS")


+ 4
- 4
pEpNotifications/pEpNotifications/Base.lproj/Main.storyboard View File

@ -21,14 +21,14 @@
<action selector="installNow:" target="Voe-Tx-rLC" id="ESa-58-lKy"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="vo5-AO-stB"/>
<menuItem title="Update now" id="aUJ-VR-2jH">
<modifierMask key="keyEquivalentModifierMask"/>
</menuItem>
<menuItem title="Hide this menu without update" id="2ca-qV-RUD">
<menuItem isSeparatorItem="YES" id="vo5-AO-stB"/>
<menuItem title="Item" id="y9Y-ru-Qxs" userLabel="Schedule updates">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="hideMenu:" target="Voe-Tx-rLC" id="qdk-Yw-VoX"/>
<action selector="scheduleUpdates:" target="Voe-Tx-rLC" id="H5q-dk-4ys"/>
</connections>
</menuItem>
<menuItem title="Quit p≡p" hidden="YES" id="4sb-4s-VLi" userLabel="Quit p≡p">
@ -48,7 +48,7 @@
</application>
<customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModule="pEp_updater" customModuleProvider="target">
<connections>
<outlet property="_hideMenu" destination="2ca-qV-RUD" id="wbZ-XM-DJj"/>
<outlet property="_scheduleUpdates" destination="y9Y-ru-Qxs" id="JEY-Hx-pbv"/>
<outlet property="_updateNow" destination="aUJ-VR-2jH" id="34x-R2-L4V"/>
<outlet property="pEpMenu" destination="uQy-DD-JDr" id="SSj-Qs-zAq"/>
<outlet property="statusText" destination="D45-AS-mhG" id="PNv-G8-o8P"/>


BIN
pEpNotifications/pEpNotifications/de.lproj/Localizable.strings View File


+ 2
- 0
pEpNotifications/pEpNotifications/de.lproj/Main.strings View File

@ -17,3 +17,5 @@
"aUJ-VR-2jH.title" = "Jetzt nach Updates suchen";
"2ca-qV-RUD.title" = "Dieses Menü ausblenden falls kein Update vorhanden";
"y9Y-ru-Qxs.title" = "Automatische Updates";

+ 1
- 0
pEpNotifications/pEpNotifications/en.lproj/Main.strings View File

@ -15,3 +15,4 @@
"uQy-DD-JDr.title" = "p≡p updates";
"aUJ-VR-2jH.title" = "Update now";
"y9Y-ru-Qxs.title" = "Keep software up to date";

Loading…
Cancel
Save