application/davcontroller.c

Sun, 10 Nov 2024 09:12:30 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 10 Nov 2024 09:12:30 +0100
changeset 75
73c8a3780c72
parent 68
79a9aadf1c70
child 77
5de33c2d94c6
permissions
-rw-r--r--

toolkit update, small UI adjustments

/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2024 Olaf Wintermann. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
*   1. Redistributions of source code must retain the above copyright
*      notice, this list of conditions and the following disclaimer.
*
*   2. Redistributions in binary form must reproduce the above copyright
*      notice, this list of conditions and the following disclaimer in the
*      documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include "davcontroller.h"
#include "window.h"

#include <cx/printf.h>

#include "config.h"

#include "system.h"
#include "common/context.h"

#include <libidav/config.h>
#include <libidav/utils.h>

DavBrowser* davbrowser_create(UiObject *toplevel) {
    DavBrowser *doc = ui_document_new(sizeof(DavBrowser));
    UiContext *ctx = ui_document_context(doc);
    doc->ctx = ctx;

    doc->navigation_stack = cxLinkedListCreateSimple(CX_STORE_POINTERS);
    doc->navstack_enabled = true;
    doc->navstack_pos = 0;

    doc->dav_queue = ui_threadpool_create(1);
    
    doc->res_open_inprogress = cxHashMapCreate(ctx->allocator, CX_STORE_POINTERS, 4);

    doc->path = ui_string_new(ctx, "path");
    doc->resources = ui_list_new(ctx, "reslist");

    return doc;
}


void davbrowser_set_collection(UiObject *ui, DavBrowser *browser, DavResource *collection) {
    if (browser->current) {
        dav_resource_free_all(browser->current);
    }
    ui_list_clear(browser->resources);

    browser->current = collection;
    browser->res_counter++;
    for (DavResource *res = collection->children; res; res = res->next) {
        ui_list_append(browser->resources, res);
    }

    browser->resources->update(browser->resources, 0);
    
    ui_set_group(ui->ctx, APP_STATE_BROWSER_SESSION);
}

// ------------------------------ davbrowser_connect2repo ------------------------------

void davbrowser_connect2repo(UiObject *ui, DavBrowser *browser, DavCfgRepository *repo, const char *path) {
    DavSession *sn = dav_session_new(application_dav_context(), repo->url.value.ptr);
    if (repo->user.value.ptr && repo->password.value.ptr) {
        cxmutstr decodedpw = dav_repository_get_decodedpassword(repo);
        dav_session_set_auth(sn, repo->user.value.ptr, decodedpw.ptr);
        free(decodedpw.ptr);
    }
    browser->sn = sn;
    if (repo->name.value.length > 0) {
        browser->repo_base = cx_strdup(cx_strn(repo->name.value.ptr, repo->name.value.length)).ptr;
    } else {
        browser->repo_base = cx_strdup(cx_strn(repo->url.value.ptr, repo->url.value.length)).ptr;
    }

    char *dav_path = util_concat_path(browser->repo_base, path);
    ui_set(browser->path, dav_path);
    free(dav_path);
    
    SessionAuthData *auth = cxMalloc(sn->mp->allocator, sizeof(SessionAuthData));
    auth->obj = ui;
    auth->cond = ui_condvar_create();
    auth->sn = sn;
    auth->user = repo->user.value.ptr ? cx_strdup_a(sn->mp->allocator, cx_strcast(repo->user.value)).ptr : NULL;
    auth->password = NULL;
    dav_session_set_authcallback(sn, jobthr_davbrowser_auth, auth);
    
    davbrowser_query_path(ui, browser, path);
}

// ------------------------------ davbrowser_auth ------------------------------

static int davbrowser_auth_dialog(void *data) {
    SessionAuthData *auth = data;
    auth_dialog(auth);
    return 0;
}

void davbrowser_auth_set_user_pwd(SessionAuthData *auth, const char *user, const char *password) {
    dav_session_free(auth->sn, auth->user);
    dav_session_free(auth->sn, auth->password);
    auth->user = user ? dav_session_strdup(auth->sn, user) : NULL;
    auth->password = password ? dav_session_strdup(auth->sn, password) : NULL;
}

int jobthr_davbrowser_auth(DavSession *sn, void *data) {
    SessionAuthData *auth = data;
    
    ui_call_mainthread(davbrowser_auth_dialog, auth);
    ui_condvar_wait(auth->cond);
    
    if(auth->cond->intdata) {
        dav_session_set_auth(sn, auth->user, auth->password);
    }
    dav_session_free(auth->sn, auth->password);
    auth->password = NULL;
    
    return 0;
}


// ------------------------------ davbrowser_query_path ------------------------------

typedef struct DavBrowserQueryPath {
    UiThreadpool *pool;
    DavBrowser *browser;
    char *path;
    DavResource *result;
} DavBrowserQueryPath;

static int browser_query_path(void *data) {
    DavBrowserQueryPath *query = data;
    DavSession *sn = query->browser->sn;

    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);
    query->result = res;

    return 0;
}

static void browser_query_finished(UiEvent *event, void *data) {
    DavBrowserQueryPath *query = data;
    DavBrowser *browser = event->document;

    if (query->pool == browser->dav_queue) {
        if (query->result) {
            davbrowser_set_collection(event->obj, browser, query->result);
        } else {
            cxmutstr error = cx_asprintf("Error %d", query->browser->sn->error);
            ui_dialog(event->obj, .title = "Error", .content = error.ptr, .closebutton_label = "OK");
        }

        window_progress(event->window, 0);
    } else {
        // operation aborted
        if (query->result) {
            dav_resource_free_all(query->result);
        }
    }

    free(query->path);
    free(query);
}

