ShowNetworksViewController.m 5.8 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 "ShowNetworksViewController.h"
  19. #import "NetworkMonitor.h"
  20. #import "NetworkInfoCell.h"
  21. #import "Network.h"
  22. BOOL hasNetworkWithID(NSArray<Network*> *list, UInt64 nwid)
  23. {
  24. for(Network *n in list) {
  25. if(n.nwid == nwid) {
  26. return YES;
  27. }
  28. }
  29. return NO;
  30. }
  31. @interface ShowNetworksViewController ()
  32. @end
  33. @implementation ShowNetworksViewController
  34. - (void)viewDidLoad {
  35. [super viewDidLoad];
  36. self.networkList = [NSMutableArray array];
  37. [self.tableView setDelegate:self];
  38. [self.tableView setDataSource:self];
  39. [self.tableView setBackgroundColor:[NSColor clearColor]];
  40. }
  41. - (void)viewWillAppear {
  42. [super viewWillAppear];
  43. self.visible = YES;
  44. }
  45. - (void)viewWillDisappear {
  46. [super viewWillDisappear];
  47. self.visible = NO;
  48. }
  49. - (NSInteger)findNetworkWithID:(UInt64)networkId
  50. {
  51. for(int i = 0; i < [_networkList count]; ++i) {
  52. Network *nw = [_networkList objectAtIndex:i];
  53. if(nw.nwid == networkId) {
  54. return i;
  55. }
  56. }
  57. return NSNotFound;
  58. }
  59. - (void)deleteNetworkFromList:(NSString *)nwid {
  60. [self.netMonitor deleteSavedNetwork:nwid];
  61. UInt64 netid = 0;
  62. NSScanner *scanner = [NSScanner scannerWithString:nwid];
  63. [scanner scanHexLongLong:&netid];
  64. for (Network *n in _networkList) {
  65. if (n.nwid == netid) {
  66. NSInteger index = [self findNetworkWithID:netid];
  67. if (index != NSNotFound) {
  68. [_networkList removeObjectAtIndex:index];
  69. [_tableView reloadData];
  70. }
  71. }
  72. }
  73. }
  74. - (void)setNetworks:(NSArray<Network *> *)list {
  75. for (Network *n in list) {
  76. if ([_networkList containsObject:n]) {
  77. // don't need to do anything here. Already an identical object in the list
  78. continue;
  79. }
  80. else {
  81. // network not in the list based on equality. Did an object change? or is it a new item?
  82. if (hasNetworkWithID(_networkList, n.nwid)) {
  83. for (int i = 0; i < [_networkList count]; ++i) {
  84. Network *n2 = [_networkList objectAtIndex:i];
  85. if (n.nwid == n2.nwid)
  86. {
  87. [_networkList replaceObjectAtIndex:i withObject:n];
  88. [_tableView reloadDataForRowIndexes:[NSIndexSet indexSetWithIndex:i]
  89. columnIndexes:[NSIndexSet indexSetWithIndex:0]];
  90. }
  91. }
  92. }
  93. else {
  94. [_networkList addObject:n];
  95. [_tableView reloadData];
  96. }
  97. }
  98. }
  99. }
  100. - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
  101. return [_networkList count];
  102. }
  103. - (NSView*)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
  104. {
  105. NetworkInfoCell *cell = (NetworkInfoCell*)[tableView makeViewWithIdentifier:@"NetworkInfoCell"
  106. owner:nil];
  107. Network *network = [_networkList objectAtIndex:row];
  108. cell.parent = self;
  109. cell.networkIdField.stringValue = [NSString stringWithFormat:@"%10llx", network.nwid];
  110. cell.networkNameField.stringValue = network.name;
  111. cell.statusField.stringValue = [network statusString];
  112. cell.typeField.stringValue = [network typeString];
  113. cell.mtuField.stringValue = [NSString stringWithFormat:@"%d", network.mtu];
  114. cell.macField.stringValue = network.mac;
  115. cell.broadcastField.stringValue = network.broadcastEnabled ? @"ENABLED" : @"DISABLED";
  116. cell.bridgingField.stringValue = network.bridge ? @"ENABLED" : @"DISABLED";
  117. cell.deviceField.stringValue = network.portDeviceName;
  118. if(network.connected) {
  119. cell.connectedCheckbox.state = NSOnState;
  120. if(network.allowDefault) {
  121. cell.allowDefault.enabled = YES;
  122. cell.allowDefault.state = NSOnState;
  123. }
  124. else {
  125. cell.allowDefault.state = NSOffState;
  126. if([Network defaultRouteExists:_networkList]) {
  127. cell.allowDefault.enabled = NO;
  128. }
  129. else {
  130. cell.allowDefault.enabled = YES;
  131. }
  132. }
  133. cell.allowGlobal.enabled = YES;
  134. cell.allowManaged.enabled = YES;
  135. cell.allowDNS.enabled = YES;
  136. }
  137. else {
  138. cell.connectedCheckbox.state = NSOffState;
  139. cell.allowDefault.enabled = NO;
  140. cell.allowGlobal.enabled = NO;
  141. cell.allowManaged.enabled = NO;
  142. cell.allowDNS.enabled = NO;
  143. }
  144. cell.allowGlobal.state = network.allowGlobal ? NSOnState : NSOffState;
  145. cell.allowManaged.state = network.allowManaged ? NSOnState : NSOffState;
  146. cell.allowDNS.state = network.allowDNS ? NSOnState : NSOffState;
  147. cell.addressesField.stringValue = @"";
  148. for(NSString *addr in network.assignedAddresses) {
  149. cell.addressesField.stringValue = [[cell.addressesField.stringValue stringByAppendingString:addr] stringByAppendingString:@"\n"];
  150. }
  151. return cell;
  152. }
  153. @end