From 83157a55f354b663f496a001295bd112070c277a Mon Sep 17 00:00:00 2001 From: Alexander Zhylin Date: Tue, 20 Jan 2026 15:17:40 +0200 Subject: [PATCH] ui: selection of upload file (needs next step realization) --- src/main.h | 6 +++++- src/ui/modals/upload_dialogue.c | 13 ++++++++++-- src/ui/widget/fs_file_list.c | 37 +++++++++++++++++++++++++-------- src/ui/widget/fs_file_list.h | 2 +- 4 files changed, 45 insertions(+), 13 deletions(-) diff --git a/src/main.h b/src/main.h index ca06b5e..f00298c 100644 --- a/src/main.h +++ b/src/main.h @@ -13,6 +13,9 @@ #define DIALOGUE_TITLE 64 #define DIALOGUE_TEXT 4096 #define INPUT_TEXT DIALOGUE_TITLE +#ifndef __USE_GNU +#define __USE_GNU 1 +#endif typedef struct params { unsigned short port; @@ -49,6 +52,7 @@ enum state { typedef struct p_file { char *name; + char *path; size_t size; size_t rest; int fd; @@ -83,7 +87,7 @@ typedef struct query_args { uint64_t capacity; uint64_t size; } server_message; - void *file_list_ui; /* TODO: find solution to insert actual type without recursion */ + void *file_list_ui; /* TODO: find solution to insert actual type without void pointers */ void *progress_bar; void *active_dialogue; char *notification; diff --git a/src/ui/modals/upload_dialogue.c b/src/ui/modals/upload_dialogue.c index a7f9c91..53997ef 100644 --- a/src/ui/modals/upload_dialogue.c +++ b/src/ui/modals/upload_dialogue.c @@ -1,8 +1,11 @@ #include "../app.h" #include "../widget/dialogue.h" #include "../widget/group.h" +#include "../widget/fs_file_list.h" #include #include +#include +#include #include void upload_dialogue_modal_cb(callback_args_t *args) { @@ -15,12 +18,19 @@ 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; if (response > -1) { switch (response) { case 0: app->query_args->state = S_WAIT_SERVER; 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); + destroy_dialogue(d, app); + app->query_args->state = S_UPLOAD_PARAMS; break; } } @@ -35,8 +45,7 @@ dialogue_t *init_upload_dialogue_modal(app_t *app) { {.type = w_end}}; group_el_init_t actions[] = { - {.type = w_button, .label = "Cancel", .is_default = 0}, - {.type = w_end}}; + {.type = w_button, .label = "Cancel", .is_default = 0}, {.type = w_end}}; app->modal.w.parent_win = &app->win; init_dialogue(&(app->modal), "Upload new file", "Enter essential data", diff --git a/src/ui/widget/fs_file_list.c b/src/ui/widget/fs_file_list.c index 623dfd4..d84b8d8 100644 --- a/src/ui/widget/fs_file_list.c +++ b/src/ui/widget/fs_file_list.c @@ -1,5 +1,6 @@ #include "fs_file_list.h" #include "progress_bar.h" +#include "../app.h" #include #include #include @@ -79,22 +80,39 @@ fs_fl_item_t *get_files_from_fs(char *path) { fl_add(&f_start, &f_current, &f_args); } } - if (d_current != NULL) d_current->next = f_start; - if (f_start != NULL) f_start->prev = d_current; + if (d_current != NULL) + d_current->next = f_start; + if (f_start != NULL) + f_start->prev = d_current; return d_start; } -void select_item(ui_fs_file_list_t *fui) { - char d_name[64] = "./"; +void select_item(ui_fs_file_list_t *fui, int32_t *resp_data) { + char *bash_case = NULL; + char *selection = NULL; + size_t dp_current; if (fui->current->d_type == DT_DIR) { if (!strcmp("/..", fui->current->name)) { - /* TODO: slice the last "/abc" */ + bash_case = strrchr(fui->d_path, '/'); + if (bash_case != NULL) { + *bash_case = '\0'; + fl_clear(&(fui->start), &(fui->current)); + fui->start = get_files_from_fs(fui->d_path); + } } else { - strcat(d_name, fui->current->name + 1); /* skip "/" in the name */ + dp_current = strlen(fui->d_path); + selection = fui->current->name + 1; + fui->d_path = + realloc(fui->d_path, dp_current + strlen(selection) + 2); + sprintf(fui->d_path, "%s/%s", fui->d_path, selection); + fl_clear(&(fui->start), &(fui->current)); + fui->start = get_files_from_fs(fui->d_path); } - fui->start = get_files_from_fs(d_name); fui->current = fui->start; + fui->current_idx = 0; draw_fs_file_list(fui); + } else if (fui->current->d_type == DT_REG) { + *resp_data = 1; } } @@ -120,7 +138,7 @@ void fs_file_list_cb(callback_args_t *args) { } break; case '\n': - select_item(fui); + select_item(fui, (int32_t *) args->resp_data); break; } } @@ -131,7 +149,8 @@ ui_fs_file_list_t *init_fs_file_list(WINDOW **win, widget_t *w_parent) { init_widget(&(fl_ui->w), w_parent, win, ""); fl_ui->current_idx = 0; fl_ui->info_win = NULL; - fl_ui->start = get_files_from_fs("./"); + fl_ui->d_path = get_current_dir_name(); + fl_ui->start = get_files_from_fs(fl_ui->d_path); fl_ui->current = fl_ui->start; fl_ui->w.callback = fs_file_list_cb; fl_ui->w.x = getmaxx(win_par) / 10 * 8; diff --git a/src/ui/widget/fs_file_list.h b/src/ui/widget/fs_file_list.h index b36f0d0..db380d8 100644 --- a/src/ui/widget/fs_file_list.h +++ b/src/ui/widget/fs_file_list.h @@ -18,7 +18,7 @@ typedef struct { fs_fl_item_t *start; int32_t current_idx; WINDOW *const* info_win; - char d_path[512]; + char *d_path; } ui_fs_file_list_t; void draw_fs_file_list(ui_fs_file_list_t *fl_ui);