void davbrowser_query_path(UiObject *ui, DavBrowser *browser, const char *path) {
    if (!browser->sn) {
        // TODO: error
        return;
    }

    // for comparison, we need the current base_url/repo_name + path
    size_t len = path ? strlen(path) : 0;
    if (len == 1 && *path == '/') {
        path = "";
    }
    
    // check if the new path is a prefix of the current path
    // if not, we have to set the pathbar string to the new path
    char *full_path = util_concat_path(browser->repo_base, path);
    char *full_path_col = util_concat_path(full_path, "/");
    char *current_path = ui_get(browser->path);
    cxstring cpath = cx_str(current_path);
    cxstring newc = cx_str(full_path_col);
    if (!cx_strprefix(cpath, newc)) {
        ui_set(browser->path, full_path);
    }
    free(full_path);
    free(full_path_col);

    DavBrowserQueryPath *query = malloc(sizeof(DavBrowserQueryPath));
    query->pool = browser->dav_queue;
    query->browser = browser;
    query->path = strdup(path);
    query->result = NULL;
    ui_threadpool_job(browser->dav_queue, ui, browser_query_path, query, browser_query_finished, query);

    window_progress(ui->window, 1);

    davbrowser_add2navstack(browser, browser->repo_base, path);
}

void davbrowser_query_url(UiObject *ui, DavBrowser *browser, const char *url) {
    if (browser->repo_base) {
        cxstring base = cx_str(browser->repo_base);
        cxstring newurl = cx_str(url);

        if (cx_strprefix(newurl, base)) {
            const char *path = url + base.length;
            davbrowser_query_path(ui, browser, path);
            return;
        }
    }

    char *path = NULL;
    DavCfgRepository *repo = dav_config_url2repo(get_config(), url, &path);
    davbrowser_connect2repo(ui, browser, repo, path);

    if (path) {
        free(path);
    }
    if (!repo->node) {
        dav_repository_free(get_config(), repo);
    }
}

void davbrowser_open_resource(UiObject *ui, DavBrowser *browser, DavResource *res, const char *contenttype) {
    DavResourceViewType type = DAV_RESOURCE_VIEW_PROPERTIES;
    if(!contenttype) {
        contenttype = res->contenttype;
    }
    
    if(res->iscollection) {
        // default type
    } else if(contenttype) {
        cxstring ctype = cx_str(contenttype);
        if(cx_strprefix(ctype, CX_STR("text/"))) {
            type = DAV_RESOURCE_VIEW_TEXT;
        } else if(cx_strprefix(ctype, CX_STR("image/"))) {
            type = DAV_RESOURCE_VIEW_IMAGE;
        } else if(cx_strprefix(ctype, CX_STR("application/"))) {
            if(cx_strsuffix(ctype, CX_STR("json"))) {
                type = DAV_RESOURCE_VIEW_TEXT;
            } else if(cx_strsuffix(ctype, CX_STR("/xml"))) {
                type = DAV_RESOURCE_VIEW_TEXT;
            } else if(cx_strsuffix(ctype, CX_STR("+xml"))) {
                type = DAV_RESOURCE_VIEW_TEXT;
            } else if(cx_strsuffix(ctype, CX_STR("/xml"))) {
                type = DAV_RESOURCE_VIEW_TEXT;
            }
        }
    } else {
        cxstring path = cx_str(res->path);
        if(cx_strsuffix(path, CX_STR(".png"))) {
            type = DAV_RESOURCE_VIEW_IMAGE;
        } else if(cx_strsuffix(path, CX_STR(".jpg"))) {
            type = DAV_RESOURCE_VIEW_IMAGE;
        } else if(cx_strsuffix(path, CX_STR(".jpeg"))) {
            type = DAV_RESOURCE_VIEW_IMAGE;
        } else if(cx_strsuffix(path, CX_STR(".tif"))) {
            type = DAV_RESOURCE_VIEW_IMAGE;
        } else if(cx_strsuffix(path, CX_STR(".tiff"))) {
            type = DAV_RESOURCE_VIEW_IMAGE;
        } else if(cx_strsuffix(path, CX_STR(".webp"))) {
            type = DAV_RESOURCE_VIEW_IMAGE;
        } else if(cx_strsuffix(path, CX_STR(".bmp"))) {
            type = DAV_RESOURCE_VIEW_IMAGE;
        } else if(cx_strsuffix(path, CX_STR(".gif"))) {
            type = DAV_RESOURCE_VIEW_IMAGE;
        } else if(cx_strsuffix(path, CX_STR(".txt"))) {
            type = DAV_RESOURCE_VIEW_TEXT;
        } else if(cx_strsuffix(path, CX_STR(".md"))) {
            type = DAV_RESOURCE_VIEW_TEXT;
        } else if(cx_strsuffix(path, CX_STR(".xml"))) {
            type = DAV_RESOURCE_VIEW_TEXT;
        }
    }
    
    resourceviewer_new(browser, res->path, type);
}

void davbrowser_add2navstack(DavBrowser *browser, const char *base, const char *path) {
    if (browser->navstack_enabled) {
        for (int i = 0; i < browser->navstack_pos; i++) {
            char *nav_url = cxListAt(browser->navigation_stack, 0);
            cxListRemove(browser->navigation_stack, 0);
            free(nav_url);
        }
        browser->navstack_pos = 0;

        char *nav_url = util_concat_path(base, path);
        cxListInsert(browser->navigation_stack, 0, nav_url);

        if (cxListSize(browser->navigation_stack) > DAVBROWSER_MAX_NAVLIST) {
            char *nav = cxListAt(browser->navigation_stack, cxListSize(browser->navigation_stack) - 1);
            free(nav);
            cxListRemove(browser->navigation_stack, cxListSize(browser->navigation_stack) - 1);
        }
    }
}

void davbrowser_navigation_parent(UiObject *ui, DavBrowser *browser) {
    if(browser->current) {
        char *parent = util_parent_path(browser->current->path);
        if(strlen(parent) > 0) {
            davbrowser_query_path(ui, browser, parent);
        }
        free(parent);
    }
}

