application/davcontroller.c

Tue, 30 Jan 2024 08:49:35 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 30 Jan 2024 08:49:35 +0100
changeset 10
f8dfc5705516
parent 8
726b24766437
child 11
26acbfa75c1f
permissions
-rw-r--r--

create DavSession in the main GUI thread

8
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2024 Olaf Wintermann. All rights reserved.
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include "davcontroller.h"
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include "window.h"
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include <cx/printf.h>
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include <libidav/utils.h>
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 DavBrowser* davbrowser_create(UiObject *toplevel) {
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 DavBrowser *doc = ui_document_new(sizeof(DavBrowser));
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 UiContext *ctx = ui_document_context(doc);
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 doc->ctx = ctx;
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 doc->dav_queue = ui_threadpool_create(1);
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 doc->path = ui_string_new(ctx, "path");
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 doc->resources = ui_list_new(ctx, "reslist");
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 return doc;
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 }
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 void davbrowser_set_collection(UiObject *ui, DavBrowser *browser, DavResource *collection) {
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 ui_list_clear(browser->resources);
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 for (DavResource *res = collection->children; res; res = res->next) {
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 ui_list_append(browser->resources, res);
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 }
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 browser->resources->update(browser->resources, 0);
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 }
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 // ------------------------------ davbrowser_connect2repo ------------------------------
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61
10
f8dfc5705516 create DavSession in the main GUI thread
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
62 void davbrowser_connect2repo(UiObject *ui, DavBrowser *browser, DavCfgRepository *repo) {
8
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 DavSession *sn = dav_session_new(application_dav_context(), repo->url.value.ptr);
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 if (repo->user.value.ptr && repo->password.value.ptr) {
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 cxmutstr decodedpw = dav_repository_get_decodedpassword(repo);
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 dav_session_set_auth(sn, repo->user.value.ptr, decodedpw.ptr);
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 free(decodedpw.ptr);
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 }
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 browser->sn = sn;
10
f8dfc5705516 create DavSession in the main GUI thread
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
70 browser->repo_base = cx_strdup(cx_strn(repo->name.value.ptr, repo->name.value.length)).ptr;
8
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71
10
f8dfc5705516 create DavSession in the main GUI thread
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
72 ui_set(browser->path, repo->name.value.ptr);
8
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73
10
f8dfc5705516 create DavSession in the main GUI thread
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
74 davbrowser_query_path(ui, browser, "");
8
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 }
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 // ------------------------------ davbrowser_query_path ------------------------------
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 typedef struct DavBrowserQueryPath {
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 DavBrowser *browser;
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 char *path;
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 DavResource *result;
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 } DavBrowserQueryPath;
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 static int browser_query_path(void *data) {
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 DavBrowserQueryPath *query = data;
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 DavSession *sn = query->browser->sn;
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 DavResource *res = dav_query(sn, "select `idav:crypto-name`,`idav:crypto-key`,D:lockdiscovery from %s with depth = 1 order by iscollection desc, name", query->path);
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 query->result = res;
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 return 0;
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 }
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 static void browser_query_finished(UiEvent *event, void *data) {
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 DavBrowserQueryPath *query = data;
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 DavBrowser *browser = event->document;
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 if (query->result) {
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 davbrowser_set_collection(event->obj, browser, query->result);
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 } else {
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 // TODO: error
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 }
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 free(query->path);
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 free(query);
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 window_progress(event->window, 0);
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 }
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 void davbrowser_query_path(UiObject *ui, DavBrowser *browser, const char *path) {
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 // for comparison, we need the current base_url/repo_name + path
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 size_t len = path ? strlen(path) : 0;
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 if (len == 1 && *path == '/') {
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 path = "";
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 }
10
f8dfc5705516 create DavSession in the main GUI thread
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
118 char *full_path = util_concat_path(browser->repo_base, path);
8
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 // compare new path with current path
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 // in theory we could always set path, but maybe this is faster
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 char *current_path = ui_get(browser->path);
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 if (strcmp(full_path, current_path)) {
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 ui_set(browser->path, full_path);
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 }
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 free(full_path);
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 DavBrowserQueryPath *query = malloc(sizeof(DavBrowserQueryPath));
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 query->browser = browser;
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 query->path = strdup(path);
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 query->result = NULL;
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131 ui_threadpool_job(browser->dav_queue, ui, browser_query_path, query, browser_query_finished, query);
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 window_progress(ui->window, 1);
726b24766437 implement connecting to repositories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 }

mercurial