8 #include "../src/config.h"
15 typedef struct W_UserDefaults
{
20 proplist_t searchListArray
;
21 proplist_t
*searchList
; /* cache for searchListArray */
25 char *path
; /* where is db located */
27 struct W_UserDefaults
*next
;
32 static UserDefaults
*sharedUserDefaults
= NULL
;
34 static Bool registeredSaveOnExit
= False
;
38 extern char *WMGetApplicationName();
40 #define DEFAULTS_DIR "/Defaults"
46 static char *path
= NULL
;
51 gspath
= getenv("GNUSTEP_USER_ROOT");
53 gspath
= wexpandpath(gspath
);
54 pathlen
= strlen(gspath
) + 4;
55 path
= wmalloc(pathlen
);
59 pathlen
= strlen(wgethomedir()) + 10;
60 path
= wmalloc(pathlen
);
61 strcpy(path
, wgethomedir());
62 strcat(path
, "/GNUstep");
71 wdefaultspathfordomain(char *domain
)
76 gspath
= wusergnusteppath();
77 path
= wmalloc(strlen(gspath
)+strlen(DEFAULTS_DIR
)+strlen(domain
)+4);
79 strcat(path
, DEFAULTS_DIR
);
89 saveDefaultsChanges(int foo
, void *bar
)
91 saveDefaultsChanges(void)
94 /* save the user defaults databases */
95 if (sharedUserDefaults
) {
96 UserDefaults
*tmp
= sharedUserDefaults
;
99 if (tmp
->appDomain
&& tmp
->dirty
)
100 PLSave(tmp
->appDomain
, YES
);
107 /* set to save changes in defaults when program is exited */
109 registerSaveOnExit(void)
111 if (!registeredSaveOnExit
) {
113 on_exit(saveDefaultsChanges
, (void*)NULL
);
115 atexit(saveDefaultsChanges
);
117 registeredSaveOnExit
= True
;
123 WMSynchronizeUserDefaults(WMUserDefaults
*database
)
125 /* TODO: check what it should really do */
126 PLSave(database
->appDomain
, YES
);
131 WMGetStandardUserDefaults(void)
133 WMUserDefaults
*defaults
;
139 if (sharedUserDefaults
) {
140 defaults
= sharedUserDefaults
;
142 /* Trick, path == NULL only for StandardUserDefaults db */
143 if (defaults
->path
== NULL
)
145 defaults
= defaults
->next
;
149 /* we didn't found the database we are looking for. Go read it. */
150 defaults
= wmalloc(sizeof(WMUserDefaults
));
151 memset(defaults
, 0, sizeof(WMUserDefaults
));
153 defaults
->defaults
= PLMakeDictionaryFromEntries(NULL
, NULL
, NULL
);
155 defaults
->searchList
= wmalloc(sizeof(proplist_t
)*3);
157 /* application domain */
158 key
= PLMakeString(WMGetApplicationName());
159 defaults
->searchList
[0] = key
;
161 /* temporary kluge */
162 if (strcmp(WMGetApplicationName(), "WindowMaker")==0) {
166 path
= wdefaultspathfordomain(PLGetString(key
));
168 domain
= PLGetProplistWithPath(path
);
173 domain
= PLMakeDictionaryFromEntries(NULL
, NULL
, NULL
);
175 p
= PLMakeString(path
);
176 PLSetFilename(domain
, p
);
183 defaults
->appDomain
= domain
;
186 PLInsertDictionaryEntry(defaults
->defaults
, key
, domain
);
191 key
= PLMakeString("WMGLOBAL");
192 defaults
->searchList
[1] = key
;
194 path
= wdefaultspathfordomain(PLGetString(key
));
196 domain
= PLGetProplistWithPath(path
);
201 domain
= PLMakeDictionaryFromEntries(NULL
, NULL
, NULL
);
204 PLInsertDictionaryEntry(defaults
->defaults
, key
, domain
);
209 defaults
->searchList
[2] = NULL
;
211 defaults
->searchListArray
=PLMakeArrayFromElements(NULL
,NULL
);
215 while (defaults
->searchList
[i
]) {
216 PLAppendArrayElement(defaults
->searchListArray
,
217 defaults
->searchList
[i
]);
221 if (sharedUserDefaults
)
222 defaults
->next
= sharedUserDefaults
;
223 sharedUserDefaults
= defaults
;
225 registerSaveOnExit();
232 WMGetDefaultsFromPath(char *path
)
234 WMUserDefaults
*defaults
;
240 assert(path
!= NULL
);
242 if (sharedUserDefaults
) {
243 defaults
= sharedUserDefaults
;
245 if (defaults
->path
&& strcmp(defaults
->path
, path
) == 0)
247 defaults
= defaults
->next
;
251 /* we didn't found the database we are looking for. Go read it. */
252 defaults
= wmalloc(sizeof(WMUserDefaults
));
253 memset(defaults
, 0, sizeof(WMUserDefaults
));
255 defaults
->defaults
= PLMakeDictionaryFromEntries(NULL
, NULL
, NULL
);
257 defaults
->searchList
= wmalloc(sizeof(proplist_t
)*2);
259 /* the domain we want, go in the first position */
260 name
= strrchr(path
, '/');
266 key
= PLMakeString(name
);
267 defaults
->searchList
[0] = key
;
269 domain
= PLGetProplistWithPath(path
);
274 domain
= PLMakeDictionaryFromEntries(NULL
, NULL
, NULL
);
275 p
= PLMakeString(path
);
276 PLSetFilename(domain
, p
);
280 defaults
->path
= wstrdup(path
);
282 defaults
->appDomain
= domain
;
285 PLInsertDictionaryEntry(defaults
->defaults
, key
, domain
);
290 defaults
->searchList
[1] = NULL
;
292 defaults
->searchListArray
=PLMakeArrayFromElements(NULL
,NULL
);
295 while (defaults
->searchList
[i
]) {
296 PLAppendArrayElement(defaults
->searchListArray
,
297 defaults
->searchList
[i
]);
301 if (sharedUserDefaults
)
302 defaults
->next
= sharedUserDefaults
;
303 sharedUserDefaults
= defaults
;
305 registerSaveOnExit();
312 WMGetUDObjectForKey(WMUserDefaults
*database
, char *defaultName
)
314 proplist_t domainName
, domain
;
315 proplist_t object
= NULL
;
316 proplist_t key
= PLMakeString(defaultName
);
319 while (database
->searchList
[i
] && !object
) {
320 domainName
= database
->searchList
[i
];
321 domain
= PLGetDictionaryEntry(database
->defaults
, domainName
);
323 object
= PLGetDictionaryEntry(domain
, key
);
334 WMSetUDObjectForKey(WMUserDefaults
*database
, proplist_t object
,
337 proplist_t key
= PLMakeString(defaultName
);
341 PLInsertDictionaryEntry(database
->appDomain
, key
, object
);
347 WMRemoveUDObjectForKey(WMUserDefaults
*database
, char *defaultName
)
349 proplist_t key
= PLMakeString(defaultName
);
353 PLRemoveDictionaryEntry(database
->appDomain
, key
);
360 WMGetUDStringForKey(WMUserDefaults
*database
, char *defaultName
)
364 val
= WMGetUDObjectForKey(database
, defaultName
);
369 if (!PLIsString(val
))
372 return PLGetString(val
);
377 WMGetUDIntegerForKey(WMUserDefaults
*database
, char *defaultName
)
383 val
= WMGetUDObjectForKey(database
, defaultName
);
388 if (!PLIsString(val
))
391 str
= PLGetString(val
);
395 if (sscanf(str
, "%i", &value
)!=1)
404 WMGetUDFloatForKey(WMUserDefaults
*database
, char *defaultName
)
410 val
= WMGetUDObjectForKey(database
, defaultName
);
412 if (!val
|| !PLIsString(val
))
415 if (!(str
= PLGetString(val
)))
418 if (sscanf(str
, "%f", &value
)!=1)
427 WMGetUDBoolForKey(WMUserDefaults
*database
, char *defaultName
)
433 val
= WMGetUDObjectForKey(database
, defaultName
);
438 if (!PLIsString(val
))
441 str
= PLGetString(val
);
445 if (sscanf(str
, "%i", &value
)==1 && value
!=0)
448 if (strcasecmp(str
, "YES")==0)
451 if (strcasecmp(str
, "Y")==0)
459 WMSetUDIntegerForKey(WMUserDefaults
*database
, int value
, char *defaultName
)
464 sprintf(buffer
, "%i", value
);
465 object
= PLMakeString(buffer
);
467 WMSetUDObjectForKey(database
, object
, defaultName
);
475 WMSetUDStringForKey(WMUserDefaults
*database
, char *value
, char *defaultName
)
479 object
= PLMakeString(value
);
481 WMSetUDObjectForKey(database
, object
, defaultName
);
488 WMSetUDFloatForKey(WMUserDefaults
*database
, float value
, char *defaultName
)
493 sprintf(buffer
, "%f", value
);
494 object
= PLMakeString(buffer
);
496 WMSetUDObjectForKey(database
, object
, defaultName
);
503 WMSetUDBoolForKey(WMUserDefaults
*database
, Bool value
, char *defaultName
)
505 static proplist_t yes
= NULL
, no
= NULL
;
508 yes
= PLMakeString("YES");
509 no
= PLMakeString("NO");
512 WMSetUDObjectForKey(database
, value
? yes
: no
, defaultName
);
517 WMGetUDSearchList(WMUserDefaults
*database
)
519 return database
->searchListArray
;
524 WMSetUDSearchList(WMUserDefaults
*database
, proplist_t list
)
528 if (database
->searchList
) {
530 while (database
->searchList
[i
]) {
531 PLRelease(database
->searchList
[i
]);
534 free(database
->searchList
);
536 if (database
->searchListArray
) {
537 PLRelease(database
->searchListArray
);
540 c
= PLGetNumberOfElements(list
);
541 database
->searchList
= wmalloc(sizeof(proplist_t
)*(c
+1));
543 for (i
=0; i
<c
; i
++) {
544 database
->searchList
[i
] = PLGetArrayElement(list
, i
);
547 database
->searchListArray
= PLDeepCopy(list
);