void davbrowser_navigation_back(UiObject *ui, DavBrowser *browser) {
    if (browser->navstack_pos+1 < cxListSize(browser->navigation_stack)) {
        browser->navstack_pos++;
        char *nav_url = cxListAt(browser->navigation_stack, browser->navstack_pos);
        browser->navstack_enabled = false;
        davbrowser_query_url(ui, browser, nav_url);
        browser->navstack_enabled = true;
        ui_set(browser->path, nav_url);
    }
}

void davbrowser_navigation_forward(UiObject *ui, DavBrowser *browser) {
    if (browser->navstack_pos > 0) {
        browser->navstack_pos--;
        char *nav_url = cxListAt(browser->navigation_stack, browser->navstack_pos);
        browser->navstack_enabled = false;
        davbrowser_query_url(ui, browser, nav_url);
        browser->navstack_enabled = true;
        ui_set(browser->path, nav_url);
    }
}


// ------------------------------------- File Upload -------------------------------------

typedef struct DavFileUpload {
    UiObject *ui;
    DavBrowser *browser;
    DavSession *sn;
    UiFileList files;
    char *base_path;
    UiThreadpool *queue;

    size_t total_bytes;
    size_t total_files;
    size_t total_directories;
    size_t uploaded_bytes;
    size_t uploaded_files;
    size_t uploaded_directories;

    DavBool upload_file;
    size_t current_file_size;
    size_t current_file_upload;

    char *current_file_name;

    UiObject *dialog;
    UiDouble *progress;
    UiString *label_top_left;
    UiString *label_top_right;
    UiString *label_bottom_left;
    UiString *label_bottom_right;

    // The collection, the files are uploaded to
    // It is only safe to access the collection ptr, if
    // collection == browser->current && collection_ctn == browser->res_counter
    // and obviously it can only be accessed from the UI thread
    DavResource *collection;

    // copy of browser->res_counter, used for integrity check
    int64_t collection_ctn;

    // current uploaded resource, created as part of the browser session
    // only if collection == browser->current && collection_ctn == browser->res_counter
    DavResource *current_resource;
} DavFileUpload;

typedef struct DUFile {
    char *path;
    char *upload_path;
    size_t bytes;
    DavBool isdirectory;
    DavFileUpload *upload;
    DavError error;
} DUFile;

static double upload_progress(DavFileUpload *upload) {
    return ((double)upload->uploaded_bytes / (double)upload->total_bytes) * 100;
}

static void update_upload_labels(DavFileUpload *upload) {
    char *sz_total = util_size_str(FALSE, upload->total_bytes);
    char *sz_uploaded = util_size_str2(FALSE, upload->uploaded_bytes, upload->total_bytes, 2);
    char *sz_uploaded_end = strchr(sz_uploaded, ' ');
    if (sz_uploaded_end) {
        *sz_uploaded_end = 0;
    }

    double progress = upload_progress(upload);
    ui_set(upload->progress, upload_progress(upload));

    cxmutstr label1;
    if (upload->total_files + upload->total_directories > 1) {
        label1 = cx_asprintf(
            "%s/%s   %zu/%zu files",
            sz_uploaded,
            sz_total,
            upload->uploaded_files+upload->uploaded_directories,
            upload->total_files+upload->total_directories);
    } else {
        label1 = cx_asprintf(
            "%s/%s",
            sz_uploaded,
            sz_total);
    }
    ui_set(upload->label_top_left, label1.ptr);

    free(sz_total);
    free(label1.ptr);

    if (upload->current_file_size > 0) {
        cxmutstr file_label = cx_asprintf("%s (%.0f%%)", upload->current_file_name, ((float)upload->current_file_upload/(float)upload->current_file_size)*100);
        ui_set(upload->label_top_right, file_label.ptr);
        free(file_label.ptr);
    }
}

static int uithr_update_upload_labels(void *data) {
    update_upload_labels(data);
    return 0;
}

static void upload_dav_progress(DavResource *res, int64_t total, int64_t now, void *data) {
    DavFileUpload *upload = data;
    if (upload->upload_file) {
        if (now > upload->current_file_size) {
            // current_file_size is not accurate (either the file was changed after the last stat
            // or we have some extra bytes because of encryption
            // adjust current_file_size and the total upload size
            int64_t extra = now - upload->current_file_size;
            upload->current_file_size += extra;
            upload->total_bytes += extra;
        }

        int64_t new_progress = now - upload->current_file_upload;
        upload->uploaded_bytes += new_progress;
        upload->current_file_upload = now;

        ui_call_mainthread(uithr_update_upload_labels, upload);
    }
}


typedef struct FileNameUpdate {
    DavFileUpload *upload;
    char *name;
    char *path;
    DavBool iscollection;
} FileNameUpdate;

static int uithr_update_file_label(FileNameUpdate *update) {
    // replace upload->current_filename with update->name
    if (update->upload->current_file_name) {
        free(update->upload->current_file_name);
    }
    update->upload->current_file_name = update->name;

    ui_set(update->upload->label_top_right, update->name);

    DavFileUpload *upload = update->upload;
    DavBrowser *browser = upload->browser;
    // update the resource list in the browser, if the current collection has not changed
    if (upload->collection == browser->current && upload->collection_ctn == browser->res_counter) {
        char *parent = util_parent_path(update->path);
        cxstring parent_s = cx_str(parent);
        cxstring colpath_s = cx_str(upload->collection->path);
        if (parent_s.length > 0 && parent_s.ptr[parent_s.length - 1] == '/') {
            parent_s.length--;
        }
        if (colpath_s.length > 0 && colpath_s.ptr[colpath_s.length - 1] == '/') {
            colpath_s.length--;
        }

        // only update, if the added resource has the current collection as parent
        if (!cx_strcmp(parent_s, colpath_s)) {
            DavResource *ui_res = dav_resource_new(upload->collection->session, update->path);
            ui_res->iscollection = update->iscollection;
            ui_res->lastmodified = time(NULL);
            ui_res->creationdate = time(NULL);
            upload->current_resource = ui_res;

            ui_list_append(browser->resources, ui_res);
            browser->resources->update(browser->resources, 0);
        } else {
            upload->current_resource = NULL; // maybe not necessary
        }
        free(parent);
    }

    free(update->path);
    free(update);
    return 0;
}

