-
-
Notifications
You must be signed in to change notification settings - Fork 132
/
AppDelegate.swift
253 lines (213 loc) · 10 KB
/
AppDelegate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import AppKit
import Cocoa
import Foundation
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDelegate, NSTextFieldDelegate, NSTextViewDelegate {
@IBOutlet var statusItemMenu: NSMenu!
@IBOutlet var testConfigWindow: NSWindow!
@IBOutlet var yourTextField: NSTextField!
@IBOutlet var textView: NSTextView!
@objc var statusItem: NSStatusItem!
var configLoader: FinickyConfig!
var shortUrlResolver: FNShortUrlResolver = FNShortUrlResolver()
@objc var isActive: Bool = true
func applicationWillFinishLaunching(_: Notification) {
yourTextField.delegate = self
let bundleId = "net.kassett.Finicky"
LSSetDefaultHandlerForURLScheme("http" as CFString, bundleId as CFString)
LSSetDefaultHandlerForURLScheme("https" as CFString, bundleId as CFString)
NSUserNotificationCenter.default.delegate = self
let img: NSImage! = NSImage(named: "statusitem")
img.isTemplate = true
let invalidImg: NSImage! = NSImage(named: "statusitemerror")
invalidImg.isTemplate = true
let bar = NSStatusBar.system
// Workaround for some bug: -1 instead of NSVariableStatusItemLength
statusItem = bar.statusItem(withLength: CGFloat(-1))
statusItem.menu = statusItemMenu
statusItem.highlightMode = true
statusItem.image = invalidImg
toggleDockIcon(showIcon: false)
func toggleIconCallback(show: Bool) {
guard statusItem != nil else { return }
statusItem.isVisible = !show
}
func setShortUrlProviders(shortUrlProviders: [String]?) {
shortUrlResolver = FNShortUrlResolver(shortUrlProviders: shortUrlProviders)
}
func updateStatus(valid: Bool) {
if valid {
statusItem.image = img
} else {
statusItem.image = invalidImg
}
}
configLoader = FinickyConfig(toggleIconCallback: toggleIconCallback, logToConsoleCallback: logToConsole, setShortUrlProviders: setShortUrlProviders, updateStatus: updateStatus)
let appleEventManager: NSAppleEventManager = NSAppleEventManager.shared()
appleEventManager.setEventHandler(self, andSelector: #selector(AppDelegate.handleGetURLEvent(_:withReplyEvent:)), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL))
}
@IBAction func reloadConfig(_: NSMenuItem) {
configLoader.listenToChanges(showInitialSuccess: true)
}
@IBAction func checkUpdates(_: NSMenuItem) {
checkForUpdate { (version: Version?) -> Void in
if version == nil {
showNotification(title: "No new version available")
DispatchQueue.main.async {
self.logToConsole("No new version available")
}
return
}
if let version = version {
print("There is a newer version out there: \(version.title) \(version.version)")
showNotification(title: "New version available: \(version.title) \(version.version)")
DispatchQueue.main.async {
self.logToConsole("There is a newer version out there: \(version.title) \(version.version). More information is available on https://github.com/johnste/finicky/releases")
}
}
}
}
@IBAction func showAboutPanel(_ sender: NSMenuItem) {
NSApp.activate(ignoringOtherApps: true)
NSApp.orderFrontStandardAboutPanel(sender)
}
@IBAction func showTestConfigWindow(_ sender: Any?) {
NSApp.activate(ignoringOtherApps: true)
testConfigWindow.center()
testConfigWindow.orderFront(sender)
}
func logToConsole(_ message: String) {
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateString = formatter.string(from: date)
textView.string = dateString + " - " + message + "\n\n" + textView.string.prefix(20000).trimmingCharacters(in: .whitespacesAndNewlines)
}
@IBAction func testUrl(_ sender: NSTextField) {
let value = sender.stringValue
if !value.starts(with: "https://"), !value.starts(with: "http://") {
logToConsole("Finicky only understand https:// and http:// urls")
return
}
if let url = URL(string: value) {
shortUrlResolver.resolveUrl(url, callback: { (URL) -> Void in
// Dispatch the call to the main thread
// https://developer.apple.com/documentation/code_diagnostics/main_thread_checker
DispatchQueue.main.async {
self.performTest(url: URL)
}
})
}
}
func performTest(url: URL) {
if let appDescriptor = configLoader.determineOpeningApp(url: url, sourceBundleIdentifier: "net.kassett.finicky") {
var description = """
Would open url: \(appDescriptor.url)
"""
if appDescriptor.browsers.count == 1 {
if let browser = appDescriptor.browsers.first {
description += "Browser:\n"
description += " \(browser.name) \(browser.openInBackground ?? false ? "(opens in background)" : "")\n"
}
} else if appDescriptor.browsers.count == 0 {
description += "Won't open any browser"
} else {
description += "First active browser of:\n"
for (index, browser) in appDescriptor.browsers.enumerated() {
description += " [\(index)]: \(browser.name) \(browser.openInBackground ?? false ? "(opens in background)" : "")\n"
}
}
logToConsole(description)
}
}
@discardableResult
@objc func toggleDockIcon(showIcon state: Bool) -> Bool {
var result: Bool
if state {
result = NSApp.setActivationPolicy(NSApplication.ActivationPolicy.regular)
} else {
result = NSApp.setActivationPolicy(NSApplication.ActivationPolicy.accessory)
}
return result
}
func getPidPath(pid: Int32) -> String? {
let pathBuffer = UnsafeMutablePointer<UInt8>.allocate(capacity: Int(MAXPATHLEN))
defer {
pathBuffer.deallocate()
}
let pathLength = proc_pidpath(pid, pathBuffer, UInt32(MAXPATHLEN))
if pathLength > 0 {
let path = String(cString: pathBuffer)
return path
}
return nil
}
@objc func handleGetURLEvent(_ event: NSAppleEventDescriptor?, withReplyEvent _: NSAppleEventDescriptor?) {
toggleDockIcon(showIcon: false)
let url: URL = URL(string: event!.paramDescriptor(forKeyword: AEKeyword(keyDirectObject))!.stringValue!)!
let pid = event!.attributeDescriptor(forKeyword: AEKeyword(keySenderPIDAttr))!.int32Value
let sourceBundleIdentifier = NSRunningApplication(processIdentifier: pid)?.bundleIdentifier
let path = getPidPath(pid: pid)
shortUrlResolver.resolveUrl(url, callback: { (URL) -> Void in
self.callUrlHandlers(sourceBundleIdentifier, url: URL, sourceProcessPath: path)
})
}
func getActiveApp(browsers: [BrowserOpts]) -> BrowserOpts? {
if browsers.count == 0 {
return nil
}
if browsers.count == 1 {
return browsers.first
}
for browser in browsers {
let apps = NSRunningApplication.runningApplications(withBundleIdentifier: browser.bundleId)
if !apps.isEmpty {
let app: NSRunningApplication = apps[0]
let bundleIdentifier = app.bundleIdentifier
if bundleIdentifier != nil {
return browser
}
}
}
// If we are here, no apps are running, so we return the first bundleIds in the array instead.
return browsers.first
}
@objc func callUrlHandlers(_ sourceBundleIdentifier: String?, url: URL, sourceProcessPath: String?) {
if let appDescriptor = configLoader.determineOpeningApp(url: url, sourceBundleIdentifier: sourceBundleIdentifier, sourceProcessPath: sourceProcessPath) {
if let appToStart = getActiveApp(browsers: appDescriptor.browsers) {
if NSWorkspace.shared.absolutePathForApplication(withBundleIdentifier: appToStart.bundleId) != nil {
openUrlWithBrowser(appDescriptor.url, bundleIdentifier: appToStart.bundleId, openInBackground: appToStart.openInBackground)
} else {
let description = "Finicky was unable to find the application \"" + appToStart.name + "\""
print(description)
logToConsole(description)
showNotification(title: "Unable to find application", informativeText: "Finicky was unable to find the application \"" + appToStart.name + "\"", error: true)
}
}
}
}
func userNotificationCenter(_: NSUserNotificationCenter, shouldPresent _: NSUserNotification) -> Bool {
return true
}
func userNotificationCenter(_: NSUserNotificationCenter, didActivate _: NSUserNotification) {
showTestConfigWindow(nil)
}
func openUrlWithBrowser(_ url: URL, bundleIdentifier: String, openInBackground: Bool?) {
let openInBackground = openInBackground ?? false
print("Opening " + bundleIdentifier + " at: " + url.absoluteString)
let command = getBrowserCommand(bundleIdentifier, url: url, openInBackground: openInBackground)
shell(command)
}
func application(_: NSApplication, openFiles filenames: [String]) {
toggleDockIcon(showIcon: false)
for filename in filenames {
callUrlHandlers(nil, url: URL(fileURLWithPath: filename), sourceProcessPath: nil)
}
}
func applicationDidBecomeActive(_: Notification) {
isActive = true
}
func applicationDidResignActive(_: Notification) {
isActive = false
}
}