ShowNetworksViewController.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. - (void)deleteNetworkFromList:(NSString *)nwid {
  50. [self.netMonitor deleteSavedNetwork:nwid];
  51. }
  52. - (void)setNetworks:(NSArray<Network *> *)list {
  53. for (Network *n in list) {
  54. if ([_networkList containsObject:n]) {
  55. // don't need to do anything here. Already an identical object in the list
  56. continue;
  57. }
  58. else {
  59. // network not in the list based on equality. Did an object change? or is it a new item?
  60. if (hasNetworkWithID(_networkList, n.nwid)) {
  61. for (int i = 0; i < [_networkList count]; ++i) {
  62. Network *n2 = [_networkList objectAtIndex:i];
  63. if (n.nwid == n2.nwid)
  64. {
  65. [_networkList replaceObjectAtIndex:i withObject:n];
  66. [_tableView reloadDataForRowIndexes:[NSIndexSet indexSetWithIndex:i]
  67. columnIndexes:[NSIndexSet indexSetWithIndex:0]];
  68. }
  69. }
  70. }
  71. else {
  72. [_networkList addObject:n];
  73. [_tableView reloadData];
  74. }
  75. }
  76. }
  77. }
  78. - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
  79. return [_networkList count];
  80. }
  81. - (NSView*)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
  82. {
  83. NetworkInfoCell *cell = (NetworkInfoCell*)[tableView makeViewWithIdentifier:@"NetworkInfoCell"
  84. owner:nil];
  85. Network *network = [_networkList objectAtIndex:row];
  86. cell.parent = self;
  87. cell.networkIdField.stringValue = [NSString stringWithFormat:@"%10llx", network.nwid];
  88. cell.networkNameField.stringValue = network.name;
  89. cell.statusField.stringValue = [network statusString];
  90. cell.typeField.stringValue = [network typeString];
  91. cell.mtuField.stringValue = [NSString stringWithFormat:@"%d", network.mtu];
  92. cell.macField.stringValue = network.mac;
  93. cell.broadcastField.stringValue = network.broadcastEnabled ? @"ENABLED" : @"DISABLED";
  94. cell.bridgingField.stringValue = network.bridge ? @"ENABLED" : @"DISABLED";
  95. cell.deviceField.stringValue = network.portDeviceName;
  96. if(network.connected) {
  97. cell.connectedCheckbox.state = NSOnState;
  98. if(network.allowDefault) {
  99. cell.allowDefault.enabled = YES;
  100. cell.allowDefault.state = NSOnState;
  101. }
  102. else {
  103. cell.allowDefault.state = NSOffState;
  104. if([Network defaultRouteExists:_networkList]) {
  105. cell.allowDefault.enabled = NO;
  106. }
  107. else {
  108. cell.allowDefault.enabled = YES;
  109. }
  110. }
  111. cell.allowGlobal.enabled = YES;
  112. cell.allowManaged.enabled = YES;
  113. }
  114. else {
  115. cell.connectedCheckbox.state = NSOffState;
  116. cell.allowDefault.enabled = NO;
  117. cell.allowGlobal.enabled = NO;
  118. cell.allowManaged.enabled = NO;
  119. }
  120. cell.allowGlobal.state = network.allowGlobal ? NSOnState : NSOffState;
  121. cell.allowManaged.state = network.allowManaged ? NSOnState : NSOffState;
  122. cell.addressesField.stringValue = @"";
  123. for(NSString *addr in network.assignedAddresses) {
  124. cell.addressesField.stringValue = [[cell.addressesField.stringValue stringByAppendingString:addr] stringByAppendingString:@"\n"];
  125. }
  126. return cell;
  127. }
  128. @end