Revision: 25898
          http://sourceforge.net/p/bibdesk/svn/25898
Author:   hofman
Date:     2021-05-07 16:14:23 +0000 (Fri, 07 May 2021)
Log Message:
-----------
Disable sharing parameters fields when not sharing. Combine sharing parameter 
change notifications. Bind sharing name field. Update status when sharing pref 
changes.

Modified Paths:
--------------
    trunk/bibdesk/BDSKSharingBrowser.m
    trunk/bibdesk/BDSKSharingServer.m
    trunk/bibdesk/BDSKStringConstants.h
    trunk/bibdesk/BDSKStringConstants.m
    trunk/bibdesk/Base.lproj/BibPref_Sharing.xib
    trunk/bibdesk/BibPref_Sharing.h
    trunk/bibdesk/BibPref_Sharing.m

Modified: trunk/bibdesk/BDSKSharingBrowser.m
===================================================================
--- trunk/bibdesk/BDSKSharingBrowser.m  2021-05-07 15:02:16 UTC (rev 25897)
+++ trunk/bibdesk/BDSKSharingBrowser.m  2021-05-07 16:14:23 UTC (rev 25898)
@@ -45,6 +45,8 @@
 
 #define BDSKEnableSharingWithSelfKey @"BDSKEnableSharingWithSelf"
 
+static char BDSKSharingBrowserDefaultsObservationContext;
+
 // Registered at http://www.dns-sd.org/ServiceTypes.html with TXT keys 
"txtvers" and "authenticate."
 NSString *BDSKNetServiceDomain = @"_bdsk._tcp.";
 
@@ -73,6 +75,7 @@
         browser = nil;
         unresolvedNetServices = nil;
         undecidedNetServices = nil;
+        [[NSUserDefaultsController sharedUserDefaultsController] 
addObserver:self forKeyPath:[@"values." 
stringByAppendingString:BDSKShouldLookForSharedFilesKey] options:0 
context:&BDSKSharingBrowserDefaultsObservationContext];
     }
     return self;
 }
@@ -229,4 +232,15 @@
     }
 }
 
+- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object 
change:(NSDictionary *)change context:(void *)context {
+    if (context == &BDSKSharingBrowserDefaultsObservationContext) {
+        if ([[NSUserDefaults standardUserDefaults] 
boolForKey:BDSKShouldLookForSharedFilesKey])
+            [self enableSharedBrowsing];
+        else
+            [self disableSharedBrowsing];
+    } else {
+        [super observeValueForKeyPath:keyPath ofObject:object change:change 
context:context];
+    }
+}
+
 @end

Modified: trunk/bibdesk/BDSKSharingServer.m
===================================================================
--- trunk/bibdesk/BDSKSharingServer.m   2021-05-07 15:02:16 UTC (rev 25897)
+++ trunk/bibdesk/BDSKSharingServer.m   2021-05-07 16:14:23 UTC (rev 25898)
@@ -61,6 +61,8 @@
 #define BDSKDisableRemoteChangeNotificationsKey 
@"BDSKDisableRemoteChangeNotifications"
 #define BDSKSharingServerMaxConnectionsKey @"BDSKSharingServerMaxConnections"
 
+static char BDSKSharingServerDefaultsObservationContext;
+
 static id sharedInstance = nil;
 
 // TXT record keys
@@ -210,6 +212,14 @@
     return sharedInstance;
 }
 
