From 975f4b9dcdbb68c444c991576b80bebb8140cc56 Mon Sep 17 00:00:00 2001 From: Alexander Zhylin Date: Tue, 20 Jan 2026 19:30:01 +0200 Subject: [PATCH] ui: next step of file upload (not finished) --- src/file_processor.c | 29 +++++++++-- src/file_processor.h | 1 + src/ui/app.c | 6 ++- src/ui/modals/upload_dialogue.c | 14 +++--- src/ui/modals/upload_props_dialogue.c | 70 +++++++++++++++++++++++++++ src/ui/modals/upload_props_dialogue.h | 9 ++++ src/ui/widget/dialogue.c | 4 +- src/ui/widget/fs_file_list.c | 1 - 8 files changed, 117 insertions(+), 17 deletions(-) create mode 100644 src/ui/modals/upload_props_dialogue.c create mode 100644 src/ui/modals/upload_props_dialogue.h diff --git a/src/file_processor.c b/src/file_processor.c index bb6ade0..7150001 100644 --- a/src/file_processor.c +++ b/src/file_processor.c @@ -195,7 +195,7 @@ void file_download(file_args_t *f_args, query_args_t *q_args) { static size_t size_rest = 0; uint32_t progress = (f_selected->size - size_rest) * 100 / f_selected->size; ui_progress_bar_t *pb = (ui_progress_bar_t *)q_args->progress_bar; - dialogue_t *d = (dialogue_t *)q_args->active_dialogue; + dialogue_t *d = (dialogue_t *)q_args->active_dialogue; if (size_rest == 0) size_rest = f_selected->size; @@ -219,11 +219,12 @@ void file_download(file_args_t *f_args, query_args_t *q_args) { f_selected->size = 0; close(f_args->file_d); d->needs_destroy = true; - sprintf(answer, "File %s is downloaded from the server!", f_selected->name); - q_args->notification = malloc(strlen(answer)+1); + sprintf(answer, "File %s is downloaded from the server!", + f_selected->name); + q_args->notification = malloc(strlen(answer) + 1); strcpy(q_args->notification, answer); free(f_selected->name); - write(q_args->sd, "file list\n", sizeof("file list\n")-1); + write(q_args->sd, "file list\n", sizeof("file list\n") - 1); q_args->state = S_FILE_LIST; } } @@ -259,6 +260,26 @@ int32_t file_upload_request(char *query, query_args_t *q_args) { return 0; } +int32_t file_upload_open(char *dpath, char *fname, query_args_t *q_args) { + char a_perm; + q_args->file = malloc(sizeof(p_file_t)); + q_args->file->path = malloc(strlen(dpath) + strlen(fname) + 2); + sprintf(q_args->file->path, "%s/%s", dpath, fname); + int fd = open(q_args->file->path, O_RDONLY); + if (fd == -1) { + perror(q_args->file->path); + return -1; + } + size_t fsize = lseek(fd, 0, SEEK_END); + lseek(fd, 0, SEEK_SET); + q_args->file->fd = fd; + q_args->file->name = malloc(strlen(fname) + 1); + strcpy(q_args->file->name, fname); + q_args->file->size = fsize; + q_args->file->rest = fsize; + return 0; +} + int32_t file_upload_start(query_args_t *q_args) { char *query; query_extract_from_buf(q_args->buf, &(q_args->buf_used), &query); diff --git a/src/file_processor.h b/src/file_processor.h index 0a3c050..8892dad 100644 --- a/src/file_processor.h +++ b/src/file_processor.h @@ -10,6 +10,7 @@ void init_file_args(file_args_t *fargs); int file_upload_request(char *, query_args_t *q_args); int file_upload_start(query_args_t *q_args); int file_upload(query_args_t *q_args); +int32_t file_upload_open(char *dpath, char *fname, query_args_t *q_args); void fl_clear(fl_item_t **start, fl_item_t **arg_current); int32_t ui_file_select(file_args_t *f_args, query_args_t *q_args, int32_t idx); #endif \ No newline at end of file diff --git a/src/ui/app.c b/src/ui/app.c index 68fab99..9986c36 100644 --- a/src/ui/app.c +++ b/src/ui/app.c @@ -6,6 +6,7 @@ #include "modals/notification.h" #include "modals/server_message.h" #include "modals/upload_dialogue.h" +#include "modals/upload_props_dialogue.h" #include "widget/dialogue.h" #include #include @@ -138,7 +139,6 @@ void app_refresh(app_t *app) { void app_draw_modal(app_t *app) { if (app->modal.needs_destroy) { destroy_dialogue(&(app->modal), app); - app_refresh(app); } if (!app->modal.is_initiated) { switch (app->query_args->state) { @@ -160,6 +160,9 @@ void app_draw_modal(app_t *app) { case S_UPLOAD_FILE: init_upload_dialogue_modal(app); break; + case S_UPLOAD_PARAMS: + init_upload_props_dialogue_modal(app); + break; case WAIT_SERVER_INIT: case WAIT_SERVER: case WAIT_REGISTER: @@ -167,7 +170,6 @@ void app_draw_modal(app_t *app) { case WAIT_CLIENT: case S_FILE_LIST: case S_FILE_SELECT: - case S_UPLOAD_PARAMS: case S_UPLOAD_REQUESTED: case S_ASK_USER_BEFORE_LOGIN: case S_ERR: diff --git a/src/ui/modals/upload_dialogue.c b/src/ui/modals/upload_dialogue.c index 53997ef..922005f 100644 --- a/src/ui/modals/upload_dialogue.c +++ b/src/ui/modals/upload_dialogue.c @@ -1,11 +1,10 @@ #include "../app.h" #include "../widget/dialogue.h" -#include "../widget/group.h" #include "../widget/fs_file_list.h" +#include "../widget/group.h" +#include "../../file_processor.h" #include #include -#include -#include #include void upload_dialogue_modal_cb(callback_args_t *args) { @@ -18,17 +17,16 @@ void upload_dialogue_modal_cb(callback_args_t *args) { d_args.resp_data = &response; d_args.element = app->active_widget; dialogue_default_callback(&d_args); - ui_fs_file_list_t *fui = (ui_fs_file_list_t *) d->g_content->elements[0].element; + ui_fs_file_list_t *fui = + (ui_fs_file_list_t *)d->g_content->elements[0].element; if (response > -1) { switch (response) { case 0: app->query_args->state = S_WAIT_SERVER; + destroy_dialogue(d, app); break; case 1: - app->query_args->file = malloc(sizeof(p_file_t)); - app->query_args->file->path = malloc(strlen(fui->d_path) + strlen(fui->current->name) + 2); - strcpy(app->query_args->file->path, fui->current->name); - sprintf(app->query_args->file->path, "%s/%s", fui->d_path, fui->current->name); + file_upload_open(fui->d_path, fui->current->name, app->query_args); destroy_dialogue(d, app); app->query_args->state = S_UPLOAD_PARAMS; break; diff --git a/src/ui/modals/upload_props_dialogue.c b/src/ui/modals/upload_props_dialogue.c new file mode 100644 index 0000000..5b1e796 --- /dev/null +++ b/src/ui/modals/upload_props_dialogue.c @@ -0,0 +1,70 @@ +#include "../app.h" +#include "../widget/dialogue.h" +#include "../widget/fs_file_list.h" +#include "../widget/group.h" +#include +#include +#include +#include +#include +#include + +void upload_props_dialogue_modal_cb(callback_args_t *args) { + /* TODO: finish */ + int32_t response; + callback_args_t d_args; + app_t *app = args->app; + dialogue_t *d = (dialogue_t *)app->active_widget; + memcpy(&d_args, args, sizeof(callback_args_t)); + d_args.app = NULL; + d_args.resp_data = &response; + d_args.element = app->active_widget; + dialogue_default_callback(&d_args); + char query[256]; + int32_t query_len = 0; + if (response > -1) { + switch (response) { + case 0: + if (app->query_args->file->name != NULL) + free(app->query_args->file->name); + if (app->query_args->file->path != NULL) + free(app->query_args->file->path); + free(app->query_args->file); + app->query_args->file = NULL; + app->query_args->state = S_WAIT_SERVER; + break; + case 1: + query_len = sprintf(query, "file upload \"%s\" %zu 1\n", app->query_args->file->name, app->query_args->file->size); + destroy_dialogue(d, app); + app->query_args->state = S_UPLOAD_REQUESTED; + write(app->query_args->sd, query, query_len+1); + break; + } + } +} + +dialogue_t *init_upload_props_dialogue_modal(app_t *app) { + if (app == NULL) + return NULL; + group_el_init_t content[] = { + {.type = w_input, .label = "File description", .length = 30}, + {.type = w_end}}; + + group_el_init_t actions[] = { + {.type = w_button, .label = "Cancel", .is_default = 0}, {.type = w_end}}; + + app->modal.w.parent_win = &app->win; + init_dialogue(&(app->modal), "Description for the file", + "Enter essential data", &(app->coordinates)); + dialogue_t *d = &(app->modal); + + d->w.callback = upload_props_dialogue_modal_cb; + d->g_content = init_group(&(d->win), &(d->w), content, horizontal); + d->g_action = init_group(&(d->win), &(d->w), actions, horizontal); + + app->query_args->active_dialogue = d; + + dialogue_init_active_id(d); + + return NULL; +} \ No newline at end of file diff --git a/src/ui/modals/upload_props_dialogue.h b/src/ui/modals/upload_props_dialogue.h new file mode 100644 index 0000000..4412f2f --- /dev/null +++ b/src/ui/modals/upload_props_dialogue.h @@ -0,0 +1,9 @@ +#ifndef UPLOAD_PROPS_DIALOGUE_H +#define UPLOAD_PROPS_DIALOGUE_H + +#include "../widget/dialogue.h" +#include "../app.h" + +dialogue_t *init_upload_props_dialogue_modal(app_t *app); + +#endif \ No newline at end of file diff --git a/src/ui/widget/dialogue.c b/src/ui/widget/dialogue.c index 45c1d0e..2dc038a 100644 --- a/src/ui/widget/dialogue.c +++ b/src/ui/widget/dialogue.c @@ -49,11 +49,10 @@ widget_t *get_active_widget(dialogue_t *d) { } else { g = d->g_content; } - widget_t *w = (widget_t *) g->elements[d->active.id - g->first_id].element; + widget_t *w = (widget_t *)g->elements[d->active.id - g->first_id].element; return w; } - void dialogue_default_callback(callback_args_t *args) { dialogue_t *d = (void *)args->element; int32_t key = *((int32_t *)args->data); @@ -324,4 +323,5 @@ void destroy_dialogue(dialogue_t *d, void *_app) { app->active_win = app->left_win; app->active_callback = file_list_cb; app->query_args->active_dialogue = NULL; + app_refresh(app); } \ No newline at end of file diff --git a/src/ui/widget/fs_file_list.c b/src/ui/widget/fs_file_list.c index d84b8d8..12de02a 100644 --- a/src/ui/widget/fs_file_list.c +++ b/src/ui/widget/fs_file_list.c @@ -1,6 +1,5 @@ #include "fs_file_list.h" #include "progress_bar.h" -#include "../app.h" #include #include #include