add resourceviewer info and properties

Mon, 21 Oct 2024 11:14:26 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 21 Oct 2024 11:14:26 +0200
changeset 52
0c881944fa10
parent 51
e324291ca9f8
child 53
da05df77652e

add resourceviewer info and properties

application/application.h file | annotate | diff | comparison | revisions
application/davcontroller.c file | annotate | diff | comparison | revisions
application/window.c file | annotate | diff | comparison | revisions
application/window.h file | annotate | diff | comparison | revisions
ui/common/context.c file | annotate | diff | comparison | revisions
ui/common/types.c file | annotate | diff | comparison | revisions
ui/ui/toolkit.h file | annotate | diff | comparison | revisions
--- a/application/application.h	Sun Oct 20 21:24:13 2024 +0200
+++ b/application/application.h	Mon Oct 21 11:14:26 2024 +0200
@@ -111,6 +111,13 @@
     UiText *text;
     UiGeneric *image;
     
+    UiString *info_url;
+    UiString *info_name;
+    UiString *info_type;
+    UiString *info_encrypted;
+    UiString *info_etag;
+    UiString *info_size;
+    
     int error;
     char *message_str;
     
--- a/application/davcontroller.c	Sun Oct 20 21:24:13 2024 +0200
+++ b/application/davcontroller.c	Mon Oct 21 11:14:26 2024 +0200
@@ -256,7 +256,7 @@
         }
     }
     
-    dav_resourceviewer_new(browser, res->path, type);
+    resourceviewer_new(browser, res->path, type);
 }
 
 void davbrowser_add2navstack(DavBrowser *browser, const char *base, const char *path) {
@@ -967,7 +967,7 @@
     download->browser = browser;
     download->sn = reslist->session;
     download->download_sn = dav_session_clone(download->sn);
-    download->reslist = reslist;
+    download->reslist = reslist; // TODO: is this safe or do we need a copy?
     download->local_path = strdup(local_path);
 
     download->queue = ui_threadpool_create(1);
@@ -1234,6 +1234,13 @@
     
     doc->properties = ui_list_new(ctx, "properties");
     
+    doc->info_url = ui_string_new(ctx, "info_url");
+    doc->info_name = ui_string_new(ctx, "info_name");
+    doc->info_type = ui_string_new(ctx, "info_type");
+    doc->info_encrypted = ui_string_new(ctx, "info_encrypted");
+    doc->info_etag = ui_string_new(ctx, "info_etag");
+    doc->info_size = ui_string_new(ctx, "info_size");
+    
     return doc;
 }
 
