File: NSUserDefaultsController.m

package info (click to toggle)
gnustep-gui 0.25.0-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 13,088 kB
  • ctags: 3,417
  • sloc: objc: 153,800; ansic: 18,239; cpp: 579; yacc: 462; makefile: 143; sh: 5
file content (339 lines) | stat: -rw-r--r-- 7,939 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/** <title>NSUserDefaultsController</title>

   <abstract>Controller class for user defaults</abstract>

   Copyright <copy>(C) 2006 Free Software Foundation, Inc.</copy>

   Author: Fred Kiefer <fredkiefer@gmx.de>
   Date: September 2006

   This file is part of the GNUstep GUI Library.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; see the file COPYING.LIB.
   If not, see <http://www.gnu.org/licenses/> or write to the 
   Free Software Foundation, 51 Franklin Street, Fifth Floor, 
   Boston, MA 02110-1301, USA.
*/

#import <Foundation/NSCoder.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSEnumerator.h>
#import <Foundation/NSKeyValueObserving.h>
#import <Foundation/NSNotification.h>
#import <Foundation/NSNull.h>
#import <Foundation/NSUserDefaults.h>
#import "AppKit/NSUserDefaultsController.h"

static id shared = nil;

@interface GSUserDefaultsHelper : NSObject
{
@public
  NSUserDefaultsController *controller;
  NSMutableDictionary *values;
}

- (id) initWithController: (NSUserDefaultsController*)udc;
- (id) valueForKey: (NSString*)key;
- (void) setValue: (id)value forKey: (NSString*)key;

- (void) revert;
- (void) revertToInitialValues: (NSDictionary *)initial;
- (void) defaultsDidChange: (NSUserDefaults *)defaults;
@end

@implementation GSUserDefaultsHelper

- (id) initWithController: (NSUserDefaultsController*)udc
{
  if ((self = [super init]) != nil)
    {
      // We are retained by the controller
      controller = udc;
      values = [[NSMutableDictionary alloc] init];
    }

  return self;
}

- (void) dealloc
{
  RELEASE(values);
  [super dealloc];
}

- (id) valueForKey: (NSString*)key
{
  id value = [values objectForKey: key];

  if (!value)
    {
      // If the value isn't cached, get it from the controller and cache it.
      value = [[controller defaults] objectForKey: key];
      if (!value)
        {
          value = [[controller initialValues] objectForKey: key];
        }
      // We need to cache the values we return to be able to 
      // report changes to them when the defaults change.
      if (value)
        {
          [values setObject: value forKey: key];
        }
      else
        {
          // Use a marker for missing values
          [values setObject: [NSNull null] forKey: key];
        }
    }
  else if ([[NSNull null] isEqual: value])
    {
      // filter out our marker value
      return nil;
    }

  return value;
}

- (void) setValue: (id)value forKey: (NSString*)key
{
  [self willChangeValueForKey: key];
  [values setObject: value forKey: key];
  if ([controller appliesImmediately])
    [[controller defaults] setObject: value forKey: key];
  [self didChangeValueForKey: key];
}

- (void) revert
{
  NSEnumerator *e;
  NSString *key;

  e = [values keyEnumerator];
  while ((key = (NSString *)[e nextObject]))
    {
      [self willChangeValueForKey: key];
      [values removeObjectForKey: key];
      [self didChangeValueForKey: key];
    }
}

- (void) revertToInitialValues: (NSDictionary *)initial
{
  NSEnumerator *e;
  NSString *key;

  e = [values keyEnumerator];
  while ((key = (NSString *)[e nextObject]))
    {
      id val = [values objectForKey: key];
      id oldVal = [initial objectForKey: key];
        
      if (oldVal && ![val isEqual: oldVal])
        {
          [self willChangeValueForKey: key];
          [values setObject: oldVal forKey: key];
          // When appliesImmediately is YES, should we save these values?
          [self didChangeValueForKey: key];
        }
   }
}

