Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / userdefaults.c
Commit [+]AuthorDateLineData
9d2e6ef9 scottc1998-09-29 22:36:29 +00001
2#include <stdlib.h>
bb50f836 dan1999-06-19 20:01:44 +00003#include <string.h>
9d2e6ef9 scottc1998-09-29 22:36:29 +00004#include <stdio.h>
5#include <assert.h>
049a69a9 dan2000-01-15 02:00:24 +00006#include <unistd.h>
7#include <sys/stat.h>
9d2e6ef9 scottc1998-09-29 22:36:29 +00008
33cc542e dan2001-10-04 03:07:34 +00009#include "wconfig.h"
10
049a69a9 dan2000-01-15 02:00:24 +000011#include "WINGs.h"
9d2e6ef9 scottc1998-09-29 22:36:29 +000012
9d2e6ef9 scottc1998-09-29 22:36:29 +000013typedef struct W_UserDefaults {
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020014 WMPropList *defaults;
674eb20e dan1999-04-18 19:41:42 +000015
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020016 WMPropList *appDomain;
9d2e6ef9 scottc1998-09-29 22:36:29 +000017
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020018 WMPropList *searchListArray;
19 WMPropList **searchList; /* cache for searchListArray */
9d2e6ef9 scottc1998-09-29 22:36:29 +000020
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020021 char dirty;
9d2e6ef9 scottc1998-09-29 22:36:29 +000022
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020023 char dontSync;
049a69a9 dan2000-01-15 02:00:24 +000024
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020025 char *path; /* where is db located */
674eb20e dan1999-04-18 19:41:42 +000026
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020027 time_t timestamp; /* last modification time */
049a69a9 dan2000-01-15 02:00:24 +000028
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020029 struct W_UserDefaults *next;
674eb20e dan1999-04-18 19:41:42 +000030
9d2e6ef9 scottc1998-09-29 22:36:29 +000031} UserDefaults;
32
9d2e6ef9 scottc1998-09-29 22:36:29 +000033static UserDefaults *sharedUserDefaults = NULL;
cf6fbf7e dan1999-04-19 00:09:21 +000034
049a69a9 dan2000-01-15 02:00:24 +000035char *WMUserDefaultsDidChangeNotification = "WMUserDefaultsDidChangeNotification";
674eb20e dan1999-04-18 19:41:42 +000036
c2ec1cfe dan2002-11-13 15:13:48 +000037static void synchronizeUserDefaults(void *foo);
cf6fbf7e dan1999-04-19 00:09:21 +000038
9d2e6ef9 scottc1998-09-29 22:36:29 +000039extern char *WMGetApplicationName();
40
41#define DEFAULTS_DIR "/Defaults"
42
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020043char *wusergnusteppath()
9d2e6ef9 scottc1998-09-29 22:36:29 +000044{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020045 static char *path = NULL;
46 char *gspath;
47 int pathlen;
48
49 if (!path) {
50 gspath = getenv("GNUSTEP_USER_ROOT");
51 if (gspath) {
52 gspath = wexpandpath(gspath);
53 pathlen = strlen(gspath) + 4;
54 path = wmalloc(pathlen);
55 strcpy(path, gspath);
56 wfree(gspath);
57 } else {
58 pathlen = strlen(wgethomedir()) + 10;
59 path = wmalloc(pathlen);
60 strcpy(path, wgethomedir());
61 strcat(path, "/GNUstep");
62 }
63 }
64
65 return path;
9d2e6ef9 scottc1998-09-29 22:36:29 +000066}
67
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020068char *wdefaultspathfordomain(char *domain)
9d2e6ef9 scottc1998-09-29 22:36:29 +000069{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020070 char *path;
71 char *gspath;
9d2e6ef9 scottc1998-09-29 22:36:29 +000072
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020073 gspath = wusergnusteppath();
74 path = wmalloc(strlen(gspath) + strlen(DEFAULTS_DIR) + strlen(domain) + 4);
75 strcpy(path, gspath);
76 strcat(path, DEFAULTS_DIR);
77 strcat(path, "/");
78 strcat(path, domain);
9d2e6ef9 scottc1998-09-29 22:36:29 +000079
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020080 return path;
9d2e6ef9 scottc1998-09-29 22:36:29 +000081}
82
9d2e6ef9 scottc1998-09-29 22:36:29 +000083static void
c2ec1cfe dan2002-11-13 15:13:48 +000084#ifdef HAVE_ATEXIT
9d2e6ef9 scottc1998-09-29 22:36:29 +000085saveDefaultsChanges(void)
c2ec1cfe dan2002-11-13 15:13:48 +000086#else
87saveDefaultsChanges(int foo, void *bar)
931a37b1 dan1999-01-29 08:11:17 +000088#endif
9d2e6ef9 scottc1998-09-29 22:36:29 +000089{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020090 /* save the user defaults databases */
91 synchronizeUserDefaults(NULL);
9d2e6ef9 scottc1998-09-29 22:36:29 +000092}
93
674eb20e dan1999-04-18 19:41:42 +000094/* set to save changes in defaults when program is exited */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020095static void registerSaveOnExit(void)
674eb20e dan1999-04-18 19:41:42 +000096{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020097 static Bool registeredSaveOnExit = False;
049a69a9 dan2000-01-15 02:00:24 +000098
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020099 if (!registeredSaveOnExit) {
c2ec1cfe dan2002-11-13 15:13:48 +0000100#ifdef HAVE_ATEXIT
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200101 atexit(saveDefaultsChanges);
c2ec1cfe dan2002-11-13 15:13:48 +0000102#else
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200103 on_exit(saveDefaultsChanges, (void *)NULL);
674eb20e dan1999-04-18 19:41:42 +0000104#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200105 registeredSaveOnExit = True;
106 }
674eb20e dan1999-04-18 19:41:42 +0000107}
108
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200109static void synchronizeUserDefaults(void *foo)
049a69a9 dan2000-01-15 02:00:24 +0000110{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200111 UserDefaults *database = sharedUserDefaults;
049a69a9 dan2000-01-15 02:00:24 +0000112
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200113 while (database) {
114 if (!database->dontSync)
115 WMSynchronizeUserDefaults(database);
116 database = database->next;
117 }
049a69a9 dan2000-01-15 02:00:24 +0000118}
119
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200120void WMEnableUDPeriodicSynchronization(WMUserDefaults * database, Bool enable)
049a69a9 dan2000-01-15 02:00:24 +0000121{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200122 database->dontSync = !enable;
049a69a9 dan2000-01-15 02:00:24 +0000123}
124
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200125void WMSynchronizeUserDefaults(WMUserDefaults * database)
ea55bc5e kojima1999-03-18 01:11:18 +0000126{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200127 Bool fileIsNewer = False, release = False, notify = False;
128 WMPropList *plF, *key;
129 char *path;
130 struct stat stbuf;
131
132 if (!database->path) {
133 path = wdefaultspathfordomain(WMGetApplicationName());
134 release = True;
135 } else {
136 path = database->path;
137 }
138
139 if (stat(path, &stbuf) >= 0 && stbuf.st_mtime > database->timestamp)
140 fileIsNewer = True;
141
142 if (database->appDomain && (database->dirty || fileIsNewer)) {
143 if (database->dirty && fileIsNewer) {
144 plF = WMReadPropListFromFile(path);
145 if (plF) {
146 plF = WMMergePLDictionaries(plF, database->appDomain, False);
147 WMReleasePropList(database->appDomain);
148 database->appDomain = plF;
149 key = database->searchList[0];
150 WMPutInPLDictionary(database->defaults, key, plF);
151 notify = True;
152 } else {
153 /* something happened with the file. just overwrite it */
154 wwarning(_("cannot read domain from file '%s' when syncing"), path);
155 WMWritePropListToFile(database->appDomain, path, True);
156 }
157 } else if (database->dirty) {
158 WMWritePropListToFile(database->appDomain, path, True);
159 } else if (fileIsNewer) {
160 plF = WMReadPropListFromFile(path);
161 if (plF) {
162 WMReleasePropList(database->appDomain);
163 database->appDomain = plF;
164 key = database->searchList[0];
165 WMPutInPLDictionary(database->defaults, key, plF);
166 notify = True;
167 } else {
168 /* something happened with the file. just overwrite it */
169 wwarning(_("cannot read domain from file '%s' when syncing"), path);
170 WMWritePropListToFile(database->appDomain, path, True);
171 }
172 }
173
174 database->dirty = 0;
175
176 if (stat(path, &stbuf) >= 0)
177 database->timestamp = stbuf.st_mtime;
178
179 if (notify) {
180 WMPostNotificationName(WMUserDefaultsDidChangeNotification, database, NULL);
181 }
182 }
183
184 if (release)
185 wfree(path);
049a69a9 dan2000-01-15 02:00:24 +0000186
ea55bc5e kojima1999-03-18 01:11:18 +0000187}
188
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200189void WMSaveUserDefaults(WMUserDefaults * database)
d66f2381 dan1999-10-08 03:32:55 +0000190{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200191 if (database->appDomain) {
192 struct stat stbuf;
193 char *path;
194 Bool release = False;
195
196 if (!database->path) {
197 path = wdefaultspathfordomain(WMGetApplicationName());
198 release = True;
199 } else {
200 path = database->path;
201 }
202 WMWritePropListToFile(database->appDomain, path, True);
203 database->dirty = 0;
204 if (stat(path, &stbuf) >= 0)
205 database->timestamp = stbuf.st_mtime;
206 if (release)
207 wfree(path);
208 }
d66f2381 dan1999-10-08 03:32:55 +0000209}
210
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200211WMUserDefaults *WMGetStandardUserDefaults(void)
cf6fbf7e dan1999-04-19 00:09:21 +0000212{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200213 WMUserDefaults *defaults;
214 WMPropList *domain;
215 WMPropList *key;
216 struct stat stbuf;
217 char *path;
218 int i;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000219
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200220 if (sharedUserDefaults) {
221 defaults = sharedUserDefaults;
222 while (defaults) {
223 /* path == NULL only for StandardUserDefaults db */
224 if (defaults->path == NULL)
225 return defaults;
226 defaults = defaults->next;
227 }
228 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000229
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200230 /* we didn't found the database we are looking for. Go read it. */
231 defaults = wmalloc(sizeof(WMUserDefaults));
232 memset(defaults, 0, sizeof(WMUserDefaults));
9d2e6ef9 scottc1998-09-29 22:36:29 +0000233
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200234 defaults->defaults = WMCreatePLDictionary(NULL, NULL);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000235
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200236 defaults->searchList = wmalloc(sizeof(WMPropList *) * 3);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000237
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200238 /* application domain */
239 key = WMCreatePLString(WMGetApplicationName());
240 defaults->searchList[0] = key;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000241
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200242 /* temporary kluge. wmaker handles synchronization itself */
243 if (strcmp(WMGetApplicationName(), "WindowMaker") == 0) {
244 defaults->dontSync = 1;
245 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000246
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200247 path = wdefaultspathfordomain(WMGetFromPLString(key));
049a69a9 dan2000-01-15 02:00:24 +0000248
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200249 if (stat(path, &stbuf) >= 0)
250 defaults->timestamp = stbuf.st_mtime;
c2ec1cfe dan2002-11-13 15:13:48 +0000251
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200252 domain = WMReadPropListFromFile(path);
33cc542e dan2001-10-04 03:07:34 +0000253
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200254 if (!domain)
255 domain = WMCreatePLDictionary(NULL, NULL);
33cc542e dan2001-10-04 03:07:34 +0000256
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200257 if (path)
258 wfree(path);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000259
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200260 defaults->appDomain = domain;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000261
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200262 if (domain)
263 WMPutInPLDictionary(defaults->defaults, key, domain);
cf6fbf7e dan1999-04-19 00:09:21 +0000264
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200265 /* global domain */
266 key = WMCreatePLString("WMGLOBAL");
267 defaults->searchList[1] = key;
cf6fbf7e dan1999-04-19 00:09:21 +0000268
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200269 path = wdefaultspathfordomain(WMGetFromPLString(key));
cf6fbf7e dan1999-04-19 00:09:21 +0000270
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200271 domain = WMReadPropListFromFile(path);
cf6fbf7e dan1999-04-19 00:09:21 +0000272
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200273 wfree(path);
cf6fbf7e dan1999-04-19 00:09:21 +0000274
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200275 if (!domain)
276 domain = WMCreatePLDictionary(NULL, NULL);
cf6fbf7e dan1999-04-19 00:09:21 +0000277
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200278 if (domain)
279 WMPutInPLDictionary(defaults->defaults, key, domain);
cf6fbf7e dan1999-04-19 00:09:21 +0000280
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200281 /* terminate list */
282 defaults->searchList[2] = NULL;
cf6fbf7e dan1999-04-19 00:09:21 +0000283
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200284 defaults->searchListArray = WMCreatePLArray(NULL, NULL);
9007a657 dan1998-11-23 11:32:19 +0000285
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200286 i = 0;
287 while (defaults->searchList[i]) {
288 WMAddToPLArray(defaults->searchListArray, defaults->searchList[i]);
289 i++;
290 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000291
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200292 if (sharedUserDefaults)
293 defaults->next = sharedUserDefaults;
294 sharedUserDefaults = defaults;
cf6fbf7e dan1999-04-19 00:09:21 +0000295
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200296 registerSaveOnExit();
cf6fbf7e dan1999-04-19 00:09:21 +0000297
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200298 return defaults;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000299}
300
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200301WMUserDefaults *WMGetDefaultsFromPath(char *path)
674eb20e dan1999-04-18 19:41:42 +0000302{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200303 WMUserDefaults *defaults;
304 WMPropList *domain;
305 WMPropList *key;
306 struct stat stbuf;
307 char *name;
308 int i;
674eb20e dan1999-04-18 19:41:42 +0000309
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200310 assert(path != NULL);
674eb20e dan1999-04-18 19:41:42 +0000311
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200312 if (sharedUserDefaults) {
313 defaults = sharedUserDefaults;
314 while (defaults) {
315 if (defaults->path && strcmp(defaults->path, path) == 0)
316 return defaults;
317 defaults = defaults->next;
318 }
319 }
674eb20e dan1999-04-18 19:41:42 +0000320
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200321 /* we didn't found the database we are looking for. Go read it. */
322 defaults = wmalloc(sizeof(WMUserDefaults));
323 memset(defaults, 0, sizeof(WMUserDefaults));
674eb20e dan1999-04-18 19:41:42 +0000324
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200325 defaults->defaults = WMCreatePLDictionary(NULL, NULL);
674eb20e dan1999-04-18 19:41:42 +0000326
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200327 defaults->searchList = wmalloc(sizeof(WMPropList *) * 2);
674eb20e dan1999-04-18 19:41:42 +0000328
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200329 /* the domain we want, go in the first position */
330 name = strrchr(path, '/');
331 if (!name)
332 name = path;
333 else
334 name++;
cf6fbf7e dan1999-04-19 00:09:21 +0000335
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200336 key = WMCreatePLString(name);
337 defaults->searchList[0] = key;
674eb20e dan1999-04-18 19:41:42 +0000338
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200339 if (stat(path, &stbuf) >= 0)
340 defaults->timestamp = stbuf.st_mtime;
049a69a9 dan2000-01-15 02:00:24 +0000341
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200342 domain = WMReadPropListFromFile(path);
674eb20e dan1999-04-18 19:41:42 +0000343
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200344 if (!domain)
345 domain = WMCreatePLDictionary(NULL, NULL);
674eb20e dan1999-04-18 19:41:42 +0000346
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200347 defaults->path = wstrdup(path);
674eb20e dan1999-04-18 19:41:42 +0000348
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200349 defaults->appDomain = domain;
674eb20e dan1999-04-18 19:41:42 +0000350
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200351 if (domain)
352 WMPutInPLDictionary(defaults->defaults, key, domain);
674eb20e dan1999-04-18 19:41:42 +0000353
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200354 /* terminate list */
355 defaults->searchList[1] = NULL;
674eb20e dan1999-04-18 19:41:42 +0000356
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200357 defaults->searchListArray = WMCreatePLArray(NULL, NULL);
674eb20e dan1999-04-18 19:41:42 +0000358
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200359 i = 0;
360 while (defaults->searchList[i]) {
361 WMAddToPLArray(defaults->searchListArray, defaults->searchList[i]);
362 i++;
363 }
674eb20e dan1999-04-18 19:41:42 +0000364
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200365 if (sharedUserDefaults)
366 defaults->next = sharedUserDefaults;
367 sharedUserDefaults = defaults;
674eb20e dan1999-04-18 19:41:42 +0000368
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200369 registerSaveOnExit();
674eb20e dan1999-04-18 19:41:42 +0000370
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200371 return defaults;
674eb20e dan1999-04-18 19:41:42 +0000372}
373
33cc542e dan2001-10-04 03:07:34 +0000374/* Returns a WMPropList array with all keys in the user defaults database.
375 * Free the array with WMReleasePropList() when no longer needed,
51a638e8 dan2000-04-12 01:45:42 +0000376 * but do not free the elements of the array! They're just references. */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200377WMPropList *WMGetUDKeys(WMUserDefaults * database)
51a638e8 dan2000-04-12 01:45:42 +0000378{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200379 return WMGetPLDictionaryKeys(database->appDomain);
51a638e8 dan2000-04-12 01:45:42 +0000380}
381
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200382WMPropList *WMGetUDObjectForKey(WMUserDefaults * database, char *defaultName)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000383{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200384 WMPropList *domainName, *domain;
385 WMPropList *object = NULL;
386 WMPropList *key = WMCreatePLString(defaultName);
387 int i = 0;
388
389 while (database->searchList[i] && !object) {
390 domainName = database->searchList[i];
391 domain = WMGetFromPLDictionary(database->defaults, domainName);
392 if (domain) {
393 object = WMGetFromPLDictionary(domain, key);
394 }
395 i++;
396 }
397 WMReleasePropList(key);
398
399 return object;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000400}
401
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200402void WMSetUDObjectForKey(WMUserDefaults * database, WMPropList * object, char *defaultName)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000403{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200404 WMPropList *key = WMCreatePLString(defaultName);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000405
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200406 database->dirty = 1;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000407
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200408 WMPutInPLDictionary(database->appDomain, key, object);
409 WMReleasePropList(key);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000410}
411
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200412void WMRemoveUDObjectForKey(WMUserDefaults * database, char *defaultName)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000413{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200414 WMPropList *key = WMCreatePLString(defaultName);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000415
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200416 database->dirty = 1;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000417
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200418 WMRemoveFromPLDictionary(database->appDomain, key);
6830b057 dan2004-10-12 21:28:27 +0000419
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200420 WMReleasePropList(key);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000421}
422
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200423char *WMGetUDStringForKey(WMUserDefaults * database, char *defaultName)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000424{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200425 WMPropList *val;
6830b057 dan2004-10-12 21:28:27 +0000426
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200427 val = WMGetUDObjectForKey(database, defaultName);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000428
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200429 if (!val)
430 return NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000431
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200432 if (!WMIsPLString(val))
433 return NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000434
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200435 return WMGetFromPLString(val);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000436}
437
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200438int WMGetUDIntegerForKey(WMUserDefaults * database, char *defaultName)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000439{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200440 WMPropList *val;
441 char *str;
442 int value;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000443
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200444 val = WMGetUDObjectForKey(database, defaultName);
6830b057 dan2004-10-12 21:28:27 +0000445
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200446 if (!val)
447 return 0;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000448
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200449 if (!WMIsPLString(val))
450 return 0;
6830b057 dan2004-10-12 21:28:27 +0000451
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200452 str = WMGetFromPLString(val);
453 if (!str)
454 return 0;
6830b057 dan2004-10-12 21:28:27 +0000455
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200456 if (sscanf(str, "%i", &value) != 1)
457 return 0;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000458
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200459 return value;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000460}
461
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200462float WMGetUDFloatForKey(WMUserDefaults * database, char *defaultName)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000463{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200464 WMPropList *val;
465 char *str;
466 float value;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000467
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200468 val = WMGetUDObjectForKey(database, defaultName);
6830b057 dan2004-10-12 21:28:27 +0000469
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200470 if (!val || !WMIsPLString(val))
471 return 0.0;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000472
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200473 if (!(str = WMGetFromPLString(val)))
474 return 0.0;
e7495baf dan1999-02-17 11:06:40 +0000475
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200476 if (sscanf(str, "%f", &value) != 1)
477 return 0.0;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000478
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200479 return value;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000480}
481
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200482Bool WMGetUDBoolForKey(WMUserDefaults * database, char *defaultName)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000483{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200484 WMPropList *val;
485 int value;
486 char *str;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000487
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200488 val = WMGetUDObjectForKey(database, defaultName);
6830b057 dan2004-10-12 21:28:27 +0000489
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200490 if (!val)
491 return False;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000492
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200493 if (!WMIsPLString(val))
494 return False;
6830b057 dan2004-10-12 21:28:27 +0000495
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200496 str = WMGetFromPLString(val);
497 if (!str)
498 return False;
6830b057 dan2004-10-12 21:28:27 +0000499
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200500 if (sscanf(str, "%i", &value) == 1 && value != 0)
501 return True;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000502
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200503 if (strcasecmp(str, "YES") == 0)
504 return True;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000505
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200506 if (strcasecmp(str, "Y") == 0)
507 return True;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000508
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200509 return False;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000510}
511
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200512void WMSetUDIntegerForKey(WMUserDefaults * database, int value, char *defaultName)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000513{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200514 WMPropList *object;
515 char buffer[128];
9d2e6ef9 scottc1998-09-29 22:36:29 +0000516
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200517 sprintf(buffer, "%i", value);
518 object = WMCreatePLString(buffer);
6830b057 dan2004-10-12 21:28:27 +0000519
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200520 WMSetUDObjectForKey(database, object, defaultName);
521 WMReleasePropList(object);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000522}
523
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200524void WMSetUDStringForKey(WMUserDefaults * database, char *value, char *defaultName)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000525{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200526 WMPropList *object;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000527
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200528 object = WMCreatePLString(value);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000529
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200530 WMSetUDObjectForKey(database, object, defaultName);
531 WMReleasePropList(object);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000532}
533
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200534void WMSetUDFloatForKey(WMUserDefaults * database, float value, char *defaultName)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000535{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200536 WMPropList *object;
537 char buffer[128];
9d2e6ef9 scottc1998-09-29 22:36:29 +0000538
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200539 sprintf(buffer, "%f", value);
540 object = WMCreatePLString(buffer);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000541
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200542 WMSetUDObjectForKey(database, object, defaultName);
543 WMReleasePropList(object);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000544}
545
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200546void WMSetUDBoolForKey(WMUserDefaults * database, Bool value, char *defaultName)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000547{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200548 static WMPropList *yes = NULL, *no = NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000549
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200550 if (!yes) {
551 yes = WMCreatePLString("YES");
552 no = WMCreatePLString("NO");
553 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000554
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200555 WMSetUDObjectForKey(database, value ? yes : no, defaultName);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000556}
557
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200558WMPropList *WMGetUDSearchList(WMUserDefaults * database)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000559{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200560 return database->searchListArray;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000561}
562
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200563void WMSetUDSearchList(WMUserDefaults * database, WMPropList * list)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000564{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200565 int i, c;
566
567 if (database->searchList) {
568 i = 0;
569 while (database->searchList[i]) {
570 WMReleasePropList(database->searchList[i]);
571 i++;
572 }
573 wfree(database->searchList);
574 }
575 if (database->searchListArray) {
576 WMReleasePropList(database->searchListArray);
577 }
578
579 c = WMGetPropListItemCount(list);
580 database->searchList = wmalloc(sizeof(WMPropList *) * (c + 1));
581
582 for (i = 0; i < c; i++) {
583 database->searchList[i] = WMGetFromPLArray(list, i);
584 }
585 database->searchList[c] = NULL;
586
587 database->searchListArray = WMDeepCopyPropList(list);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000588}