application/davcontroller.c

changeset 28
1ecc1183f046
parent 27
c254ed644edf
child 29
3fc287f06305
--- a/application/davcontroller.c	Sun Feb 11 13:59:02 2024 +0100
+++ b/application/davcontroller.c	Sun Feb 11 15:59:56 2024 +0100
@@ -734,10 +734,40 @@
     return 0;
 }
 
+static int uithr_pathop_mkcol_error(void *data) {
+    DavPathOpResult *result = data;
+
+    cxmutstr msg = cx_asprintf("Cannot create collection %s", result->path);
+    ui_dialog(result->ui, .title = "Error", .content = msg.ptr, .button1_label = "OK");
+    free(msg.ptr);
+
+    if (result->errormsg) {
+        free(result->errormsg);
+    }
+    free(result->path);
+    free(result);
+    return 0;
+}
+
+static int uithr_pathop_mkcol_sucess(void *data) {
+    DavPathOpResult *result = data;
+
+    if (result->browser->current == result->collection && result->browser->res_counter == result->collection_ctn) {
+        DavResource *res = dav_resource_new(result->browser->sn, result->path);
+        res->iscollection = TRUE;
+        // TODO: add the resource at the correct position or sort the list after append
+        ui_list_append(result->browser->resources, res);
+        result->browser->resources->update(result->browser->resources, 0);
+    }
+
+    free(result->path);
+    free(result);
+    return 0;
+}
+
 static int jobthr_path_op(void *data) {
     DavPathOp *op = data;
 
-
     for (int i = op->nelm-1; i >= 0; i--) {
         if (op->path[i]) {
             DavResource *res = dav_resource_new(op->sn, op->path[i]);
@@ -759,6 +789,14 @@
                     result_callback = uithr_pathop_delete_error;
                 }
                 ui_call_mainthread(result_callback, result);
+            } else if (op->op == DAV_PATH_OP_MKCOL) {
+                res->iscollection = TRUE;
+                ui_threadfunc result_callback = uithr_pathop_mkcol_sucess;
+                if (dav_create(res)) {
+                    result->errormsg = op->sn->errorstr ? strdup(op->sn->errorstr) : NULL;
+                    result_callback = uithr_pathop_mkcol_error;
+                }
+                ui_call_mainthread(result_callback, result);
             }
 
             dav_resource_free(res);
@@ -799,3 +837,21 @@
 
     ui_job(ui, jobthr_path_op, op, NULL, NULL);
 }
+
+void davbrowser_mkcol(UiObject *ui, DavBrowser *browser, const char *name) {
+    DavPathOp *op = malloc(sizeof(DavPathOp));
+    op->ui = ui;
+    op->browser = browser;
+    op->op = DAV_PATH_OP_MKCOL;
+    op->sn = dav_session_clone(browser->sn);
+    op->path = calloc(1, sizeof(char*));
+    op->list_indices = calloc(1, sizeof(size_t));
+    op->nelm = 1;
+
+    op->path[0] = util_concat_path(browser->current->path, name);
+
+    op->collection = browser->current;
+    op->collection_ctn = browser->res_counter;
+
+    ui_job(ui, jobthr_path_op, op, NULL, NULL);
+}

mercurial