static int qthr_file_upload(void *data) {
    DUFile *f = data;
    DavFileUpload *upload = f->upload;
    DavSession *sn = upload->sn;

    FILE *in = sys_fopen(f->path, "rb");
    if (!in) {
        // TODO: error msg
        return 0;
    }

    upload->upload_file = TRUE;
    upload->current_file_size = f->bytes;
    upload->current_file_upload = 0;

    DavResource *res = dav_resource_new(sn, f->upload_path);

    FileNameUpdate *ui_update = malloc(sizeof(FileNameUpdate));
    ui_update->upload = upload;
    ui_update->name = strdup(res->name);
    ui_update->path = strdup(res->path);
    ui_update->iscollection = FALSE;
    ui_call_mainthread((ui_threadfunc)uithr_update_file_label, ui_update);

    dav_set_content(res, in, (dav_read_func)fread, (dav_seek_func)fseek);
    if (dav_store(res)) {
        f->error = sn->error;
    }
    dav_resource_free(res);

    fclose(in);

    upload->upload_file = FALSE;

    return 0;
}

static void uithr_file_uploaded(UiEvent *event, void *data) {
    DUFile *file = data;
    DavFileUpload *upload = file->upload;

    upload->uploaded_files++;
    //upload->uploaded_bytes += file->bytes;

    double progress = upload_progress(upload);
    ui_set(upload->progress, upload_progress(upload));

    update_upload_labels(upload);

    // update resource content length in the browser 
    DavBrowser *browser = upload->browser;
    if (upload->collection == browser->current && upload->collection_ctn == browser->res_counter) {
        if (upload->current_resource) {
            upload->current_resource->contentlength = upload->current_file_upload;
            browser->resources->update(browser->resources, 0);
        }
    }
    upload->current_resource = NULL;

    free(file->path);
    free(file->upload_path);
}

static int qthr_dir_upload(void *data) {
    DUFile *f = data;
    DavFileUpload *upload = f->upload;
    DavSession *sn = upload->sn;

    DavResource *res = dav_resource_new(sn, f->upload_path);
    res->iscollection = TRUE;

    FileNameUpdate *ui_update = malloc(sizeof(FileNameUpdate));
    ui_update->upload = upload;
    ui_update->name = strdup(res->name);
    ui_update->path = strdup(res->path);
    ui_update->iscollection = TRUE;
    ui_call_mainthread((ui_threadfunc)uithr_update_file_label, ui_update);

    if (dav_create(res)) {
        f->error = sn->error;
    }

    dav_resource_free(res);

    return 0;
}

static void uithr_dir_uploaded(UiEvent *event, void *data) {
    DUFile *file = data;
    DavFileUpload *upload = file->upload;

    upload->uploaded_directories++;

    update_upload_labels(upload);

    upload->current_resource = NULL;

    free(file->path);
    free(file->upload_path);
}

static int qthr_upload_finished(void *data) {
    return 0;
}

static void uithr_upload_finished(UiEvent *event, void *data) {
    DavFileUpload *upload = data;
    ui_threadpool_destroy(upload->queue);

    free(upload->base_path);
    dav_session_destroy(upload->sn);
}

static int jobthr_upload_scan(void *data) {
    DavFileUpload *upload = data;

    CxList *stack = cxLinkedListCreateSimple(CX_STORE_POINTERS);
    for (int i = 0; i < upload->files.nfiles; i++) {
        DUFile *f = malloc(sizeof(DUFile));
        f->path = strdup(upload->files.files[i]);
        f->upload_path = util_concat_path(upload->base_path, util_path_file_name(f->path));
        f->isdirectory = FALSE;
        f->bytes = 0;
        f->upload = upload;
        f->error = 0;
        cxListInsert(stack, 0, f);
    }

    while (cxListSize(stack) > 0) {
        DUFile *f = cxListAt(stack, 0);

        char *path = util_concat_path(upload->base_path, f->upload_path);
        cxListRemove(stack, 0);

        SYS_STAT s;
        if (!sys_stat(f->path, &s)) {
            if (S_ISDIR(s.st_mode)) {
                f->isdirectory = TRUE;
                upload->total_directories++;
                ui_threadpool_job(upload->queue, upload->ui, qthr_dir_upload, f, uithr_dir_uploaded, f);

                SYS_DIR dir = sys_opendir(f->path);
                if (dir) {
                    SysDirEnt *entry;
                    int nument = 0;
                    while((entry = sys_readdir(dir)) != NULL) {
                        if(!strcmp(entry->name, ".") || !strcmp(entry->name, "..")) {
                            continue;
                        }

                        cxmutstr newpath = util_concat_sys_path(cx_str(f->path), cx_str(entry->name));
                        char *new_upload_path = util_concat_path(f->upload_path, entry->name);

                        DUFile *child = malloc(sizeof(DUFile));
                        child->path = newpath.ptr;
                        child->upload_path = new_upload_path;
                        child->isdirectory = FALSE;
                        child->bytes = 0;
                        child->upload = upload;
                        child->error = 0;
                        cxListAdd(stack, child);
                    }

                    sys_closedir(dir);
                }
            } else if (S_ISREG(s.st_mode)) {
                f->isdirectory = FALSE;
                f->bytes = s.st_size;
                upload->total_files++;
                upload->total_bytes += s.st_size;
                ui_threadpool_job(upload->queue, upload->ui, qthr_file_upload, f, uithr_file_uploaded, f);
            }
        }
    } // TODO: else error msg

    ui_threadpool_job(upload->queue, upload->ui, qthr_upload_finished, upload, uithr_upload_finished, upload);

    ui_filelist_free(upload->files);

    return 0;
}

