Skip to content
This repository has been archived by the owner on Oct 26, 2021. It is now read-only.

Commit

Permalink
Merge pull request #86 from balancemymoney/fix-autolaunch
Browse files Browse the repository at this point in the history
Autolaunch bug fix
  • Loading branch information
Ben Baron authored Aug 16, 2017
2 parents 2ab308f + 653879f commit fa7013c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
4 changes: 2 additions & 2 deletions BalanceOpen.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
26 changes: 14 additions & 12 deletions BalanceOpen/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import Cocoa
import ServiceManagement

let autolaunchBundleId = "software.balanced.balance-open-autolaunch"

class AppDelegate: NSObject, NSApplicationDelegate {

//
Expand All @@ -21,6 +23,12 @@ class AppDelegate: NSObject, NSApplicationDelegate {
var contentViewController: PopoverViewController!
var preferencesWindowController: NSWindowController!

var launchedAtLogin = false
var isAutolaunchHelperRunning: Bool {
let appsRunning = NSRunningApplication.runningApplications(withBundleIdentifier: autolaunchBundleId)
return appsRunning.count > 0
}

var pinned: Bool {
get {
return statusItem.windowConfiguration.isPinned
Expand Down Expand Up @@ -84,8 +92,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)
forceAutoLaunch()
// Check for the helper app to see if we were auto launched
launchedAtLogin = isAutolaunchHelperRunning

// Initialize singletons
initializeSingletons()
Expand Down Expand Up @@ -221,10 +229,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
}

//DO WE NEED THIS?
// if !autoLaunch.wasLaunchedAtLogin && !showedAlert {
// self.showPopover()
// }
// Show Popover if we are manually lauching the app
if !self.launchedAtLogin && !showedAlert {
self.showPopover()
}
}
}

Expand Down Expand Up @@ -333,12 +341,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {
NSApp.terminate(nil)
}

func forceAutoLaunch() {
if (!SMLoginItemSetEnabled("balance.money.AutoLaunchBalanceHelper" as CFString, defaults.launchAtLogin)) {
print("Auto login was not successful");
}
}

func sendFeedback() {
let urlString = "https://github.com/balancemymoney/BalanceForBlockchain/issues"
_ = try? NSWorkspace.shared.open(URL(string: urlString)!, options: [], configuration: [:])
Expand Down
13 changes: 12 additions & 1 deletion BalanceOpen/Data Model/Static & Singletons/Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import ServiceManagement

class Defaults {
internal struct Keys {
Expand Down Expand Up @@ -46,7 +47,17 @@ class Defaults {
return defaults.bool(forKey: Keys.launchAtLogin)
}
set {
defaults.set(newValue, forKey: Keys.launchAtLogin)
if SMLoginItemSetEnabled(autolaunchBundleId as CFString, newValue) {
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()
}
}
}

Expand Down

0 comments on commit fa7013c

Please sign in to comment.