summaryrefslogtreecommitdiff
path: root/ZeroTier One/JoinNetworkViewController.swift
blob: 446e8719970e51408e78a50f4318bc3c391393d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//
//  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
}