static void uithr_upload_scan_finished(UiEvent *event, void *data) {
    DavFileUpload *upload = data;

    update_upload_labels(upload);
}

static void upload_window_closed(UiEvent *event, void *data) {
    // noop, prevents context destruction
}

void davbrowser_upload_files(UiObject *ui, DavBrowser *browser, UiFileList files) {
    if (!browser->sn) {
        return; // TODO: error msg
    }

    // we need a clone of the current session, because the upload
    // is done in a separate thread
    DavSession *upload_session = dav_session_clone(browser->sn);

    // create upload obj, that contains all relevant data for the upload
    DavFileUpload *upload = malloc(sizeof(DavFileUpload));
    memset(upload, 0, sizeof(DavFileUpload));

    dav_session_set_progresscallback(upload_session, NULL, upload_dav_progress, upload);

    upload->ui = ui;
    upload->browser = browser;
    upload->sn = upload_session;
    upload->files = files;
    upload->base_path = strdup(browser->current->path);
    upload->queue = ui_threadpool_create(1);

    upload->collection = browser->current;
    upload->collection_ctn = browser->res_counter;

    // create upload progress window
    cxmutstr wtitle = cx_asprintf("Upload to: %s", ui_get(browser->path));
    UiObject *dialog = ui_simple_window(wtitle.ptr, upload);
    ui_context_closefunc(dialog->ctx, upload_window_closed, NULL);
    free(wtitle.ptr);
    upload->dialog = dialog;
    ui_window_size(dialog, 550, 120);
    upload->progress = ui_double_new(dialog->ctx, NULL);
    upload->label_top_left = ui_string_new(dialog->ctx, NULL);
    upload->label_top_right = ui_string_new(dialog->ctx, NULL);
    upload->label_bottom_left = ui_string_new(dialog->ctx, NULL);
    upload->label_bottom_right = ui_string_new(dialog->ctx, NULL);

    ui_grid(dialog, .margin = 10, .spacing = 10, .fill = TRUE) {
        ui_llabel(dialog, .value = upload->label_top_left, .hexpand = TRUE);
        ui_rlabel(dialog, .value = upload->label_top_right);
        ui_newline(dialog);

        ui_progressbar(dialog, .value = upload->progress, .colspan = 2, .hexpand = TRUE);
        ui_newline(dialog);

        ui_llabel(dialog, .value = upload->label_bottom_left);
        ui_rlabel(dialog, .value = upload->label_bottom_right);
        ui_newline(dialog);
    }

    ui_set(upload->label_top_left, "");
    ui_set(upload->label_top_right, "");
    ui_set(upload->label_bottom_left, "");
    ui_set(upload->label_bottom_right, "");
    ui_set(upload->progress, 0);

    ui_show(dialog);

    // start upload and stat threads
    ui_job(ui, jobthr_upload_scan, upload, uithr_upload_scan_finished, upload);
}


// ------------------------------------- File Download -------------------------------------

typedef struct DavFileDownload {
    UiObject *ui;
    DavBrowser *browser;

    DavSession *sn;
    DavSession *download_sn;
    DavResource *reslist;
    char *local_path;
    DavBool isdirectory;

    UiThreadpool *queue;

    size_t total_bytes;
    size_t total_files;
    size_t total_directories;
    size_t downloaded_bytes;
    size_t downloaded_files;
    size_t downloaded_directories;

    size_t current_file_size;
    size_t current_file_downloaded;

    UiObject *dialog;
    UiDouble *progress;
    UiString *label_top_left;
    UiString *label_top_right;
    UiString *label_bottom_left;
    UiString *label_bottom_right;
} DavFileDownload;


static int uithr_download_update_progress(void *data) {
    DavFileDownload *download = data;
    char *sz_total = util_size_str(FALSE, download->total_bytes);
    char *sz_downloaded = util_size_str2(FALSE, download->downloaded_bytes, download->total_bytes, 2);
    char *sz_downloaded_end = strchr(sz_downloaded, ' ');
    if (sz_downloaded_end) {
        *sz_downloaded_end = 0;
    }
    
    if (download->total_bytes > 0) {
        double progress = (double)download->downloaded_bytes / (double)download->total_bytes;
        ui_set(download->progress, progress*100);
    }
    

    cxmutstr label1;
    if (download->total_files + download->total_directories > 1) {
        label1 = cx_asprintf(
            "%s/%s   %zu/%zu files",
            sz_downloaded,
            sz_total,
            download->downloaded_files+download->downloaded_directories,
            download->total_files+download->total_directories);
    } else {
        label1 = cx_asprintf(
            "%s/%s",
            sz_downloaded,
            sz_total);
    }
    ui_set(download->label_top_left, label1.ptr);

    free(sz_total);
    free(label1.ptr);


    return 1;
}

// dav download file
typedef struct DDFile {
    DavFileDownload *download;
    size_t size;
    char *path;
    char *to;
    FILE *fd;
} DDFile;

static size_t ddfile_write(const void *buf, size_t size, size_t count, void *stream) {
    DDFile *file = stream;

    size_t w = fwrite(buf, size, count, file->fd);
    file->download->current_file_downloaded += w;

    file->download->downloaded_bytes += w;

    if (file->download->current_file_downloaded > file->download->current_file_size) {
        size_t diff = file->download->current_file_downloaded - file->download->current_file_size;
        file->download->current_file_size = file->download->current_file_downloaded;
        file->download->total_bytes += diff;
    }

    ui_call_mainthread(uithr_download_update_progress, file->download);

    return w;
}

