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