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
|
//
// AppDelegate.swift
// DWA140Menu
//
// Created by FivePixels on 9/3/18.
// Copyright © 2018 FivePixels. All rights reserved.
//
import Cocoa
import Foundation
import SystemConfiguration
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
var interfaceName : String = ""
let dynRef = SCDynamicStoreCreate(kCFAllocatorSystemDefault, "iked" as CFString, nil, nil)
let ipv4key = SCDynamicStoreCopyValue(dynRef, "State:/Network/Global/IPv4" as CFString)
var isDeviceInterfaceConnected : Bool?
let applicationStoryboard = NSStoryboard(name: "Main", bundle: nil)
let mainWindowController = applicationStoryboard.instantiateController(withIdentifier: "MainWindowController") as! NSWindowController
let mainViewController = applicationStoryboard.instantiateController(withIdentifier: "MainViewController") as! NSViewController
let tabViewController = applicationStoryboard.instantiateController(withIdentifier: "TabViewController") as! NSTabViewController
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDelegate {
@objc func openApplicationWindow() {
if mainWindowController.window?.isVisible == false {
mainWindowController.showWindow(self)
NSApp.activate(ignoringOtherApps: true)
} else {
NSApp.activate(ignoringOtherApps: true)
}
}
@objc func openDWAPreferencePane() {
let fileManager = FileManager.default
let allUsersPath = "/Library/PreferencePanes/DWA-140WirelessUtility.prefpane"
let currentUserPath = "~/Library/PreferencePanes/DWA-140WirelessUtility.prefPane"
if fileManager.fileExists(atPath: allUsersPath) {
NSWorkspace.shared.open(URL(fileURLWithPath: allUsersPath)) }
else if fileManager.fileExists(atPath: currentUserPath) {
NSWorkspace.shared.open(URL(fileURLWithPath: currentUserPath))
} else {
showNotification(title: "DWA-140 Shortcut Error", withText: "The preference pane does not exist.")
}
}
@objc func openNetworkPreferencePane() {
NSWorkspace.shared.open(URL(fileURLWithPath: "/System/Library/PreferencePanes/Network.prefPane"))
}
@objc func refreshApplication() {
let url = URL(fileURLWithPath: Bundle.main.resourcePath!)
let path = url.deletingLastPathComponent().deletingLastPathComponent().absoluteString
let task = Process()
task.launchPath = "/usr/bin/open"
task.arguments = [path]
task.launch()
exit(0)
}
@objc func showNotification(title: String, withText: String) -> Void {
let notification = NSUserNotification()
notification.title = title
notification.informativeText = withText
notification.soundName = NSUserNotificationDefaultSoundName
NSUserNotificationCenter.default.delegate = self
NSUserNotificationCenter.default.deliver(notification)
}
func getInterface() -> String {
// thanks NetUtils-Swift :p
if var dict1 = ipv4key as? [String: AnyObject] {
if (dict1["PrimaryInterface"] == nil) {
interfaceName = "none"
}
interfaceName = (dict1["PrimaryInterface"] as! String)
}
return interfaceName
}
func constructMenu() {
let applicationMenu = NSMenu()
applicationMenu.addItem(NSMenuItem(title: "DWA-140 Shortcut", action: #selector(openApplicationWindow), keyEquivalent: ""))
applicationMenu.addItem(NSMenuItem.separator())
applicationMenu.addItem(NSMenuItem(title: isInterfaceConnected(), action: nil, keyEquivalent: ""))
applicationMenu.addItem(NSMenuItem.separator())
applicationMenu.addItem(NSMenuItem(title: "Refresh Status", action: #selector(refreshApplication), keyEquivalent: "r"))
applicationMenu.addItem(NSMenuItem(title: "Open DWA-140", action: #selector(openDWAPreferencePane), keyEquivalent: "t"))
applicationMenu.addItem(NSMenuItem(title: "Network Preferences", action: #selector(openNetworkPreferencePane), keyEquivalent: ""))
applicationMenu.addItem(NSMenuItem(title: "Quit", action: #selector(NSApplication.terminate(_:)), keyEquivalent: ""))
statusItem.menu = applicationMenu
}
func isInterfaceConnected() -> String {
if getInterface() == "en0" {
isDeviceInterfaceConnected = true
DispatchQueue.main.async {
self.setStatusIcon(icon: "WiFiConnected")
}
return "USB WiFi is connected."
} else {
isDeviceInterfaceConnected = false
DispatchQueue.main.async {
self.setStatusIcon(icon: "WiFiDisconnected")
}
return "USB Wifi is disconnected."
}
}
func userNotificationCenter(_ center: NSUserNotificationCenter,
shouldPresent notification: NSUserNotification) -> Bool {
return true
}
func applicationDidFinishLaunching(_ notification: Notification) {
constructMenu()
}
func setStatusIcon(icon: String) {
if let button = statusItem.button {
if button.image?.name() != icon {
button.image = NSImage(named: NSImage.Name(icon))
}
}
}
}
|