Skip to content

Commit

Permalink
Case-insensitive comparison of service names.
Browse files Browse the repository at this point in the history
Fix for lapcat#7
  • Loading branch information
lapcat committed Oct 15, 2017
1 parent 91a6082 commit 5cedd28
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion source/BonjourNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
2 changes: 1 addition & 1 deletion source/BrowserDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions source/BrowserWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -15,6 +16,7 @@ class BrowserWindowController:NSObject, NSOutlineViewDataSource, NSOutlineViewDe
}()

func open(_ title:String) {
migrateUserDefaultsIfNecessary()
browserDelegate.start()

window.center()
Expand Down Expand Up @@ -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)
}
}
}
}
6 changes: 3 additions & 3 deletions source/DomainBrowserDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions source/RootBrowserDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion source/ServiceBrowserDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion source/ServiceDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down

0 comments on commit 5cedd28

Please sign in to comment.