dav/tags.c

changeset 363
e9ed8e130ccf
parent 361
b6f2462ee055
child 373
dcc03142eb5f
equal deleted inserted replaced
362:9a75b6df3307 363:e9ed8e130ccf
33 #include <ucx/utils.h> 33 #include <ucx/utils.h>
34 34
35 #include "libxattr.h" 35 #include "libxattr.h"
36 36
37 #include "tags.h" 37 #include "tags.h"
38
39 #ifdef __APPLE__
40 #include <CoreFoundation/CoreFoundation.h>
41 #endif
38 42
39 UcxList* parse_text_taglist(const char *buf, size_t length) { 43 UcxList* parse_text_taglist(const char *buf, size_t length) {
40 UcxList *tags = NULL; 44 UcxList *tags = NULL;
41 45
42 int line_start = 0; 46 int line_start = 0;
173 } 177 }
174 lasttag = tagelm; 178 lasttag = tagelm;
175 } 179 }
176 return tag1; 180 return tag1;
177 } 181 }
182
183
184 #ifdef __APPLE__
185 static DavTag* tagstr2davtag(const char *str) {
186 const char *name = str;
187 const char *color = NULL;
188 size_t len = strlen(str);
189 size_t namelen = len;
190
191 if(len == 0) {
192 return NULL;
193 }
194
195 // start with 1 because the first char should not be a linebreak
196 for(int i=1;i<len;i++) {
197 if(str[i] == '\n') {
198 if(!color) {
199 color = str + i + 1;
200 namelen = i;
201 }
202 }
203 }
204 int colorlen = len - namelen - 1;
205
206 DavTag *tag = malloc(sizeof(DavTag));
207 tag = malloc(sizeof(DavTag));
208 tag->name = malloc(namelen + 1);
209 memcpy(tag->name, name, namelen);
210 tag->name[namelen] = 0;
211 if(colorlen > 0) {
212 tag->color = malloc(colorlen + 1);
213 memcpy(tag->color, color, colorlen);
214 tag->color[colorlen] = 0;
215 } else {
216 tag->color = NULL;
217 }
218
219 return tag;
220 }
221
222 UcxList* parse_macos_taglist(const char *buf, size_t length) {
223 UcxList *taglist = NULL;
224
225 CFDataRef data = CFDataCreateWithBytesNoCopy(
226 kCFAllocatorDefault,
227 (const UInt8*)buf,
228 length,
229 kCFAllocatorNull);
230 CFPropertyListRef propertylist = CFPropertyListCreateWithData(kCFAllocatorDefault, data, 0, NULL, NULL);
231 CFArrayRef array = propertylist;
232 int count = CFArrayGetCount(array);
233 for(int i=0;i<count;i++) {
234 CFStringRef str = CFArrayGetValueAtIndex(array, i);
235 int slen = CFStringGetLength(str);
236 size_t cstrbuflen = slen * 4 + 4;
237 char *cstr = malloc(cstrbuflen);
238 if(CFStringGetCString(str, cstr, cstrbuflen, kCFStringEncodingUTF8)) {
239 DavTag *tag = tagstr2davtag(cstr);
240 if(tag) {
241 taglist = ucx_list_append(taglist, tag);
242 }
243 }
244 free(cstr);
245 }
246
247 CFRelease(propertylist);
248 CFRelease(data);
249
250 return taglist;
251 }
252
253 UcxBuffer* create_macos_taglist(UcxList *tags) {
254 size_t count = ucx_list_size(tags);
255 if(count == 0) {
256 return NULL;
257 }
258
259 CFStringRef *strings = calloc(sizeof(CFStringRef), count);
260 int i = 0;
261 UCX_FOREACH(elm, tags) {
262 DavTag *tag = elm->data;
263 CFStringRef str = NULL;
264 if(tag->color) {
265 sstr_t s = sstrcat(3, sstr(tag->name), S("\n"), sstr(tag->color));
266 str = CFStringCreateWithCString(kCFAllocatorDefault, s.ptr, kCFStringEncodingUTF8);
267 free(s.ptr);
268 } else {
269 str = CFStringCreateWithCString(kCFAllocatorDefault, tag->name, kCFStringEncodingUTF8);
270 }
271 strings[i] = str;
272 i++;
273 }
274
275 CFPropertyListRef array = CFArrayCreate(kCFAllocatorDefault, (const void**)strings, count, &kCFTypeArrayCallBacks);
276 CFDataRef data = CFPropertyListCreateData(kCFAllocatorDefault, array, kCFPropertyListBinaryFormat_v1_0, 0, NULL);
277
278 UcxBuffer *buf = NULL;
279 if(data) {
280 int datalen = CFDataGetLength(data);
281 CFRange range;
282 range.location = 0;
283 range.length = datalen;
284 buf = ucx_buffer_new(NULL, datalen, 0);
285 CFDataGetBytes(data, range, (UInt8*)buf->space);
286 buf->size = datalen;
287 CFRelease(data);
288 }
289
290 for(int i=0;i<count;i++) {
291 CFRelease(strings[i]);
292 }
293 CFRelease(array);
294
295 return buf;
296 }
297
298 #else
299 UcxList* parse_macos_taglist(const char *buf, size_t length) {
300 return NULL;
301 }
302 UcxBuffer* create_macos_taglist(UcxList *tags) {
303 return NULL;
304 }
305 #endif
306

mercurial