macOS: Ability to import tunnels from file

For now, the open panel shows as a separate window.
Later, we'll open it as a sheet on the 'Manage tunnels' window.

Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
Roopesh Chander 2018-12-29 15:33:41 +05:30
parent 6a27626fc0
commit 545f8c88f4
4 changed files with 82 additions and 9 deletions

View File

@ -111,6 +111,7 @@
6FBA103E21D6B6D70051C35F /* TunnelImporter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FBA103D21D6B6D70051C35F /* TunnelImporter.swift */; };
6FBA103F21D6B6FF0051C35F /* TunnelImporter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FBA103D21D6B6D70051C35F /* TunnelImporter.swift */; };
6FBA104021D6B7040051C35F /* ErrorPresenterProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FBA103A21D6B4280051C35F /* ErrorPresenterProtocol.swift */; };
6FBA104321D6BC250051C35F /* ErrorPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FBA104121D6BC210051C35F /* ErrorPresenter.swift */; };
6FDEF7E421846C1A00D8FBF6 /* libwg-go.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 6FDEF7E321846C1A00D8FBF6 /* libwg-go.a */; };
6FDEF7E62185EFB200D8FBF6 /* QRScanViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FDEF7E52185EFAF00D8FBF6 /* QRScanViewController.swift */; };
6FDEF7FB21863B6100D8FBF6 /* unzip.c in Sources */ = {isa = PBXBuildFile; fileRef = 6FDEF7F621863B6100D8FBF6 /* unzip.c */; };
@ -256,6 +257,7 @@
6FBA101621D655340051C35F /* StatusMenu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusMenu.swift; sourceTree = "<group>"; };
6FBA103A21D6B4280051C35F /* ErrorPresenterProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ErrorPresenterProtocol.swift; sourceTree = "<group>"; };
6FBA103D21D6B6D70051C35F /* TunnelImporter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TunnelImporter.swift; sourceTree = "<group>"; };
6FBA104121D6BC210051C35F /* ErrorPresenter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ErrorPresenter.swift; sourceTree = "<group>"; };
6FDEF7E321846C1A00D8FBF6 /* libwg-go.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; path = "libwg-go.a"; sourceTree = BUILT_PRODUCTS_DIR; };
6FDEF7E52185EFAF00D8FBF6 /* QRScanViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QRScanViewController.swift; sourceTree = "<group>"; };
6FDEF7F621863B6100D8FBF6 /* unzip.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = unzip.c; sourceTree = "<group>"; };
@ -462,12 +464,13 @@
6FB1BD5E21D2607A00A991BF /* macOS */ = {
isa = PBXGroup;
children = (
6FBA101321D613F30051C35F /* Application.swift */,
6FB1BD5F21D2607A00A991BF /* AppDelegate.swift */,
6FBA101621D655340051C35F /* StatusMenu.swift */,
6FBA104121D6BC210051C35F /* ErrorPresenter.swift */,
6FB1BD6121D2607E00A991BF /* Assets.xcassets */,
6FB1BD6621D2607E00A991BF /* Info.plist */,
6FB1BD6721D2607E00A991BF /* WireGuard.entitlements */,
6FBA101321D613F30051C35F /* Application.swift */,
6FBA101621D655340051C35F /* StatusMenu.swift */,
);
path = macOS;
sourceTree = "<group>";
@ -1040,6 +1043,7 @@
6FBA104021D6B7040051C35F /* ErrorPresenterProtocol.swift in Sources */,
6FB1BDC321D50F0300A991BF /* TunnelConfiguration.swift in Sources */,
6FB1BDC421D50F0300A991BF /* IPAddressRange.swift in Sources */,
6FBA104321D6BC250051C35F /* ErrorPresenter.swift in Sources */,
6FB1BDC521D50F0300A991BF /* Endpoint.swift in Sources */,
6FB1BDC621D50F0300A991BF /* DNSServer.swift in Sources */,
6FB1BDC721D50F0300A991BF /* InterfaceConfiguration.swift in Sources */,

View File

@ -16,6 +16,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
let tunnelsManager: TunnelsManager = result.value!
let statusMenu = StatusMenu(tunnelsManager: tunnelsManager)
self.statusItem = createStatusBarItem(with: statusMenu)
tunnelsManager.tunnelsListDelegate = statusMenu
}
}
}

