Clover.app: different way to add itself as login Item

Switch to LSSharedFileXXX to enale loging as SMLoginItemSetEnabled() is buggie.
This commit is contained in:
vectorsigma72 2020-05-03 14:03:26 +02:00
parent 2502b0d825
commit 3513285889
4 changed files with 113 additions and 15 deletions

View File

@ -656,8 +656,8 @@
955BEE1123C6B43C00425AB0 /* ThemeManager */,
95E68AC9235B862F002B37A5 /* AppDelegate.swift */,
95C5152E236A0A7400E4A3A8 /* SettingsView.swift */,
95696B7A2401829800AFAD37 /* GengConfig.swift */,
95C51535236B1F7700E4A3A8 /* RunAtLogin.swift */,
95696B7A2401829800AFAD37 /* GengConfig.swift */,
95E68ACB235B862F002B37A5 /* ViewController.swift */,
95E68ADE235B86A1002B37A5 /* bdmesg.swift */,
955689DA23A2728000AD323C /* IO.swift */,

View File

@ -10,6 +10,7 @@ import Cocoa
import ServiceManagement
extension AppDelegate {
//MARK: new login item methods (but buggie)
func setLaunchAtStartup() {
let success : Bool = SMLoginItemSetEnabled(gHelperID, true)
UDs.set(success, forKey: kRunAtLogin)
@ -21,4 +22,100 @@ extension AppDelegate {
UDs.set(success ? false : true, forKey: kRunAtLogin)
UDs.synchronize()
}
//MARK: old login item methods (but working)
func getLoginItemURL(for item: LSSharedFileListItem) -> URL? {
var url : URL? = nil
if #available(OSX 10.10, *) {
url = LSSharedFileListItemCopyResolvedURL(item, 0, nil)?.takeRetainedValue() as URL?
} else {
var ItemURL : Unmanaged<CFURL>? = nil
let flags : UInt32 = UInt32(kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes)
LSSharedFileListItemResolve(item, flags, &ItemURL, nil)
url = ItemURL?.takeRetainedValue() as URL?
}
return url
}
func addAsLoginItem() -> Bool {
var found : Bool = false
let currentUrl = Bundle.main.bundleURL
if let sharedFileList = LSSharedFileListCreate(nil,
kLSSharedFileListSessionLoginItems.takeRetainedValue(),
nil)?.takeRetainedValue() {
if let snapshot = LSSharedFileListCopySnapshot(sharedFileList,
nil).takeRetainedValue() as? [LSSharedFileListItem] {
for item in snapshot {
if let itemUrl = self.getLoginItemURL(for: item) {
guard let info = NSDictionary(contentsOfFile: itemUrl.path.addPath("Contents/Info.plist")) as? [String: Any] else { continue }
let bi = info[kCFBundleIdentifierKey as String] as? String
if bi == Bundle.main.bundleIdentifier {
// is Clover.app, but is the current one?
if itemUrl == currentUrl {
found = true
} else {
LSSharedFileListItemRemove(sharedFileList, item)
}
}
}
}
}
if !found {
LSSharedFileListInsertItemURL(sharedFileList,
kLSSharedFileListItemBeforeFirst.takeRetainedValue(),
nil,
nil,
currentUrl as CFURL,
nil,
nil)
found = true
}
}
return found
}
func removeAsLoginItem() -> Bool {
// remove any Clover.app logged in
self.removeLaunchAtStartup() // call new method too (just in case store login item somewhere..)
print("removeAsLoginItem()")
let sharedFileList = LSSharedFileListCreate(nil,
kLSSharedFileListSessionLoginItems.takeRetainedValue(),
nil).takeRetainedValue()
if let snapshot = LSSharedFileListCopySnapshot(sharedFileList, nil).takeRetainedValue() as? [LSSharedFileListItem] {
for item in snapshot {
if let url = self.getLoginItemURL(for: item) {
guard let info = NSDictionary(contentsOfFile: url.path.addPath("Contents/Info.plist")) as? [String: Any] else { continue }
let bi = info[kCFBundleIdentifierKey as String] as? String
if bi == Bundle.main.bundleIdentifier {
let status = LSSharedFileListItemRemove(sharedFileList, item)
print(status)
}
}
}
}
return true
}
func amILoginItem() -> Bool {
let sharedFileList = LSSharedFileListCreate(nil,
kLSSharedFileListSessionLoginItems.takeRetainedValue(),
nil).takeRetainedValue()
if let snapshot = LSSharedFileListCopySnapshot(sharedFileList, nil).takeRetainedValue() as? [LSSharedFileListItem] {
for item in snapshot {
if let url = self.getLoginItemURL(for: item) {
guard let info = NSDictionary(contentsOfFile: url.path.addPath("Contents/Info.plist")) as? [String: Any] else { continue }
let bi = info[kCFBundleIdentifierKey as String] as? String
if bi == Bundle.main.bundleIdentifier && url == Bundle.main.bundleURL {
return true
}
}
}
}
return false
}
}

View File

@ -202,7 +202,7 @@ final class SettingsViewController:
self.progressBar.isHidden = true
self.runAtLoginButton.state = UDs.bool(forKey: kRunAtLogin) ? .on : .off
self.runAtLoginButton.state = AppSD.amILoginItem() ? .on : .off
self.unmountButton.isEnabled = false
self.autoMountButton.isEnabled = false
self.autoMountButton.isHidden = true
@ -963,13 +963,15 @@ final class SettingsViewController:
// MARK: Run At Login
@IBAction func runAtLogin(_ sender: NSButton!) {
if sender.state == .on {
AppSD.setLaunchAtStartup()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
sender.state = AppSD.addAsLoginItem() ? .on : .off
}
} else {
AppSD.removeLaunchAtStartup()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
//sender.state = AppSD.removeAsLoginItem() ? .on : .off
_ = AppSD.removeAsLoginItem()
}
}
// check the result
sender.state = UDs.bool(forKey: kRunAtLogin) ? .on : .off
}
// MARK: NVRAM editing

View File

@ -8,7 +8,7 @@
import Foundation
let daemonVersion = "1.1.3"
let daemonVersion = "1.1.4"
let fm = FileManager.default
@ -277,13 +277,12 @@ func main() {
}
if fm.fileExists(atPath: logPath) {
if let log : String? = try? String(contentsOfFile: logPath) {
if let lines = log?.components(separatedBy: CharacterSet.newlines) {
if lines.count > logLinesMax {
// take only latests
run(cmd: "tail -n \(logLinesMax) \(logPath) 2>/dev/null > \(logPath).tmp")
run(cmd: "mv -f \(logPath).tmp \(logPath)")
}
if let log = try? String(contentsOfFile: logPath) {
let lines = log.components(separatedBy: CharacterSet.newlines)
if lines.count > logLinesMax {
// take only latests
run(cmd: "tail -n \(logLinesMax) \(logPath) 2>/dev/null > \(logPath).tmp")
run(cmd: "mv -f \(logPath).tmp \(logPath)")
}
}
}