2007-05-25 19:30:24 +02:00
|
|
|
/* NSDictionary+Utilities.m - this file is part of SOGo
|
|
|
|
*
|
2011-05-10 20:07:05 +02:00
|
|
|
* Copyright (C) 2007-2011 Inverse inc.
|
2007-05-25 19:30:24 +02:00
|
|
|
*
|
|
|
|
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
|
|
|
|
*
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <Foundation/NSArray.h>
|
2007-09-15 00:01:02 +02:00
|
|
|
#import <Foundation/NSData.h>
|
2008-07-04 18:06:09 +02:00
|
|
|
#import <Foundation/NSEnumerator.h>
|
2008-07-12 00:28:12 +02:00
|
|
|
#import <Foundation/NSException.h>
|
2007-10-23 23:59:46 +02:00
|
|
|
#import <Foundation/NSNull.h>
|
2007-05-25 19:30:24 +02:00
|
|
|
#import <Foundation/NSString.h>
|
2009-09-09 15:31:45 +02:00
|
|
|
#import <Foundation/NSCharacterSet.h>
|
|
|
|
|
|
|
|
#import <NGExtensions/NGBase64Coding.h>
|
2007-05-25 19:30:24 +02:00
|
|
|
|
2007-08-15 21:48:35 +02:00
|
|
|
#import "NSArray+Utilities.h"
|
2007-05-25 19:30:24 +02:00
|
|
|
#import "NSObject+Utilities.h"
|
2009-11-04 17:26:22 +01:00
|
|
|
#import "NSString+Utilities.h"
|
|
|
|
|
2007-05-25 19:30:24 +02:00
|
|
|
#import "NSDictionary+Utilities.h"
|
|
|
|
|
|
|
|
@implementation NSDictionary (SOGoDictionaryUtilities)
|
|
|
|
|
2007-09-15 00:01:02 +02:00
|
|
|
+ (NSDictionary *) dictionaryFromStringsFile: (NSString *) file
|
|
|
|
{
|
|
|
|
NSString *serialized;
|
|
|
|
NSMutableData *content;
|
|
|
|
NSDictionary *newDictionary;
|
|
|
|
|
2009-11-04 17:26:22 +01:00
|
|
|
content = [NSMutableData data];
|
2007-09-15 00:01:02 +02:00
|
|
|
[content appendBytes: "{" length: 1];
|
|
|
|
[content appendData: [NSData dataWithContentsOfFile: file]];
|
|
|
|
[content appendBytes: "}" length: 1];
|
|
|
|
serialized = [[NSString alloc] initWithData: content
|
|
|
|
encoding: NSUTF8StringEncoding];
|
2009-11-04 17:26:22 +01:00
|
|
|
[serialized autorelease];
|
2007-09-15 00:01:02 +02:00
|
|
|
newDictionary = [serialized propertyList];
|
|
|
|
|
|
|
|
return newDictionary;
|
|
|
|
}
|
|
|
|
|
2007-05-25 19:30:24 +02:00
|
|
|
- (NSString *) jsonRepresentation
|
|
|
|
{
|
|
|
|
NSMutableArray *values;
|
|
|
|
NSString *representation, *currentKey, *currentValue, *currentPair;
|
|
|
|
NSEnumerator *keys;
|
|
|
|
|
2009-11-04 17:26:22 +01:00
|
|
|
values = [NSMutableArray array];
|
2007-05-25 19:30:24 +02:00
|
|
|
keys = [[self allKeys] objectEnumerator];
|
2009-11-04 17:26:22 +01:00
|
|
|
while ((currentKey = [keys nextObject]))
|
2007-05-25 19:30:24 +02:00
|
|
|
{
|
|
|
|
currentValue = [[self objectForKey: currentKey] jsonRepresentation];
|
|
|
|
currentPair = [NSString stringWithFormat: @"%@: %@",
|
|
|
|
[currentKey jsonRepresentation], currentValue];
|
|
|
|
[values addObject: currentPair];
|
|
|
|
}
|
|
|
|
representation = [NSString stringWithFormat: @"{%@}",
|
|
|
|
[values componentsJoinedByString: @", "]];
|
|
|
|
|
|
|
|
return representation;
|
|
|
|
}
|
|
|
|
|
2007-08-15 21:48:35 +02:00
|
|
|
- (NSString *) keysWithFormat: (NSString *) keyFormat
|
|
|
|
{
|
|
|
|
NSArray *keys, *allKeys;
|
|
|
|
unsigned int count, max;
|
|
|
|
NSMutableString *keysWithFormat;
|
|
|
|
id value;
|
|
|
|
|
|
|
|
keysWithFormat = [NSMutableString stringWithString: keyFormat];
|
|
|
|
|
|
|
|
allKeys = [self allKeys];
|
|
|
|
keys = [allKeys stringsWithFormat: @"%{%@}"];
|
|
|
|
|
|
|
|
max = [allKeys count];
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
{
|
|
|
|
value = [self objectForKey: [allKeys objectAtIndex: count]];
|
2007-10-23 23:59:46 +02:00
|
|
|
if ([value isKindOfClass: [NSNull class]])
|
|
|
|
[keysWithFormat replaceString: [keys objectAtIndex: count]
|
|
|
|
withString: @""];
|
|
|
|
else
|
|
|
|
[keysWithFormat replaceString: [keys objectAtIndex: count]
|
|
|
|
withString: [value description]];
|
2007-08-15 21:48:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return keysWithFormat;
|
|
|
|
}
|
|
|
|
|
2008-11-24 22:25:42 +01:00
|
|
|
- (NSComparisonResult) caseInsensitiveDisplayNameCompare: (NSDictionary *) theDictionary
|
|
|
|
{
|
|
|
|
return [[self objectForKey: @"cn"] caseInsensitiveCompare: [theDictionary objectForKey: @"cn"]];
|
|
|
|
}
|
|
|
|
|
2009-09-09 15:31:45 +02:00
|
|
|
// LDIF Methods
|
|
|
|
- (void) _appendLDIFKey: (NSString *) key
|
|
|
|
value: (NSString *) value
|
|
|
|
toString: (NSMutableString *) ldifString
|
|
|
|
{
|
2011-05-10 20:33:46 +02:00
|
|
|
if ([value isKindOfClass: [NSString class]])
|
|
|
|
{
|
|
|
|
if ([value _isLDIFSafe])
|
|
|
|
[ldifString appendFormat: @"%@: %@\n", key, value];
|
|
|
|
else
|
|
|
|
[ldifString appendFormat: @"%@:: %@\n",
|
|
|
|
key, [value stringByEncodingBase64]];
|
|
|
|
}
|
2009-09-09 15:31:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) _appendLDIFKey: (NSString *) key
|
|
|
|
toString: (NSMutableString *) ldifString
|
|
|
|
{
|
|
|
|
id value;
|
|
|
|
int count, max;
|
|
|
|
|
|
|
|
value = [self objectForKey: key];
|
|
|
|
if ([value isKindOfClass: [NSArray class]])
|
|
|
|
{
|
|
|
|
max = [value count];
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
[self _appendLDIFKey: key value: [value objectAtIndex: count]
|
|
|
|
toString: ldifString];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
[self _appendLDIFKey: key value: [self objectForKey: key]
|
|
|
|
toString: ldifString];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) _appendObjectClassesToString: (NSMutableString *) ldifString
|
|
|
|
{
|
|
|
|
NSEnumerator *classes;
|
|
|
|
NSString *currentClass;
|
|
|
|
|
|
|
|
classes = [[self objectForKey: @"objectClasses"] objectEnumerator];
|
|
|
|
while ((currentClass = [classes nextObject]))
|
|
|
|
[self _appendLDIFKey: @"objectClass" value: currentClass
|
|
|
|
toString: ldifString];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) userRecordAsLDIFEntry
|
|
|
|
{
|
|
|
|
NSMutableString *ldifString;
|
|
|
|
NSEnumerator *keys;
|
|
|
|
NSString *currentKey;
|
|
|
|
|
|
|
|
// {CalendarAccess = YES; MailAccess = YES; c_cn = "Wolfgang Sourdeau"; c_emails = ("wolfgang@test.com"); c_name = "wolfgang@test.com"; c_uid = "wolfgang@test.com"; cn = "wolfgang@test.com"; displayName = "Wolfgang Sourdeau"; dn = "cn=wolfgang@test.com,ou=evariste,o=inverse.ca"; givenName = Wolfgang; mail = "wolfgang@test.com"; objectClass = organizationalPerson; sn = Sourdeau; }
|
|
|
|
|
|
|
|
ldifString = [NSMutableString string];
|
|
|
|
[self _appendLDIFKey: @"dn" toString: ldifString];
|
|
|
|
[self _appendObjectClassesToString: ldifString];
|
|
|
|
|
|
|
|
keys = [[self allKeys] objectEnumerator];
|
|
|
|
while ((currentKey = [keys nextObject]))
|
|
|
|
{
|
|
|
|
if (!([currentKey isEqualToString: @"CalendarAccess"]
|
|
|
|
|| [currentKey isEqualToString: @"MailAccess"]
|
|
|
|
|| [currentKey isEqualToString: @"ContactAccess"]
|
|
|
|
|| [currentKey hasPrefix: @"objectClass"]
|
|
|
|
|| [currentKey hasPrefix: @"c_"]
|
2009-12-23 22:39:09 +01:00
|
|
|
|| [currentKey isEqualToString: @"dn"]
|
2011-05-10 20:07:05 +02:00
|
|
|
|| [currentKey isEqualToString: @"isGroup"]
|
|
|
|
|| [currentKey isEqualToString: @"isResource"]
|
2011-05-10 20:33:46 +02:00
|
|
|
|| [currentKey isEqualToString: @"numberOfSimultaneousBookings"]
|
|
|
|
|| [currentKey isEqualToString: @"canAuthenticate"]))
|
2009-09-09 15:31:45 +02:00
|
|
|
[self _appendLDIFKey: currentKey toString: ldifString];
|
|
|
|
}
|
|
|
|
|
|
|
|
return ldifString;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-25 19:30:24 +02:00
|
|
|
@end
|
2008-07-12 00:28:12 +02:00
|
|
|
|
|
|
|
@implementation NSMutableDictionary (SOGoDictionaryUtilities)
|
|
|
|
|
2010-03-02 20:57:30 +01:00
|
|
|
- (void) setObject: (id) object
|
|
|
|
forKeys: (NSArray *) keys
|
|
|
|
{
|
|
|
|
unsigned int count, max;
|
|
|
|
|
|
|
|
max = [keys count];
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
[self setObject: object
|
|
|
|
forKey: [keys objectAtIndex: count]];
|
|
|
|
}
|
|
|
|
|
2008-07-12 00:28:12 +02:00
|
|
|
- (void) setObjects: (NSArray *) objects
|
|
|
|
forKeys: (NSArray *) keys
|
|
|
|
{
|
|
|
|
unsigned int count, max;
|
|
|
|
|
|
|
|
max = [objects count];
|
|
|
|
if ([keys count] == max)
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
[self setObject: [objects objectAtIndex: count]
|
|
|
|
forKey: [keys objectAtIndex: count]];
|
|
|
|
else
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"Number of objects does not match"
|
|
|
|
@" the number of keys."];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|