Browse Source

Replace JoinNetworkViewController with obj-c

Grant Limberg 9 years ago
parent
commit
5b13b282ed

+ 7 - 5
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 = "<group>"; };
 		93326BE01CE7C816005CA2AC /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/MainMenu.xib; sourceTree = "<group>"; };
 		93326BE21CE7C816005CA2AC /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
-		93326BE81CE7D9B9005CA2AC /* JoinNetworkViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JoinNetworkViewController.swift; sourceTree = "<group>"; };
 		93326BE91CE7D9B9005CA2AC /* JoinNetworkViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = JoinNetworkViewController.xib; sourceTree = "<group>"; };
 		93326BED1CE7DA30005CA2AC /* ShowNetworksViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = ShowNetworksViewController.xib; sourceTree = "<group>"; };
 		93D1675D1D54191C00330C99 /* NodeStatus.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NodeStatus.h; sourceTree = "<group>"; };
@@ -56,6 +55,8 @@
 		93D167721D58093C00330C99 /* NetworkInfoCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NetworkInfoCell.m; sourceTree = "<group>"; };
 		93D167741D580C3500330C99 /* ShowNetworksViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ShowNetworksViewController.h; sourceTree = "<group>"; };
 		93D167751D580C3500330C99 /* ShowNetworksViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ShowNetworksViewController.m; sourceTree = "<group>"; };
+		93D167771D5815E600330C99 /* JoinNetworkViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JoinNetworkViewController.h; sourceTree = "<group>"; };
+		93D167781D5815E600330C99 /* JoinNetworkViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JoinNetworkViewController.m; sourceTree = "<group>"; };
 		93DAFB261D3F0BEE004D5417 /* about.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = about.html; sourceTree = "<group>"; };
 		93DAFE491CFE53C900547CC4 /* ZeroTier One-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "ZeroTier One-Bridging-Header.h"; sourceTree = "<group>"; };
 		93DAFE4A1CFE53CA00547CC4 /* AuthtokenCopy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AuthtokenCopy.m; sourceTree = "<group>"; };
@@ -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 = "<group>";
@@ -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;
 		};

+ 27 - 0
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 <Cocoa/Cocoa.h>
+
+
+extern NSString * const JoinedNetworksKey;
+
+@interface JoinNetworkViewController : NSViewController <NSComboBoxDelegate, NSComboBoxDataSource>
+
+@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<NSString*> *values;
+
+- (IBAction)onJoinClicked:(id)sender;
+
+
+@end

+ 166 - 0
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<NSString*> *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

+ 0 - 164
ZeroTier One/JoinNetworkViewController.swift

@@ -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
-}

+ 1 - 0
ZeroTier One/ZeroTier One-Bridging-Header.h

@@ -11,3 +11,4 @@
 #import "NetworkMonitor.h"
 #import "NetworkInfoCell.h"
 #import "ShowNetworksViewController.h"
+#import "JoinNetworkViewController.h"