application/davcontroller.c

changeset 78
ad7ced6cf00b
parent 77
5de33c2d94c6
--- a/application/davcontroller.c	Mon Nov 11 20:45:34 2024 +0100
+++ b/application/davcontroller.c	Mon Nov 11 22:42:09 2024 +0100
@@ -1433,7 +1433,6 @@
 
 static void resourceviewer_load_finished(UiEvent *event, void *data) {
     DavResourceViewer *doc = data;
-    doc->loaded = TRUE;
     
     if(doc->window_closed) {
         dav_resourceviewer_destroy(doc);
@@ -1450,6 +1449,8 @@
     }
       
     ui_set(doc->tabview, 1);
+    
+    doc->loaded = TRUE;
 }
 
 void dav_resourceviewer_load(UiObject *ui, DavResourceViewer *res) {
@@ -1460,6 +1461,57 @@
     ui_job(ui, jobthr_resourceviewer_load, res, resourceviewer_load_finished, res);
 }
 
+
+typedef struct ResourceViewerUploadFile {
+    UiObject *ui;
+    DavSession *sn;
+    char *path;
+    cxmutstr text;
+    int error;
+} ResourceViewerUploadFile;
+
+static int jobthr_upload_text(void *data) {
+    ResourceViewerUploadFile *upload = data;
+    
+    DavResource *res = dav_resource_new(upload->sn, upload->path);
+    dav_set_content_data(res, upload->text.ptr, upload->text.length);
+    upload->error = dav_store(res);
+    dav_resource_free(res);
+    
+    return 0;
+}
+
+static void uithr_upload_text_finished(UiEvent *event, void *data) {
+    ResourceViewerUploadFile *upload = data;
+    
+    if(upload->error) {
+        cxmutstr errormsg = cx_asprintf("Upload failed: %d", upload->sn->error); // TODO: add full error message
+        ui_dialog(event->obj, .title = "Error", .content = errormsg.ptr, .closebutton_label = "OK");
+        free(errormsg.ptr);
+        ui_set_group(event->obj->ctx, RESOURCEVIEWER_STATE_MODIFIED);
+    }
+    
+    dav_session_destroy(upload->sn);
+    free(upload->path);
+    free(upload->text.ptr);
+    free(upload);
+}
+
+void dav_resourceviewer_save(UiObject *ui, DavResourceViewer *res) {
+    if(res->type == DAV_RESOURCE_VIEW_TEXT) {
+        DavSession *newsn = dav_session_clone(res->current->session);
+        ResourceViewerUploadFile *upload = malloc(sizeof(ResourceViewerUploadFile));
+        upload->ui = ui;
+        upload->sn = newsn;
+        upload->path = strdup(res->current->path);
+        char *text = ui_get(res->text);
+        size_t textlen = strlen(text);
+        upload->text = cx_strdup(cx_strn(text, textlen));
+        ui_job(ui, jobthr_upload_text, upload, uithr_upload_text_finished, upload);
+        ui_unset_group(ui->ctx, RESOURCEVIEWER_STATE_MODIFIED);
+    }
+}
+
 void dav_resourceviewer_destroy(DavResourceViewer *res) {
     
 }

mercurial