ZeroTierOne/ZeroTier One/ShowNetworksViewController.swift
Grant Limberg 1756e8b0f2 Wire up checkboxes for allowGlobal, allowDefault, allowManaged to set the proper state in zerotier daemon
checkboxes are disabled if the network is not currently connected.
2016-06-26 17:33:46 -07:00

127 lines
3.9 KiB
Swift

//
// ShowNetworksViewController.swift
// ZeroTier One
//
// Created by Grant Limberg on 5/14/16.
// Copyright © 2016 ZeroTier, Inc. All rights reserved.
//
import Cocoa
class ShowNetworksViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource {
@IBOutlet var tableView: NSTableView?
var networkList: [Network] = [Network]()
var netMonitor: NetworkMonitor!
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
tableView?.setDelegate(self)
tableView?.setDataSource(self)
tableView?.backgroundColor = NSColor.clearColor()
}
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!
}
override func viewWillAppear() {
super.viewWillAppear()
}
override func viewWillDisappear() {
super.viewWillDisappear()
let filePath = dataFile()
NSKeyedArchiver.archiveRootObject(self.networkList, toFile: filePath)
}
func deleteNetworkFromList(nwid: String) {
}
func setNetworks(list: [Network]) {
networkList = list
tableView?.reloadData()
}
// NSTableViewDataSource
func numberOfRowsInTableView(tableView: NSTableView) -> Int {
return networkList.count
}
// end NSTableViewDataSource
// 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]
cell.parent = self
cell.networkIdField.stringValue = String(network.nwid, radix: 16)
cell.networkNameField.stringValue = network.name
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
if network.allowDefault {
cell.allowDefault.state = NSOnState
}
else {
cell.allowDefault.state = NSOffState
if defaultRouteExists(networkList) {
cell.allowDefault.enabled = false
}
else {
cell.allowDefault.enabled = true
}
}
cell.allowGlobal.state = (network.allowGlobal ? NSOnState : NSOffState)
cell.allowManaged.state = (network.allowManaged ? NSOnState : NSOffState)
cell.addressesField.stringValue = ""
for nw in network.assignedAddresses {
cell.addressesField.stringValue += nw
cell.addressesField.stringValue += "\n"
}
if network.connected {
cell.connectedCheckbox.state = NSOnState
cell.allowDefault.enabled = true
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
}
return cell
}
return nil
}
// end NSTableViewDelegate
}