From 5b13b282ede30b8cfb2f5ff1875b96f3b277acd9 Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Sun, 7 Aug 2016 19:13:12 -0700 Subject: Replace JoinNetworkViewController with obj-c --- ZeroTier One.xcodeproj/project.pbxproj | 12 +- ZeroTier One/JoinNetworkViewController.h | 27 +++++ ZeroTier One/JoinNetworkViewController.m | 166 +++++++++++++++++++++++++++ ZeroTier One/JoinNetworkViewController.swift | 164 -------------------------- ZeroTier One/ZeroTier One-Bridging-Header.h | 1 + 5 files changed, 201 insertions(+), 169 deletions(-) create mode 100644 ZeroTier One/JoinNetworkViewController.h create mode 100644 ZeroTier One/JoinNetworkViewController.m delete mode 100644 ZeroTier One/JoinNetworkViewController.swift diff --git a/ZeroTier One.xcodeproj/project.pbxproj b/ZeroTier One.xcodeproj/project.pbxproj index 60aa89ce..e3b9f551 100644 --- a/ZeroTier One.xcodeproj/project.pbxproj +++ b/ZeroTier One.xcodeproj/project.pbxproj @@ -13,7 +13,6 @@ 93326BDC1CE7C816005CA2AC /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93326BDB1CE7C816005CA2AC /* AppDelegate.swift */; }; 93326BDE1CE7C816005CA2AC /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 93326BDD1CE7C816005CA2AC /* Assets.xcassets */; }; 93326BE11CE7C816005CA2AC /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 93326BDF1CE7C816005CA2AC /* MainMenu.xib */; }; - 93326BEA1CE7D9B9005CA2AC /* JoinNetworkViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93326BE81CE7D9B9005CA2AC /* JoinNetworkViewController.swift */; }; 93326BEB1CE7D9B9005CA2AC /* JoinNetworkViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 93326BE91CE7D9B9005CA2AC /* JoinNetworkViewController.xib */; }; 93326BEF1CE7DA30005CA2AC /* ShowNetworksViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 93326BED1CE7DA30005CA2AC /* ShowNetworksViewController.xib */; }; 93D1675F1D54191C00330C99 /* NodeStatus.m in Sources */ = {isa = PBXBuildFile; fileRef = 93D1675E1D54191C00330C99 /* NodeStatus.m */; }; @@ -24,6 +23,7 @@ 93D167701D57FD3800330C99 /* NetworkMonitor.m in Sources */ = {isa = PBXBuildFile; fileRef = 93D1676F1D57FD3800330C99 /* NetworkMonitor.m */; }; 93D167731D58093C00330C99 /* NetworkInfoCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 93D167721D58093C00330C99 /* NetworkInfoCell.m */; }; 93D167761D580C3500330C99 /* ShowNetworksViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 93D167751D580C3500330C99 /* ShowNetworksViewController.m */; }; + 93D167791D5815E600330C99 /* JoinNetworkViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 93D167781D5815E600330C99 /* JoinNetworkViewController.m */; }; 93DAFB271D3F0BEE004D5417 /* about.html in Resources */ = {isa = PBXBuildFile; fileRef = 93DAFB261D3F0BEE004D5417 /* about.html */; }; 93DAFE4B1CFE53CA00547CC4 /* AuthtokenCopy.m in Sources */ = {isa = PBXBuildFile; fileRef = 93DAFE4A1CFE53CA00547CC4 /* AuthtokenCopy.m */; }; /* End PBXBuildFile section */ @@ -37,7 +37,6 @@ 93326BDD1CE7C816005CA2AC /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 93326BE01CE7C816005CA2AC /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/MainMenu.xib; sourceTree = ""; }; 93326BE21CE7C816005CA2AC /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - 93326BE81CE7D9B9005CA2AC /* JoinNetworkViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JoinNetworkViewController.swift; sourceTree = ""; }; 93326BE91CE7D9B9005CA2AC /* JoinNetworkViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = JoinNetworkViewController.xib; sourceTree = ""; }; 93326BED1CE7DA30005CA2AC /* ShowNetworksViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = ShowNetworksViewController.xib; sourceTree = ""; }; 93D1675D1D54191C00330C99 /* NodeStatus.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NodeStatus.h; sourceTree = ""; }; @@ -56,6 +55,8 @@ 93D167721D58093C00330C99 /* NetworkInfoCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NetworkInfoCell.m; sourceTree = ""; }; 93D167741D580C3500330C99 /* ShowNetworksViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ShowNetworksViewController.h; sourceTree = ""; }; 93D167751D580C3500330C99 /* ShowNetworksViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ShowNetworksViewController.m; sourceTree = ""; }; + 93D167771D5815E600330C99 /* JoinNetworkViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JoinNetworkViewController.h; sourceTree = ""; }; + 93D167781D5815E600330C99 /* JoinNetworkViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JoinNetworkViewController.m; sourceTree = ""; }; 93DAFB261D3F0BEE004D5417 /* about.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = about.html; sourceTree = ""; }; 93DAFE491CFE53C900547CC4 /* ZeroTier One-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "ZeroTier One-Bridging-Header.h"; sourceTree = ""; }; 93DAFE4A1CFE53CA00547CC4 /* AuthtokenCopy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AuthtokenCopy.m; sourceTree = ""; }; @@ -94,8 +95,6 @@ children = ( 932D472E1D1CD499004BCFE2 /* ZeroTierIcon.icns */, 93326BDB1CE7C816005CA2AC /* AppDelegate.swift */, - 93326BE81CE7D9B9005CA2AC /* JoinNetworkViewController.swift */, - 93326BE91CE7D9B9005CA2AC /* JoinNetworkViewController.xib */, 93326BDD1CE7C816005CA2AC /* Assets.xcassets */, 93326BDF1CE7C816005CA2AC /* MainMenu.xib */, 93326BE21CE7C816005CA2AC /* Info.plist */, @@ -122,6 +121,9 @@ 93D167741D580C3500330C99 /* ShowNetworksViewController.h */, 93D167751D580C3500330C99 /* ShowNetworksViewController.m */, 93326BED1CE7DA30005CA2AC /* ShowNetworksViewController.xib */, + 93D167771D5815E600330C99 /* JoinNetworkViewController.h */, + 93D167781D5815E600330C99 /* JoinNetworkViewController.m */, + 93326BE91CE7D9B9005CA2AC /* JoinNetworkViewController.xib */, ); path = "ZeroTier One"; sourceTree = ""; @@ -207,12 +209,12 @@ 93326BDC1CE7C816005CA2AC /* AppDelegate.swift in Sources */, 93DAFE4B1CFE53CA00547CC4 /* AuthtokenCopy.m in Sources */, 93D167701D57FD3800330C99 /* NetworkMonitor.m in Sources */, - 93326BEA1CE7D9B9005CA2AC /* JoinNetworkViewController.swift in Sources */, 93D1675F1D54191C00330C99 /* NodeStatus.m in Sources */, 93D167691D57E7EA00330C99 /* AboutViewController.m in Sources */, 93D1676D1D57EB8400330C99 /* PreferencesViewController.m in Sources */, 93D167731D58093C00330C99 /* NetworkInfoCell.m in Sources */, 93D167661D54308200330C99 /* Network.m in Sources */, + 93D167791D5815E600330C99 /* JoinNetworkViewController.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/ZeroTier One/JoinNetworkViewController.h b/ZeroTier One/JoinNetworkViewController.h new file mode 100644 index 00000000..894b4ec2 --- /dev/null +++ b/ZeroTier One/JoinNetworkViewController.h @@ -0,0 +1,27 @@ +// +// JoinNetworkViewController.h +// ZeroTier One +// +// Created by Grant Limberg on 8/7/16. +// Copyright © 2016 ZeroTier, Inc. All rights reserved. +// + +#import + + +extern NSString * const JoinedNetworksKey; + +@interface JoinNetworkViewController : NSViewController + +@property (nonatomic, weak) IBOutlet NSComboBox *network; +@property (nonatomic, weak) IBOutlet NSButton *joinButton; +@property (nonatomic, weak) IBOutlet NSButton *allowManagedCheckBox; +@property (nonatomic, weak) IBOutlet NSButton *allowGlobalCheckBox; +@property (nonatomic, weak) IBOutlet NSButton *allowDefaultCheckBox; + +@property (nonatomic) NSMutableArray *values; + +- (IBAction)onJoinClicked:(id)sender; + + +@end diff --git a/ZeroTier One/JoinNetworkViewController.m b/ZeroTier One/JoinNetworkViewController.m new file mode 100644 index 00000000..60e8e1cc --- /dev/null +++ b/ZeroTier One/JoinNetworkViewController.m @@ -0,0 +1,166 @@ +// +// JoinNetworkViewController.m +// ZeroTier One +// +// Created by Grant Limberg on 8/7/16. +// Copyright © 2016 ZeroTier, Inc. All rights reserved. +// + +#import "JoinNetworkViewController.h" +#import "ServiceCom.h" + + + +NSString * const JoinedNetworksKey = @"com.zerotier.one.joined-networks"; + +@interface NSString (extra) + +- (BOOL)contains:(NSString*)find; +//- (NSString*)trunc:(NSInteger)length trailing:(NSString*)trailing; + +@end + +@implementation NSString (extra) + +- (BOOL)contains:(NSString*)find { + NSRange range = [self rangeOfString:find]; + return range.location != NSNotFound; +} +// +//- (NSString*)trunc:(NSInteger)length trailing:(NSString*)trailing { +// +//} + +@end + + +@implementation JoinNetworkViewController + +- (void)viewDidLoad { + [super viewDidLoad]; + // Do view setup here. + [self.network setDelegate:self]; + [self.network setDataSource:self]; +} + +- (void)viewWillAppear { + [super viewWillAppear]; + + self.allowManagedCheckBox.state = NSOnState; + self.allowGlobalCheckBox.state = NSOffState; + self.allowDefaultCheckBox.state = NSOffState; + + self.network.stringValue = @""; + + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + + NSMutableArray *vals = [[defaults stringArrayForKey:JoinedNetworksKey] mutableCopy]; + + if(vals) { + self.values = vals; + } +} + +- (void)viewWillDisappear { + [super viewWillDisappear]; + + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + + [defaults setObject:self.values forKey:JoinedNetworksKey]; +} + +- (IBAction)onJoinClicked:(id)sender { + NSString *networkId = self.network.stringValue; + + [[ServiceCom sharedInstance] joinNetwork:networkId + allowManaged:(self.allowManagedCheckBox.state == NSOnState) + allowGlobal:(self.allowGlobalCheckBox.state == NSOnState) + allowDefault:(self.allowDefaultCheckBox.state == NSOnState)]; + + self.network.stringValue = @""; + + if(![self.values containsObject:networkId]) { + [self.values insertObject:networkId atIndex:0]; + + while([self.values count] > 20) { + [self.values removeLastObject]; + } + } +} + +// NSComboBoxDelegate methods + +- (void)controlTextDidChange:(NSNotification *)obj { + NSComboBox *cb = (NSComboBox*)obj.object; + NSString *value = cb.stringValue; + + NSString *allowedCharacters = @"abcdefABCDEF0123456789"; + + NSString *outValue = @""; + + for(int i = 0; i < [value length]; ++i) { + if(![allowedCharacters contains:[NSString stringWithFormat:@"%C", [value characterAtIndex:i]]]) { + NSBeep(); + } + else { + outValue = [outValue stringByAppendingString:[NSString stringWithFormat:@"%C", [value characterAtIndex:i]]]; + } + } + + if([outValue lengthOfBytesUsingEncoding:NSUTF8StringEncoding] == 16) { + self.joinButton.enabled = YES; + } + else { + if([outValue lengthOfBytesUsingEncoding:NSUTF8StringEncoding] > 16) { + NSRange range = {0, 16}; + range = [outValue rangeOfComposedCharacterSequencesForRange:range]; + outValue = [outValue substringWithRange:range]; + NSBeep(); + self.joinButton.enabled = YES; + } + else { + self.joinButton.enabled = NO; + } + } + + cb.stringValue = outValue; +} + +// end NSComboBoxDelegate methods + +// NSComboBoxDataSource methods + +- (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox { + return [self.values count]; +} + +- (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index { + return [self.values objectAtIndex:index]; +} + +- (NSUInteger)comboBox:(NSComboBox *)aComboBox indexOfItemWithStringValue:(NSString *)string { + NSUInteger counter = 0; + + for(NSString *val in self.values) { + if([val isEqualToString:string]) { + return counter; + } + + counter += 1; + } + + return NSNotFound; +} + +- (NSString*)comboBox:(NSComboBox *)aComboBox completedString:(NSString *)string { + for(NSString *val in self.values) { + if([val hasPrefix:string]) { + return val; + } + } + return nil; +} + +// end NSComboBoxDataSource methods + +@end diff --git a/ZeroTier One/JoinNetworkViewController.swift b/ZeroTier One/JoinNetworkViewController.swift deleted file mode 100644 index 0effda59..00000000 --- a/ZeroTier One/JoinNetworkViewController.swift +++ /dev/null @@ -1,164 +0,0 @@ -// -// JoinNetworkViewController.swift -// ZeroTier One -// -// Created by Grant Limberg on 5/14/16. -// Copyright © 2016 ZeroTier, Inc. All rights reserved. -// - -import Cocoa - -extension String { - func contains(find: String) -> Bool { - return self.rangeOfString(find) != nil - } - - func trunc(length: Int, trailing: String? = "...") -> String { - if self.characters.count > length { - return self.substringToIndex(self.startIndex.advancedBy(length)) + (trailing ?? "") - } else { - return self - } - } -} - -let joinedNetworksKey = "com.zerotier.one.joined-networks" - - -class JoinNetworkViewController: NSViewController, NSComboBoxDelegate, NSComboBoxDataSource { - - @IBOutlet var network: NSComboBox! - @IBOutlet var joinButton: NSButton! - - @IBOutlet var allowManagedCheckBox: NSButton! - @IBOutlet var allowGlobalCheckBox: NSButton! - @IBOutlet var allowDefaultCheckBox:NSButton! - - var values: [String] = [String]() - - override func viewDidLoad() { - super.viewDidLoad() - network.setDelegate(self) - network.dataSource = self - } - - override func viewWillAppear() { - super.viewWillAppear() - - allowManagedCheckBox.state = NSOnState - allowGlobalCheckBox.state = NSOffState - allowDefaultCheckBox.state = NSOffState - - let defaults = NSUserDefaults.standardUserDefaults() - - let vals = defaults.stringArrayForKey(joinedNetworksKey) - - if let v = vals { - values = v - } - } - - override func viewDidDisappear() { - super.viewWillDisappear() - - let defaults = NSUserDefaults.standardUserDefaults() - - defaults.setObject(values, forKey: joinedNetworksKey) - } - - @IBAction func onJoinClicked(sender: AnyObject?) { - let networkString = network.stringValue - - ServiceCom.sharedInstance().joinNetwork(networkString, - allowManaged: allowManagedCheckBox.state == NSOnState, - allowGlobal: allowGlobalCheckBox.state == NSOnState, - allowDefault: allowDefaultCheckBox.state == NSOnState) - network.stringValue = "" - - - if !values.contains(networkString) { - values.insert(networkString, atIndex: 0) - - while values.count > 20 { - values.removeLast() - } - } - } - - - // NSComboBoxDelegate Methods - - override func controlTextDidChange(obj: NSNotification) { - let cb = obj.object as! NSComboBox - let value = cb.stringValue - - - let allowedCharacters = "abcdefABCDEF0123456789" - - var outValue = "" - - for char in value.characters { - if !allowedCharacters.contains(String(char)) { - NSBeep() - } - else { - outValue += String(char) - } - } - - - - if outValue.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) == 16 { - joinButton.enabled = true - } - else { - - if outValue.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) > 16 { - outValue = outValue.trunc(16, trailing: "") - NSBeep() - joinButton.enabled = true - } - else { - joinButton.enabled = false - } - } - - cb.stringValue = outValue - } - // end NSComboBoxDelegate Methods - - - // NSComboBoxDataSource methods - - func numberOfItemsInComboBox(aComboBox: NSComboBox) -> Int { - return values.count - } - - func comboBox(aComboBox: NSComboBox, objectValueForItemAtIndex index: Int) -> AnyObject { - return values[index] - } - - func comboBox(aComboBox: NSComboBox, indexOfItemWithStringValue string: String) -> Int { - - var counter = 0 - for val in values { - if val == string { - return counter - } - counter += 1 - } - return NSNotFound - } - - func comboBox(aComboBox: NSComboBox, completedString string: String) -> String? { - for val in values { - if val.hasPrefix(string) { - return val - } - } - - return nil - } - - // end NSComboBoxDataSorce methods -} diff --git a/ZeroTier One/ZeroTier One-Bridging-Header.h b/ZeroTier One/ZeroTier One-Bridging-Header.h index 07da0d00..60700eeb 100644 --- a/ZeroTier One/ZeroTier One-Bridging-Header.h +++ b/ZeroTier One/ZeroTier One-Bridging-Header.h @@ -11,3 +11,4 @@ #import "NetworkMonitor.h" #import "NetworkInfoCell.h" #import "ShowNetworksViewController.h" +#import "JoinNetworkViewController.h" -- cgit v1.2.3