Skip to content

Commit 7529009

Browse files
committed
Add VPN groups to make it easy to connect to integration vs production
1 parent 436c6ae commit 7529009

5 files changed

+77
-23
lines changed

GoVPN/AppDelegate.swift

+48-13
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ class AppDelegate: NSObject, NSApplicationDelegate {
4040
}
4141
}
4242

43+
@objc func selectVPNGroup(_ sender: Any?) {
44+
if let menuItem = sender as? NSMenuItem {
45+
let vpnGroups = Dictionary(grouping: self.vpns, by: { $0.group })
46+
if let groupsVpns = vpnGroups[menuItem.title] {
47+
connectTo(groupsVpns)
48+
}
49+
}
50+
}
51+
4352
func connectToVPN(vpnName: String) {
4453
print("connectToVPN \(vpnName)")
4554

@@ -58,6 +67,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
5867
}
5968

6069
@objc func connectAll(_ sender: Any?) {
70+
connectTo(vpns)
71+
}
72+
73+
func connectTo(_ vpns: [VPN]) {
6174
for vpn in vpns {
6275
if vpn.enabled {
6376
connectToVPN(vpnName: vpn.name)
@@ -70,10 +83,16 @@ class AppDelegate: NSObject, NSApplicationDelegate {
7083
}
7184

7285
func loadVPNConfig() {
73-
if let filePath = UserDefaults.standard.string(forKey: "ConfigFilePath"), let vpns = VPNConfigParser.load(filePath: filePath) {
74-
self.vpns = vpns
75-
for vpn in vpns {
76-
vpn.enabled = UserDefaults.standard.bool(forKey: vpn.name)
86+
if let filePath = UserDefaults.standard.string(forKey: "ConfigFilePath"), let vpnConfigs = VPNConfigParser.load(filePath: filePath) {
87+
self.vpns = []
88+
for vpnConfig in vpnConfigs {
89+
var vpn: VPN
90+
if let encodedData = UserDefaults.standard.data(forKey: vpnConfig.name) {
91+
vpn = try! JSONDecoder().decode(VPN.self, from: encodedData)
92+
} else {
93+
vpn = vpnConfig
94+
}
95+
self.vpns.append(vpn)
7796
}
7897
}
7998

@@ -83,18 +102,34 @@ class AppDelegate: NSObject, NSApplicationDelegate {
83102
func constructMenu() {
84103
let menu = NSMenu()
85104

105+
let vpnGroups = Dictionary(grouping: self.vpns, by: { $0.group })
106+
86107
if vpns.count > 0 {
87108
var count = 0
88-
for vpn in vpns {
89-
if vpn.enabled {
90-
count += 1
91-
menu.addItem(
92-
NSMenuItem(
93-
title: vpn.name,
94-
action: #selector(AppDelegate.selectVPN(_:)),
95-
keyEquivalent: "\(count)"
96-
)
109+
for (groupName, groupVpns) in vpnGroups {
110+
if groupVpns.filter({ $0.enabled }).count > 0 {
111+
let name = groupName ?? "Unknown"
112+
let vpnGroupMenuItem = NSMenuItem(
113+
title: name,
114+
action: #selector(AppDelegate.selectVPNGroup(_:)),
115+
keyEquivalent: "\(name.prefix(1))"
97116
)
117+
// vpnGroupMenuItem.submenu = NSMenu()
118+
menu.addItem(vpnGroupMenuItem)
119+
120+
for vpn in groupVpns {
121+
if vpn.enabled {
122+
count += 1
123+
let menuItem = NSMenuItem(
124+
title: " \(vpn.name)",
125+
action: #selector(AppDelegate.selectVPN(_:)),
126+
keyEquivalent: "\(count)"
127+
)
128+
// vpnGroupMenuItem.submenu?.addItem(menuItem)
129+
menu.addItem(menuItem)
130+
}
131+
}
132+
menu.addItem(NSMenuItem.separator())
98133
}
99134
}
100135
} else {

GoVPN/Info.plist

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
<key>CFBundlePackageType</key>
1818
<string>APPL</string>
1919
<key>CFBundleShortVersionString</key>
20-
<string>1.0</string>
20+
<string>1.1</string>
2121
<key>CFBundleVersion</key>
22-
<string>7</string>
22+
<string>8</string>
2323
<key>LSApplicationCategoryType</key>
2424
<string>public.app-category.productivity</string>
2525
<key>LSMinimumSystemVersion</key>

GoVPN/Models/VPN.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
import Foundation
1111

1212
@objc
13-
class VPN: NSObject {
13+
class VPN: NSObject, Codable {
1414
@objc var name: String
1515
@objc var enabled: Bool
16+
@objc var group: String?
1617

17-
init(name: String, enabled: Bool) {
18+
init(name: String, enabled: Bool, group: String? = nil) {
1819
self.name = name
1920
self.enabled = enabled
21+
self.group = group
2022
}
2123
}

GoVPN/Preferences.storyboard

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="379-GU-2Up">
2+
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="379-GU-2Up">
33
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14460.31"/>
4+
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14490.70"/>
55
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
66
</dependencies>
77
<scenes>
@@ -53,7 +53,7 @@
5353
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
5454
<subviews>
5555
<tableView verticalHuggingPriority="750" allowsExpansionToolTips="YES" columnAutoresizingStyle="lastColumnOnly" columnReordering="NO" columnSelection="YES" columnResizing="NO" multipleSelection="NO" autosaveColumns="NO" headerView="owK-tf-Q9r" id="Z9h-HB-iB8">
56-
<rect key="frame" x="0.0" y="0.0" width="438" height="134"/>
56+
<rect key="frame" x="0.0" y="0.0" width="505" height="134"/>
5757
<autoresizingMask key="autoresizingMask"/>
5858
<size key="intercellSpacing" width="3" height="2"/>
5959
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
@@ -75,7 +75,7 @@
7575
<binding destination="2G4-PT-4Ci" name="value" keyPath="arrangedObjects.name" id="YKI-n7-uJG"/>
7676
</connections>
7777
</tableColumn>
78-
<tableColumn width="305" minWidth="10" maxWidth="3.4028234663852886e+38" id="Xm2-tx-54J">
78+
<tableColumn width="103" minWidth="10" maxWidth="3.4028234663852886e+38" id="Xm2-tx-54J">
7979
<tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border" alignment="left" title="Enabled">
8080
<font key="font" metaFont="smallSystem"/>
8181
<color key="textColor" name="headerTextColor" catalog="System" colorSpace="catalog"/>
@@ -90,6 +90,22 @@
9090
<binding destination="2G4-PT-4Ci" name="value" keyPath="arrangedObjects.enabled" id="M7s-8i-k3E"/>
9191
</connections>
9292
</tableColumn>
93+
<tableColumn width="266" minWidth="10" maxWidth="3.4028234663852886e+38" id="c6m-2P-GHP">
94+
<tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border" alignment="left" title="Group">
95+
<font key="font" metaFont="smallSystem"/>
96+
<color key="textColor" name="headerTextColor" catalog="System" colorSpace="catalog"/>
97+
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
98+
</tableHeaderCell>
99+
<textFieldCell key="dataCell" lineBreakMode="truncatingTail" selectable="YES" editable="YES" alignment="left" title="Text Cell" id="ylC-9x-8QM">
100+
<font key="font" metaFont="system"/>
101+
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
102+
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
103+
</textFieldCell>
104+
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
105+
<connections>
106+
<binding destination="2G4-PT-4Ci" name="value" keyPath="arrangedObjects.group" id="GVN-eB-UGZ"/>
107+
</connections>
108+
</tableColumn>
93109
</tableColumns>
94110
<connections>
95111
<binding destination="2G4-PT-4Ci" name="content" keyPath="arrangedObjects" id="McP-pk-lDd"/>
@@ -106,7 +122,7 @@
106122
<autoresizingMask key="autoresizingMask"/>
107123
</scroller>
108124
<tableHeaderView key="headerView" id="owK-tf-Q9r">
109-
<rect key="frame" x="0.0" y="0.0" width="438" height="25"/>
125+
<rect key="frame" x="0.0" y="0.0" width="505" height="25"/>
110126
<autoresizingMask key="autoresizingMask"/>
111127
</tableHeaderView>
112128
</scrollView>

GoVPN/PreferencesViewController.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ class PreferencesViewController: NSViewController {
5757

5858
@IBAction func save(_ sender: Any) {
5959
for vpn in availableVPNsAC!.arrangedObjects as! [VPN] {
60-
UserDefaults.standard.set(vpn.enabled, forKey: vpn.name)
60+
let encodedData = try! JSONEncoder().encode(vpn)
61+
UserDefaults.standard.set(encodedData, forKey: vpn.name)
6162
}
6263
reloadVPNConfig()
6364
self.view.window?.windowController?.close()

0 commit comments

Comments
 (0)