JoinNetworkViewController.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #import "JoinNetworkViewController.h"
  19. #import "ServiceCom.h"
  20. #import "AppDelegate.h"
  21. NSString * const JoinedNetworksKey = @"com.zerotier.one.joined-networks";
  22. @interface NSString (extra)
  23. - (BOOL)contains:(NSString*)find;
  24. @end
  25. @implementation NSString (extra)
  26. - (BOOL)contains:(NSString*)find {
  27. NSRange range = [self rangeOfString:find];
  28. return range.location != NSNotFound;
  29. }
  30. @end
  31. @implementation JoinNetworkViewController
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. // Do view setup here.
  35. [self.network setDelegate:self];
  36. [self.network setDataSource:self];
  37. }
  38. - (void)viewWillAppear {
  39. [super viewWillAppear];
  40. self.allowManagedCheckBox.state = NSOnState;
  41. self.allowGlobalCheckBox.state = NSOffState;
  42. self.allowDefaultCheckBox.state = NSOffState;
  43. self.allowDNSCheckBox.state = NSOffState;
  44. self.network.stringValue = @"";
  45. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  46. NSMutableArray<NSString*> *vals = [[defaults stringArrayForKey:JoinedNetworksKey] mutableCopy];
  47. if(vals) {
  48. self.values = vals;
  49. }
  50. }
  51. - (void)viewWillDisappear {
  52. [super viewWillDisappear];
  53. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  54. [defaults setObject:self.values forKey:JoinedNetworksKey];
  55. }
  56. - (IBAction)onJoinClicked:(id)sender {
  57. NSString *networkId = self.network.stringValue;
  58. NSError *error = nil;
  59. [[ServiceCom sharedInstance] joinNetwork:networkId
  60. allowManaged:(self.allowManagedCheckBox.state == NSOnState)
  61. allowGlobal:(self.allowGlobalCheckBox.state == NSOnState)
  62. allowDefault:(self.allowDefaultCheckBox.state == NSOnState)
  63. allowDNS:(self.allowDNSCheckBox.state == NSOnState)
  64. error:&error];
  65. if(error) {
  66. NSAlert *alert = [NSAlert alertWithError:error];
  67. alert.alertStyle = NSCriticalAlertStyle;
  68. [alert addButtonWithTitle:@"Ok"];
  69. [alert runModal];
  70. return;
  71. }
  72. self.network.stringValue = @"";
  73. if(![self.values containsObject:networkId]) {
  74. [self.values insertObject:networkId atIndex:0];
  75. while([self.values count] > 20) {
  76. [self.values removeLastObject];
  77. }
  78. }
  79. [self.appDelegate closeJoinNetworkPopover];
  80. }
  81. // NSComboBoxDelegate methods
  82. - (void)controlTextDidChange:(NSNotification *)obj {
  83. NSComboBox *cb = (NSComboBox*)obj.object;
  84. NSString *value = cb.stringValue;
  85. NSString *allowedCharacters = @"abcdefABCDEF0123456789";
  86. NSString *outValue = @"";
  87. for(int i = 0; i < [value length]; ++i) {
  88. if(![allowedCharacters contains:[NSString stringWithFormat:@"%C", [value characterAtIndex:i]]]) {
  89. NSBeep();
  90. }
  91. else {
  92. outValue = [outValue stringByAppendingString:[NSString stringWithFormat:@"%C", [value characterAtIndex:i]]];
  93. }
  94. }
  95. if([outValue lengthOfBytesUsingEncoding:NSUTF8StringEncoding] == 16) {
  96. self.joinButton.enabled = YES;
  97. }
  98. else {
  99. if([outValue lengthOfBytesUsingEncoding:NSUTF8StringEncoding] > 16) {
  100. NSRange range = {0, 16};
  101. range = [outValue rangeOfComposedCharacterSequencesForRange:range];
  102. outValue = [outValue substringWithRange:range];
  103. NSBeep();
  104. self.joinButton.enabled = YES;
  105. }
  106. else {
  107. self.joinButton.enabled = NO;
  108. }
  109. }
  110. cb.stringValue = outValue;
  111. }
  112. // end NSComboBoxDelegate methods
  113. // NSComboBoxDataSource methods
  114. - (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox {
  115. return [self.values count];
  116. }
  117. - (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index {
  118. return [self.values objectAtIndex:index];
  119. }
  120. - (NSUInteger)comboBox:(NSComboBox *)aComboBox indexOfItemWithStringValue:(NSString *)string {
  121. NSUInteger counter = 0;
  122. for(NSString *val in self.values) {
  123. if([val isEqualToString:string]) {
  124. return counter;
  125. }
  126. counter += 1;
  127. }
  128. return NSNotFound;
  129. }
  130. - (NSString*)comboBox:(NSComboBox *)aComboBox completedString:(NSString *)string {
  131. for(NSString *val in self.values) {
  132. if([val hasPrefix:string]) {
  133. return val;
  134. }
  135. }
  136. return nil;
  137. }
  138. // end NSComboBoxDataSource methods
  139. @end