Author: rfm
Date: Wed Nov 18 18:28:59 2015
New Revision: 39181

URL: http://svn.gna.org/viewcvs/gnustep?rev=39181&view=rev
Log:
Allow default registration to specify a fallback value

Modified:
    libs/ec/trunk/EcProcess.h
    libs/ec/trunk/EcProcess.m

Modified: libs/ec/trunk/EcProcess.h
URL: 
http://svn.gna.org/viewcvs/gnustep/libs/ec/trunk/EcProcess.h?rev=39181&r1=39180&r2=39181&view=diff
==============================================================================
--- libs/ec/trunk/EcProcess.h   (original)
+++ libs/ec/trunk/EcProcess.h   Wed Nov 18 18:28:59 2015
@@ -434,6 +434,9 @@
  * The action may either be NULL or a selector for a message to be sent
  * to the EcProc instance with a single argument (the new default value)
  * when the value of the user default changes.<br />
+ * The value may either be NULL, or be an object to be set in the registration
+ * domain of the defaults system (as long as this method is called before
+ * the EcProcess instance is initialized).<br />
  * If the same default name is registered more than once, the values
  * from the last registration are used, except for the case where the
  * cmd argument is NULL, in that case the previous selector is kept
@@ -448,7 +451,8 @@
 + (void) ecRegisterDefault: (NSString*)name
               withTypeText: (NSString*)type
                andHelpText: (NSString*)help
-                    action: (SEL)cmd;
+                    action: (SEL)cmd
+                     value: (id)value;
 
 /** Convenience method to create the singleton EcProcess instance
  * using the initial configuration provided by the +ecInitialDefaults

Modified: libs/ec/trunk/EcProcess.m
URL: 
http://svn.gna.org/viewcvs/gnustep/libs/ec/trunk/EcProcess.m?rev=39181&r1=39180&r2=39181&view=diff
==============================================================================
--- libs/ec/trunk/EcProcess.m   (original)
+++ libs/ec/trunk/EcProcess.m   Wed Nov 18 18:28:59 2015
@@ -124,13 +124,16 @@
   NSString      *help;          // The help text for the default
   SEL           cmd;            // method to update when default values change
   id            obj;            // The latest value of the default
+  id            val;            // The fallback value of the default
 }
 + (void) defaultsChanged: (NSUserDefaults*)defs;
 + (NSMutableString*) listHelp;
++ (NSDictionary*) merge: (NSDictionary*)d;
 + (void) registerDefault: (NSString*)name
             withTypeText: (NSString*)type
              andHelpText: (NSString*)help
-                  action: (SEL)cmd;
+                  action: (SEL)cmd
+                   value: (id)value;
 + (void) showHelp;
 @end
 
@@ -1155,7 +1158,21 @@
   [EcDefaultRegistration registerDefault: name
                             withTypeText: type
                              andHelpText: help
-                                  action: cmd];
+                                  action: cmd
+                                   value: nil];
+}
+
++ (void) ecRegisterDefault: (NSString*)name
+              withTypeText: (NSString*)type
+               andHelpText: (NSString*)help
+                    action: (SEL)cmd
+                     value: (id)value
+{
+  [EcDefaultRegistration registerDefault: name
+                            withTypeText: type
+                             andHelpText: help
+                                  action: cmd
+                                   value: value];
 }
 
 + (void) ecSetup
@@ -3819,6 +3836,7 @@
       ASSIGN(cmdDefs, [NSUserDefaults
        userDefaultsWithPrefix: prf
        strict: EC_DEFAULTS_STRICT]);
+      defs = [EcDefaultRegistration merge: defs];
       if (defs != nil)
        {
          [cmdDefs registerDefaults: defs];
@@ -5240,12 +5258,16 @@
       prf = @"";
     }
 
+  [ecLock lock];
   keys = [regDefs allKeys];
+  [ecLock unlock];
   e = [keys objectEnumerator];
   while (nil != (k = [e nextObject]))
     {
-      EcDefaultRegistration     *d = [regDefs objectForKey: k];
-
+      EcDefaultRegistration     *d;
+
+      [ecLock lock];
+      d = [regDefs objectForKey: k];
       if (nil != d->type && nil != d->help)
         {
           NSUInteger    length = [prf length] + 5;
@@ -5256,14 +5278,17 @@
               max = length;
             }
         }
+      [ecLock unlock];
     }
 
   keys = [keys sortedArrayUsingSelector: @selector(compare:)];
   e = [keys objectEnumerator];
   while (nil != (k = [e nextObject]))
     {
-      EcDefaultRegistration     *d = [regDefs objectForKey: k];
-
+      EcDefaultRegistration     *d;
+
+      [ecLock lock];
+      d = [regDefs objectForKey: k];
       if (nil != d->type && nil != d->help)
         {
           /* If the help text is short enough, put it all on one line.
@@ -5286,14 +5311,41 @@
                 prf, k, d->type, d->help];
             }
         }
+      [ecLock unlock];
     }
   return out;
+}
+
++ (NSDictionary*) merge: (NSDictionary*)d
+{
+  NSMutableDictionary   *m = AUTORELEASE([d mutableCopy]);
+  NSEnumerator          *e;
+  NSString              *k;
+
+  if (nil == m)
+    {
+      m = [NSMutableDictionary dictionaryWithCapacity: [regDefs count]];
+    }
+  [ecLock lock];
+  e = [regDefs keyEnumerator];
+  while (nil != (k = [e nextObject]))
+    {
+      EcDefaultRegistration     *r = [regDefs objectForKey: k];
+
+      if (nil != r->val && nil == [d objectForKey: k])
+        {
+          [m setObject: r->val forKey: k];
+        }
+    }
+  [ecLock unlock];
+  return m;
 }
 
 + (void) registerDefault: (NSString*)name
             withTypeText: (NSString*)type
              andHelpText: (NSString*)help
                   action: (SEL)cmd
+                   value: (id)value
 {
   static NSCharacterSet *w = nil;
   EcDefaultRegistration *d;
@@ -5355,6 +5407,7 @@
     {
       d->cmd = cmd;
     }
+  ASSIGNCOPY(d->val, value);
   [ecLock unlock];
 }
 


_______________________________________________
Gnustep-cvs mailing list
[email protected]
https://mail.gna.org/listinfo/gnustep-cvs

Reply via email to