Revision: 29711
          http://sourceforge.net/p/bibdesk/svn/29711
Author:   hofman
Date:     2025-10-12 15:35:23 +0000 (Sun, 12 Oct 2025)
Log Message:
-----------
use kvo for sharingserver properties instead of change notifications

Modified Paths:
--------------
    trunk/bibdesk/BDSKSharingServer.h
    trunk/bibdesk/BDSKSharingServer.m
    trunk/bibdesk/BDSKStringConstants.h
    trunk/bibdesk/BDSKStringConstants.m
    trunk/bibdesk/BibPref_Sharing.m

Modified: trunk/bibdesk/BDSKSharingServer.h
===================================================================
--- trunk/bibdesk/BDSKSharingServer.h   2025-10-12 09:30:02 UTC (rev 29710)
+++ trunk/bibdesk/BDSKSharingServer.h   2025-10-12 15:35:23 UTC (rev 29711)
@@ -82,6 +82,7 @@
     BDSKAsyncSharingServer *asyncServer;
     int socketDescriptor;
     BDSKSharingStatus status;
+    NSUInteger numberOfConnections;
     NSInteger tryCount;
 }
 

Modified: trunk/bibdesk/BDSKSharingServer.m
===================================================================
--- trunk/bibdesk/BDSKSharingServer.m   2025-10-12 09:30:02 UTC (rev 29710)
+++ trunk/bibdesk/BDSKSharingServer.m   2025-10-12 15:35:23 UTC (rev 29711)
@@ -132,7 +132,6 @@
     NSConnection *connection;
     NSMutableArray *connectedClients;
     NSMutableDictionary *registeredClients;
-    NSUInteger numberOfConnections;
     BDSKReadWriteLock *rwLock;
 }
 #pragma clang diagnostic pop
@@ -140,8 +139,6 @@
 - (instancetype)initWithSharingName:(NSString *)aSharingName 
tryCount:(NSInteger)tryCount forSharingServer:(BDSKSharingServer 
*)aSharingServer NS_DESIGNATED_INITIALIZER;
 - (instancetype)init NS_UNAVAILABLE;
 
-@property NSUInteger numberOfConnections;
-
 - (void)notifyClientsOfChange;
 
 @end
@@ -151,6 +148,7 @@
 @interface BDSKSharingServer ()
 
 @property (nonatomic) BDSKSharingStatus status;
+@property (nonatomic) NSUInteger numberOfConnections;
 
 @end
 
@@ -158,8 +156,8 @@
 
 @implementation BDSKSharingServer
 
-@synthesize sharingName, status;
-@dynamic numberOfConnections;
+@synthesize status, numberOfConnections;
+@dynamic sharingName;
 
 + (void)initialize;
 {
@@ -243,18 +241,6 @@
     return [netService name];
 }
 
-- (void)setStatus:(BDSKSharingStatus)newStatus {
-    if (status != newStatus) {
-        status = newStatus;
-        [[NSNotificationCenter defaultCenter] 
postNotificationName:BDSKSharingStatusChangedNotification object:nil];
-    }
-}
-
-- (NSUInteger)numberOfConnections { 
-    // minor thread-safety issue here; this may be off by one
-    return [asyncServer numberOfConnections];
-}
-
 - (void)handleQueuedDataChanged;
 {
     // not the default connection here; we want to call our background thread, 
but only if it's running
@@ -579,8 +565,6 @@
 
 @implementation BDSKAsyncSharingServer
 
-@dynamic numberOfConnections;
-
 // This is the minimal version for the client that we require
 // If we introduce incompatible changes in future, bump this to avoid sharing 
breakage
 + (NSString *)requiredClientProtocolVersion { return @"0"; }
@@ -594,7 +578,6 @@
         tryCount = aTryCount;
         connectedClients = [[NSMutableArray alloc] init];
         registeredClients = [[NSMutableDictionary alloc] init];
-        numberOfConnections = 0;
         rwLock = [[BDSKReadWriteLock alloc] init];
         [self start];
     }   
@@ -601,31 +584,16 @@
     return self;
 }
 
