2016-05-14 22:42:45 +00:00
|
|
|
//
|
|
|
|
// JoinNetworkViewController.swift
|
|
|
|
// ZeroTier One
|
|
|
|
//
|
|
|
|
// Created by Grant Limberg on 5/14/16.
|
|
|
|
// Copyright © 2016 ZeroTier, Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
|
2016-05-17 00:51:14 +00:00
|
|
|
extension String {
|
|
|
|
func contains(find: String) -> Bool {
|
|
|
|
return self.rangeOfString(find) != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func trunc(length: Int, trailing: String? = "...") -> String {
|
|
|
|
if self.characters.count > length {
|
|
|
|
return self.substringToIndex(self.startIndex.advancedBy(length)) + (trailing ?? "")
|
|
|
|
} else {
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-17 01:26:33 +00:00
|
|
|
let joinedNetworksKey = "com.zerotier.one.joined-networks"
|
|
|
|
|
|
|
|
|
|
|
|
class JoinNetworkViewController: NSViewController, NSComboBoxDelegate, NSComboBoxDataSource {
|
2016-05-17 00:51:14 +00:00
|
|
|
|
|
|
|
@IBOutlet var network: NSComboBox!
|
|
|
|
@IBOutlet var joinButton: NSButton!
|
2016-05-14 22:42:45 +00:00
|
|
|
|
2016-06-26 23:11:30 +00:00
|
|
|
@IBOutlet var allowManagedCheckBox: NSButton!
|
|
|
|
@IBOutlet var allowGlobalCheckBox: NSButton!
|
|
|
|
@IBOutlet var allowDefaultCheckBox:NSButton!
|
|
|
|
|
2016-05-17 01:26:33 +00:00
|
|
|
var values: [String] = [String]()
|
|
|
|
|
2016-05-14 22:42:45 +00:00
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
2016-05-17 00:51:14 +00:00
|
|
|
network.setDelegate(self)
|
2016-05-17 01:26:33 +00:00
|
|
|
network.dataSource = self
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewWillAppear() {
|
2016-05-17 02:25:20 +00:00
|
|
|
super.viewWillAppear()
|
|
|
|
|
2016-06-27 01:18:59 +00:00
|
|
|
allowManagedCheckBox.state = NSOnState
|
|
|
|
allowGlobalCheckBox.state = NSOffState
|
|
|
|
allowDefaultCheckBox.state = NSOffState
|
|
|
|
|
2016-05-17 01:26:33 +00:00
|
|
|
let defaults = NSUserDefaults.standardUserDefaults()
|
|
|
|
|
|
|
|
let vals = defaults.stringArrayForKey(joinedNetworksKey)
|
|
|
|
|
|
|
|
if let v = vals {
|
|
|
|
values = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidDisappear() {
|
2016-05-17 02:25:20 +00:00
|
|
|
super.viewWillDisappear()
|
|
|
|
|
2016-05-17 01:26:33 +00:00
|
|
|
let defaults = NSUserDefaults.standardUserDefaults()
|
|
|
|
|
|
|
|
defaults.setObject(values, forKey: joinedNetworksKey)
|
2016-05-17 00:51:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func onJoinClicked(sender: AnyObject?) {
|
2016-05-17 01:26:33 +00:00
|
|
|
let networkString = network.stringValue
|
2016-05-17 00:51:14 +00:00
|
|
|
|
2016-06-26 23:11:30 +00:00
|
|
|
ServiceCom.joinNetwork(networkString,
|
|
|
|
allowManaged: allowManagedCheckBox.state == NSOnState,
|
|
|
|
allowGlobal: allowGlobalCheckBox.state == NSOnState,
|
|
|
|
allowDefault: allowDefaultCheckBox.state == NSOnState)
|
2016-05-17 00:51:14 +00:00
|
|
|
network.stringValue = ""
|
2016-05-17 01:26:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
if !values.contains(networkString) {
|
|
|
|
values.insert(networkString, atIndex: 0)
|
|
|
|
|
|
|
|
while values.count > 20 {
|
|
|
|
values.removeLast()
|
|
|
|
}
|
|
|
|
}
|
2016-05-17 00:51:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NSComboBoxDelegate Methods
|
|
|
|
|
|
|
|
override func controlTextDidChange(obj: NSNotification) {
|
|
|
|
let cb = obj.object as! NSComboBox
|
|
|
|
let value = cb.stringValue
|
|
|
|
|
|
|
|
|
|
|
|
let allowedCharacters = "abcdefABCDEF0123456789"
|
|
|
|
|
|
|
|
var outValue = ""
|
|
|
|
|
|
|
|
for char in value.characters {
|
|
|
|
if !allowedCharacters.contains(String(char)) {
|
|
|
|
NSBeep()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
outValue += String(char)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if outValue.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) == 16 {
|
|
|
|
joinButton.enabled = true
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
if outValue.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) > 16 {
|
|
|
|
outValue = outValue.trunc(16, trailing: "")
|
|
|
|
NSBeep()
|
|
|
|
joinButton.enabled = true
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
joinButton.enabled = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cb.stringValue = outValue
|
2016-05-14 22:42:45 +00:00
|
|
|
}
|
2016-05-17 00:51:14 +00:00
|
|
|
// end NSComboBoxDelegate Methods
|
2016-05-17 01:26:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
// NSComboBoxDataSource methods
|
|
|
|
|
|
|
|
func numberOfItemsInComboBox(aComboBox: NSComboBox) -> Int {
|
|
|
|
return values.count
|
|
|
|
}
|
|
|
|
|
|
|
|
func comboBox(aComboBox: NSComboBox, objectValueForItemAtIndex index: Int) -> AnyObject {
|
|
|
|
return values[index]
|
|
|
|
}
|
|
|
|
|
|
|
|
func comboBox(aComboBox: NSComboBox, indexOfItemWithStringValue string: String) -> Int {
|
|
|
|
|
|
|
|
var counter = 0
|
|
|
|
for val in values {
|
|
|
|
if val == string {
|
|
|
|
return counter
|
|
|
|
}
|
|
|
|
counter += 1
|
|
|
|
}
|
|
|
|
return NSNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
func comboBox(aComboBox: NSComboBox, completedString string: String) -> String? {
|
|
|
|
for val in values {
|
|
|
|
if val.hasPrefix(string) {
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// end NSComboBoxDataSorce methods
|
2016-05-14 22:42:45 +00:00
|
|
|
}
|