application/davcontroller.c

changeset 54
3ca3acefc66a
parent 53
da05df77652e
equal deleted inserted replaced
53:da05df77652e 54:3ca3acefc66a
1252 return tmp.ptr; 1252 return tmp.ptr;
1253 } 1253 }
1254 1254
1255 static int jobthr_resourceviewer_load(void *data) { 1255 static int jobthr_resourceviewer_load(void *data) {
1256 DavResourceViewer *doc = data; 1256 DavResourceViewer *doc = data;
1257 1257
1258 DavResource *res = dav_resource_new(doc->sn, doc->path); 1258 DavResource *res = dav_resource_new(doc->sn, doc->path);
1259 doc->error = dav_load(res); 1259 doc->error = dav_load(res);
1260 if(!doc->error) { 1260 if(!doc->error) {
1261 doc->current = res; 1261 doc->current = res;
1262
1263 char *url = util_concat_path(res->session->base_url, res->href);
1264 ui_set(doc->info_url, url);
1265 free(url);
1266
1267 ui_set(doc->info_name, res->name);
1268
1269 if(res->iscollection) {
1270 ui_set(doc->info_type, "Collection");
1271 } else {
1272 if(res->contenttype) {
1273 cxmutstr type = cx_asprintf("Resource (%s)", res->contenttype);
1274 ui_set(doc->info_type, type.ptr);
1275 free(type.ptr);
1276 } else {
1277 ui_set(doc->info_type, "Resource");
1278 }
1279 }
1280
1281 char *keyprop = dav_get_string_property_ns(
1282 res,
1283 DAV_NS,
1284 "crypto-key");
1285 if(keyprop) {
1286 cxmutstr info_encrypted = cx_asprintf("Yes Key: %s", keyprop);
1287 ui_set(doc->info_encrypted, info_encrypted.ptr);
1288 free(info_encrypted.ptr);
1289 } else {
1290 ui_set(doc->info_encrypted, "No");
1291 }
1292
1293 char *etag = dav_get_string_property_ns(
1294 res,
1295 "DAV:",
1296 "getetag");
1297 ui_set(doc->info_etag, etag);
1298
1299 if(res->contentlength > 0) {
1300 char *sz = util_size_str(FALSE, res->contentlength);
1301 cxmutstr size_str = cx_asprintf("%s (%" PRIu64 " bytes)", sz, res->contentlength);
1302 ui_set(doc->info_size, size_str.ptr);
1303 free(size_str.ptr);
1304 } else {
1305 ui_set(doc->info_size, "0");
1306 }
1307
1308
1309 size_t count = 0;
1310 DavPropName *properties = dav_get_property_names(res, &count);
1311 for(int i=0;i<count;i++) {
1312 DavPropertyList *prop = ui_malloc(doc->ctx, sizeof(DavPropertyList));
1313 prop->ns = properties[i].ns ? ui_strdup(doc->ctx, properties[i].ns) : NULL;
1314 prop->name = ui_strdup(doc->ctx, properties[i].name);
1315
1316 DavXmlNode *xval = dav_get_property_ns(res, prop->ns, prop->name);
1317 if(xval) {
1318 if(dav_xml_isstring(xval)) {
1319 char *value = dav_xml_getstring(xval);
1320 if(value) {
1321 prop->value_simplified = NULL;
1322 prop->value_full = ui_strdup(doc->ctx, value);
1323 }
1324 } else {
1325 DavXmlNode *x = xval->type == DAV_XML_ELEMENT ? xval : dav_xml_nextelm(xval);
1326 cxmutstr value = cx_asprintf_a(ui_allocator(doc->ctx), "<%s>...</%s>", x->name, x->name);
1327 prop->value_simplified = value.ptr;
1328 }
1329 }
1330
1331 ui_list_append(doc->properties, prop);
1332 }
1333 doc->properties->update(doc->properties, 0);
1334
1335 1262
1336 if(res->contentlength < DAV_RESOURCEVIEWER_PREVIEW_MAX_SIZE) { 1263 if(res->contentlength < DAV_RESOURCEVIEWER_PREVIEW_MAX_SIZE) {
1337 if(doc->type == DAV_RESOURCE_VIEW_TEXT) { 1264 if(doc->type == DAV_RESOURCE_VIEW_TEXT) {
1338 doc->text_content = cxBufferCreate(NULL, res->contentlength, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS); 1265 doc->text_content = cxBufferCreate(NULL, res->contentlength, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS);
1339 int err = dav_get_content(res, doc->text_content, (dav_write_func)cxBufferWrite); 1266 int err = dav_get_content(res, doc->text_content, (dav_write_func)cxBufferWrite);
1366 } 1293 }
1367 1294
1368 return 0; 1295 return 0;
1369 } 1296 }
1370 1297
1298 static void resourceviewer_set_info(DavResourceViewer *doc) {
1299 DavResource *res = doc->current;
1300 if(!res) {
1301 return;
1302 }
1303
1304 char *url = util_concat_path(res->session->base_url, res->href);
1305 ui_set(doc->info_url, url);
1306 free(url);
1307
1308 ui_set(doc->info_name, res->name);
1309
1310 if(res->iscollection) {
1311 ui_set(doc->info_type, "Collection");
1312 } else {
1313 if(res->contenttype) {
1314 cxmutstr type = cx_asprintf("Resource (%s)", res->contenttype);
1315 ui_set(doc->info_type, type.ptr);
1316 free(type.ptr);
1317 } else {
1318 ui_set(doc->info_type, "Resource");
1319 }
1320 }
1321
1322 char *keyprop = dav_get_string_property_ns(
1323 res,
1324 DAV_NS,
1325 "crypto-key");
1326 if(keyprop) {
1327 cxmutstr info_encrypted = cx_asprintf("Yes Key: %s", keyprop);
1328 ui_set(doc->info_encrypted, info_encrypted.ptr);
1329 free(info_encrypted.ptr);
1330 } else {
1331 ui_set(doc->info_encrypted, "No");
1332 }
1333
1334 char *etag = dav_get_string_property_ns(
1335 res,
1336 "DAV:",
1337 "getetag");
1338 ui_set(doc->info_etag, etag);
1339
1340 if(res->contentlength > 0) {
1341 char *sz = util_size_str(FALSE, res->contentlength);
1342 cxmutstr size_str = cx_asprintf("%s (%" PRIu64 " bytes)", sz, res->contentlength);
1343 ui_set(doc->info_size, size_str.ptr);
1344 free(size_str.ptr);
1345 } else {
1346 ui_set(doc->info_size, "0");
1347 }
1348 }
1349
1350 static void resourceviewer_update_proplist(DavResourceViewer *doc) {
1351 DavResource *res = doc->current;
1352 if(!res) {
1353 return;
1354 }
1355
1356 size_t count = 0;
1357 DavPropName *properties = dav_get_property_names(res, &count);
1358 for(int i=0;i<count;i++) {
1359 DavPropertyList *prop = ui_malloc(doc->ctx, sizeof(DavPropertyList));
1360 prop->ns = properties[i].ns ? ui_strdup(doc->ctx, properties[i].ns) : NULL;
1361 prop->name = ui_strdup(doc->ctx, properties[i].name);
1362
1363 DavXmlNode *xval = dav_get_property_ns(res, prop->ns, prop->name);
1364 if(xval) {
1365 if(dav_xml_isstring(xval)) {
1366 char *value = dav_xml_getstring(xval);
1367 if(value) {
1368 prop->value_simplified = NULL;
1369 prop->value_full = ui_strdup(doc->ctx, value);
1370 }
1371 } else {
1372 DavXmlNode *x = xval->type == DAV_XML_ELEMENT ? xval : dav_xml_nextelm(xval);
1373 cxmutstr value = cx_asprintf_a(ui_allocator(doc->ctx), "<%s>...</%s>", x->name, x->name);
1374 prop->value_simplified = value.ptr;
1375 }
1376 }
1377
1378 ui_list_append(doc->properties, prop);
1379 }
1380 doc->properties->update(doc->properties, 0);
1381 }
1382
1371 static void resourceviewer_load_finished(UiEvent *event, void *data) { 1383 static void resourceviewer_load_finished(UiEvent *event, void *data) {
1372 DavResourceViewer *doc = data; 1384 DavResourceViewer *doc = data;
1385 doc->loaded = TRUE;
1386
1387 if(doc->window_closed) {
1388 dav_resourceviewer_destroy(doc);
1389 return;
1390 }
1391
1392 resourceviewer_set_info(doc);
1393 resourceviewer_update_proplist(doc);
1373 1394
1374 if(doc->type == DAV_RESOURCE_VIEW_TEXT) { 1395 if(doc->type == DAV_RESOURCE_VIEW_TEXT) {
1375 ui_set(doc->text, doc->text_content->space); 1396 ui_set(doc->text, doc->text_content->space);
1376 } else if(doc->type == DAV_RESOURCE_VIEW_IMAGE) { 1397 } else if(doc->type == DAV_RESOURCE_VIEW_IMAGE) {
1377 ui_image_load_file(doc->image, doc->tmp_file); 1398 ui_image_load_file(doc->image, doc->tmp_file);
1378 unlink(doc->tmp_file); 1399 unlink(doc->tmp_file);
1379 } 1400 }
1380 1401
1381 ui_set(doc->tabview, 1); 1402 ui_set(doc->tabview, 1);
1382 } 1403 }
1383 1404
1384 void dav_resourceviewer_load(UiObject *ui, DavResourceViewer *res) { 1405 void dav_resourceviewer_load(UiObject *ui, DavResourceViewer *res) {
1385 ui_set(res->loading, 1); 1406 ui_set(res->loading, 1);
1387 ui_set(res->tabview, 0); 1408 ui_set(res->tabview, 0);
1388 1409
1389 ui_job(ui, jobthr_resourceviewer_load, res, resourceviewer_load_finished, res); 1410 ui_job(ui, jobthr_resourceviewer_load, res, resourceviewer_load_finished, res);
1390 } 1411 }
1391 1412
1413 void dav_resourceviewer_destroy(DavResourceViewer *res) {
1414
1415 }

mercurial