View File

@ -0,0 +1,15 @@
// SPDX-License-Identifier: MIT
// Copyright © 2018 WireGuard LLC. All Rights Reserved.
import Cocoa
class ErrorPresenter: ErrorPresenterProtocol {
static func showErrorAlert(title: String, message: String, from sourceVC: AnyObject?, onPresented: (() -> Void)?, onDismissal: (() -> Void)?) {
let alert = NSAlert()
alert.messageText = title
alert.informativeText = message
onPresented?()
alert.runModal()
onDismissal?()
}
}

View File

@ -6,12 +6,16 @@ import Cocoa
class StatusMenu: NSMenu {
let tunnelsManager: TunnelsManager
var firstTunnelMenuItemIndex: Int = 0
init(tunnelsManager: TunnelsManager) {
self.tunnelsManager = tunnelsManager
super.init(title: "WireGuard Status Bar Menu")
addTunnelMenuItems()
addItem(NSMenuItem.separator())
firstTunnelMenuItemIndex = numberOfItems
let isAdded = addTunnelMenuItems()
if isAdded {
addItem(NSMenuItem.separator())
}
addTunnelManagementItems()
}
@ -19,14 +23,21 @@ class StatusMenu: NSMenu {
fatalError("init(coder:) has not been implemented")
}
func addTunnelMenuItems() {
func addTunnelMenuItems() -> Bool {
let numberOfTunnels = tunnelsManager.numberOfTunnels()
for index in 0 ..< tunnelsManager.numberOfTunnels() {
let tunnel = tunnelsManager.tunnel(at: index)
let menuItem = NSMenuItem(title: tunnel.name, action: #selector(tunnelClicked(sender:)), keyEquivalent: "")
menuItem.target = self
menuItem.representedObject = tunnel
let menuItem = createTunnelMenuItem(for: tunnel)
addItem(menuItem)
}
return numberOfTunnels > 0
}
func createTunnelMenuItem(for tunnel: TunnelContainer) -> NSMenuItem {
let menuItem = NSMenuItem(title: tunnel.name, action: #selector(tunnelClicked(sender:)), keyEquivalent: "")
menuItem.target = self
menuItem.representedObject = tunnel
return menuItem
}
@objc func tunnelClicked(sender: AnyObject) {
@ -48,6 +59,47 @@ class StatusMenu: NSMenu {
}
@objc func importTunnelsClicked() {
print("Unimplemented")
let openPanel = NSOpenPanel()
openPanel.allowedFileTypes = ["conf", "zip"]
openPanel.begin { [weak tunnelsManager] response in
guard let tunnelsManager = tunnelsManager else { return }
guard response == .OK else { return }
guard let url = openPanel.url else { return }
TunnelImporter.importFromFile(url: url, into: tunnelsManager, sourceVC: nil, errorPresenterType: ErrorPresenter.self)
}
}
}
extension StatusMenu: TunnelsManagerListDelegate {
func tunnelAdded(at index: Int) {
let tunnel = tunnelsManager.tunnel(at: index)
let menuItem = createTunnelMenuItem(for: tunnel)
if tunnelsManager.numberOfTunnels() == 1 {
insertItem(NSMenuItem.separator(), at: firstTunnelMenuItemIndex + index)
}
insertItem(menuItem, at: firstTunnelMenuItemIndex + index)
}
func tunnelModified(at index: Int) {
let tunnel = tunnelsManager.tunnel(at: index)
if let menuItem = item(at: firstTunnelMenuItemIndex + index) {
menuItem.title = tunnel.name
}
}
func tunnelMoved(from oldIndex: Int, to newIndex: Int) {
let tunnel = tunnelsManager.tunnel(at: oldIndex)
let menuItem = createTunnelMenuItem(for: tunnel)
removeItem(at: firstTunnelMenuItemIndex + oldIndex)
insertItem(menuItem, at: firstTunnelMenuItemIndex + newIndex)
}
func tunnelRemoved(at index: Int) {
removeItem(at: firstTunnelMenuItemIndex + index)
if tunnelsManager.numberOfTunnels() == 0 {
if let firstItem = item(at: firstTunnelMenuItemIndex), firstItem.isSeparatorItem {
removeItem(at: firstTunnelMenuItemIndex)
}
}
}
}