46 doc->navigation_stack = cxLinkedListCreateSimple(CX_STORE_POINTERS); |
47 doc->navigation_stack = cxLinkedListCreateSimple(CX_STORE_POINTERS); |
47 doc->navstack_enabled = true; |
48 doc->navstack_enabled = true; |
48 doc->navstack_pos = 0; |
49 doc->navstack_pos = 0; |
49 |
50 |
50 doc->dav_queue = ui_threadpool_create(1); |
51 doc->dav_queue = ui_threadpool_create(1); |
|
52 |
|
53 doc->res_open_inprogress = cxHashMapCreate(ctx->allocator, CX_STORE_POINTERS, 4); |
51 |
54 |
52 doc->path = ui_string_new(ctx, "path"); |
55 doc->path = ui_string_new(ctx, "path"); |
53 doc->resources = ui_list_new(ctx, "reslist"); |
56 doc->resources = ui_list_new(ctx, "reslist"); |
54 |
57 |
55 return doc; |
58 return doc; |
194 free(path); |
197 free(path); |
195 } |
198 } |
196 if (!repo->node) { |
199 if (!repo->node) { |
197 dav_repository_free(get_config(), repo); |
200 dav_repository_free(get_config(), repo); |
198 } |
201 } |
|
202 } |
|
203 |
|
204 void davbrowser_open_resource(UiObject *ui, DavBrowser *browser, DavResource *res) { |
|
205 char *url = util_concat_path(browser->sn->base_url, res->path); |
|
206 void *x = cxMapGet(browser->res_open_inprogress, url); |
|
207 free(url); |
|
208 if(x) { |
|
209 return; // open resource already in progress |
|
210 } |
|
211 |
|
212 DavResourceViewType type = DAV_RESOURCE_VIEW_PROPERTIES; |
|
213 if(res->iscollection) { |
|
214 // default type |
|
215 } else if(res->contenttype) { |
|
216 cxstring contenttype = cx_str(res->contenttype); |
|
217 if(cx_strprefix(contenttype, CX_STR("text/"))) { |
|
218 type = DAV_RESOURCE_VIEW_TEXT; |
|
219 } else if(cx_strprefix(contenttype, CX_STR("image/"))) { |
|
220 type = DAV_RESOURCE_VIEW_IMAGE; |
|
221 } else if(cx_strprefix(contenttype, CX_STR("application/"))) { |
|
222 if(cx_strsuffix(contenttype, CX_STR("json"))) { |
|
223 type = DAV_RESOURCE_VIEW_TEXT; |
|
224 } else if(cx_strsuffix(contenttype, CX_STR("/xml"))) { |
|
225 type = DAV_RESOURCE_VIEW_TEXT; |
|
226 } else if(cx_strsuffix(contenttype, CX_STR("+xml"))) { |
|
227 type = DAV_RESOURCE_VIEW_TEXT; |
|
228 } else if(cx_strsuffix(contenttype, CX_STR("/xml"))) { |
|
229 type = DAV_RESOURCE_VIEW_TEXT; |
|
230 } |
|
231 } |
|
232 } else { |
|
233 cxstring path = cx_str(res->path); |
|
234 if(cx_strsuffix(path, CX_STR(".png"))) { |
|
235 type = DAV_RESOURCE_VIEW_IMAGE; |
|
236 } else if(cx_strsuffix(path, CX_STR(".jpg"))) { |
|
237 type = DAV_RESOURCE_VIEW_IMAGE; |
|
238 } else if(cx_strsuffix(path, CX_STR(".jpeg"))) { |
|
239 type = DAV_RESOURCE_VIEW_IMAGE; |
|
240 } else if(cx_strsuffix(path, CX_STR(".tif"))) { |
|
241 type = DAV_RESOURCE_VIEW_IMAGE; |
|
242 } else if(cx_strsuffix(path, CX_STR(".tiff"))) { |
|
243 type = DAV_RESOURCE_VIEW_IMAGE; |
|
244 } else if(cx_strsuffix(path, CX_STR(".webp"))) { |
|
245 type = DAV_RESOURCE_VIEW_IMAGE; |
|
246 } else if(cx_strsuffix(path, CX_STR(".bmp"))) { |
|
247 type = DAV_RESOURCE_VIEW_IMAGE; |
|
248 } else if(cx_strsuffix(path, CX_STR(".gif"))) { |
|
249 type = DAV_RESOURCE_VIEW_IMAGE; |
|
250 } else if(cx_strsuffix(path, CX_STR(".txt"))) { |
|
251 type = DAV_RESOURCE_VIEW_TEXT; |
|
252 } else if(cx_strsuffix(path, CX_STR(".md"))) { |
|
253 type = DAV_RESOURCE_VIEW_TEXT; |
|
254 } else if(cx_strsuffix(path, CX_STR(".xml"))) { |
|
255 type = DAV_RESOURCE_VIEW_TEXT; |
|
256 } |
|
257 } |
|
258 |
|
259 dav_resourceviewer_new(browser, res->path, type); |
199 } |
260 } |
200 |
261 |
201 void davbrowser_add2navstack(DavBrowser *browser, const char *base, const char *path) { |
262 void davbrowser_add2navstack(DavBrowser *browser, const char *base, const char *path) { |
202 if (browser->navstack_enabled) { |
263 if (browser->navstack_enabled) { |
203 for (int i = 0; i < browser->navstack_pos; i++) { |
264 for (int i = 0; i < browser->navstack_pos; i++) { |
1150 } |
1211 } |
1151 |
1212 |
1152 void davbrowser_newfile(UiObject *ui, DavBrowser *browser, const char *name) { |
1213 void davbrowser_newfile(UiObject *ui, DavBrowser *browser, const char *name) { |
1153 davbrowser_create_resource(ui, browser, name, FALSE); |
1214 davbrowser_create_resource(ui, browser, name, FALSE); |
1154 } |
1215 } |
|
1216 |
|
1217 |
|
1218 |
|
1219 DavResourceViewer* dav_resourceviewer_create(DavSession *sn, const char *path, DavResourceViewType type) { |
|
1220 DavResourceViewer *doc = ui_document_new(sizeof(DavResourceViewer)); |
|
1221 UiContext *ctx = ui_document_context(doc); |
|
1222 doc->ctx = ctx; |
|
1223 |
|
1224 doc->sn = dav_session_clone(sn); |
|
1225 doc->path = strdup(path); |
|
1226 doc->type = type; |
|
1227 |
|
1228 doc->tabview = ui_int_new(ctx, "tabview"); |
|
1229 doc->loading = ui_int_new(ctx, "loading"); |
|
1230 doc->message = ui_string_new(ctx, "message"); |
|
1231 |
|
1232 doc->text = ui_text_new(ctx, "text"); |
|
1233 doc->image = ui_generic_new(ctx, "image"); |
|
1234 |
|
1235 doc->properties = ui_list_new(ctx, "properties"); |
|
1236 |
|
1237 return doc; |
|
1238 } |
|
1239 |
|
1240 static char* gen_tmp_download_filename(const char *name) { |
|
1241 char *dir = ui_getappdir(); |
|
1242 |
|
1243 unsigned char rd[8]; |
|
1244 memset(rd, 0, 8); |
|
1245 dav_rand_bytes(rd, 8); |
|
1246 char *hex = util_hexstr(rd, 8); |
|
1247 cxmutstr tmp = cx_asprintf("%sdownload-%s-%s", dir, name, hex); |
|
1248 return tmp.ptr; |
|
1249 } |
|
1250 |
|
1251 static int jobthr_resourceviewer_load(void *data) { |
|
1252 DavResourceViewer *doc = data; |
|
1253 |
|
1254 DavResource *res = dav_resource_new(doc->sn, doc->path); |
|
1255 doc->error = dav_load(res); |
|
1256 if(!doc->error) { |
|
1257 doc->current = res; |
|
1258 |
|
1259 if(res->contentlength < DAV_RESOURCEVIEWER_PREVIEW_MAX_SIZE) { |
|
1260 if(doc->type == DAV_RESOURCE_VIEW_TEXT) { |
|
1261 doc->text_content = cxBufferCreate(NULL, res->contentlength, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS); |
|
1262 int err = dav_get_content(res, doc->text_content, (dav_write_func)cxBufferWrite); |
|
1263 cxBufferPut(doc->text_content, 0); |
|
1264 if(err) { |
|
1265 doc->error = err; |
|
1266 doc->message_str = "Cannot load content"; // TODO: better message |
|
1267 } |
|
1268 } else if(doc->type == DAV_RESOURCE_VIEW_IMAGE) { |
|
1269 char *tmp = gen_tmp_download_filename(res->name); |
|
1270 FILE *f = fopen(tmp, "w"); |
|
1271 if(f) { |
|
1272 int err = dav_get_content(res, f, (dav_write_func)fwrite); |
|
1273 if(!err) { |
|
1274 doc->tmp_file = tmp; |
|
1275 } else { |
|
1276 free(tmp); |
|
1277 } |
|
1278 fclose(f); |
|
1279 } else { |
|
1280 free(tmp); |
|
1281 } |
|
1282 } |
|
1283 } else { |
|
1284 // TODO: add file too large message |
|
1285 } |
|
1286 } else { |
|
1287 doc->message_str = "Cannot load properties"; // TODO: better message |
|
1288 dav_resource_free(res); |
|
1289 } |
|
1290 |
|
1291 return 0; |
|
1292 } |
|
1293 |
|
1294 static void resourceviewer_load_finished(UiEvent *event, void *data) { |
|
1295 DavResourceViewer *doc = data; |
|
1296 |
|
1297 if(doc->type == DAV_RESOURCE_VIEW_TEXT) { |
|
1298 ui_set(doc->text, doc->text_content->space); |
|
1299 } else if(doc->type == DAV_RESOURCE_VIEW_IMAGE) { |
|
1300 ui_image_load_file(doc->image, doc->tmp_file); |
|
1301 unlink(doc->tmp_file); |
|
1302 } |
|
1303 |
|
1304 ui_set(doc->tabview, 1); |
|
1305 } |
|
1306 |
|
1307 void dav_resourceviewer_load(UiObject *ui, DavResourceViewer *res) { |
|
1308 ui_set(res->loading, 1); |
|
1309 ui_set(res->message, "Loading..."); |
|
1310 ui_set(res->tabview, 0); |
|
1311 |
|
1312 ui_job(ui, jobthr_resourceviewer_load, res, resourceviewer_load_finished, res); |
|
1313 } |
|
1314 |