1/* 2 * ZeroTier One - Network Virtualization Everywhere 3 * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/ 4 * 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19#import "JoinNetworkViewController.h" 20#import "ServiceCom.h" 21#import "AppDelegate.h" 22 23 24NSString * const JoinedNetworksKey = @"com.zerotier.one.joined-networks"; 25 26@interface NSString (extra) 27 28- (BOOL)contains:(NSString*)find; 29 30@end 31 32@implementation NSString (extra) 33 34- (BOOL)contains:(NSString*)find { 35 NSRange range = [self rangeOfString:find]; 36 return range.location != NSNotFound; 37} 38 39@end 40 41 42@implementation JoinNetworkViewController 43 44- (void)viewDidLoad { 45 [super viewDidLoad]; 46 // Do view setup here. 47 [self.network setDelegate:self]; 48 [self.network setDataSource:self]; 49} 50 51- (void)viewWillAppear { 52 [super viewWillAppear]; 53 54 self.allowManagedCheckBox.state = NSOnState; 55 self.allowGlobalCheckBox.state = NSOffState; 56 self.allowDefaultCheckBox.state = NSOffState; 57 self.allowDNSCheckBox.state = NSOffState; 58 59 self.network.stringValue = @""; 60 61 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 62 63 NSMutableArray<NSString*> *vals = [[defaults stringArrayForKey:JoinedNetworksKey] mutableCopy]; 64 65 if(vals) { 66 self.values = vals; 67 } 68} 69 70- (void)viewWillDisappear { 71 [super viewWillDisappear]; 72 73 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 74 75 [defaults setObject:self.values forKey:JoinedNetworksKey]; 76} 77 78- (IBAction)onJoinClicked:(id)sender { 79 NSString *networkId = self.network.stringValue; 80 81 NSError *error = nil; 82 [[ServiceCom sharedInstance] joinNetwork:networkId 83 allowManaged:(self.allowManagedCheckBox.state == NSOnState) 84 allowGlobal:(self.allowGlobalCheckBox.state == NSOnState) 85 allowDefault:(self.allowDefaultCheckBox.state == NSOnState) 86 allowDNS:(self.allowDNSCheckBox.state == NSOnState) 87 error:&error]; 88 89 if(error) { 90 NSAlert *alert = [NSAlert alertWithError:error]; 91 alert.alertStyle = NSCriticalAlertStyle; 92 [alert addButtonWithTitle:@"Ok"]; 93 94 [alert runModal]; 95 return; 96 } 97 98 self.network.stringValue = @""; 99 100 if(![self.values containsObject:networkId]) { 101 [self.values insertObject:networkId atIndex:0]; 102 103 while([self.values count] > 20) { 104 [self.values removeLastObject]; 105 } 106 } 107 108 [self.appDelegate closeJoinNetworkPopover]; 109} 110 111// NSComboBoxDelegate methods 112 113- (void)controlTextDidChange:(NSNotification *)obj { 114 NSComboBox *cb = (NSComboBox*)obj.object; 115 NSString *value = cb.stringValue; 116 117 NSString *allowedCharacters = @"abcdefABCDEF0123456789"; 118 119 NSString *outValue = @""; 120 121 for(int i = 0; i < [value length]; ++i) { 122 if(![allowedCharacters contains:[NSString stringWithFormat:@"%C", [value characterAtIndex:i]]]) { 123 NSBeep(); 124 } 125 else { 126 outValue = [outValue stringByAppendingString:[NSString stringWithFormat:@"%C", [value characterAtIndex:i]]]; 127 } 128 } 129 130 if([outValue lengthOfBytesUsingEncoding:NSUTF8StringEncoding] == 16) { 131 self.joinButton.enabled = YES; 132 } 133 else { 134 if([outValue lengthOfBytesUsingEncoding:NSUTF8StringEncoding] > 16) { 135 NSRange range = {0, 16}; 136 range = [outValue rangeOfComposedCharacterSequencesForRange:range]; 137 outValue = [outValue substringWithRange:range]; 138 NSBeep(); 139 self.joinButton.enabled = YES; 140 } 141 else { 142 self.joinButton.enabled = NO; 143 } 144 } 145 146 cb.stringValue = outValue; 147} 148 149// end NSComboBoxDelegate methods 150 151// NSComboBoxDataSource methods 152 153- (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox { 154 return [self.values count]; 155} 156 157- (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index { 158 return [self.values objectAtIndex:index]; 159} 160 161- (NSUInteger)comboBox:(NSComboBox *)aComboBox indexOfItemWithStringValue:(NSString *)string { 162 NSUInteger counter = 0; 163 164 for(NSString *val in self.values) { 165 if([val isEqualToString:string]) { 166 return counter; 167 } 168 169 counter += 1; 170 } 171 172 return NSNotFound; 173} 174 175- (NSString*)comboBox:(NSComboBox *)aComboBox completedString:(NSString *)string { 176 for(NSString *val in self.values) { 177 if([val hasPrefix:string]) { 178 return val; 179 } 180 } 181 return nil; 182} 183 184// end NSComboBoxDataSource methods 185 186@end 187