+- (id)init {
+    self = [super init];
+    if (self) {
+        [[NSUserDefaultsController sharedUserDefaultsController] 
addObserver:self forKeyPath:[@"values." 
stringByAppendingString:BDSKShouldShareFilesKey] options:0 
context:&BDSKSharingServerDefaultsObservationContext];
+    }
+    return self;
+}
+
 - (void)setStatus:(BDSKSharingStatus)newStatus {
     if (status != newStatus) {
         status = newStatus;
@@ -355,8 +365,7 @@
         NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
         
         [nc removeObserver:self name:BDSKComputerNameChangedNotification 
object:nil];
-        [nc removeObserver:self name:BDSKSharingNameChangedNotification 
object:nil];
-        [nc removeObserver:self name:BDSKSharingPasswordChangedNotification 
object:nil];
+        [nc removeObserver:self name:BDSKSharingParametersChangedNotification 
object:nil];
         [nc removeObserver:self 
name:BDSKDocumentControllerAddDocumentNotification object:nil];
         [nc removeObserver:self 
name:BDSKDocumentControllerRemoveDocumentNotification object:nil];              
                                         
         [nc removeObserver:self name:BDSKDocumentDidAddItemNotification 
object:nil];
@@ -403,15 +412,10 @@
             
             [nc addObserver:self
                    
selector:@selector(handleSharingParametersChangedNotification:)
-                       name:BDSKSharingNameChangedNotification
+                       name:BDSKSharingParametersChangedNotification
                      object:nil];
             
             [nc addObserver:self
-                   
selector:@selector(handleSharingParametersChangedNotification:)
-                       name:BDSKSharingPasswordChangedNotification
-                     object:nil];
-            
-            [nc addObserver:self
                    selector:@selector(queueDataChangedNotification:)
                        name:BDSKDocumentControllerAddDocumentNotification
                      object:nil];
@@ -526,6 +530,17 @@
     [self disableSharing];
 }
 
+- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object 
change:(NSDictionary *)change context:(void *)context {
+    if (context == &BDSKSharingServerDefaultsObservationContext) {
+        if ([[NSUserDefaults standardUserDefaults] 
boolForKey:BDSKShouldShareFilesKey])
+            [self enableSharing];
+        else
+            [self disableSharing];
+    } else {
+        [super observeValueForKeyPath:keyPath ofObject:object change:change 
context:context];
+    }
+}
+
 @end
 
 #pragma mark -

Modified: trunk/bibdesk/BDSKStringConstants.h
===================================================================
--- trunk/bibdesk/BDSKStringConstants.h 2021-05-07 15:02:16 UTC (rev 25897)
+++ trunk/bibdesk/BDSKStringConstants.h 2021-05-07 16:14:23 UTC (rev 25898)
@@ -317,8 +317,7 @@
 extern NSString *BDSKWillRemoveGroupsNotification;
 extern NSString *BDSKClientConnectionsChangedNotification;
 extern NSString *BDSKSharingStatusChangedNotification;
-extern NSString *BDSKSharingNameChangedNotification;
-extern NSString *BDSKSharingPasswordChangedNotification;
+extern NSString *BDSKSharingParametersChangedNotification;
 extern NSString *BDSKDocumentControllerAddDocumentNotification;
 extern NSString *BDSKDocumentControllerRemoveDocumentNotification;
 extern NSString *BDSKDocumentControllerDidChangeMainDocumentNotification;

Modified: trunk/bibdesk/BDSKStringConstants.m
===================================================================
--- trunk/bibdesk/BDSKStringConstants.m 2021-05-07 15:02:16 UTC (rev 25897)
+++ trunk/bibdesk/BDSKStringConstants.m 2021-05-07 16:14:23 UTC (rev 25898)
@@ -321,7 +321,6 @@
 NSString *BDSKStaticGroupChangedNotification = 
@"BDSKStaticGroupChangedNotification";
 NSString *BDSKSharingClientsChangedNotification = 
@"BDSKSharingClientsChangedNotification";
 NSString *BDSKSharingClientUpdatedNotification = 
@"BDSKSharingClientUpdatedNotification";
-NSString *BDSKSharingNameChangedNotification = 
@"BDSKSharingNameChangedNotification";
 NSString *BDSKExternalGroupUpdatedNotification = 
@"BDSKExternalGroupUpdatedNotification";
 NSString *BDSKExternalGroupWillRemoveItemNotification = 
@"BDSKExternalGroupWillRemoveItemNotification";
 NSString *BDSKDidAddRemoveGroupNotification = 
@"BDSKDidAddRemoveGroupNotification";
@@ -328,7 +327,7 @@
 NSString *BDSKWillRemoveGroupsNotification = 
@"BDSKWillRemoveGroupsNotification";
 NSString *BDSKClientConnectionsChangedNotification = 
@"BDSKClientConnectionsChangedNotification";
 NSString *BDSKSharingStatusChangedNotification = 
@"BDSKSharingStatusChangedNotification";
-NSString *BDSKSharingPasswordChangedNotification = 
@"BDSKSharingPasswordChangedNotification";
+NSString *BDSKSharingParametersChangedNotification = 
@"BDSKSharingParametersChangedNotification";
 NSString *BDSKDocumentControllerAddDocumentNotification = 
@"BDSKDocumentControllerAddDocumentNotification";
 NSString *BDSKDocumentControllerRemoveDocumentNotification = 
@"BDSKDocumentControllerRemoveDocumentNotification";
 NSString *BDSKDocumentControllerDidChangeMainDocumentNotification = 
@"BDSKDocumentControllerDidChangeMainDocumentNotification";

Modified: trunk/bibdesk/Base.lproj/BibPref_Sharing.xib
===================================================================
--- trunk/bibdesk/Base.lproj/BibPref_Sharing.xib        2021-05-07 15:02:16 UTC 
(rev 25897)
+++ trunk/bibdesk/Base.lproj/BibPref_Sharing.xib        2021-05-07 16:14:23 UTC 
(rev 25898)
@@ -70,6 +70,7 @@
                         <font key="font" metaFont="system"/>
                     </buttonCell>
                     <connections>
+                        <binding destination="XpF-7e-rBu" name="enabled" 
keyPath="values.BDSKShouldShareFilesKey" id="NjX-jC-xcT"/>
                         <binding destination="XpF-7e-rBu" name="value" 
keyPath="values.BDSKBrowsingRequiresPasswordKey" id="40o-cM-YNI"/>
                     </connections>
                 </button>
@@ -91,6 +92,14 @@
                     <connections>
                         <accessibilityConnection property="title" 
destination="12" id="35"/>
                         <action selector="changePassword:" target="-2" 
id="20"/>
+                        <binding destination="XpF-7e-rBu" name="enabled2" 
keyPath="values.BDSKShouldShareFilesKey" previousBinding="xZg-69-zsA" 
id="jLV-5Y-b8y">
+                            <dictionary key="options">
+                                <integer key="NSMultipleValuesPlaceholder" 
value="-1"/>
+                                <integer key="NSNoSelectionPlaceholder" 
value="-1"/>
+                                <integer key="NSNotApplicablePlaceholder" 
value="-1"/>
+                                <integer key="NSNullPlaceholder" value="-1"/>
+                            </dictionary>
+                        </binding>
                         <binding destination="XpF-7e-rBu" name="enabled" 
keyPath="values.BDSKBrowsingRequiresPasswordKey" id="xZg-69-zsA"/>
                         <outlet property="nextKeyView" destination="6" 
id="32"/>
                     </connections>
@@ -104,7 +113,7 @@
                     </textFieldCell>
                     <connections>
                         <accessibilityConnection property="title" 
destination="11" id="34"/>
-                        <action selector="changeSharedName:" target="-2" 
id="21"/>
+                        <binding destination="XpF-7e-rBu" name="value" 
keyPath="values.BDSKSharingNameKey" id="1WJ-WY-7Yg"/>
                         <binding destination="XpF-7e-rBu" name="enabled" 
keyPath="values.BDSKShouldShareFilesKey" id="XlR-C3-jUE"/>
                         <outlet property="nextKeyView" destination="9" 
id="31"/>
                     </connections>

Modified: trunk/bibdesk/BibPref_Sharing.h
===================================================================
--- trunk/bibdesk/BibPref_Sharing.h     2021-05-07 15:02:16 UTC (rev 25897)
+++ trunk/bibdesk/BibPref_Sharing.h     2021-05-07 16:14:23 UTC (rev 25898)
@@ -63,6 +63,5 @@
 @property (nonatomic, assign) IBOutlet NSImageView *statusImageView;
 
 - (IBAction)changePassword:(id)sender;
-- (IBAction)changeSharedName:(id)sender;
 
 @end

Modified: trunk/bibdesk/BibPref_Sharing.m
===================================================================
--- trunk/bibdesk/BibPref_Sharing.m     2021-05-07 15:02:16 UTC (rev 25897)
+++ trunk/bibdesk/BibPref_Sharing.m     2021-05-07 16:14:23 UTC (rev 25898)
@@ -46,9 +46,8 @@
 
 @interface BibPref_Sharing (Private)
 - (void)updateStatusUI;
-- (void)handleSharingNameChanged:(NSNotification *)aNotification;
+- (void)handleComputerNameChanged:(NSNotification *)aNotification;
 - (void)handleSharingStatusChanged:(NSNotification *)aNotification;
-- (void)handleClientConnectionsChanged:(NSNotification *)aNotification;
 @end
 
 
@@ -59,21 +58,18 @@
 - (void)loadView {
     [super loadView];
     
-    [[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(handleSharingNameChanged:) 
name:BDSKComputerNameChangedNotification object:nil];
-    [[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(handleSharingNameChanged:) 
name:BDSKSharingNameChangedNotification object:nil];
-    [[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(handleSharingStatusChanged:) 
name:BDSKSharingStatusChangedNotification object:nil];
-    [[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(handleClientConnectionsChanged:) 
name:BDSKClientConnectionsChangedNotification object:nil];
+    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];
     
-    [enableSharingButton setState:[sud boolForKey:BDSKShouldShareFilesKey] ? 
NSOnState : NSOffState];
-    [enableBrowsingButton setState:[sud 
boolForKey:BDSKShouldLookForSharedFilesKey] ? NSOnState : NSOffState];
-    [usePasswordButton setState:[sud 
boolForKey:BDSKSharingRequiresPasswordKey] ? NSOnState : NSOffState];
+    [[sharedNameField cell] setPlaceholderString:[BDSKSharingServer 
defaultSharingName]];
     
-    [self updateNameUI];
     [self updateStatusUI];
     
+    [self addObserverForUserDefaultsKey:BDSKShouldShareFilesKey];
+    [self addObserverForUserDefaultsKey:BDSKSharingNameKey];
     [self addObserverForUserDefaultsKey:BDSKSharingRequiresPasswordKey];
-    [self addObserverForUserDefaultsKey:BDSKShouldLookForSharedFilesKey];
-    [self addObserverForUserDefaultsKey:BDSKShouldLookForSharedFilesKey];
 }
 
 - (void)willSelect {
@@ -91,12 +87,10 @@
 - (void)defaultsDidRevert {
     // always clear the password, as that's not set in our prefs, and always 
send the notifications
     [BDSKPasswordController addOrModifyPassword:@"" 
forKeychainService:BDSKServiceNameForKeychain account:nil name:nil];
-    [[NSNotificationCenter defaultCenter] 
postNotificationName:BDSKSharingPasswordChangedNotification object:nil];
-    [[NSNotificationCenter defaultCenter] 
postNotificationName:BDSKSharingNameChangedNotification object:self];
+    [[NSNotificationCenter defaultCenter] 
postNotificationName:BDSKSharingParametersChangedNotification object:nil];
     // reset UI, but only if we loaded the nib
     if ([self isViewLoaded]) {
         [passwordField setStringValue:@""];
-        [self updateNameUI];
         [self updateStatusUI];
     }
 }
@@ -107,9 +101,9 @@
     [super dealloc];
 }
 
-- (void)handleSharingNameChanged:(NSNotification *)aNotification;
+- (void)handleComputerNameChanged:(NSNotification *)aNotification;
 {
-    [self updateNameUI];
+    [[sharedNameField cell] setPlaceholderString:[BDSKSharingServer 
defaultSharingName]];
 }
 
 - (void)handleSharingStatusChanged:(NSNotification *)aNotification;
@@ -117,17 +111,6 @@
     [self updateStatusUI];
 }
 
-- (void)handleClientConnectionsChanged:(NSNotification *)aNotification;
-{
-    [self updateStatusUI];
-}
-
-- (void)updateNameUI
-{
-    [[sharedNameField cell] setPlaceholderString:[BDSKSharingServer 
defaultSharingName]];
-    [sharedNameField setStringValue:[sud stringForKey:BDSKSharingNameKey]];
-}
-
 - (void)updateStatusUI
 {
     BDSKSharingServer *server = [BDSKSharingServer defaultServer];
@@ -160,37 +143,14 @@
 - (IBAction)changePassword:(id)sender
 {
     if ([BDSKPasswordController addOrModifyPassword:[sender stringValue] 
forKeychainService:BDSKServiceNameForKeychain account:nil name:nil])
-        [[NSNotificationCenter defaultCenter] 
postNotificationName:BDSKSharingPasswordChangedNotification object:nil];
+        [[NSNotificationCenter defaultCenter] 
postNotificationName:BDSKSharingParametersChangedNotification object:nil];
 }
 
-// setting to the empty string will restore the default
-- (IBAction)changeSharedName:(id)sender
-{
-    NSString *oldName = [sud stringForKey:BDSKSharingNameKey] ?: @"";
-    NSString *newName = [sender stringValue] ?: @"";
-    if ([oldName isEqualToString:newName] == NO) {
-        [sud setObject:newName forKey:BDSKSharingNameKey];
-        [[NSNotificationCenter defaultCenter] 
postNotificationName:BDSKSharingNameChangedNotification object:self];
-    }
-}
-
 - (void)observeValueForUserDefaultsKey:(NSString *)key {
-    if ([key isEqualToString:BDSKSharingRequiresPasswordKey]) {
-        [[NSNotificationCenter defaultCenter] 
postNotificationName:BDSKSharingPasswordChangedNotification object:nil];
-    } else if ([key isEqualToString:BDSKShouldLookForSharedFilesKey]) {
-        if ([sud boolForKey:BDSKShouldLookForSharedFilesKey])
-            [[BDSKSharingBrowser sharedBrowser] enableSharedBrowsing];
-        else
-            [[BDSKSharingBrowser sharedBrowser] disableSharedBrowsing];
-        if ([self isViewLoaded])
-            [self updateStatusUI];
-    } else if ([key isEqualToString:BDSKShouldShareFilesKey]) {
-        if ([sud boolForKey:BDSKShouldShareFilesKey])
-            [[BDSKSharingServer defaultServer] enableSharing];
-        else
-            [[BDSKSharingServer defaultServer] disableSharing];
+    if ([key isEqualToString:BDSKSharingRequiresPasswordKey] || [key 
isEqualToString:BDSKSharingNameKey])
+        [[NSNotificationCenter defaultCenter] 
postNotificationName:BDSKSharingParametersChangedNotification object:nil];
+    else if ([key isEqualToString:BDSKShouldShareFilesKey])
         [self updateStatusUI];
-    }
 }
 
 @end

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



_______________________________________________
Bibdesk-commit mailing list
Bibdesk-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to