application/davcontroller.c

Thu, 08 Feb 2024 10:22:58 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 08 Feb 2024 10:22:58 +0100
changeset 21
3060a5a1d5fd
parent 20
db263186edf3
child 22
d7942163a2a3
permissions
-rw-r--r--

don't crash when the upload is in progress and the window is closed

/*
* 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 <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->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;
    for (DavResource *res = collection->children; res; res = res->next) {
        ui_list_append(browser->resources, res);
    }

    browser->resources->update(browser->resources, 0);
}

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

    davbrowser_query_path(ui, browser, path);
}


// ------------------------------ 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 {
            // TODO: error
        }

        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_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 (browser->navigation_stack->size > DAVBROWSER_MAX_NAVLIST) {
            char *nav = cxListAt(browser->navigation_stack, browser->navigation_stack->size - 1);
            free(nav);
            cxListRemove(browser->navigation_stack, browser->navigation_stack->size - 1);
        }
    }
}

void davbrowser_navigation_back(UiObject *ui, DavBrowser *browser) {
    if (browser->navstack_pos+1 < browser->navigation_stack->size) {
        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;
    }
}

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


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

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

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


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

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

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

    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 (stack->size > 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);

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

mercurial