forked from R-macos/Mac-GUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Preferences.m
298 lines (261 loc) · 7.55 KB
/
Preferences.m
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
/*
* R.app : a Cocoa front end to: "R A Computer Language for Statistical Data Analysis"
*
* R.app Copyright notes:
* Copyright (C) 2004-5 The R Foundation
* written by Stefano M. Iacus and Simon Urbanek
*
*
* R Copyright notes:
* Copyright (C) 1995-1996 Robert Gentleman and Ross Ihaka
* Copyright (C) 1998-2001 The R Development Core Team
* Copyright (C) 2002-2004 The R Foundation
*
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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.
*
* A copy of the GNU General Public License is available via WWW at
* http://www.gnu.org/copyleft/gpl.html. You can also obtain it by
* writing to the Free Software Foundation, Inc., 59 Temple Place,
* Suite 330, Boston, MA 02111-1307 USA.
*/
/*
* Short examples of intended usage for preferences:
*
* "useHighlighting" is time-critical in a sense that it is used every time the
* user presses a key - therefore the flag "useHighlighting" it's queried very
* often - It could be cached (see below)
*
* "openInEditor" isn't time critical, because a file is opened only once in a
* while so it's ok to fetch it directly from Preferences
*
* For uncached access, use something like: [Preferences flagForKey: openInEditorKey]
* (see method in Prefernces.h for signatures supported
* For cached access, use:
*
* @implementation xxx
* - (void) updatePreferences
* {
* flag = [Preferences flagForKey: xxxKey withDefault:YES];
* }
*
* - (void) awakeFromNib
* {
* [[Preferences sharedPreferences] addDependent: self];
* [self updatePreferences];
* ....
* }
*
* - (void) dealloc
* {
* [[Preferences sharedPreferences] removeDependent: self];
* ....
* }
*
* For additional examples, see MiscPrefPane.m and EditorPrefPane.m
*/
#import "Preferences.h"
#import "RGUI.h"
Preferences *globalPrefs=nil;
@interface Preferences (Private)
- (BOOL) writeDefaults;
@end
@implementation Preferences
- (id) init
{
self = [super init];
dependents = [[NSMutableArray alloc] init];
batch = NO;
changed = NO;
writeDefaults = YES;
insideNotify = NO;
return self;
}
- (void) dealloc
{
[self commit];
[super dealloc];
}
- (void) commit
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (defaults)
[defaults synchronize];
}
+ (void) commit
{
[[Preferences sharedPreferences] commit];
}
- (void) addDependent: (id<PreferencesDependent>) dep
{
[dependents addObject:dep];
}
- (void) removeDependent: (id<PreferencesDependent>) dep
{
[dependents removeObject:dep];
}
- (void) notifyDependents
{
if (!batch) {
if (!insideNotify) {
NSEnumerator *enumerator = [dependents objectEnumerator];
id<PreferencesDependent> dep;
while ((dep = (id<PreferencesDependent>) [enumerator nextObject])) {
insideNotify = dep;
[dep updatePreferences];
}
insideNotify = nil;
changed = NO;
} else
SLog(@"Preferences.notifyDependents: WARNING, cascaded notify attempted while %@ is being notified! Notify request cancelled.", insideNotify);
}
}
- (void) beginBatch
{
batch = YES;
}
- (void) endBatch
{
batch = NO;
if (changed) [self notifyDependents];
}
- (BOOL) writeDefaults
{
return writeDefaults;
}
//--- global methods ---
+ (void) setKey: (NSString*) key withObject: (id) value
{
[[Preferences sharedPreferences] setKey: key withObject: value];
}
+ (void) setKey: (NSString*) key withArchivedObject: (id) value
{
[[Preferences sharedPreferences] setKey: key withObject: [NSArchiver archivedDataWithRootObject:value]];
}
+ (void) setKey: (NSString*) key withFlag: (BOOL) value
{
[[Preferences sharedPreferences] setKey: key withObject: value?@"YES":@"NO"];
}
+ (void) setKey: (NSString*) key withFloat: (float) value
{
[[Preferences sharedPreferences] setKey: key withFloat: value];
}
- (void) setKey: (NSString*) key withArchivedObject: (id) value
{
[self setKey: key withObject: [NSArchiver archivedDataWithRootObject:value]];
}
- (void) setKey: (NSString*) key withFlag: (BOOL) value
{
[self setKey: key withObject: value?@"YES":@"NO"];
}
- (void) setKey: (NSString*) key withFloat: (float) value
{
SLog(@"Preferences.setKey:\"%@\" withFloat:\"%f\"", key, value);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (defaults) {
[defaults setFloat:value forKey:key];
changed = YES;
[self notifyDependents];
}
}
- (void) setKey: (NSString*) key withObject: (id) value
{
SLog(@"Preferences.setKey:\"%@\" withObject:\"%@\"", key, value);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (defaults) {
[defaults setObject:value forKey:key];
changed=YES;
[self notifyDependents];
}
}
+ (NSString *) stringForKey: (NSString*) key withDefault: (NSString*) defaultString
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (defaults) {
NSString *s=[defaults stringForKey:key];
if (s) return s;
}
if (defaultString && [[Preferences sharedPreferences] writeDefaults])
[globalPrefs setKey: key withObject: defaultString];
return defaultString;
}
+ (NSString *) stringForKey: (NSString*) key
{
return [Preferences stringForKey: key withDefault: nil];
}
+ (float) floatForKey: (NSString*) key withDefault: (float) defaultValue
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (defaults) {
NSString *s=[defaults stringForKey:key];
if (s) return [s floatValue];
}
return defaultValue;
}
+ (int) integerForKey: (NSString*) key withDefault: (int) defaultValue
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (defaults) {
NSString *s=[defaults stringForKey:key];
if (s) return [s intValue];
}
return defaultValue;
}
+ (BOOL) flagForKey: (NSString*) key withDefault: (BOOL) flag
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (defaults) {
NSString *s=[defaults stringForKey:key];
if (s) {
if ([s isEqualTo: @"YES"]) return YES;
if ([s isEqualTo: @"NO"]) return NO;
}
}
if ((flag==YES || flag==NO) &&
[[Preferences sharedPreferences] writeDefaults])
[globalPrefs setKey: key withObject: flag?@"YES":@"NO"];
return flag;
}
+ (BOOL) flagForKey: (NSString*) key
{
return [Preferences flagForKey: key withDefault: UNKNOWN];
}
+ (id) objectForKey: (NSString*) key withDefault: (id) defaultObj
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (defaults) {
id obj=[defaults objectForKey:key];
if (obj) return obj;
}
if (defaultObj && [[Preferences sharedPreferences] writeDefaults])
[globalPrefs setKey: key withObject: defaultObj];
return defaultObj;
}
+ (id) unarchivedObjectForKey: (NSString*) key withDefault: (id) defaultObj
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (defaults) {
NSData *data=[defaults dataForKey:key];
if (data) {
id obj = [NSUnarchiver unarchiveObjectWithData:data];
if (obj) return obj;
}
}
if (defaultObj && [[Preferences sharedPreferences] writeDefaults])
[globalPrefs setKey: key withArchivedObject: defaultObj];
return defaultObj;
}
+ (Preferences*) sharedPreferences
{
if (!globalPrefs)
globalPrefs=[[Preferences alloc] init];
return globalPrefs;
}
@end