JoinNetworkViewController.m 5.0 KB

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