From ba1b7c789c95ea48e6581b39b56750e5b09dc1a7 Mon Sep 17 00:00:00 2001 From: Raimon Lapuente Date: Fri, 11 Aug 2017 12:05:15 +0200 Subject: [PATCH 1/7] Bugfix Added Logic for autolaunching the popover on the first run --- BalanceOpen/AppDelegate.swift | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/BalanceOpen/AppDelegate.swift b/BalanceOpen/AppDelegate.swift index a8dee887..6de57199 100644 --- a/BalanceOpen/AppDelegate.swift +++ b/BalanceOpen/AppDelegate.swift @@ -20,6 +20,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { let statusItem = CCNStatusItem.sharedInstance()! var contentViewController: PopoverViewController! var preferencesWindowController: NSWindowController! + var launchAtLogin: Bool var pinned: Bool { get { @@ -46,6 +47,8 @@ class AppDelegate: NSObject, NSApplicationDelegate { // override init() { + self.launchAtLogin = false + super.init() terminateIfAlreadyRunning() @@ -85,7 +88,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { ) // Query the helper app to see if we were auto launched (dirty hack because Apple makes simple shit difficult) - forceAutoLaunch() + launchAtLogin = forceAutoLaunch() // Initialize singletons initializeSingletons() @@ -222,9 +225,9 @@ class AppDelegate: NSObject, NSApplicationDelegate { } //DO WE NEED THIS? -// if !autoLaunch.wasLaunchedAtLogin && !showedAlert { -// self.showPopover() -// } + if !self.launchAtLogin && !showedAlert { + self.showPopover() + } } } @@ -333,9 +336,12 @@ class AppDelegate: NSObject, NSApplicationDelegate { NSApp.terminate(nil) } - func forceAutoLaunch() { + func forceAutoLaunch() -> Bool{ if (!SMLoginItemSetEnabled("balance.money.AutoLaunchBalanceHelper" as CFString, defaults.launchAtLogin)) { print("Auto login was not successful"); + return false + } else { + return true } } From e2ff45f8682d6a80df8e8a785f98fc7d3d67230e Mon Sep 17 00:00:00 2001 From: Raimon Lapuente Date: Fri, 11 Aug 2017 12:08:40 +0200 Subject: [PATCH 2/7] Comment Have a more meaningful comment on autolaunch --- BalanceOpen/AppDelegate.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BalanceOpen/AppDelegate.swift b/BalanceOpen/AppDelegate.swift index 6de57199..0bce0145 100644 --- a/BalanceOpen/AppDelegate.swift +++ b/BalanceOpen/AppDelegate.swift @@ -224,7 +224,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { } } - //DO WE NEED THIS? + //Show Popover if we are manually lauching the app if !self.launchAtLogin && !showedAlert { self.showPopover() } From af0f60286b3bf25fa50392bdf867e68049d1a39b Mon Sep 17 00:00:00 2001 From: Raimon Lapuente Date: Fri, 11 Aug 2017 15:21:24 +0200 Subject: [PATCH 3/7] Refactor Suggested changes by reiew --- BalanceOpen/AppDelegate.swift | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/BalanceOpen/AppDelegate.swift b/BalanceOpen/AppDelegate.swift index 0bce0145..740777f3 100644 --- a/BalanceOpen/AppDelegate.swift +++ b/BalanceOpen/AppDelegate.swift @@ -20,7 +20,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { let statusItem = CCNStatusItem.sharedInstance()! var contentViewController: PopoverViewController! var preferencesWindowController: NSWindowController! - var launchAtLogin: Bool + var launchAtLogin: Bool = false var pinned: Bool { get { @@ -47,8 +47,6 @@ class AppDelegate: NSObject, NSApplicationDelegate { // override init() { - self.launchAtLogin = false - super.init() terminateIfAlreadyRunning() From a9c35cc0c734ebe825d930f8f737d1500c7155ff Mon Sep 17 00:00:00 2001 From: Ben Baron Date: Fri, 11 Aug 2017 16:55:25 +0300 Subject: [PATCH 4/7] WIP fixed login item and autolaunch detection --- BalanceOpen.xcodeproj/project.pbxproj | 4 +-- BalanceOpen/AppDelegate.swift | 27 +++++++++---------- .../Static & Singletons/Defaults.swift | 13 ++++++++- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/BalanceOpen.xcodeproj/project.pbxproj b/BalanceOpen.xcodeproj/project.pbxproj index 0871cad5..10c4781c 100644 --- a/BalanceOpen.xcodeproj/project.pbxproj +++ b/BalanceOpen.xcodeproj/project.pbxproj @@ -995,7 +995,7 @@ DEVELOPMENT_TEAM = Q4G66XSS36; INFOPLIST_FILE = AutoLaunchBalanceHelper/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = balance.money.AutoLaunchBalanceHelper; + PRODUCT_BUNDLE_IDENTIFIER = "software.balanced.balance-open-autolaunch"; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; SKIP_INSTALL = YES; @@ -1011,7 +1011,7 @@ DEVELOPMENT_TEAM = Q4G66XSS36; INFOPLIST_FILE = AutoLaunchBalanceHelper/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = balance.money.AutoLaunchBalanceHelper; + PRODUCT_BUNDLE_IDENTIFIER = "software.balanced.balance-open-autolaunch"; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; SKIP_INSTALL = YES; diff --git a/BalanceOpen/AppDelegate.swift b/BalanceOpen/AppDelegate.swift index 740777f3..b3582940 100644 --- a/BalanceOpen/AppDelegate.swift +++ b/BalanceOpen/AppDelegate.swift @@ -9,6 +9,8 @@ import Cocoa import ServiceManagement +let autolaunchBundleId = "software.balanced.balance-open-autolaunch" + class AppDelegate: NSObject, NSApplicationDelegate { // @@ -20,8 +22,14 @@ class AppDelegate: NSObject, NSApplicationDelegate { let statusItem = CCNStatusItem.sharedInstance()! var contentViewController: PopoverViewController! var preferencesWindowController: NSWindowController! - var launchAtLogin: Bool = false + var launchedAtLogin = false + var isAutolaunchHelperRunning: Bool { + // Check for running process and return bool + fatalError("implement") + } +} + var pinned: Bool { get { return statusItem.windowConfiguration.isPinned @@ -85,8 +93,8 @@ class AppDelegate: NSObject, NSApplicationDelegate { NSAppleEventManager.shared().setEventHandler(self, andSelector: #selector(AppDelegate.handleURLEvent(event:withReply:)), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL) ) - // Query the helper app to see if we were auto launched (dirty hack because Apple makes simple shit difficult) - launchAtLogin = forceAutoLaunch() + // Check for the helper app to see if we were auto launched + launchedAtLogin = isAutolaunchHelperRunning // Initialize singletons initializeSingletons() @@ -222,8 +230,8 @@ class AppDelegate: NSObject, NSApplicationDelegate { } } - //Show Popover if we are manually lauching the app - if !self.launchAtLogin && !showedAlert { + // Show Popover if we are manually lauching the app + if !self.launchedAtLogin && !showedAlert { self.showPopover() } } @@ -334,15 +342,6 @@ class AppDelegate: NSObject, NSApplicationDelegate { NSApp.terminate(nil) } - func forceAutoLaunch() -> Bool{ - if (!SMLoginItemSetEnabled("balance.money.AutoLaunchBalanceHelper" as CFString, defaults.launchAtLogin)) { - print("Auto login was not successful"); - return false - } else { - return true - } - } - func sendFeedback() { let urlString = "https://github.com/balancemymoney/BalanceForBlockchain/issues" _ = try? NSWorkspace.shared.open(URL(string: urlString)!, options: [], configuration: [:]) diff --git a/BalanceOpen/Data Model/Static & Singletons/Defaults.swift b/BalanceOpen/Data Model/Static & Singletons/Defaults.swift index a28e0ea2..bc479dd9 100644 --- a/BalanceOpen/Data Model/Static & Singletons/Defaults.swift +++ b/BalanceOpen/Data Model/Static & Singletons/Defaults.swift @@ -7,6 +7,7 @@ // import Foundation +import ServiceManagement class Defaults { internal struct Keys { @@ -46,7 +47,17 @@ class Defaults { return defaults.bool(forKey: Keys.launchAtLogin) } set { - defaults.set(newValue, forKey: Keys.launchAtLogin) + if SMLoginItemSetEnabled(autolaunchBundleId as CFString, enabled) { + defaults.set(newValue, forKey: Keys.launchAtLogin) + } else { + log.severe("Failed to set login at launch preference") + + let alert = NSAlert() + alert.alertStyle = .warning + alert.messageText = newValue ? "Unable to create the login item" : "Unable to remove the login item" + alert.addButton(withTitle: "OK") + alert.runModal() + } } } From 75df8555af07f89a22b29b0a040865005ae1154c Mon Sep 17 00:00:00 2001 From: Raimon Lapuente Date: Fri, 11 Aug 2017 15:56:21 +0200 Subject: [PATCH 5/7] Update bundle with proper prefix identifier --- BalanceOpen.xcodeproj/project.pbxproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BalanceOpen.xcodeproj/project.pbxproj b/BalanceOpen.xcodeproj/project.pbxproj index 0871cad5..9987e7d8 100644 --- a/BalanceOpen.xcodeproj/project.pbxproj +++ b/BalanceOpen.xcodeproj/project.pbxproj @@ -995,7 +995,7 @@ DEVELOPMENT_TEAM = Q4G66XSS36; INFOPLIST_FILE = AutoLaunchBalanceHelper/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = balance.money.AutoLaunchBalanceHelper; + PRODUCT_BUNDLE_IDENTIFIER = software.balanced.AutoLaunchBalanceHelper; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; SKIP_INSTALL = YES; @@ -1011,7 +1011,7 @@ DEVELOPMENT_TEAM = Q4G66XSS36; INFOPLIST_FILE = AutoLaunchBalanceHelper/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = balance.money.AutoLaunchBalanceHelper; + PRODUCT_BUNDLE_IDENTIFIER = software.balanced.AutoLaunchBalanceHelper; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; SKIP_INSTALL = YES; From 39d4e3ba31184f58f64fc6d3c1c116f37950c0ac Mon Sep 17 00:00:00 2001 From: Ben Baron Date: Fri, 11 Aug 2017 16:56:24 +0300 Subject: [PATCH 6/7] Removed extra } --- BalanceOpen/AppDelegate.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/BalanceOpen/AppDelegate.swift b/BalanceOpen/AppDelegate.swift index b3582940..f22b0499 100644 --- a/BalanceOpen/AppDelegate.swift +++ b/BalanceOpen/AppDelegate.swift @@ -28,7 +28,6 @@ class AppDelegate: NSObject, NSApplicationDelegate { // Check for running process and return bool fatalError("implement") } -} var pinned: Bool { get { From 653879f6937207cb45ab998cf8b13f2d9fa791fb Mon Sep 17 00:00:00 2001 From: Raimon Lapuente Date: Fri, 11 Aug 2017 18:14:24 +0200 Subject: [PATCH 7/7] Refactor Add missing lines for checking the helper app open --- BalanceOpen/AppDelegate.swift | 4 ++-- BalanceOpen/Data Model/Static & Singletons/Defaults.swift | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/BalanceOpen/AppDelegate.swift b/BalanceOpen/AppDelegate.swift index f22b0499..bd3c5f9a 100644 --- a/BalanceOpen/AppDelegate.swift +++ b/BalanceOpen/AppDelegate.swift @@ -25,8 +25,8 @@ class AppDelegate: NSObject, NSApplicationDelegate { var launchedAtLogin = false var isAutolaunchHelperRunning: Bool { - // Check for running process and return bool - fatalError("implement") + let appsRunning = NSRunningApplication.runningApplications(withBundleIdentifier: autolaunchBundleId) + return appsRunning.count > 0 } var pinned: Bool { diff --git a/BalanceOpen/Data Model/Static & Singletons/Defaults.swift b/BalanceOpen/Data Model/Static & Singletons/Defaults.swift index bc479dd9..8708ffb0 100644 --- a/BalanceOpen/Data Model/Static & Singletons/Defaults.swift +++ b/BalanceOpen/Data Model/Static & Singletons/Defaults.swift @@ -47,7 +47,7 @@ class Defaults { return defaults.bool(forKey: Keys.launchAtLogin) } set { - if SMLoginItemSetEnabled(autolaunchBundleId as CFString, enabled) { + if SMLoginItemSetEnabled(autolaunchBundleId as CFString, newValue) { defaults.set(newValue, forKey: Keys.launchAtLogin) } else { log.severe("Failed to set login at launch preference")