@@ -1256,6 +1263,79 @@
     if(!doc->error) {
         doc->current = res;
         
+        char *url = util_concat_path(res->session->base_url, res->href);
+        ui_set(doc->info_url, url);
+        free(url);
+        
+        ui_set(doc->info_name, res->name);
+        
+        if(res->iscollection) {
+            ui_set(doc->info_type, "Collection");
+        } else {
+            if(res->contenttype) {
+                cxmutstr type = cx_asprintf("Resource (%s)", res->contenttype);
+                ui_set(doc->info_type, type.ptr);
+                free(type.ptr);
+            } else {
+                ui_set(doc->info_type, "Resource");
+            }
+        }
+        
+        char *keyprop = dav_get_string_property_ns(
+            res,
+            DAV_NS,
+            "crypto-key");
+        if(keyprop) {
+            cxmutstr info_encrypted = cx_asprintf("Yes   Key: %s", keyprop);
+            ui_set(doc->info_encrypted, info_encrypted.ptr);
+            free(info_encrypted.ptr);
+        } else {
+            ui_set(doc->info_encrypted, "No");
+        }
+        
+        char *etag = dav_get_string_property_ns(
+            res,
+            "DAV:",
+            "getetag");
+        ui_set(doc->info_etag, etag);
+        
+        if(res->contentlength > 0) {
+            char *sz = util_size_str(FALSE, res->contentlength);
+            cxmutstr size_str = cx_asprintf("%s (%" PRIu64 " bytes)", sz, res->contentlength);
+            ui_set(doc->info_size, size_str.ptr);
+            free(size_str.ptr);
+        } else {
+            ui_set(doc->info_size, "0");
+        }
+        
+        
+        size_t count = 0;
+        DavPropName *properties = dav_get_property_names(res, &count);
+        for(int i=0;i<count;i++) {
+            DavPropertyList *prop = ui_malloc(doc->ctx, sizeof(DavPropertyList));
+            prop->ns = properties[i].ns ? ui_strdup(doc->ctx, properties[i].ns) : NULL;
+            prop->name = ui_strdup(doc->ctx, properties[i].name);
+            
+            DavXmlNode *xval = dav_get_property_ns(res, prop->ns, prop->name);
+            if(xval) {
+                if(dav_xml_isstring(xval)) {
+                    char *value = dav_xml_getstring(xval);
+                    if(value) {
+                        prop->value_simplified = NULL;
+                        prop->value_full = ui_strdup(doc->ctx, value);
+                    }
+                } else {
+                    DavXmlNode *x = xval->type == DAV_XML_ELEMENT ? xval : dav_xml_nextelm(xval);
+                    cxmutstr value = cx_asprintf_a(ui_allocator(doc->ctx), "<%s>...</%s>", x->name, x->name);
+                    prop->value_simplified = value.ptr;
+                }
+            }
+            
+            ui_list_append(doc->properties, prop);
+        }
+        doc->properties->update(doc->properties, 0);
+        
+        
         if(res->contentlength < DAV_RESOURCEVIEWER_PREVIEW_MAX_SIZE) {
             if(doc->type == DAV_RESOURCE_VIEW_TEXT) {
                 doc->text_content = cxBufferCreate(NULL, res->contentlength, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS);
--- a/application/window.c	Sun Oct 20 21:24:13 2024 +0200
+++ b/application/window.c	Mon Oct 21 11:14:26 2024 +0200
@@ -111,7 +111,7 @@
 
 
 
-void dav_resourceviewer_new(DavBrowser *browser, const char *path, DavResourceViewType type) {
+void resourceviewer_new(DavBrowser *browser, const char *path, DavResourceViewType type) {
     const char *name = util_resource_name(path);
     UiObject *win = ui_simple_window(name, NULL);
     
@@ -140,6 +140,34 @@
                     }
                 }
                 
+                ui_tab(win, "Info") {
+                    ui_grid(win, .margin = 16, .columnspacing = 30, .rowspacing = 6) {
+                        ui_llabel(win, .label = "URL");
+                        ui_llabel(win, .varname = "info_url");
+                        ui_newline(win);
+                        
+                        ui_llabel(win, .label = "Name");
+                        ui_llabel(win, .varname = "info_name");
+                        ui_newline(win);
+                        
+                        ui_llabel(win, .label = "Type");
+                        ui_llabel(win, .varname = "info_type");
+                        ui_newline(win);
+                        
+                        ui_llabel(win, .label = "Encrypted");
+                        ui_llabel(win, .varname = "info_encrypted");
+                        ui_newline(win);
+                        
+                        ui_llabel(win, .label = "ETag");
+                        ui_llabel(win, .varname = "info_etag");
+                        ui_newline(win);
+                        
+                        ui_llabel(win, .label = "Size");
+                        ui_llabel(win, .varname = "info_size");
+                        ui_newline(win);
+                    }
+                }
+                
                 ui_tab(win, "Properties") {
                     UiModel* model = ui_model(win->ctx, UI_STRING, "Namespace", UI_STRING, "Name", UI_STRING, "Value", -1);
                     model->getvalue = (ui_getvaluefunc) resourceviewer_proplist_getvalue;
@@ -155,6 +183,18 @@
 }
 
 void* resourceviewer_proplist_getvalue(DavPropertyList *property, int col) {
+    switch(col) {
+        case 0: {
+            return property->ns;
+        }
+        case 1: {
+            return property->name;
+        }
+        case 2: {
+            return property->value_simplified ? property->value_simplified : property->value_full;
+        }
+    }
+    
     return NULL;
 }
 
--- a/application/window.h	Sun Oct 20 21:24:13 2024 +0200
+++ b/application/window.h	Mon Oct 21 11:14:26 2024 +0200
@@ -56,7 +56,7 @@
 void window_progress(MainWindow *win, int on);
 
 
-void dav_resourceviewer_new(DavBrowser *browser, const char *path, DavResourceViewType type);
+void resourceviewer_new(DavBrowser *browser, const char *path, DavResourceViewType type);
 void* resourceviewer_proplist_getvalue(DavPropertyList *property, int col);
 
 void action_go_back(UiEvent *event, void *data);
--- a/ui/common/context.c	Sun Oct 20 21:24:13 2024 +0200
+++ b/ui/common/context.c	Mon Oct 21 11:14:26 2024 +0200
@@ -541,8 +541,8 @@
     cxListAdd(ctx->group_widgets, &gw);
 }
 
-UIEXPORT void *ui_ctx_allocator(UiContext *ctx) {
-    return (void*) ctx ? ctx->allocator : NULL;
+UIEXPORT void *ui_allocator(UiContext *ctx) {
+    return ctx ? (void*)ctx->allocator : NULL;
 }
 
 void* ui_malloc(UiContext *ctx, size_t size) {
@@ -563,3 +563,11 @@
     return ctx ? cxRealloc(ctx->allocator, ptr, size) : NULL;
 }
 
+UIEXPORT char* ui_strdup(UiContext *ctx, const char *str) {
+    if(!ctx) {
+        return NULL;
+    }
+    cxstring s = cx_str(str);
+    cxmutstr d = cx_strdup_a(ctx->allocator, s);
+    return d.ptr;
+}
--- a/ui/common/types.c	Sun Oct 20 21:24:13 2024 +0200
+++ b/ui/common/types.c	Mon Oct 21 11:14:26 2024 +0200
@@ -158,6 +158,10 @@
     cxListClear(list->data);
 }
 
+UIEXPORT void ui_list_update(UiList *list) {
+    list->update(list, 0);
+}
+
 void ui_list_addobsv(UiList *list, ui_callback f, void *data) {
     list->observers = ui_add_observer(list->observers, f, data);
 }
--- a/ui/ui/toolkit.h	Sun Oct 20 21:24:13 2024 +0200
+++ b/ui/ui/toolkit.h	Mon Oct 21 11:14:26 2024 +0200
@@ -447,11 +447,12 @@
 UIEXPORT void ui_unset_group(UiContext *ctx, int group);
 UIEXPORT int* ui_active_groups(UiContext *ctx, int *ngroups);
     
-UIEXPORT void *ui_ctx_allocator(UiContext *ctx);
+UIEXPORT void* ui_allocator(UiContext *ctx);
 UIEXPORT void* ui_malloc(UiContext *ctx, size_t size);
 UIEXPORT void* ui_calloc(UiContext *ctx, size_t nelem, size_t elsize);
 UIEXPORT void  ui_free(UiContext *ctx, void *ptr);
 UIEXPORT void* ui_realloc(UiContext *ctx, void *ptr, size_t size);
+UIEXPORT char* ui_strdup(UiContext *ctx, const char *str);
 
 // types
 
@@ -502,13 +503,14 @@
 UIEXPORT void* ui_list_first(UiList *list);
 UIEXPORT void* ui_list_next(UiList *list);
 UIEXPORT void* ui_list_get(UiList *list, int i);
-UIEXPORT int   ui_list_count(UiList *list);
-UIEXPORT void  ui_list_append(UiList *list, void *data);
-UIEXPORT void  ui_list_prepend(UiList *list, void *data);
+UIEXPORT int ui_list_count(UiList *list);
+UIEXPORT void ui_list_append(UiList *list, void *data);
+UIEXPORT void ui_list_prepend(UiList *list, void *data);
 UIEXPORT void ui_list_remove(UiList *list, int i);
 UIEXPORT void ui_list_clear(UiList *list);
-UIEXPORT void  ui_list_addobsv(UiList *list, ui_callback f, void *data);
-UIEXPORT void  ui_list_notify(UiList *list);
+UIEXPORT void ui_list_update(UiList *list);
+UIEXPORT void ui_list_addobsv(UiList *list, ui_callback f, void *data);
+UIEXPORT void ui_list_notify(UiList *list);
 
 UIEXPORT UiListSelection ui_list_getselection(UiList *list);
 

mercurial