123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- //
- // JoinNetworkViewController.swift
- // ZeroTier One
- //
- // Created by Grant Limberg on 5/14/16.
- // Copyright © 2016 ZeroTier, Inc. All rights reserved.
- //
- import Cocoa
- 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
- }
- }
- }
- let joinedNetworksKey = "com.zerotier.one.joined-networks"
- class JoinNetworkViewController: NSViewController, NSComboBoxDelegate, NSComboBoxDataSource {
- @IBOutlet var network: NSComboBox!
- @IBOutlet var joinButton: NSButton!
- @IBOutlet var allowManagedCheckBox: NSButton!
- @IBOutlet var allowGlobalCheckBox: NSButton!
- @IBOutlet var allowDefaultCheckBox:NSButton!
- var values: [String] = [String]()
- override func viewDidLoad() {
- super.viewDidLoad()
- network.setDelegate(self)
- network.dataSource = self
- }
- override func viewWillAppear() {
- super.viewWillAppear()
- allowManagedCheckBox.state = NSOnState
- allowGlobalCheckBox.state = NSOffState
- allowDefaultCheckBox.state = NSOffState
- let defaults = NSUserDefaults.standardUserDefaults()
- let vals = defaults.stringArrayForKey(joinedNetworksKey)
- if let v = vals {
- values = v
- }
- }
- override func viewDidDisappear() {
- super.viewWillDisappear()
-
- let defaults = NSUserDefaults.standardUserDefaults()
- defaults.setObject(values, forKey: joinedNetworksKey)
- }
- @IBAction func onJoinClicked(sender: AnyObject?) {
- let networkString = network.stringValue
- ServiceCom.sharedInstance().joinNetwork(networkString,
- allowManaged: allowManagedCheckBox.state == NSOnState,
- allowGlobal: allowGlobalCheckBox.state == NSOnState,
- allowDefault: allowDefaultCheckBox.state == NSOnState)
- network.stringValue = ""
- if !values.contains(networkString) {
- values.insert(networkString, atIndex: 0)
- while values.count > 20 {
- values.removeLast()
- }
- }
- }
- // 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
- }
- // end NSComboBoxDelegate Methods
- // 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
- }
|