Revision: 27943
          http://sourceforge.net/p/bibdesk/svn/27943
Author:   hofman
Date:     2022-09-27 14:08:48 +0000 (Tue, 27 Sep 2022)
Log Message:
-----------
Change accessors and KVO relations between accessors of mutable server info. 
Provate accessors for passwordInKeychain, instead of separate initializer.

Modified Paths:
--------------
    trunk/bibdesk/BDSKServerInfo.m

Modified: trunk/bibdesk/BDSKServerInfo.m
===================================================================
--- trunk/bibdesk/BDSKServerInfo.m      2022-09-27 09:40:56 UTC (rev 27942)
+++ trunk/bibdesk/BDSKServerInfo.m      2022-09-27 14:08:48 UTC (rev 27943)
@@ -69,9 +69,21 @@
 // Currently, anything other than zoom is expected to have just a type, name, 
and database.
 // Also when other validations are necessary, changing the type must make sure 
that the data validates properly for the new type, if necessary adding missing 
values.
 
+static inline BOOL isEqualOrBothNil(id object1, id object2) {
+    return (object1 == nil && object2 == nil) || [object1 isEqual:object2];
+}
+
+static inline BOOL isEqualOrBothEmpty(id object1, id object2) {
+    return ([object1 count] == 0 && [object2 count] == 0) || [object1 
isEqual:object2];
+}
+
+@interface BDSKServerInfo ()
+@property (nonatomic, getter=isPasswordInKeychain) BOOL passwordInKeychain;
+@end
+
 @implementation BDSKServerInfo
 
-@synthesize type, name, database;
+@synthesize type, name, database, passwordInKeychain;
 @dynamic dictionaryValue, host, port, password, username, recordSyntax, 
resultEncoding, queryConfig, removeDiacritics, lite, options, entrez, zoom, 
ISI, DBLP, serverType, URLValue;
 
 + (BOOL)accessInstanceVariablesDirectly { return NO; }
@@ -88,7 +100,7 @@
                                        options:isZoom ? [NSDictionary 
dictionary] : nil] autorelease];
 }
 
-- (id)initWithType:(NSString *)aType name:(NSString *)aName database:(NSString 
*)aDbase host:(NSString *)aHost port:(NSString *)aPort options:(NSDictionary 
*)opts passwordInKeychain:(BOOL)flag;
+- (id)initWithType:(NSString *)aType name:(NSString *)aName database:(NSString 
*)aDbase host:(NSString *)aHost port:(NSString *)aPort options:(NSDictionary 
*)opts;
 {
     self = [super init];
     if (self) {
@@ -95,7 +107,7 @@
         type = [aType copy];
         name = [aName copy];
         database = [aDbase copy];
-        passwordInKeychain = flag;
+        passwordInKeychain = NO;
         if ([self isEntrez] || [self isISI] || [self isDBLP]) {
             host = nil;
             port = nil;
@@ -112,18 +124,6 @@
     return self;
 }
 
-- (id)initWithType:(NSString *)aType name:(NSString *)aName database:(NSString 
*)aDbase host:(NSString *)aHost port:(NSString *)aPort options:(NSDictionary 
*)opts;
-{
-    self = [self initWithType:aType
-                         name:aName
-                     database:aDbase
-                         host:aHost
-                         port:aPort
-                      options:opts
-           passwordInKeychain:NO];
-    return self;
-}
-
 - (id)initWithDictionary:(NSDictionary *)info;
 {    
     self = [self initWithType:[info objectForKey:TYPE_KEY]
@@ -131,8 +131,7 @@
                      database:[info objectForKey:DATABASE_KEY]
                          host:[info objectForKey:HOST_KEY]
                          port:[info objectForKey:PORT_KEY]
-                      options:[info objectForKey:OPTIONS_KEY]
-           passwordInKeychain:NO];
+                      options:[info objectForKey:OPTIONS_KEY]];
     return self;
 }
 
@@ -142,8 +141,7 @@
                      database:[decoder decodeObjectForKey:DATABASE_KEY]
                          host:[decoder decodeObjectForKey:HOST_KEY]
                          port:[decoder decodeObjectForKey:PORT_KEY]
-                      options:[decoder decodeObjectForKey:OPTIONS_KEY]
-           passwordInKeychain:NO];
+                      options:[decoder decodeObjectForKey:OPTIONS_KEY]];
     return self;
 }
 
@@ -157,12 +155,14 @@
 }
 
 - (id)copyWithZone:(NSZone *)aZone {
-    id copy = [[BDSKServerInfo allocWithZone:aZone] initWithType:[self type] 
name:[self name] database:[self database] host:[self host] port:[self port] 
options:options passwordInKeychain:passwordInKeychain];
+    id copy = [[BDSKServerInfo allocWithZone:aZone] initWithType:[self type] 
name:[self name] database:[self database] host:[self host] port:[self port] 
options:options];
+    [copy setPasswordInKeychain:passwordInKeychain];
     return copy;
 }
 
 - (id)mutableCopyWithZone:(NSZone *)aZone {
-    id copy = [[BDSKMutableServerInfo allocWithZone:aZone] initWithType:[self 
type] name:[self name] database:[self database] host:[self host] port:[self 
port] options:options passwordInKeychain:passwordInKeychain];
+    id copy = [[BDSKMutableServerInfo allocWithZone:aZone] initWithType:[self 
type] name:[self name] database:[self database] host:[self host] port:[self 
port] options:options];
+    [copy setPasswordInKeychain:passwordInKeychain];
     return copy;
 }
 
