implement new-collection toolbar button

Sun, 11 Feb 2024 15:59:56 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 11 Feb 2024 15:59:56 +0100
changeset 28
1ecc1183f046
parent 27
c254ed644edf
child 29
3fc287f06305

implement new-collection toolbar button

application/application.c file | annotate | diff | comparison | revisions
application/application.h file | annotate | diff | comparison | revisions
application/davcontroller.c file | annotate | diff | comparison | revisions
application/davcontroller.h file | annotate | diff | comparison | revisions
ui/ui/window.h file | annotate | diff | comparison | revisions
ui/winui/window.cpp file | annotate | diff | comparison | revisions
--- a/application/application.c	Sun Feb 11 13:59:02 2024 +0100
+++ b/application/application.c	Sun Feb 11 15:59:56 2024 +0100
@@ -85,8 +85,8 @@
     ui_toolbar_menu("Repo", .label = "Repository") {
         ui_menu_itemlist(.varname = "repolist", .getvalue = davrepo_getname, .onselect = action_repo_selected);
     }
-	ui_toolbar_item("NewFolder", .icon = "NewFolder");
-	ui_toolbar_item("NewFile", .icon = "Add");
+	ui_toolbar_item("NewFolder", .icon = "NewFolder", .onclick = action_mkcol);
+	ui_toolbar_item("NewFile", .icon = "Add", .onclick = action_newfile);
 	ui_toolbar_item("Upload", .label = "Upload", .icon = "Upload", .onclick = action_upload_file);
 	ui_toolbar_item("Download", .icon = "SaveLocal");
 	ui_toolbar_item("Remove", .icon = "Delete", .onclick = action_delete );
@@ -187,3 +187,20 @@
 	}
 	ui_listselection_free(sel);
 }
+
+void action_newfile(UiEvent *event, void *data) {
+
+}
+
+
+static void newfolderdialog_result(UiEvent *event, void *data) {
+	DavBrowser *browser = event->document;
+	char *path = event->eventdata;
+	if (event->intval == 1) {
+		davbrowser_mkcol(event->obj, browser, path);
+	}
+}
+
+void action_mkcol(UiEvent *event, void *data) {
+	ui_dialog(event->obj, .content = "New Folder", .input =  TRUE, .button1_label = "Create Folder", .button2_label = "Close", .result = newfolderdialog_result);
+}
--- a/application/application.h	Sun Feb 11 13:59:02 2024 +0100
+++ b/application/application.h	Sun Feb 11 15:59:56 2024 +0100
@@ -111,6 +111,10 @@
 
 void action_delete(UiEvent *event, void *data);
 
+void action_newfile(UiEvent *event, void *data);
+
+void action_mkcol(UiEvent *event, void *data);
+
 #ifdef	__cplusplus
 }
 #endif
--- 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);
+}
--- a/application/davcontroller.h	Sun Feb 11 13:59:02 2024 +0100
+++ b/application/davcontroller.h	Sun Feb 11 15:59:56 2024 +0100
@@ -60,6 +60,8 @@
 
 void davbrowser_delete(UiObject *ui, DavBrowser *browser, UiListSelection selection);
 
+void davbrowser_mkcol(UiObject *ui, DavBrowser *browser, const char *name);
+
 #ifdef	__cplusplus
 }
 #endif
--- a/ui/ui/window.h	Sun Feb 11 13:59:02 2024 +0100
+++ b/ui/ui/window.h	Sun Feb 11 15:59:56 2024 +0100
@@ -45,6 +45,7 @@
     const char *button1_label;
     const char *button2_label;
     const char *closebutton_label;
+    UiBool input;
     ui_callback result;
     void *resultdata;
 } UiDialogArgs;
--- a/ui/winui/window.cpp	Sun Feb 11 13:59:02 2024 +0100
+++ b/ui/winui/window.cpp	Sun Feb 11 15:59:56 2024 +0100
@@ -220,16 +220,41 @@
 		dialog.Title(winrt::box_value(str));
 		free(str);
 	}
-	if (args.content) {
-		wchar_t *str = str2wstr(args.content, nullptr);
-		dialog.Content(winrt::box_value(str));
-		free(str);
+
+	TextBox textfield{ nullptr };
+	if (args.input) {
+		StackPanel panel = StackPanel();
+		panel.Orientation(Orientation::Vertical);
+		if (args.content) {
+			wchar_t *str = str2wstr(args.content, nullptr);
+			TextBlock label = TextBlock();
+			label.Text(str);
+			panel.Children().Append(label);
+			free(str);
+		}
+
+		textfield = TextBox();
+		Thickness margin = { 0, 5, 0, 0 };
+		textfield.Margin(margin);
+		panel.Margin(margin);
+
+		panel.Children().Append(textfield);
+
+		dialog.Content(panel);
+
+	} else {
+		if (args.content) {
+			wchar_t *str = str2wstr(args.content, nullptr);
+			dialog.Content(winrt::box_value(str));
+			free(str);
+		}
 	}
 
 	if (args.button1_label) {
 		wchar_t *str = str2wstr(args.button1_label, nullptr);
 		dialog.PrimaryButtonText(winrt::hstring(str));
 		free(str);
+		dialog.DefaultButton(ContentDialogButton::Primary);
 	}
 	if (args.button2_label) {
 		wchar_t *str = str2wstr(args.button2_label, nullptr);
@@ -257,7 +282,17 @@
 			evt.intval = 2;
 		}
 
+		if (args.input) {
+			std::wstring wstr(textfield.Text());
+			char *text = wchar2utf8(wstr.c_str(), wstr.length());
+			evt.eventdata = text;
+		}
+
 		args.result(&evt, args.resultdata);
+
+		if (evt.eventdata) {
+			free(evt.eventdata);
+		}
 	}
 }
 

mercurial