static int qthr_download_resource(void *data) {
    DDFile *file = data;

    file->download->current_file_downloaded = 0;
    file->download->current_file_size = file->size;

    FILE *f = fopen(file->to, "wb");
    if (!f) {
        return 0;
    }
    file->fd = f;

    DavResource *res = dav_resource_new(file->download->download_sn, file->path);
    dav_get_content(res, file, (dav_write_func)ddfile_write);

    file->download->downloaded_files++;

    ui_call_mainthread(uithr_download_update_progress, file->download);

    dav_resource_free(res);

    fclose(f);

    free(file->path);
    free(file->to);
    free(file);

    return 0;
}


typedef struct DlStackElm {
    DavResource *resource;
    char *sub_path;
} DlStackElm;

static int jobthr_download_scan(void *data) {
    DavFileDownload *download = data;
    DavBrowser *browser = download->browser;

    // check if the specified local location is a directory
    SYS_STAT s;
    if (!sys_stat(download->local_path, &s)) {
        if (S_ISDIR(s.st_mode)) {
            download->isdirectory = TRUE;
        }
    }

    CxList *stack = cxLinkedListCreateSimple(sizeof(DlStackElm));

    // add selected files to the download queue
    DavResource *res = download->reslist;
    while (res) {
        DlStackElm elm;
        elm.resource = res;
        elm.sub_path = strdup(res->name);
        cxListAdd(stack, &elm);

        res = res->next;
    }

    while (cxListSize(stack) > 0) {
        DlStackElm *elm = cxListAt(stack, 0);
        DavResource *res = elm->resource;
        char *sub_path = elm->sub_path;
        cxListRemove(stack, 0);

        if (res->iscollection) {
            if (dav_load(res)) {
                // TODO: handle error
                continue;
            }

            // update ui
            ui_call_mainthread(uithr_download_update_progress, download);
            
            char *path = util_concat_path(download->local_path, sub_path);
            int err = sys_mkdir(path);
            free(path);
            if (err) {
                // TODO: handle error
            }

            DavResource *child = res->children;
            while (child) {
                char *child_path = util_concat_path(sub_path, child->name);
                DlStackElm childelm;
                childelm.resource = child;
                childelm.sub_path = child_path;
                cxListAdd(stack, &childelm);

                child = child->next;
            }
        } else {
            // add the file to the download queue
            DDFile *file = malloc(sizeof(DDFile));
            file->download = download;
            file->path = strdup(res->path);
            file->size = res->contentlength;
            if (download->isdirectory) {
                file->to = util_concat_path(download->local_path, sub_path);
            } else {
                file->to = strdup(download->local_path);
            }

            // stats
            download->total_files++;
            download->total_bytes += res->contentlength;

            // update ui
            ui_call_mainthread(uithr_download_update_progress, download);

            ui_threadpool_job(download->queue, download->ui, qthr_download_resource, file, NULL, NULL);
        } 
    }

    cxListDestroy(stack);

    return 0;
}

static void uithr_download_scan_finished(UiEvent *event, void *data) {
    DavFileDownload *download = data;

}

static void download_window_closed(UiEvent *event, void *data) {

}


void davbrowser_download(UiObject *ui, DavBrowser *browser, DavResource *reslist, const char *local_path) {
    DavFileDownload *download = malloc(sizeof(DavFileDownload));
    memset(download, 0, sizeof(DavFileDownload));

    download->ui = ui;
    download->browser = browser;
    download->sn = reslist->session;
    download->download_sn = dav_session_clone(download->sn);
    download->reslist = reslist; // TODO: is this safe or do we need a copy?
    download->local_path = strdup(local_path);

    download->queue = ui_threadpool_create(1);

    // create download progress window
    cxmutstr wtitle = cx_asprintf("Download to: %s", local_path);
    UiObject *dialog = ui_simple_window(wtitle.ptr, download);
    ui_context_closefunc(dialog->ctx, download_window_closed, NULL);
    free(wtitle.ptr);
    download->dialog = dialog;
    ui_window_size(dialog, 550, 120);
    download->progress = ui_double_new(dialog->ctx, NULL);
    download->label_top_left = ui_string_new(dialog->ctx, NULL);
    download->label_top_right = ui_string_new(dialog->ctx, NULL);
    download->label_bottom_left = ui_string_new(dialog->ctx, NULL);
    download->label_bottom_right = ui_string_new(dialog->ctx, NULL);

    ui_grid(dialog, .margin = 10, .spacing = 10, .fill = TRUE) {
        ui_llabel(dialog, .value = download->label_top_left, .hexpand = TRUE);
        ui_rlabel(dialog, .value = download->label_top_right);
        ui_newline(dialog);

        ui_progressbar(dialog, .value = download->progress, .min = 0, .max = 100, .colspan = 2, .hexpand = TRUE);
        ui_newline(dialog);

        ui_llabel(dialog, .value = download->label_bottom_left);
        ui_rlabel(dialog, .value = download->label_bottom_right);
        ui_newline(dialog);
    }

    ui_set(download->label_top_left, "");
    ui_set(download->label_top_right, "");
    ui_set(download->label_bottom_left, "");
    ui_set(download->label_bottom_right, "");
    ui_set(download->progress, 0);

    ui_show(dialog);

    // start upload and stat threads
    ui_job(ui, jobthr_download_scan, download, uithr_download_scan_finished, download);
}


// ------------------------------------- Path Operation (DELETE, MKCOL) -------------------------------------

enum DavPathOpType {
    DAV_PATH_OP_DELETE = 0,
    DAV_PATH_OP_CREATE
};