-#pragma mark Thread Safe
-
-- (NSUInteger)numberOfConnections {
-    [rwLock lockForReading];
-    NSUInteger count = numberOfConnections; 
-    [rwLock unlock];
-    return count;
-}
-
 - (void)setNumberOfConnections:(NSUInteger)count {
-    BOOL changed = NO;
-    [rwLock lockForWriting];
-    changed = (numberOfConnections != count);
-    numberOfConnections = count;
-    [rwLock unlock];
-    if (changed) {
-        dispatch_async(dispatch_get_main_queue(), ^{
-            [[NSNotificationCenter defaultCenter] 
postNotificationName:BDSKClientConnectionsChangedNotification object:nil];
-        });
-    }
+    dispatch_async(dispatch_get_main_queue(), ^{
+        [sharingServer setNumberOfConnections:count];
+    });
 }
 
 #pragma mark Main Thread
 
 - (void)stop {
+    [sharingServer setNumberOfConnections:0];
     // make sure we don't message our sharingServer
     sharingServer = nil;
     [super stop];

Modified: trunk/bibdesk/BDSKStringConstants.h
===================================================================
--- trunk/bibdesk/BDSKStringConstants.h 2025-10-12 09:30:02 UTC (rev 29710)
+++ trunk/bibdesk/BDSKStringConstants.h 2025-10-12 15:35:23 UTC (rev 29711)
@@ -314,8 +314,6 @@
 extern NSString *BDSKExternalGroupUpdatedNotification;
 extern NSString *BDSKExternalGroupWillRemoveItemNotification;
 extern NSString *BDSKWillRemoveExternalGroupsNotification;
-extern NSString *BDSKClientConnectionsChangedNotification;
-extern NSString *BDSKSharingStatusChangedNotification;
 extern NSString *BDSKSharingPasswordChangedNotification;
 extern NSString *BDSKDocumentControllerAddDocumentNotification;
 extern NSString *BDSKDocumentControllerRemoveDocumentNotification;

Modified: trunk/bibdesk/BDSKStringConstants.m
===================================================================
--- trunk/bibdesk/BDSKStringConstants.m 2025-10-12 09:30:02 UTC (rev 29710)
+++ trunk/bibdesk/BDSKStringConstants.m 2025-10-12 15:35:23 UTC (rev 29711)
@@ -324,8 +324,6 @@
 NSString *BDSKExternalGroupUpdatedNotification = 
@"BDSKExternalGroupUpdatedNotification";
 NSString *BDSKExternalGroupWillRemoveItemNotification = 
@"BDSKExternalGroupWillRemoveItemNotification";
 NSString *BDSKWillRemoveExternalGroupsNotification = 
@"BDSKWillRemoveExternalGroupsNotification";
-NSString *BDSKClientConnectionsChangedNotification = 
@"BDSKClientConnectionsChangedNotification";
-NSString *BDSKSharingStatusChangedNotification = 
@"BDSKSharingStatusChangedNotification";
 NSString *BDSKSharingPasswordChangedNotification = 
@"BDSKSharingPasswordChangedNotification";
 NSString *BDSKDocumentControllerAddDocumentNotification = 
@"BDSKDocumentControllerAddDocumentNotification";
 NSString *BDSKDocumentControllerRemoveDocumentNotification = 
@"BDSKDocumentControllerRemoveDocumentNotification";

Modified: trunk/bibdesk/BibPref_Sharing.m
===================================================================
--- trunk/bibdesk/BibPref_Sharing.m     2025-10-12 09:30:02 UTC (rev 29710)
+++ trunk/bibdesk/BibPref_Sharing.m     2025-10-12 15:35:23 UTC (rev 29711)
@@ -43,10 +43,11 @@
 #import "BDSKSharingServer.h"
 #import "BDSKPasswordController.h"
 
+static char BDSKSharingServerObservationContext;
 
 @interface BibPref_Sharing (Private)
 - (void)handleComputerNameChanged:(NSNotification *)aNotification;
-- (void)handleSharingStatusChanged:(NSNotification *)aNotification;
+- (void)updateSharingUI;
 @end
 
 
@@ -58,15 +59,15 @@
 - (void)viewDidLoad {
     [super viewDidLoad];
     
-    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
-    [nc addObserver:self selector:@selector(handleComputerNameChanged:) 
name:BDSKComputerNameChangedNotification object:nil];
-    [nc addObserver:self selector:@selector(handleSharingStatusChanged:) 
name:BDSKSharingStatusChangedNotification object:nil];
-    [nc addObserver:self selector:@selector(handleSharingStatusChanged:) 
name:BDSKClientConnectionsChangedNotification object:nil];
-    
     [self handleComputerNameChanged:nil];
-    [self handleSharingStatusChanged:nil];
+    [self updateSharingUI];
     
+    [[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(handleComputerNameChanged:) 
name:BDSKComputerNameChangedNotification object:nil];
+    
     [self addObserverForUserDefaultsKey:BDSKShouldShareFilesKey];
+    
+    [[BDSKSharingServer sharedServer] addObserver:self forKeyPath:@"status" 
options:0 context:&BDSKSharingServerObservationContext];
+    [[BDSKSharingServer sharedServer] addObserver:self 
forKeyPath:@"numberOfConnections" options:0 
context:&BDSKSharingServerObservationContext];
 }
 
 - (NSString *)password {
@@ -96,38 +97,29 @@
     // reset UI, but only if we loaded the nib
     if ([self isViewLoaded]) {
         [passwordField setStringValue:@""];
-        [self handleSharingStatusChanged:nil];
+        [self updateSharingUI];
     }
 }
 
-- (void)handleComputerNameChanged:(NSNotification *)aNotification;
-{
-    [[sharedNameField cell] setPlaceholderString:[BDSKSharingServer 
defaultSharingName]];
-}
-
-- (void)handleSharingStatusChanged:(NSNotification *)aNotification;
-{
+- (void)updateSharingUI {
     BDSKSharingServer *server = [BDSKSharingServer sharedServer];
     NSString *statusMessage = nil;
     NSString *sharingName = nil;
     NSString *imageName = nil;
-    if ([sud boolForKey:BDSKShouldShareFilesKey]) {
-        NSUInteger number = [server numberOfConnections];
-        if ([server status] < BDSKSharingStatusPublishing) {
-            statusMessage = [NSString 
stringWithFormat:NSLocalizedString(@"Standby", @"Bonjour sharing is standby 
status message"), number];
-            imageName = NSImageNameStatusPartiallyAvailable;
-        } else {
-            if (number == 1)
-                statusMessage = NSLocalizedString(@"On, 1 user connected", 
@"Bonjour sharing is on status message, single connection");
-            else
-                statusMessage = [NSString 
stringWithFormat:NSLocalizedString(@"On, %lu users connected", @"Bonjour 
sharing is on status message, zero or multiple connections"), (unsigned 
long)number];
-            imageName = NSImageNameStatusAvailable;
-        }
-        if ([server status] >= BDSKSharingStatusPublishing)
-            sharingName = [server sharingName];
-    } else {
+    if ([sud boolForKey:BDSKShouldShareFilesKey] == NO) {
         statusMessage = NSLocalizedString(@"Off", @"Bonjour sharing is off 
status message");
         imageName = NSImageNameStatusUnavailable;
+    } else if ([server status] < BDSKSharingStatusPublishing) {
+        statusMessage = NSLocalizedString(@"Standby", @"Bonjour sharing is 
standby status message");
+        imageName = NSImageNameStatusPartiallyAvailable;
+    } else {
+        NSUInteger number = [server numberOfConnections];
+        if (number == 1)
+            statusMessage = NSLocalizedString(@"On, 1 user connected", 
@"Bonjour sharing is on status message, single connection");
+        else
+            statusMessage = [NSString stringWithFormat:NSLocalizedString(@"On, 
%lu users connected", @"Bonjour sharing is on status message, zero or multiple 
connections"), (unsigned long)number];
+        imageName = NSImageNameStatusAvailable;
+        sharingName = [server sharingName];
     }
     [statusField setStringValue:statusMessage];
     [statusImageView setImage:[NSImage imageNamed:imageName]];
@@ -134,8 +126,19 @@
     [usedNameField setStringValue:sharingName ?: @""];
 }
 
+- (void)handleComputerNameChanged:(NSNotification *)aNotification {
+    [[sharedNameField cell] setPlaceholderString:[BDSKSharingServer 
defaultSharingName]];
+}
+
 - (void)observeValueForUserDefaultsKey:(NSString *)key {
-    [self handleSharingStatusChanged:nil];
+    [self updateSharingUI];
 }
 
+- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object 
change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
+    if (context == &BDSKSharingServerObservationContext)
+        [self updateSharingUI];
+    else
+        [super observeValueForKeyPath:keyPath ofObject:object change:change 
context:context];
+}
+
 @end

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
Bibdesk-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to