37 #include "crypto.h" |
37 #include "crypto.h" |
38 #include "ucx/buffer.h" |
38 #include "ucx/buffer.h" |
39 #include "ucx/utils.h" |
39 #include "ucx/utils.h" |
40 |
40 |
41 #include "resource.h" |
41 #include "resource.h" |
|
42 #include "davqlexec.h" |
42 |
43 |
43 #define xstreq(a,b) xmlStrEqual(BAD_CAST a, BAD_CAST b) |
44 #define xstreq(a,b) xmlStrEqual(BAD_CAST a, BAD_CAST b) |
44 |
45 |
45 DavResource* dav_resource_new(DavSession *sn, char *path) { |
46 DavResource* dav_resource_new(DavSession *sn, char *path) { |
46 //char *href = util_url_path(url); |
47 //char *href = util_url_path(url); |
234 free(key.data); |
235 free(key.data); |
235 } |
236 } |
236 |
237 |
237 char* resource_get_property(DavResource *res, char *ns, char *name) { |
238 char* resource_get_property(DavResource *res, char *ns, char *name) { |
238 UcxKey key = dav_property_key(ns, name); |
239 UcxKey key = dav_property_key(ns, name); |
239 DavResourceData *data = (DavResourceData*)res->data; |
240 char *property = resource_get_property_k(res, key); |
240 char *property = ucx_map_get(data->properties, key); |
|
241 free(key.data); |
241 free(key.data); |
242 return property; |
242 return property; |
243 } |
243 } |
244 |
244 |
|
245 char* resource_get_property_k(DavResource *res, UcxKey key) { |
|
246 DavResourceData *data = (DavResourceData*)res->data; |
|
247 return ucx_map_get(data->properties, key); |
|
248 } |
|
249 |
245 UcxKey dav_property_key(char *ns, char *name) { |
250 UcxKey dav_property_key(char *ns, char *name) { |
|
251 return dav_property_key_a(ucx_default_allocator(), ns, name); |
|
252 } |
|
253 |
|
254 UcxKey dav_property_key_a(UcxAllocator *a, char *ns, char *name) { |
246 sstr_t ns_str = sstr(ns); |
255 sstr_t ns_str = sstr(ns); |
247 sstr_t name_str = sstr(name); |
256 sstr_t name_str = sstr(name); |
248 |
257 |
249 sstr_t key; |
258 sstr_t key; |
250 key = sstrcat(4, ns_str, S("\0"), name_str, S("\0")); |
259 key = sstrcat_a(a, 4, ns_str, S("\0"), name_str, S("\0")); |
251 |
260 |
252 return ucx_key(key.ptr, key.length); |
261 return ucx_key(key.ptr, key.length); |
253 } |
262 } |
254 |
263 |
255 |
264 |
267 } else { |
276 } else { |
268 child->prev = NULL; |
277 child->prev = NULL; |
269 parent->children = child; |
278 parent->children = child; |
270 } |
279 } |
271 child->parent = parent; |
280 child->parent = parent; |
|
281 } |
|
282 |
|
283 static int resource_cmp(DavResource *res1, DavResource *res2, DavOrderCriterion *cr) { |
|
284 if(!(res1 && res2)) { |
|
285 return 0; |
|
286 } |
|
287 |
|
288 int ret; |
|
289 if(cr->type == 0) { |
|
290 switch(cr->column.resprop) { |
|
291 case DAVQL_RES_NAME: { |
|
292 ret = strcmp(res1->name, res2->name); |
|
293 break; |
|
294 } |
|
295 case DAVQL_RES_PATH: { |
|
296 ret = strcmp(res1->path, res2->path); |
|
297 break; |
|
298 } |
|
299 case DAVQL_RES_HREF: { |
|
300 ret = strcmp(res1->href, res2->href); |
|
301 break; |
|
302 } |
|
303 case DAVQL_RES_CONTENTLENGTH: { |
|
304 int c = res1->contentlength == res2->contentlength; |
|
305 ret = c ? 0 : (res1->contentlength < res2->contentlength?-1:1); |
|
306 break; |
|
307 } |
|
308 case DAVQL_RES_CONTENTTYPE: { |
|
309 ret = strcmp(res1->contenttype, res2->contenttype); |
|
310 break; |
|
311 } |
|
312 case DAVQL_RES_CREATIONDATE: { |
|
313 int c = res1->creationdate == res2->creationdate; |
|
314 ret = c ? 0 : (res1->creationdate < res2->creationdate?-1:1); |
|
315 break; |
|
316 } |
|
317 case DAVQL_RES_LASTMODIFIED: { |
|
318 int c = res1->lastmodified == res2->lastmodified; |
|
319 ret = c ? 0 : (res1->lastmodified < res2->lastmodified?-1:1); |
|
320 break; |
|
321 } |
|
322 case DAVQL_RES_ISCOLLECTION: { |
|
323 int c = res1->iscollection == res2->iscollection; |
|
324 ret = c ? 0 : (res1->iscollection < res2->iscollection?-1:1); |
|
325 break; |
|
326 } |
|
327 default: ret = 0; |
|
328 } |
|
329 } else if(cr->type == 1) { |
|
330 char *value1 = resource_get_property_k(res1, cr->column.property); |
|
331 char *value2 = resource_get_property_k(res2, cr->column.property); |
|
332 if(!value1) { |
|
333 ret = value2 ? -1 : 0; |
|
334 } else if(!value2) { |
|
335 ret = value1 ? 1 : 0; |
|
336 } else { |
|
337 ret = strcmp(value1, value2); |
|
338 } |
|
339 } else { |
|
340 return 0; |
|
341 } |
|
342 |
|
343 return cr->descending ? -ret : ret; |
|
344 } |
|
345 |
|
346 void resource_add_ordered_child(DavResource *parent, DavResource *child, UcxList *ordercr) { |
|
347 if(!ordercr) { |
|
348 resource_add_child(parent, child); |
|
349 return; |
|
350 } |
|
351 |
|
352 child->parent = parent; |
|
353 |
|
354 if(!parent->children) { |
|
355 child->next = NULL; |
|
356 child->prev = NULL; |
|
357 parent->children = child; |
|
358 } else { |
|
359 DavResource *resource = parent->children; |
|
360 while(resource) { |
|
361 int r = 0; |
|
362 UCX_FOREACH(elm, ordercr) { |
|
363 DavOrderCriterion *cr = elm->data; |
|
364 r = resource_cmp(child, resource, cr); |
|
365 if(r != 0) { |
|
366 break; |
|
367 } |
|
368 } |
|
369 |
|
370 if(r < 0 || !resource->next) { |
|
371 // insert child before resource |
|
372 child->prev = resource->prev; |
|
373 child->next = resource; |
|
374 if(resource->prev) { |
|
375 resource->prev->next = child; |
|
376 } else { |
|
377 parent->children = child; |
|
378 } |
|
379 resource->prev = child; |
|
380 break; |
|
381 } else { |
|
382 resource = resource->next; |
|
383 } |
|
384 } |
|
385 } |
272 } |
386 } |
273 |
387 |
274 char* dav_get_property(DavResource *res, char *name) { |
388 char* dav_get_property(DavResource *res, char *name) { |
275 char *pns; |
389 char *pns; |
276 char *pname; |
390 char *pname; |