typedef struct DavPathOp {
    UiObject *ui;
    DavBrowser *browser;

    // operation type (delete, mkcol, ...)
    enum DavPathOpType op;
    // clone of the browser's DavSession
    DavSession *sn;
    // path array
    char **path;
    // browser->resources indices
    size_t *list_indices;
    // number of path/list_indices elements
    size_t nelm;
    // path is collection
    DavBool iscollection;

    // browser->current ptr when the PathOp started
    // used in combination with collection_ctn to check if the browser list changed
    DavResource *collection;
    int64_t collection_ctn;
} DavPathOp;

typedef struct DavPathOpResult {
    UiObject *ui;
    DavBrowser *browser;
    DavResource *collection;
    int64_t collection_ctn;
    enum DavPathOpType op;
    DavBool iscollection;

    char *path;
    int res_index;
    int result;
    char *errormsg;
} DavPathOpResult;

static int uithr_pathop_delete_error(void *data) {
    DavPathOpResult *result = data;

    cxmutstr msg = cx_asprintf("Cannot delete resource %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_delete_sucess(void *data) {
    DavPathOpResult *result = data;

    if (result->browser->current == result->collection && result->browser->res_counter == result->collection_ctn) {
        ui_list_remove(result->browser->resources, result->res_index);
        result->browser->resources->update(result->browser->resources, 0);
    }

    free(result->path);
    free(result);
    return 0;
}

static int uithr_pathop_create_resource_error(void *data) {
    DavPathOpResult *result = data;

    cxmutstr msg = cx_asprintf("Cannot create %s %s", result->iscollection ? "collection" : "resource", 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_create_resource_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 = result->iscollection;
        // 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]);

            DavPathOpResult *result = malloc(sizeof(DavPathOpResult));
            result->ui = op->ui;
            result->browser = op->browser;
            result->collection = op->collection;
            result->collection_ctn = op->collection_ctn;
            result->op = op->op;
            result->path = strdup(res->path);
            result->result = 0;
            result->res_index = op->list_indices[i];
            result->errormsg = NULL;
            result->iscollection = op->iscollection;
    
            if (op->op == DAV_PATH_OP_DELETE) {
                ui_threadfunc result_callback = uithr_pathop_delete_sucess;
                if (dav_delete(res)) {
                    result->errormsg = op->sn->errorstr ? strdup(op->sn->errorstr) : NULL;
                    result_callback = uithr_pathop_delete_error;
                }
                ui_call_mainthread(result_callback, result);
            } else if (op->op == DAV_PATH_OP_CREATE) {
                res->iscollection = op->iscollection;
                ui_threadfunc result_callback = uithr_pathop_create_resource_sucess;
                if (dav_create(res)) {
                    result->errormsg = op->sn->errorstr ? strdup(op->sn->errorstr) : NULL;
                    result_callback = uithr_pathop_create_resource_error;
                }
                ui_call_mainthread(result_callback, result);
            } 

            dav_resource_free(res);
            free(op->path[i]);
        }
    }

    dav_session_destroy(op->sn);
    free(op->path);
    free(op->list_indices);
    free(op);

    return 0;
}

void davbrowser_delete(UiObject *ui, DavBrowser *browser, UiListSelection selection) {
    DavPathOp *op = malloc(sizeof(DavPathOp));
    op->ui = ui;
    op->browser = browser;
    op->op = DAV_PATH_OP_DELETE;
    op->sn = dav_session_clone(browser->sn);
    op->path = calloc(selection.count, sizeof(char*));
    op->list_indices = calloc(selection.count, sizeof(size_t));
    op->nelm = selection.count;

    op->collection = browser->current;
    op->collection_ctn = browser->res_counter;

    for (int i = 0; i < selection.count; i++) {
        DavResource *res = ui_list_get(browser->resources, selection.rows[i]);
        if (res) {
            op->path[i] = strdup(res->path);
            op->list_indices[i] = selection.rows[i];
        }
    }

    ui_job(ui, jobthr_path_op, op, NULL, NULL);
}

void davbrowser_create_resource(UiObject *ui, DavBrowser *browser, const char *name, DavBool iscollection) {
    DavPathOp *op = malloc(sizeof(DavPathOp));
    op->ui = ui;
    op->browser = browser;
    op->op = DAV_PATH_OP_CREATE;
    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->iscollection = iscollection;

    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);
}

void davbrowser_mkcol(UiObject *ui, DavBrowser *browser, const char *name) {
    davbrowser_create_resource(ui, browser, name, TRUE);
}

void davbrowser_newfile(UiObject *ui, DavBrowser *browser, const char *name) {
    davbrowser_create_resource(ui, browser, name, FALSE);
}



DavResourceViewer* dav_resourceviewer_create(DavSession *sn, const char *path, DavResourceViewType type) {
    DavResourceViewer *doc = ui_document_new(sizeof(DavResourceViewer));
    UiContext *ctx = ui_document_context(doc);
    doc->ctx = ctx;
    
    doc->sn = dav_session_clone(sn);
    doc->path = strdup(path);
    doc->type = type;
    
    doc->tabview = ui_int_new(ctx, "tabview");
    doc->loading = ui_int_new(ctx, "loading");
    doc->message = ui_string_new(ctx, "message");
    
    doc->text = ui_text_new(ctx, "text");
    doc->image = ui_generic_new(ctx, "image");
    
    doc->properties = ui_list_new(ctx, "properties");
    
    doc->info_url = ui_string_new(ctx, "info_url");
    doc->info_name = ui_string_new(ctx, "info_name");
    doc->info_type = ui_string_new(ctx, "info_type");
    doc->info_encrypted = ui_string_new(ctx, "info_encrypted");
    doc->info_etag = ui_string_new(ctx, "info_etag");
    doc->info_size = ui_string_new(ctx, "info_size");
    
    return doc;
}

