Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
insanj committed Mar 13, 2021
1 parent b971b35 commit e17d589
Show file tree
Hide file tree
Showing 8 changed files with 453 additions and 712 deletions.
16 changes: 2 additions & 14 deletions whilom.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
1E5B4D6A25FBFE4B00092422 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1E5B4D6925FBFE4B00092422 /* Cocoa.framework */; };
1E5B4DD625FC0DBF00092422 /* String+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E5B4DD525FC0DBF00092422 /* String+Extension.swift */; };
1E5B4DE525FC24CA00092422 /* LaunchAtLogin in Frameworks */ = {isa = PBXBuildFile; productRef = 1E5B4DE425FC24CA00092422 /* LaunchAtLogin */; };
1E6E8FD625FC8CED00ABCFE3 /* STPrivilegedTask.m in Sources */ = {isa = PBXBuildFile; fileRef = 1E6E8FD525FC8CED00ABCFE3 /* STPrivilegedTask.m */; };
1E6E8FDE25FC910E00ABCFE3 /* HelperAuthorization.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E6E8FDD25FC910E00ABCFE3 /* HelperAuthorization.swift */; };
1E6E8FE125FC94AA00ABCFE3 /* HelperTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E6E8FE025FC94AA00ABCFE3 /* HelperTask.swift */; };
1EE4E20E24F97E240097C32C /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1EE4E20D24F97E240097C32C /* AppDelegate.swift */; };
1EE4E21224F97E250097C32C /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 1EE4E21124F97E250097C32C /* Assets.xcassets */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -49,10 +46,7 @@
1E5B4D6925FBFE4B00092422 /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; };
1E5B4DD525FC0DBF00092422 /* String+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Extension.swift"; sourceTree = "<group>"; };
1E6E8FD325FC8CEC00ABCFE3 /* whilom-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "whilom-Bridging-Header.h"; sourceTree = "<group>"; };
1E6E8FD425FC8CED00ABCFE3 /* STPrivilegedTask.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = STPrivilegedTask.h; sourceTree = "<group>"; };
1E6E8FD525FC8CED00ABCFE3 /* STPrivilegedTask.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = STPrivilegedTask.m; sourceTree = "<group>"; };
1E6E8FDD25FC910E00ABCFE3 /* HelperAuthorization.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HelperAuthorization.swift; sourceTree = "<group>"; };
1E6E8FE025FC94AA00ABCFE3 /* HelperTask.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HelperTask.swift; sourceTree = "<group>"; };
1E6E8FE625FC973A00ABCFE3 /* SMJobBlessUtil.py */ = {isa = PBXFileReference; lastKnownFileType = text.script.python; path = SMJobBlessUtil.py; sourceTree = "<group>"; };
1EE4E20A24F97E240097C32C /* whilom.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = whilom.app; sourceTree = BUILT_PRODUCTS_DIR; };
1EE4E20D24F97E240097C32C /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
1EE4E21124F97E250097C32C /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
Expand Down Expand Up @@ -100,6 +94,7 @@
1EE4E20C24F97E240097C32C /* whilom */ = {
isa = PBXGroup;
children = (
1E6E8FE625FC973A00ABCFE3 /* SMJobBlessUtil.py */,
1EE4E20D24F97E240097C32C /* AppDelegate.swift */,
1E396DC425FB4C26008E2122 /* NSImage+Extension.swift */,
1E5B4DD525FC0DBF00092422 /* String+Extension.swift */,
Expand All @@ -108,10 +103,6 @@
1E5B4D0725FB779900092422 /* Main.storyboard */,
1EE4E21124F97E250097C32C /* Assets.xcassets */,
1EE4E21624F97E250097C32C /* Info.plist */,
1E6E8FDD25FC910E00ABCFE3 /* HelperAuthorization.swift */,
1E6E8FE025FC94AA00ABCFE3 /* HelperTask.swift */,
1E6E8FD425FC8CED00ABCFE3 /* STPrivilegedTask.h */,
1E6E8FD525FC8CED00ABCFE3 /* STPrivilegedTask.m */,
1E6E8FD325FC8CEC00ABCFE3 /* whilom-Bridging-Header.h */,
);
path = whilom;
Expand Down Expand Up @@ -217,11 +208,8 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
1E6E8FDE25FC910E00ABCFE3 /* HelperAuthorization.swift in Sources */,
1E5B4DD625FC0DBF00092422 /* String+Extension.swift in Sources */,
1E6E8FE125FC94AA00ABCFE3 /* HelperTask.swift in Sources */,
1EE4E20E24F97E240097C32C /* AppDelegate.swift in Sources */,
1E6E8FD625FC8CED00ABCFE3 /* STPrivilegedTask.m in Sources */,
1E396DC525FB4C26008E2122 /* NSImage+Extension.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
58 changes: 14 additions & 44 deletions whilom/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@

import Cocoa
import LaunchAtLogin
import Security

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
// MARK: - properties
// MARK: state
private var rememberedPassword: String?
private let helper = HelperAuthorization()

private var hasShownRememberPasswordAlert: Bool = false
// MARK: modals that need to be referenced twice
private var passwordRememberAlert: NSAlert?

Expand Down Expand Up @@ -92,7 +91,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

let myAppleScript = """
do shell script "sudo pmset -a disablesleep 0"\(shouldAppend ? appendString : "") with administrator privileges
do shell script "sudo pmset -a disablesleep 0 && caffeinate"\(shouldAppend ? appendString : "") with administrator privileges
"""

guard let scriptObject = NSAppleScript(source: myAppleScript) else {
Expand All @@ -112,7 +111,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

let myAppleScript = """
do shell script "sudo pmset -a disablesleep 1"\(shouldAppend ? appendString : "") with administrator privileges
do shell script "sudo pmset -a disablesleep 1 && caffeinate"\(shouldAppend ? appendString : "") with administrator privileges
"""

guard let scriptObject = NSAppleScript(source: myAppleScript) else {
Expand Down Expand Up @@ -142,9 +141,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
enableSleepScript = buildEnableSleepScript()
disableSleepScript = buildDisableSleepScript()

let authData1 = helper.authorizeHelper()
let authRef1 = helper.checkAuthorization(authData: authData1, command: "pmset")
// showPasswordRememberAlert()
if !hasShownRememberPasswordAlert { // TODO
hasShownRememberPasswordAlert = true
showPasswordRememberAlert()
}
}

func applicationWillTerminate(_ aNotification: Notification) {
Expand Down Expand Up @@ -229,39 +229,13 @@ class AppDelegate: NSObject, NSApplicationDelegate {
// MARK: execute scripts based on state
@objc func disableSleep() -> Bool {
if !isJustMessingAround {
// STPrivilegedTask.launchedPrivilegedTask(withLaunchPath: "/", arguments: ["sudo pmset -a disablesleep 1"], currentDirectory: "/", authorization: authRef)

// let rightname = "sys.openfile.readonly./tmp/cantread.txt"
//
// var status: OSStatus
//
// var authref: AuthorizationRef?
// let flags = AuthorizationFlags([.interactionAllowed, .extendRights, .preAuthorize])
// status = AuthorizationCreate(nil, nil, flags, &authref)
//
// var item = AuthorizationItem(name: kAuthorizationEnvironmentPassword, valueLength: 0, value: nil, flags: 0)
// var rights = AuthorizationRights(count: 1, items: &item)
// status = AuthorizationCopyRights(authref!, &rights, nil, flags, nil)
//
// var token = AuthorizationExternalForm()
// status = AuthorizationMakeExternalForm(authref!, &token)
//
// let data = NSData(bytes: &token.bytes, length: kAuthorizationExternalFormLength)
// data.write(toFile: "./token", atomically: true)
// print("External form written to ./token")

var error: NSDictionary?
disableSleepScript?.executeAndReturnError(&error)

// var error: NSDictionary?
// disableSleepScript?.executeAndReturnError(&error)
//
// if let error = error {
// let alert = NSAlert(error: NSError(domain: "com.insanj.whilom", code: 0, userInfo: [NSLocalizedDescriptionKey: error["NSAppleScriptErrorMessage"]!]))
// alert.runModal()
// return false
// }

HelperTask.runTask(command: "pmset", arguments: ["-a", "disablesleep", "0"]) { (number) in
print(number)
if let error = error {
let alert = NSAlert(error: NSError(domain: "com.insanj.whilom", code: 0, userInfo: [NSLocalizedDescriptionKey: error["NSAppleScriptErrorMessage"]!]))
alert.runModal()
return false
}
}

Expand All @@ -272,10 +246,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {

@objc func enableSleep() -> Bool {
if !isJustMessingAround {
// let authData = helper.authorizeHelper()
// let authRef = helper.checkAuthorization(authData: authData, command: "sudo pmset -a disablesleep 0")

// STPrivilegedTask.launchedPrivilegedTask(withLaunchPath: "/bin/sh", arguments: ["sudo pmset -a disablesleep 0"])
var error: NSDictionary?
enableSleepScript?.executeAndReturnError(&error)

Expand Down
Loading

0 comments on commit e17d589

Please sign in to comment.