]> git.tdb.fi Git - ext/subsurface.git/blob - macos.c
Split reading/writing preferences into OS specific files
[ext/subsurface.git] / macos.c
1 /* macos.c */
2 /* implements Mac OS X specific functions */
3 #include "display-gtk.h"
4 #include <CoreFoundation/CoreFoundation.h>
5
6 static CFURLRef fileURL;
7 static CFPropertyListRef propertyList;
8 static CFMutableDictionaryRef dict = NULL;
9
10 /* macos defines CFSTR to create a CFString object from a constant,
11  * but no similar macros if a C string variable is supposed to be
12  * the argument. We add this here (hardcoding the default allocator
13  * and MacRoman encoding */
14 #define CFSTR_VAR(_var) CFStringCreateWithCStringNoCopy(kCFAllocatorDefault,    \
15                                         (_var), kCFStringEncodingMacRoman,      \
16                                         kCFAllocatorNull)
17
18 void subsurface_open_conf(void)
19 {
20         CFStringRef errorString;
21         CFDataRef resourceData;
22         Boolean status;
23         SInt32 errorCode;
24
25         fileURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
26                                                 CFSTR("subsurface.pref"),// file path name
27                                                 kCFURLPOSIXPathStyle,    // interpret as POSIX path
28                                                 false );                 // is it a directory?
29
30         status = CFURLCreateDataAndPropertiesFromResource(kCFAllocatorDefault,
31                                                         fileURL, &resourceData,
32                                                         NULL, NULL, &errorCode);
33         if (status) {
34                 propertyList = CFPropertyListCreateFromXMLData(kCFAllocatorDefault,
35                                                         resourceData, kCFPropertyListImmutable,
36                                                         &errorString);
37                 CFRelease(resourceData);
38         }
39 }
40
41 void subsurface_set_conf(char *name, pref_type_t type, const void *value)
42 {
43         if (!dict)
44                 dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
45                                                 &kCFTypeDictionaryKeyCallBacks,
46                                                 &kCFTypeDictionaryValueCallBacks);
47         switch (type) {
48         case PREF_BOOL:
49                 CFDictionarySetValue(dict, CFSTR_VAR(name), value == NULL ? CFSTR("0") : CFSTR("1"));
50                 break;
51         case PREF_STRING:
52                 CFDictionarySetValue(dict, CFSTR_VAR(name), CFSTR_VAR(value));
53         }
54 }
55 const void *subsurface_get_conf(char *name, pref_type_t type)
56 {
57         CFStringRef dict_entry;
58
59         /* if no settings exist, we return the value for FALSE */
60         if (!propertyList)
61                 return NULL;
62
63         switch (type) {
64         case PREF_BOOL:
65                 dict_entry = CFDictionaryGetValue(propertyList, CFSTR_VAR(name));
66                 if (dict_entry && ! CFStringCompare(CFSTR("1"), dict_entry, 0))
67                         return (void *) 1;
68                 else
69                         return NULL;
70         case PREF_STRING:
71                 return CFStringGetCStringPtr(CFDictionaryGetValue(propertyList,
72                                                 CFSTR_VAR(name)), kCFStringEncodingMacRoman);
73         }
74         /* we shouldn't get here, but having this line makes the compiler happy */
75         return NULL;
76 }
77
78 void subsurface_close_conf(void)
79 {
80         Boolean status;
81         SInt32 errorCode;
82         CFDataRef xmlData;
83
84         propertyList = dict;
85         dict = NULL;
86         xmlData = CFPropertyListCreateXMLData(kCFAllocatorDefault, propertyList);
87         status = CFURLWriteDataAndPropertiesToResource (fileURL, xmlData, NULL, &errorCode);
88         // some error handling - but really, what can we do?
89         CFRelease(xmlData);
90         CFRelease(propertyList);
91 }