1/* NSDictionary+Utilities.m - this file is part of SOGo
2 *
3 * Copyright (C) 2007-2011 Inverse inc.
4 *
5 * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
6 *
7 * This file is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * This file is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING.  If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23#import <Foundation/NSArray.h>
24#import <Foundation/NSData.h>
25#import <Foundation/NSEnumerator.h>
26#import <Foundation/NSException.h>
27#import <Foundation/NSNull.h>
28#import <Foundation/NSString.h>
29
30#import "NSArray+Utilities.h"
31#import "NSString+Utilities.h"
32
33#import "NSDictionary+Utilities.h"
34
35@implementation NSDictionary (SOGoDictionaryUtilities)
36
37+ (NSDictionary *) dictionaryFromStringsFile: (NSString *) file
38{
39  NSString *serialized;
40  NSMutableData *content;
41  NSDictionary *newDictionary;
42
43  content = [NSMutableData data];
44  [content appendBytes: "{" length: 1];
45  [content appendData: [NSData dataWithContentsOfFile: file]];
46  [content appendBytes: "}" length: 1];
47  serialized = [[NSString alloc] initWithData: content
48				 encoding: NSUTF8StringEncoding];
49  [serialized autorelease];
50  newDictionary = [serialized propertyList];
51
52  return newDictionary;
53}
54
55- (NSString *) jsonRepresentation
56{
57  NSMutableArray *values;
58  NSString *representation, *currentKey, *currentValue, *currentPair;
59  NSEnumerator *keys;
60
61  values = [NSMutableArray array];
62  keys = [[self allKeys] objectEnumerator];
63  while ((currentKey = [keys nextObject]))
64    {
65      currentValue = [[self objectForKey: currentKey] jsonRepresentation];
66      currentPair = [NSString stringWithFormat: @"%@: %@",
67			      [currentKey jsonRepresentation], currentValue];
68      [values addObject: currentPair];
69    }
70  representation = [NSString stringWithFormat: @"{%@}",
71			     [values componentsJoinedByString: @", "]];
72
73  return representation;
74}
75
76- (NSString *) keysWithFormat: (NSString *) keyFormat
77{
78  NSArray *keys, *allKeys;
79  unsigned int count, max;
80  NSMutableString *keysWithFormat;
81  id value;
82
83  keysWithFormat = [NSMutableString stringWithString: keyFormat];
84
85  allKeys = [self allKeys];
86  keys = [allKeys stringsWithFormat: @"%{%@}"];
87
88  max = [allKeys count];
89  for (count = 0; count < max; count++)
90    {
91      value = [self objectForKey: [allKeys objectAtIndex: count]];
92      if ([value isKindOfClass: [NSNull class]])
93	[keysWithFormat replaceString: [keys objectAtIndex: count]
94			withString: @""];
95      else
96	[keysWithFormat replaceString: [keys objectAtIndex: count]
97			withString: [value description]];
98    }
99
100  return keysWithFormat;
101}
102
103- (NSComparisonResult) caseInsensitiveDisplayNameCompare: (NSDictionary *) theDictionary
104{
105  return [[self objectForKey: @"cn"] caseInsensitiveCompare: [theDictionary objectForKey: @"cn"]];
106}
107
108@end
109
110@implementation NSMutableDictionary (SOGoDictionaryUtilities)
111
112- (void) setObject: (id) object
113           forKeys: (NSArray *) keys
114{
115  unsigned int count, max;
116
117  max = [keys count];
118  for (count = 0; count < max; count++)
119    [self setObject: object
120             forKey: [keys objectAtIndex: count]];
121}
122
123- (void) setObjects: (NSArray *) objects
124	    forKeys: (NSArray *) keys
125{
126  unsigned int count, max;
127
128  max = [objects count];
129  if ([keys count] == max)
130    for (count = 0; count < max; count++)
131      [self setObject: [objects objectAtIndex: count]
132	    forKey: [keys objectAtIndex: count]];
133  else
134    [NSException raise: NSInvalidArgumentException
135		 format: @"Number of objects does not match"
136		 @" the number of keys."];
137}
138
139@end
140