home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Mail / mailapp-utilities-2.1-MIHS / Source / compat.m < prev    next >
Encoding:
Text File  |  1998-02-15  |  2.3 KB  |  92 lines

  1. /*+++*
  2.  *  title:    compat.m
  3.  *  abstract:    OPENSTEP/Rhapsody compatibility routines for mailapp-utilities. 
  4.  *  author:    Tom Hageman <tom@basil.icce.rug.nl>
  5.  *  created:    January 1998
  6.  *  modified:    (see RCS Log at end)
  7.  *  copyleft:
  8.  *
  9.  *    Copyright (C) 1998 Tom R. Hageman, but otherwise perfect freeware.
  10.  *
  11.  *  description:
  12.  *
  13.  *---*/
  14.  
  15. static const char * const RCSid = ((void)&RCSid,
  16.     "@(#)compat.m,v 1.4 1998/02/15 17:03:18");
  17. #define RCS_compat_ID
  18.  
  19. #import "compat.h"
  20.  
  21. #define NSSTRING(s)  [NSString stringWithCString:(s)]
  22.  
  23. #if (OPENSTEP || RHAPSODY)
  24.  
  25. #import <Foundation/Foundation.h>
  26.  
  27. #if RHAPSODY
  28.  
  29. // {{OPENSTEP 4.2 still has NXGetDefault() in System.framework}}
  30.  
  31. @interface NSUserDefaults (OtherDomains)
  32. + (NSUserDefaults *)userDefaultsForDomainNamed:(NSString *)aDomain;
  33. @end
  34.  
  35. static void setupDefaults(NSUserDefaults *defaults, NSString *aDomain)
  36. {
  37.    NSMutableArray *searchList = [NSMutableArray arrayWithArray:[defaults searchList]];
  38.  
  39.    [searchList insertObject:aDomain atIndex:0];
  40.    [defaults setSearchList:searchList];
  41. }
  42.  
  43. @implementation NSUserDefaults (OtherDomains)
  44. + (NSUserDefaults *)userDefaultsForDomainNamed:(NSString *)aDomain
  45. {
  46.    static NSMutableDictionary *defaultsByDomain = nil;
  47.    NSUserDefaults *defaults;
  48.  
  49.    if ((defaults = [defaultsByDomain objectForKey:aDomain]) == nil)
  50.    {
  51.       defaults = [[NSUserDefaults alloc] init];
  52.       setupDefaults(defaults, aDomain);
  53.       if (defaultsByDomain == nil)
  54.       {
  55.      defaultsByDomain = [[NSMutableDictionary alloc] initWithCapacity:1];
  56.       }
  57.       [defaultsByDomain setObject:defaults forKey:aDomain];
  58.       [defaults release]; // retained by dictionary.
  59.    }
  60.    return defaults;
  61. }
  62. @end
  63.  
  64. static NSUserDefaults *userDefaultsForOwner(NSString *owner)
  65. {
  66.    return [NSUserDefaults userDefaultsForDomainNamed:owner];
  67. }
  68.  
  69. extern const char *NXGetDefaultValue(const char *owner, const char *name)
  70. {
  71.    if (strcmp(owner, "GLOBAL") == 0) owner = "NSGlobalDomain"; // Cheat.
  72.  
  73.    return [[[userDefaultsForOwner(NSSTRING(owner))
  74.          objectForKey:NSSTRING(name)] description] cString];
  75.    /* {{Note this is not the same as -stringForKey, which returns non-nil
  76.       only if value is a string, and nil for array/dictionary objects.}} */
  77. }
  78.  
  79. #endif // RHAPSODY
  80.  
  81. const char *NXHomeDirectory()
  82. {
  83.    return [NSHomeDirectory() cString];
  84. }
  85.  
  86. const char *NXUserName()
  87. {
  88.    return [NSUserName() cString];
  89. }
  90.  
  91. #endif // (OPENSTEP || RHAPSODY)
  92.