JoinNetworkViewController.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // JoinNetworkViewController.swift
  3. // ZeroTier One
  4. //
  5. // Created by Grant Limberg on 5/14/16.
  6. // Copyright © 2016 ZeroTier, Inc. All rights reserved.
  7. //
  8. import Cocoa
  9. extension String {
  10. func contains(find: String) -> Bool {
  11. return self.rangeOfString(find) != nil
  12. }
  13. func trunc(length: Int, trailing: String? = "...") -> String {
  14. if self.characters.count > length {
  15. return self.substringToIndex(self.startIndex.advancedBy(length)) + (trailing ?? "")
  16. } else {
  17. return self
  18. }
  19. }
  20. }
  21. let joinedNetworksKey = "com.zerotier.one.joined-networks"
  22. class JoinNetworkViewController: NSViewController, NSComboBoxDelegate, NSComboBoxDataSource {
  23. @IBOutlet var network: NSComboBox!
  24. @IBOutlet var joinButton: NSButton!
  25. @IBOutlet var allowManagedCheckBox: NSButton!
  26. @IBOutlet var allowGlobalCheckBox: NSButton!
  27. @IBOutlet var allowDefaultCheckBox:NSButton!
  28. var values: [String] = [String]()
  29. override func viewDidLoad() {
  30. super.viewDidLoad()
  31. network.setDelegate(self)
  32. network.dataSource = self
  33. }
  34. override func viewWillAppear() {
  35. super.viewWillAppear()
  36. allowManagedCheckBox.state = NSOnState
  37. allowGlobalCheckBox.state = NSOffState
  38. allowDefaultCheckBox.state = NSOffState
  39. let defaults = NSUserDefaults.standardUserDefaults()
  40. let vals = defaults.stringArrayForKey(joinedNetworksKey)
  41. if let v = vals {
  42. values = v
  43. }
  44. }
  45. override func viewDidDisappear() {
  46. super.viewWillDisappear()
  47. let defaults = NSUserDefaults.standardUserDefaults()
  48. defaults.setObject(values, forKey: joinedNetworksKey)
  49. }
  50. @IBAction func onJoinClicked(sender: AnyObject?) {
  51. let networkString = network.stringValue
  52. ServiceCom.sharedInstance().joinNetwork(networkString,
  53. allowManaged: allowManagedCheckBox.state == NSOnState,
  54. allowGlobal: allowGlobalCheckBox.state == NSOnState,
  55. allowDefault: allowDefaultCheckBox.state == NSOnState)
  56. network.stringValue = ""
  57. if !values.contains(networkString) {
  58. values.insert(networkString, atIndex: 0)
  59. while values.count > 20 {
  60. values.removeLast()
  61. }
  62. }
  63. }
  64. // NSComboBoxDelegate Methods
  65. override func controlTextDidChange(obj: NSNotification) {
  66. let cb = obj.object as! NSComboBox
  67. let value = cb.stringValue
  68. let allowedCharacters = "abcdefABCDEF0123456789"
  69. var outValue = ""
  70. for char in value.characters {
  71. if !allowedCharacters.contains(String(char)) {
  72. NSBeep()
  73. }
  74. else {
  75. outValue += String(char)
  76. }
  77. }
  78. if outValue.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) == 16 {
  79. joinButton.enabled = true
  80. }
  81. else {
  82. if outValue.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) > 16 {
  83. outValue = outValue.trunc(16, trailing: "")
  84. NSBeep()
  85. joinButton.enabled = true
  86. }
  87. else {
  88. joinButton.enabled = false
  89. }
  90. }
  91. cb.stringValue = outValue
  92. }
  93. // end NSComboBoxDelegate Methods
  94. // NSComboBoxDataSource methods
  95. func numberOfItemsInComboBox(aComboBox: NSComboBox) -> Int {
  96. return values.count
  97. }
  98. func comboBox(aComboBox: NSComboBox, objectValueForItemAtIndex index: Int) -> AnyObject {
  99. return values[index]
  100. }
  101. func comboBox(aComboBox: NSComboBox, indexOfItemWithStringValue string: String) -> Int {
  102. var counter = 0
  103. for val in values {
  104. if val == string {
  105. return counter
  106. }
  107. counter += 1
  108. }
  109. return NSNotFound
  110. }
  111. func comboBox(aComboBox: NSComboBox, completedString string: String) -> String? {
  112. for val in values {
  113. if val.hasPrefix(string) {
  114. return val
  115. }
  116. }
  117. return nil
  118. }
  119. // end NSComboBoxDataSorce methods
  120. }