add resourceviewer cleanup

Mon, 21 Oct 2024 15:45:12 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 21 Oct 2024 15:45:12 +0200
changeset 54
3ca3acefc66a
parent 53
da05df77652e
child 55
1ce14068ef31

add resourceviewer cleanup

application/application.h file | annotate | diff | comparison | revisions
application/davcontroller.c file | annotate | diff | comparison | revisions
application/davcontroller.h file | annotate | diff | comparison | revisions
application/window.c file | annotate | diff | comparison | revisions
ui/common/document.c file | annotate | diff | comparison | revisions
--- a/application/application.h	Mon Oct 21 13:22:45 2024 +0200
+++ b/application/application.h	Mon Oct 21 15:45:12 2024 +0200
@@ -123,6 +123,9 @@
     
     CxBuffer *text_content;
     char *tmp_file;
+    
+    bool loaded;
+    bool window_closed;
 } DavResourceViewer;
 
 typedef struct DavPropertyList {
--- a/application/davcontroller.c	Mon Oct 21 13:22:45 2024 +0200
+++ b/application/davcontroller.c	Mon Oct 21 15:45:12 2024 +0200
@@ -1254,85 +1254,12 @@
 
 static int jobthr_resourceviewer_load(void *data) {
     DavResourceViewer *doc = data;
-    
+     
     DavResource *res = dav_resource_new(doc->sn, doc->path);
     doc->error = dav_load(res);
     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);
@@ -1368,8 +1295,102 @@
     return 0;
 }
 
+static void resourceviewer_set_info(DavResourceViewer *doc) {
+    DavResource *res = doc->current;
+    if(!res) {
+        return;
+    }
+    
+    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");
+    }
+}
+
+static void resourceviewer_update_proplist(DavResourceViewer *doc) {
+    DavResource *res = doc->current;
+    if(!res) {
+        return;
+    }
+    
+    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);
+}
+
 static void resourceviewer_load_finished(UiEvent *event, void *data) {
     DavResourceViewer *doc = data;
+    doc->loaded = TRUE;
+    
+    if(doc->window_closed) {
+        dav_resourceviewer_destroy(doc);
+        return;
+    }
+    
+    resourceviewer_set_info(doc);
+    resourceviewer_update_proplist(doc);
     
     if(doc->type == DAV_RESOURCE_VIEW_TEXT) {
         ui_set(doc->text, doc->text_content->space);
@@ -1377,7 +1398,7 @@
         ui_image_load_file(doc->image, doc->tmp_file);
         unlink(doc->tmp_file);
     }
-    
+      
     ui_set(doc->tabview, 1);
 }
 
@@ -1389,3 +1410,6 @@
     ui_job(ui, jobthr_resourceviewer_load, res, resourceviewer_load_finished, res);
 }
 
+void dav_resourceviewer_destroy(DavResourceViewer *res) {
+    
+}
--- a/application/davcontroller.h	Mon Oct 21 13:22:45 2024 +0200
+++ b/application/davcontroller.h	Mon Oct 21 15:45:12 2024 +0200
@@ -76,6 +76,8 @@
 
 void dav_resourceviewer_load(UiObject *ui, DavResourceViewer *res);
 
+void dav_resourceviewer_destroy(DavResourceViewer *res);
+
 #ifdef	__cplusplus
 }
 #endif
--- a/application/window.c	Mon Oct 21 13:22:45 2024 +0200
+++ b/application/window.c	Mon Oct 21 15:45:12 2024 +0200
@@ -109,7 +109,13 @@
 
 
 
-
+static void resourceviewer_close(UiEvent *event, void *data) {
+    DavResourceViewer *doc = data;
+    doc->window_closed = TRUE;
+    if(doc->loaded) {
+        dav_resourceviewer_destroy(doc);
+    }
+}
 
 void resourceviewer_new(DavBrowser *browser, const char *path, DavResourceViewType type) {
     const char *name = util_resource_name(path);
@@ -117,6 +123,7 @@
     
     DavResourceViewer *doc = dav_resourceviewer_create(browser->sn, path, type);
     ui_attach_document(win->ctx, doc);
+    ui_context_closefunc(win->ctx, resourceviewer_close, doc);
     
     ui_tabview(win, .tabview = UI_TABVIEW_INVISIBLE, .varname = "tabview") {
         /* loading / message tab */
--- a/ui/common/document.c	Mon Oct 21 13:22:45 2024 +0200
+++ b/ui/common/document.c	Mon Oct 21 15:45:12 2024 +0200
@@ -96,7 +96,20 @@
 }
 
 void ui_document_destroy(void *doc) {
-    // TODO
+    UiContext *ctx = ui_document_context(doc);
+    if(ctx) {
+        UiEvent ev;
+        ev.window = NULL;
+        ev.document = doc;
+        ev.obj = NULL;
+        ev.eventdata = NULL;
+        ev.intval = 0;
+
+        if(ctx->close_callback) {
+            ctx->close_callback(&ev, ctx->close_data);
+        }
+        cxMempoolDestroy(ctx->mp);
+    }
 }
 
 UiContext* ui_document_context(void *doc) {

mercurial