@@ -176,14 +176,6 @@
     [super dealloc];
 }
 
-static inline BOOL isEqualOrBothNil(id object1, id object2) {
-    return (object1 == nil && object2 == nil) || [object1 isEqual:object2];
-}
-
-static inline BOOL isEqualOrBothEmpty(id object1, id object2) {
-    return ([object1 count] == 0 && [object2 count] == 0) || [object1 
isEqual:object2];
-}
-
 - (BOOL)isEqual:(id)other {
     BOOL isEqual = YES;
     // we don't compare the name, as that is just a label
@@ -381,16 +373,12 @@
 @dynamic type, name, database, host, port, password, username, recordSyntax, 
resultEncoding, removeDiacritics, lite, options;
 
 static NSSet *keysAffectedByType = nil;
-static NSSet *keysAffectedByOptions = nil;
 static NSSet *typeSet = nil;
-static NSSet *optionsSet = nil;
 
 + (void)initialize {
     BDSKINITIALIZE;
-    keysAffectedByType = [[NSSet alloc] initWithObjects:@"serverType", 
HOST_KEY, PORT_KEY, OPTIONS_KEY, nil];
-    keysAffectedByOptions = [[NSSet alloc] initWithObjects:PASSWORD_KEY, 
USERNAME_KEY, RECORDSYNTAX_KEY, RESULTENCODING_KEY, REMOVEDIACRITICS_KEY, 
LITE_KEY, nil];
+    keysAffectedByType = [[NSSet alloc] initWithObjects:@"serverType", 
HOST_KEY, PORT_KEY, PASSWORD_KEY, USERNAME_KEY, RECORDSYNTAX_KEY, 
RESULTENCODING_KEY, REMOVEDIACRITICS_KEY, LITE_KEY, nil];
     typeSet = [[NSSet alloc] initWithObjects:TYPE_KEY, nil];
-    optionsSet = [[NSSet alloc] initWithObjects:OPTIONS_KEY, nil];
 }
 
 + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key {
@@ -397,8 +385,6 @@
     NSSet *set = [super keyPathsForValuesAffectingValueForKey:key];
     if ([keysAffectedByType containsObject:key])
         return [set count] > 0 ? [set setByAddingObjectsFromSet:typeSet] : 
typeSet;
-    if ([keysAffectedByOptions containsObject:key])
-        return [set count] > 0 ? [set setByAddingObjectsFromSet:optionsSet] : 
optionsSet;
     return set;
 }
 
@@ -423,30 +409,38 @@
 
 - (void)setName:(NSString *)newName;
 {
-    [name autorelease];
-    name = [newName copy];
+    if (name != newName) {
+        [name release];
+        name = [newName copy];
+    }
 }
 
 - (void)setDatabase:(NSString *)newDbase;
 {
-    [database autorelease];
-    database = [newDbase copy];
+    if (database != newDbase) {
+        [database release];
+        database = [newDbase copy];
+    }
 }
 
 - (void)setHost:(NSString *)newHost;
 {
-    if (passwordInKeychain && isEqualOrBothNil(host, newHost) == NO)
-        passwordInKeychain = NO;
-    [host autorelease];
-    host = [newHost copy];
+    if (host != newHost) {
+        if (passwordInKeychain && isEqualOrBothNil(host, newHost) == NO)
+            passwordInKeychain = NO;
+        [host release];
+        host = [newHost copy];
+    }
 }
 
 - (void)setPort:(NSString *)newPort;
 {
-    if (passwordInKeychain && isEqualOrBothNil(port, newPort) == NO)
-        passwordInKeychain = NO;
-    [port autorelease];
-    port = [newPort copy];
+    if (port != newPort) {
+        if (passwordInKeychain && isEqualOrBothNil(port, newPort) == NO)
+            passwordInKeychain = NO;
+        [port release];
+        port = [newPort copy];
+    }
 }
 
 - (void)setOptionValue:(id)value forKey:(NSString *)key {
@@ -465,15 +459,23 @@
 
 - (void)setPassword:(NSString *)newPassword;
 {
+    if (passwordInKeychain && isEqualOrBothNil([options 
objectForKey:PASSWORD_KEY], newPassword) == NO)
+        passwordInKeychain = NO;
     [self setOptionValue:newPassword forKey:PASSWORD_KEY];
 }
 
 - (void)setUsername:(NSString *)newUser;
 {
-    if (passwordInKeychain && isEqualOrBothNil([self username], newUser) == NO)
+    if (isEqualOrBothNil([self username], newUser) == NO) {
         passwordInKeychain = NO;
-    [self setOptionValue:newUser forKey:USERNAME_KEY];
-}
+        BOOL passwordChanges = [options objectForKey:PASSWORD_KEY] == nil;
+        if (passwordChanges)
+            [self willChangeValueForKey:PASSWORD_KEY];
+        [self setOptionValue:newUser forKey:USERNAME_KEY];
+        if (passwordChanges)
+            [self didChangeValueForKey:PASSWORD_KEY];
+    }
+ }
 
 - (void)setRecordSyntax:(NSString *)newSyntax;
 {

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