static char* gen_tmp_download_filename(const char *name) {
    char *dir = ui_getappdir();
    
    unsigned char rd[8];
    memset(rd, 0, 8);
    dav_rand_bytes(rd, 8);   
    char *hex = util_hexstr(rd, 8);
    cxmutstr tmp = cx_asprintf("%sdownload-%s-%s", dir, name, hex);
    return tmp.ptr;
}

static int jobthr_resourceviewer_load(void *data) {
    DavResourceViewer *doc = data;
     
    DavResource *res = dav_resource_new(doc->sn, doc->path);
    doc->error = dav_load(res);
    if(!doc->error) {
        doc->current = res;
        
        if(res->contentlength < DAV_RESOURCEVIEWER_PREVIEW_MAX_SIZE) {
            if(doc->type == DAV_RESOURCE_VIEW_TEXT) {
                doc->text_content = cxBufferCreate(NULL, res->contentlength, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS);
                int err = dav_get_content(res, doc->text_content, (dav_write_func)cxBufferWrite);
                cxBufferPut(doc->text_content, 0);
                if(err) {
                    doc->error = err;
                    doc->message_str = "Cannot load content"; // TODO: better message
                }
            } else if(doc->type == DAV_RESOURCE_VIEW_IMAGE) {
                char *tmp = gen_tmp_download_filename(res->name);
                FILE *f = fopen(tmp, "w");
                if(f) {
                    int err = dav_get_content(res, f, (dav_write_func)fwrite);
                    if(!err) {
                        doc->tmp_file = tmp;
                    } else {
                        free(tmp);
                    }
                    fclose(f);
                } else {
                    free(tmp);
                }
            }
        } else {
            // TODO: add file too large message
        }
    } else {
        doc->message_str = "Cannot load properties"; // TODO: better message
        dav_resource_free(res);
    }
    
    return 0;
}

static void resourceviewer_set_info(DavResourceViewer *doc) {
    DavResource *res = doc->current;
    if(!res) {
        return;
    }
    
    char *url = util_concat_path(res->session->base_url, res->href);
    ui_set(doc->info_url, url);
    free(url);

    ui_set(doc->info_name, res->name);

    if(res->iscollection) {
        ui_set(doc->info_type, "Collection");
    } else {
        if(res->contenttype) {
            cxmutstr type = cx_asprintf("Resource (%s)", res->contenttype);
            ui_set(doc->info_type, type.ptr);
            free(type.ptr);
        } else {
            ui_set(doc->info_type, "Resource");
        }
    }

    char *keyprop = dav_get_string_property_ns(
        res,
        DAV_NS,
        "crypto-key");
    if(keyprop) {
        cxmutstr info_encrypted = cx_asprintf("Yes   Key: %s", keyprop);
        ui_set(doc->info_encrypted, info_encrypted.ptr);
        free(info_encrypted.ptr);
    } else {
        ui_set(doc->info_encrypted, "No");
    }

    char *etag = dav_get_string_property_ns(
        res,
        "DAV:",
        "getetag");
    ui_set(doc->info_etag, etag);

    if(res->contentlength > 0) {
        char *sz = util_size_str(FALSE, res->contentlength);
        cxmutstr size_str = cx_asprintf("%s (%" PRIu64 " bytes)", sz, res->contentlength);
        ui_set(doc->info_size, size_str.ptr);
        free(size_str.ptr);
    } else {
        ui_set(doc->info_size, "0");
    }
}

static void resourceviewer_update_proplist(DavResourceViewer *doc) {
    DavResource *res = doc->current;
    if(!res) {
        return;
    }
    
    size_t count = 0;
    DavPropName *properties = dav_get_property_names(res, &count);
    for(int i=0;i<count;i++) {
        DavPropertyList *prop = ui_malloc(doc->ctx, sizeof(DavPropertyList));
        prop->ns = properties[i].ns ? ui_strdup(doc->ctx, properties[i].ns) : NULL;
        prop->name = ui_strdup(doc->ctx, properties[i].name);

        DavXmlNode *xval = dav_get_property_ns(res, prop->ns, prop->name);
        if(xval) {
            if(dav_xml_isstring(xval)) {
                char *value = dav_xml_getstring(xval);
                if(value) {
                    prop->value_simplified = NULL;
                    prop->value_full = ui_strdup(doc->ctx, value);
                }
            } else {
                DavXmlNode *x = xval->type == DAV_XML_ELEMENT ? xval : dav_xml_nextelm(xval);
                cxmutstr value = cx_asprintf_a(ui_allocator(doc->ctx), "<%s>...</%s>", x->name, x->name);
                prop->value_simplified = value.ptr;
            }
        }

        ui_list_append(doc->properties, prop);
    }
    doc->properties->update(doc->properties, 0);
}

static void resourceviewer_load_finished(UiEvent *event, void *data) {
    DavResourceViewer *doc = data;
    doc->loaded = TRUE;
    
    if(doc->window_closed) {
        dav_resourceviewer_destroy(doc);
        return;
    }
    
    resourceviewer_set_info(doc);
    resourceviewer_update_proplist(doc);
    
    if(doc->type == DAV_RESOURCE_VIEW_TEXT) {
        ui_set(doc->text, doc->text_content->space);
    } else if(doc->type == DAV_RESOURCE_VIEW_IMAGE) {
        ui_image_load_file(doc->image, doc->tmp_file);
        unlink(doc->tmp_file);
    }
      
    ui_set(doc->tabview, 1);
}

void dav_resourceviewer_load(UiObject *ui, DavResourceViewer *res) {
    ui_set(res->loading, 1);
    ui_set(res->message, "Loading...");
    ui_set(res->tabview, 0);
    
    ui_job(ui, jobthr_resourceviewer_load, res, resourceviewer_load_finished, res);
}

void dav_resourceviewer_destroy(DavResourceViewer *res) {
    
}

mercurial