From 5cedd2889abb7271a1ce34a4c005209f81ec5a77 Mon Sep 17 00:00:00 2001 From: Jeff Johnson Date: Sun, 15 Oct 2017 08:36:30 -0500 Subject: [PATCH] Case-insensitive comparison of service names. Fix for https://github.com/lapcat/Bonjeff/issues/7 --- source/BonjourNode.swift | 2 +- source/BrowserDelegate.swift | 2 +- source/BrowserWindowController.swift | 22 ++++++++++++++++++++++ source/DomainBrowserDelegate.swift | 6 +++--- source/RootBrowserDelegate.swift | 4 ++-- source/ServiceBrowserDelegate.swift | 3 ++- source/ServiceDelegate.swift | 2 +- 7 files changed, 32 insertions(+), 9 deletions(-) diff --git a/source/BonjourNode.swift b/source/BonjourNode.swift index 14fe501..d89f712 100644 --- a/source/BonjourNode.swift +++ b/source/BonjourNode.swift @@ -3,7 +3,7 @@ import Cocoa protocol BonjourNode:NSObjectProtocol { var children:[Any] { get } var objectValue:String { get } - var persistentName:String { get } + var persistentName:String { get set } func start() func stop() } diff --git a/source/BrowserDelegate.swift b/source/BrowserDelegate.swift index 2b239e9..98d3e0c 100644 --- a/source/BrowserDelegate.swift +++ b/source/BrowserDelegate.swift @@ -7,7 +7,7 @@ class BrowserDelegate:NSObject, NetServiceBrowserDelegate, BonjourNode { // These need to be overridden var children:[Any] { fatalError() } var objectValue:String { fatalError() } - var persistentName:String { fatalError() } + var persistentName:String = "" func start() { browser.includesPeerToPeer = true diff --git a/source/BrowserWindowController.swift b/source/BrowserWindowController.swift index 8ad2d8c..54b682c 100644 --- a/source/BrowserWindowController.swift +++ b/source/BrowserWindowController.swift @@ -2,6 +2,7 @@ import Cocoa class BrowserWindowController:NSObject, NSOutlineViewDataSource, NSOutlineViewDelegate, NSWindowDelegate { static let expandedUserDefaultsKey = "ExpandedItems" + static let lowercasedExpandedUserDefaultsKey = "LowercasedExpandedItems" let browserDelegate = RootBrowserDelegate() let outlineView = CopyOutlineView() let window:NSWindow = { @@ -15,6 +16,7 @@ class BrowserWindowController:NSObject, NSOutlineViewDataSource, NSOutlineViewDe }() func open(_ title:String) { + migrateUserDefaultsIfNecessary() browserDelegate.start() window.center() @@ -204,4 +206,24 @@ class BrowserWindowController:NSObject, NSOutlineViewDataSource, NSOutlineViewDe standardUserDefaults.set([persistentName:expanded], forKey:key) } } + + func migrateUserDefaultsIfNecessary() { + // UserDefaults keys are lowercased starting in Bonjeff 1.0.2 + // Migrate Bonjeff 1.0.0 and 1.0.1 keys if necessary + let standardUserDefaults = UserDefaults.standard + let lowercaseExpandedKey = BrowserWindowController.lowercasedExpandedUserDefaultsKey + let lowercasedExpanded = standardUserDefaults.bool(forKey:lowercaseExpandedKey) + if !lowercasedExpanded { + standardUserDefaults.set(true, forKey:lowercaseExpandedKey) + + let expandedKey = BrowserWindowController.expandedUserDefaultsKey + if let expandedDefaults = standardUserDefaults.dictionary(forKey:expandedKey) { + var newDefaults = [String:Any]() + for (key, value) in expandedDefaults { + newDefaults[key.lowercased()] = value + } + standardUserDefaults.set(newDefaults, forKey:expandedKey) + } + } + } } diff --git a/source/DomainBrowserDelegate.swift b/source/DomainBrowserDelegate.swift index 72cb89f..3abf035 100644 --- a/source/DomainBrowserDelegate.swift +++ b/source/DomainBrowserDelegate.swift @@ -5,11 +5,11 @@ class DomainBrowserDelegate:BrowserDelegate { var delegates = [ServiceBrowserDelegate]() override var children:[Any] { return delegates } override var objectValue:String { return domain } - override var persistentName:String { return domain } required init(_ domain:String) { self.domain = domain super.init() + persistentName = domain.lowercased() } @available(*, unavailable) @@ -51,7 +51,7 @@ class DomainBrowserDelegate:BrowserDelegate { } for delegate in delegates { - if delegate.type == type { + if delegate.type.caseInsensitiveCompare(type) == .orderedSame { NSLog("didFind duplicate service:%@", service) return } @@ -70,7 +70,7 @@ class DomainBrowserDelegate:BrowserDelegate { } for (index, delegate) in delegates.enumerated() { - if delegate.type == type { + if delegate.type.caseInsensitiveCompare(type) == .orderedSame { delegates.remove(at:index) delegate.stop() NotificationCenter.default.post(name:.nodeDidRemove, object:self) diff --git a/source/RootBrowserDelegate.swift b/source/RootBrowserDelegate.swift index 0ea4d3f..60b57d2 100644 --- a/source/RootBrowserDelegate.swift +++ b/source/RootBrowserDelegate.swift @@ -11,7 +11,7 @@ class RootBrowserDelegate:BrowserDelegate { func netServiceBrowser(_ sender:NetServiceBrowser, didFindDomain domain:String, moreComing:Bool) { for delegate in delegates { - if delegate.domain == domain { + if delegate.domain.caseInsensitiveCompare(domain) == .orderedSame { NSLog("didFind duplicate domain:%@", domain) return } @@ -26,7 +26,7 @@ class RootBrowserDelegate:BrowserDelegate { func netServiceBrowser(_ sender:NetServiceBrowser, didRemoveDomain domain:String, moreComing:Bool) { for (index, delegate) in delegates.enumerated() { - if delegate.domain == domain { + if delegate.domain.caseInsensitiveCompare(domain) == .orderedSame { delegates.remove(at:index) delegate.stop() NotificationCenter.default.post(name:.nodeDidRemove, object:nil) // nil is root item for outline view diff --git a/source/ServiceBrowserDelegate.swift b/source/ServiceBrowserDelegate.swift index fc521ec..f407f42 100644 --- a/source/ServiceBrowserDelegate.swift +++ b/source/ServiceBrowserDelegate.swift @@ -6,12 +6,13 @@ class ServiceBrowserDelegate:BrowserDelegate { var delegates = [ServiceDelegate]() override var children:[Any] { return delegates } override var objectValue:String { return type } - override var persistentName:String { return type + domain } required init(type:String, domain:String) { self.type = type self.domain = domain super.init() + let name = type + domain + persistentName = name.lowercased() } @available(*, unavailable) diff --git a/source/ServiceDelegate.swift b/source/ServiceDelegate.swift index 741ef84..c7a49f6 100644 --- a/source/ServiceDelegate.swift +++ b/source/ServiceDelegate.swift @@ -11,7 +11,7 @@ class ServiceDelegate:NSObject, NetServiceDelegate, BonjourNode { required init(_ service:NetService) { self.service = service - persistentName = "\(service.name).\(service.type)\(service.domain)" + persistentName = "\(service.name).\(service.type)\(service.domain)".lowercased() super.init() }