- (void) defaultsDidChange: (NSUserDefaults *)defaults
{
  NSEnumerator *e;
  NSString *key;

  e = [values keyEnumerator];
  while ((key = (NSString *)[e nextObject]))
    {
      id val = [values objectForKey: key];
      id newVal = [defaults objectForKey: key];

      if (newVal && ![val isEqual: newVal])
        {
          [self willChangeValueForKey: key];
          [values setObject: newVal forKey: key];
          [self didChangeValueForKey: key];
        }      
    }
}

@end

@implementation NSUserDefaultsController

+ (id) sharedUserDefaultsController
{
  if (shared == nil)
    {
      shared = [[NSUserDefaultsController alloc] 
                   initWithDefaults: nil
                   initialValues: nil];    
    }
  return shared;
}

- (id) initWithDefaults: (NSUserDefaults*)defaults
          initialValues: (NSDictionary*)initialValues
{
  if ((self = [super init]) != nil)
    {
      if (defaults == nil)
        {
          defaults = [NSUserDefaults standardUserDefaults];
        }

      ASSIGN(_defaults, defaults);
      [self setAppliesImmediately: YES];
      [self setInitialValues: initialValues];
      _values = [[GSUserDefaultsHelper alloc] 
                    initWithController: self];
      // Watch for user default change notifications
      [[NSNotificationCenter defaultCenter] 
          addObserver: self
          selector: @selector(defaultsDidChange:)
          name: NSUserDefaultsDidChangeNotification
          object: _defaults];
    }

  return self;
}

- (void) dealloc
{
  if (self == shared)
    {
      // Should never get here
      shared = nil;
    }

  [[NSNotificationCenter defaultCenter] removeObserver: self];
  RELEASE(_values);
  RELEASE(_defaults);
  RELEASE(_initial_values);
  [super dealloc];
}

- (NSUserDefaults*) defaults
{
  return _defaults;
}

- (id) values
{
  return _values;  
}

- (NSDictionary*) initialValues
{
  return _initial_values;
}

- (void) setInitialValues: (NSDictionary*)values
{
  ASSIGNCOPY(_initial_values, values);
}

- (BOOL) appliesImmediately
{
  return _applies_immediately;
}

- (void) setAppliesImmediately: (BOOL)flag
{
  _applies_immediately = flag; 
}

- (void) revert: (id)sender
{
  [self discardEditing];
  if (![self appliesImmediately])
    {
      [_values revert];
    } 
}

- (void) revertToInitialValues: (id)sender
{
  [self discardEditing];
  [_values revertToInitialValues: _initial_values];
}

- (void) save: (id)sender
{
  if (![self appliesImmediately])
    {
      NSDictionary *values = ((GSUserDefaultsHelper*)_values)->values;
      NSEnumerator *e;
      NSString *key;
      
      e = [values keyEnumerator];
      while ((key = (NSString *)[e nextObject]))
        {
          [_defaults setObject: [values objectForKey: key] forKey: key];
        }
    }
}

- (BOOL) hasUnappliedChanges
{
  NSDictionary *values = ((GSUserDefaultsHelper*)_values)->values;
  NSEnumerator *e;
  NSString *key;

  e = [values keyEnumerator];
  while ((key = (NSString *)[e nextObject]))
    {
      id val = [values objectForKey: key];
      id newVal = [_defaults objectForKey: key];

      if (![val isEqual: newVal])
        {
          return YES;
        }      
    }
  return NO;
}

- (void) defaultsDidChange: (NSNotification*)notification
{
  [_values defaultsDidChange: _defaults];
}

- (void) encodeWithCoder: (NSCoder *)aCoder
{ 
  if ([aCoder allowsKeyedCoding])
    if (self == shared)
      {
        [aCoder encodeBool: YES forKey: @"NSSharedInstance"];
        return;
      }

  [super encodeWithCoder: aCoder];
}

- (id) initWithCoder: (NSCoder *)aDecoder
{
  if ([aDecoder allowsKeyedCoding])
    if ([aDecoder decodeBoolForKey: @"NSSharedInstance"])
      {
        RELEASE(self);
        return RETAIN([NSUserDefaultsController sharedUserDefaultsController]);
      }

  return [super initWithCoder: aDecoder];
}

@end