2016-05-14 22:42:45 +00:00
|
|
|
//
|
|
|
|
// ShowNetworksViewController.swift
|
|
|
|
// ZeroTier One
|
|
|
|
//
|
|
|
|
// Created by Grant Limberg on 5/14/16.
|
|
|
|
// Copyright © 2016 ZeroTier, Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
|
2016-05-18 02:41:54 +00:00
|
|
|
class ShowNetworksViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource {
|
2016-05-14 22:42:45 +00:00
|
|
|
|
2016-06-17 03:53:55 +00:00
|
|
|
@IBOutlet var tableView: NSTableView?
|
2016-05-17 02:08:00 +00:00
|
|
|
|
2016-05-18 02:41:54 +00:00
|
|
|
var networkList: [Network] = [Network]()
|
2016-06-17 03:53:55 +00:00
|
|
|
var netMonitor: NetworkMonitor!
|
2016-06-15 02:59:45 +00:00
|
|
|
|
2016-07-27 03:05:33 +00:00
|
|
|
var visible = false
|
|
|
|
|
2016-05-14 22:42:45 +00:00
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
// Do view setup here.
|
2016-05-18 02:41:54 +00:00
|
|
|
|
2016-06-17 03:53:55 +00:00
|
|
|
tableView?.setDelegate(self)
|
|
|
|
tableView?.setDataSource(self)
|
|
|
|
tableView?.backgroundColor = NSColor.clearColor()
|
2016-05-14 22:42:45 +00:00
|
|
|
}
|
2016-05-17 02:58:24 +00:00
|
|
|
|
2016-06-14 02:46:54 +00:00
|
|
|
private func dataFile() -> String {
|
|
|
|
var appSupport = NSFileManager.defaultManager().URLsForDirectory(.ApplicationSupportDirectory, inDomains: .UserDomainMask)[0]
|
|
|
|
appSupport = appSupport.URLByAppendingPathComponent("ZeroTier").URLByAppendingPathComponent("One").URLByAppendingPathComponent("networks.dat")
|
|
|
|
return appSupport.path!
|
|
|
|
}
|
|
|
|
|
2016-05-17 02:58:24 +00:00
|
|
|
override func viewWillAppear() {
|
|
|
|
super.viewWillAppear()
|
2016-07-27 03:05:33 +00:00
|
|
|
visible = true
|
2016-06-15 02:59:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func viewWillDisappear() {
|
|
|
|
super.viewWillDisappear()
|
|
|
|
|
2016-06-14 02:46:54 +00:00
|
|
|
let filePath = dataFile()
|
2016-06-15 02:59:45 +00:00
|
|
|
NSKeyedArchiver.archiveRootObject(self.networkList, toFile: filePath)
|
2016-07-27 03:05:33 +00:00
|
|
|
visible = false
|
2016-06-15 02:59:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func deleteNetworkFromList(nwid: String) {
|
2016-06-28 00:54:54 +00:00
|
|
|
netMonitor.deleteSavedNetwork(nwid)
|
2016-06-15 02:59:45 +00:00
|
|
|
}
|
|
|
|
|
2016-06-17 03:53:55 +00:00
|
|
|
|
|
|
|
func setNetworks(list: [Network]) {
|
|
|
|
networkList = list
|
2016-07-27 03:05:33 +00:00
|
|
|
if visible {
|
|
|
|
tableView?.reloadData()
|
|
|
|
}
|
|
|
|
|
|
|
|
NSLog("Visible? \(visible)")
|
2016-05-18 02:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NSTableViewDataSource
|
|
|
|
|
|
|
|
func numberOfRowsInTableView(tableView: NSTableView) -> Int {
|
|
|
|
return networkList.count
|
2016-05-17 02:58:24 +00:00
|
|
|
}
|
2016-05-18 02:41:54 +00:00
|
|
|
|
|
|
|
// end NSTableViewDataSource
|
2016-05-27 20:28:32 +00:00
|
|
|
|
|
|
|
// NSTableViewDelegate
|
|
|
|
|
|
|
|
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {
|
|
|
|
if let cell = tableView.makeViewWithIdentifier("NetworkInfoCell", owner: nil) as? NetworkInfoCell {
|
|
|
|
let network = networkList[row]
|
2016-06-10 02:59:05 +00:00
|
|
|
cell.parent = self
|
2016-05-27 20:28:32 +00:00
|
|
|
cell.networkIdField.stringValue = String(network.nwid, radix: 16)
|
|
|
|
cell.networkNameField.stringValue = network.name
|
|
|
|
|
2016-05-27 20:48:22 +00:00
|
|
|
cell.statusField.stringValue = network.status.description
|
|
|
|
cell.typeField.stringValue = network.type.description
|
|
|
|
cell.macField.stringValue = network.mac
|
|
|
|
cell.mtuField.stringValue = String(network.mtu)
|
|
|
|
cell.broadcastField.stringValue = network.broadcastEnabled ? "ENABLED" : "DISABLED"
|
|
|
|
cell.bridgingField.stringValue = network.bridge ? "ENABLED" : "DISABLED"
|
|
|
|
cell.deviceField.stringValue = network.portDeviceName
|
|
|
|
|
2016-06-27 01:18:59 +00:00
|
|
|
if network.connected {
|
|
|
|
cell.connectedCheckbox.state = NSOnState
|
|
|
|
|
2016-07-01 03:35:32 +00:00
|
|
|
if network.allowDefault {
|
|
|
|
cell.allowDefault.enabled = true
|
|
|
|
cell.allowDefault.state = NSOnState
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cell.allowDefault.state = NSOffState
|
|
|
|
|
|
|
|
if defaultRouteExists(networkList) {
|
|
|
|
cell.allowDefault.enabled = false
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cell.allowDefault.enabled = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-27 01:18:59 +00:00
|
|
|
cell.allowGlobal.enabled = true
|
|
|
|
cell.allowManaged.enabled = true
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cell.connectedCheckbox.state = NSOffState
|
|
|
|
|
|
|
|
cell.allowDefault.enabled = false
|
|
|
|
cell.allowGlobal.enabled = false
|
|
|
|
cell.allowManaged.enabled = false
|
|
|
|
}
|
|
|
|
|
2016-06-27 00:21:45 +00:00
|
|
|
cell.allowGlobal.state = (network.allowGlobal ? NSOnState : NSOffState)
|
|
|
|
cell.allowManaged.state = (network.allowManaged ? NSOnState : NSOffState)
|
|
|
|
|
2016-05-27 20:48:22 +00:00
|
|
|
cell.addressesField.stringValue = ""
|
|
|
|
|
|
|
|
for nw in network.assignedAddresses {
|
2016-07-01 02:42:23 +00:00
|
|
|
cell.addressesField.stringValue += "\(nw)\n"
|
2016-05-27 20:48:22 +00:00
|
|
|
}
|
|
|
|
|
2016-06-27 00:33:46 +00:00
|
|
|
|
2016-05-27 20:48:22 +00:00
|
|
|
|
2016-05-27 20:28:32 +00:00
|
|
|
return cell
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// end NSTableViewDelegate
|
2016-05-14 22:42:45 +00:00
|
|
|
}
|