From b56875c9a9814450650e791f25c691aacfe85b5e Mon Sep 17 00:00:00 2001 From: Alexander Zhylin Date: Wed, 18 Mar 2026 16:03:29 +0200 Subject: [PATCH] changes upload method --- CMakeLists.txt | 2 +- TODO.md | 4 +++- src/file_processor.c | 53 ++++++++++++------------------------------ src/main.h | 1 + src/modals/alert.c | 19 ++++++++------- src/modals/alert.h | 7 +++--- src/query.c | 53 ++++++++++++++++++++++++++++++------------ src/query.h | 2 ++ src/server.c | 4 +++- src/widget/app.h | 4 ++-- src/widget/dialogue.c | 20 +++++++++------- src/widget/dialogue.h | 5 +++- src/widget/file_list.c | 8 ++++--- 13 files changed, 101 insertions(+), 81 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c7695c..f034140 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ if( (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang") ) - add_compile_definitions(_GNU_SOURCE) + add_compile_definitions(_GNU_SOURCE) endif() set(CURSES_NEED_WIDE TRUE) diff --git a/TODO.md b/TODO.md index 0a6f4dc..d3bcfc2 100644 --- a/TODO.md +++ b/TODO.md @@ -4,7 +4,6 @@ - [ ] PageUp/PageDown key functionality in lists - [ ] File upload multiline description (window) - [x] Permission checkboxes for file upload/edit -- [ ] Check file upload - [ ] More intuitive navigation between nested groups (by pressing left and right buttons) ## Features: @@ -15,6 +14,9 @@ - [ ] Files removal - [ ] Encryption - [x] UTF-8 support +- [ ] Check MD5 sum after download/upload +- [ ] Get rid of process_server_command (use callbacks) +- [ ] UI: separate file browser with more functions ## Future ideas - [ ] Get rid of old notification mechanism diff --git a/src/file_processor.c b/src/file_processor.c index 3f4fd0b..781859b 100644 --- a/src/file_processor.c +++ b/src/file_processor.c @@ -214,36 +214,6 @@ void file_download(file_args_t *f_args, query_args_t *q_args) { } } } -/* obsolete */ -int32_t file_upload_request(char *query, query_args_t *q_args) { - char qbuf[INBUFSIZE]; - char fpath[512]; - char a_perm; - sscanf(query, "file upload \"%s %c", fpath, &a_perm); - fpath[strlen(fpath) - 1] = 0; // remove \" - int fd = open(fpath, O_RDONLY); - if (fd == -1) { - perror(fpath); - return -1; - } - size_t fsize = lseek(fd, 0, SEEK_END); - lseek(fd, 0, SEEK_SET); - char *fname = strrchr(fpath, '/') + 1; - int qlen = snprintf(qbuf, INBUFSIZE, "file upload \"%s\" %zu %c", fname, - fsize, a_perm); - if (qlen > INBUFSIZE) { - fprintf(stderr, "query is too large to be sent\n"); - return -1; - } - q_args->file = malloc(sizeof(p_file_t)); - 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; - write(q_args->sd, qbuf, qlen); - return 0; -} int32_t file_upload_open(char *dpath, char *fname, query_args_t *q_args) { q_args->file = malloc(sizeof(p_file_t)); @@ -314,7 +284,9 @@ int32_t file_upload(query_args_t *q_args) { void clear_file_in_query(query_args_t *q_args) { free(q_args->file->name); free(q_args->file->path); - free(q_args->file->description); + if (q_args->file->description != NULL) { + free(q_args->file->description); + } free(q_args->file); q_args->file = NULL; } @@ -332,6 +304,7 @@ void init_file_args(file_args_t *f_args) { static void fl_add(fl_item_t **cur, fl_item_t **start, char *line) { char fname[128]; char fowner[32]; + char *descr_begin = NULL; int h_len; fl_item_t *fitem = malloc(sizeof(fl_item_t)); sscanf(line, "%s %zu %s%n", fname, &(fitem->size), fowner, &h_len); @@ -342,14 +315,18 @@ static void fl_add(fl_item_t **cur, fl_item_t **start, char *line) { fitem->owner = malloc((sizeof(char)) * (strlen(fowner) + 1)); strcpy(fitem->owner, fowner); /* file description */ - int d_len = strlen(line) - h_len; - fitem->description = malloc((sizeof(char)) * (d_len)); - strcpy(fitem->description, line + h_len + 1); // +1 to escape the dividing \32 - int i; - for (i = 0; i < d_len; i++) { - if (fitem->description[i] == '\a') { - fitem->description[i] = '\n'; + descr_begin = line + h_len + 1; + if (strcmp(descr_begin, "\n")) { + int d_len = strlen(line) - h_len; + fitem->description = malloc((sizeof(char)) * (d_len)); + strcpy(fitem->description, descr_begin); // +1 to escape the dividing \32 + for (int i = 0; i < d_len; i++) { + if (fitem->description[i] == '\a') { + fitem->description[i] = '\n'; + } } + } else { + fitem->description = NULL; } /* fitem->description[strlen(line + line_pos)] = 0; */ fitem->next = NULL; diff --git a/src/main.h b/src/main.h index 05f9560..65c87b6 100644 --- a/src/main.h +++ b/src/main.h @@ -43,6 +43,7 @@ enum state { S_UPLOAD_FILE, S_UPLOAD_FILE_SELECT, S_UPLOAD_REQUESTED, + S_UPLOAD_SERVER_FINISHES, S_ASK_USER_BEFORE_LOGIN, S_ERR, S_NEXT_ACTION, diff --git a/src/modals/alert.c b/src/modals/alert.c index a6eeb96..f312cd7 100644 --- a/src/modals/alert.c +++ b/src/modals/alert.c @@ -1,13 +1,13 @@ -#include +#include "alert.h" +#include #include #include #include +#include /* Short notifications. Prefer using this method to simply notify the client */ static app_t *app; -void notification(const char *title, const char *message, - enum d_color_scheme color); void alert_cb(callback_args_t *args) { // @@ -15,10 +15,10 @@ void alert_cb(callback_args_t *args) { void init_alert(app_t *_app) { app = _app; } -void alert(const char *message) { notification("Alert", message, dc_alert); } +void alert(const char *message) { notification("Alert", dc_alert, message); } -void notification(const char *title, const char *message, - enum d_color_scheme color) { +void notification(const char *title, enum d_color_scheme color, + const char *f_message, ...) { if (app == NULL) return; @@ -26,12 +26,15 @@ void notification(const char *title, const char *message, {.type = w_button, .label = "OK", .is_default = true}, {.type = w_end}}; dialogue_t *d = malloc(sizeof(dialogue_t)); + va_list args; + va_start(args, f_message); - init_dialogue(d, title, message, &(app->coordinates)); + vinit_dialogue(d, title, &(app->coordinates), f_message, &args); d->w.callback = alert_cb; d->g_content = NULL; - d->g_action = init_group(&(d->win), &(d->w), actions,&(d->id_map), horizontal, g_action); + d->g_action = init_group(&(d->win), &(d->w), actions, &(d->id_map), + horizontal, g_action); dialogue_init_active_id(d); d->color_scheme = color; diff --git a/src/modals/alert.h b/src/modals/alert.h index 99a20d6..156f6fa 100644 --- a/src/modals/alert.h +++ b/src/modals/alert.h @@ -1,8 +1,9 @@ #ifndef ALERT_H #define ALERT_H #include "app.h" +#include void init_alert(app_t *app); -void alert(char *message); -void notification(const char *title, const char *message, - enum d_color_scheme color); +void alert(const char *message); +void notification(const char *title, enum d_color_scheme color, + const char *f_message, ...); #endif \ No newline at end of file diff --git a/src/query.c b/src/query.c index 04e6bfd..000f8b1 100644 --- a/src/query.c +++ b/src/query.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: MIT */ /* Copyright (c) 2026 Oleksandr Zhylin */ +#include "query.h" #include #include #include @@ -19,17 +20,19 @@ #include #include -#include "connection.h" -#include "draw_modal.h" -#include "file_processor.h" -#include "main.h" -#include "server.h" -#include "types.h" #include #include #include -static void wait_side(app_t *app); +#include "alert.h" +#include "connection.h" +#include "dialogue.h" +#include "file_processor.h" +#include "main.h" +#include "server.h" +#include "types.h" + +static int wait_side(app_t *app, wait_server_cb *cb); void user_request_description(query_args_t *q_args); int process_query(app_t *app); int32_t process_user_input(app_t *app, callback_args_t *d_args); @@ -131,6 +134,18 @@ void wait_register(query_args_t *q_args) { write(q_args->sd, r_buf, strlen(r_buf) - 1); } +int upload_confirm_cb(app_t *app, char *query) { + if (!strncmp("finished\n", query, sizeof("finished\n") - 1)) { + notification("File upload", dc_normal, "File %s is uploaded to the server!", + app->query_args->file->name); + clear_file_in_query(app->query_args); + app->query_args->state = WAIT_CLIENT; + app->modal.needs_destroy = true; + return 0; + } + return 1; /* TODO: Error case */ +} + int process_query(app_t *app) { query_args_t *query_args = app->query_args; file_args_t *file_args = app->file_args; @@ -144,7 +159,7 @@ int process_query(app_t *app) { case S_WAIT_SERVER: case WAIT_REGISTER_CONFIRMATION: case S_WAIT_REGISTER_CONFIRMATION: - wait_side(app); + wait_side(app, NULL); break; case S_ERR: break; @@ -153,7 +168,7 @@ int process_query(app_t *app) { break; case S_FILE_SELECT: if (query_args->from_server) { - wait_side(app); + wait_side(app, NULL); } else { file_select(file_args, query_args); } @@ -171,14 +186,16 @@ int process_query(app_t *app) { case S_UPLOAD_PARAMS: case S_UPLOAD_FILE: if ((res = file_upload(query_args)) == 1) { - /* upload finishes */ - user_request_description(query_args); - clear_file_in_query(query_args); - query_args->state = S_WAIT_SERVER; + /* wait server for finish upload */ + query_args->state = S_UPLOAD_SERVER_FINISHES; } else if (res == -1) { /* TODO: Error */ } break; + case S_UPLOAD_SERVER_FINISHES: + wait_side(app, upload_confirm_cb); + /* user_request_description(query_args); */ + break; case S_ASK_USER_BEFORE_LOGIN: break; case WAIT_REGISTER: @@ -228,17 +245,23 @@ int query_extract_from_buf(char *buf, int *buf_used, char **output_line) { return pos + 1; } -static void wait_side(app_t *app) { +static int wait_side(app_t *app, wait_server_cb *callback) { query_args_t *q_args = app->query_args; int qlen; int buf_used = q_args->buf_used; char *query = NULL; + int res = 0; while ((qlen = query_extract_from_buf(q_args->buf, &buf_used, &query))) { - process_server_command(query, qlen, app); + if (callback == NULL) { + process_server_command(query, qlen, app); + } else { + res = callback(app, query); + } free(query); query = NULL; } + return res; } void init_query_args(query_args_t *q_args, params_t *params) { diff --git a/src/query.h b/src/query.h index 30cf1b1..7d533a5 100644 --- a/src/query.h +++ b/src/query.h @@ -4,6 +4,7 @@ #ifndef QUERY_H #define QUERY_H +#include "app.h" #include "main.h" #include @@ -12,5 +13,6 @@ void wait_register(query_args_t *q_args); int process_query(app_t *app); int query_extract_from_buf(char *buf, int *buf_used, char **output_line); void init_query_args(query_args_t *q_args, params_t *params); +typedef int wait_server_cb(app_t *app, char *query); #endif \ No newline at end of file diff --git a/src/server.c b/src/server.c index 0b775b7..8880a51 100644 --- a/src/server.c +++ b/src/server.c @@ -29,6 +29,7 @@ void ask_register(params_t *params, char *email); return 0; \ } + /* TODO: refactor to process callbacks */ int process_server_command(char *line, int l_len, app_t *app) { int ws_pos = l_len; query_args_t *q_args = app->query_args; @@ -87,7 +88,7 @@ int process_server_command(char *line, int l_len, app_t *app) { "You've been successfully registered.\n" "Welcome, %s!", app->params->uname); - notification("Registration", query, dc_normal); + notification("Registration", dc_normal, query); print_bars(app); return 0; } else { @@ -111,6 +112,7 @@ int process_server_command(char *line, int l_len, app_t *app) { return 0; } + /* TODO: Hide details */ if (q_args->state == S_WAIT_SERVER) { if (q_args->server_message.text == NULL) { q_args->server_message.text = malloc(l_len + 1); diff --git a/src/widget/app.h b/src/widget/app.h index d0dfbf6..f2635cb 100644 --- a/src/widget/app.h +++ b/src/widget/app.h @@ -36,8 +36,8 @@ typedef struct { void *active_widget; } app_t; -app_t *init_app(); -void init_nc(); +app_t *init_app(void); +void init_nc(void); void destroy_app(app_t *app, int32_t exit_code); void draw_borders(app_t *app); void print_bars(app_t *app); diff --git a/src/widget/dialogue.c b/src/widget/dialogue.c index 26cf7c3..8b502cc 100644 --- a/src/widget/dialogue.c +++ b/src/widget/dialogue.c @@ -1,20 +1,16 @@ /* SPDX-License-Identifier: MIT */ /* Copyright (c) 2026 Oleksandr Zhylin */ - #include #include +#include #include #include -#include "app.h" -#include "button.h" -#include "checkbox.h" -#include "dialogue.h" -#include "file_list.h" -#include "group.h" -#include "input.h" +#include #include +#include "file_list.h" + void incr_active_id(dialogue_t *d) { group_el_t *active_el = NULL; while (true) { @@ -170,6 +166,14 @@ void init_dialogue(dialogue_t *dialogue, const char title[], const char text[], dialogue->text[t_size - 1] = '\0'; } +/* variadic */ +void vinit_dialogue(dialogue_t *dialogue, const char title[], + coordinates_t *p_coordinates, const char fmt[], va_list *v_args) { + char f_text[DIALOGUE_TEXT]; + vsprintf(f_text, fmt, *v_args); + init_dialogue(dialogue, title, f_text, p_coordinates); +} + int group_init_active_id(group_t *g, dialogue_t *d) { enum w_type wt; diff --git a/src/widget/dialogue.h b/src/widget/dialogue.h index 2d201db..52aebcc 100644 --- a/src/widget/dialogue.h +++ b/src/widget/dialogue.h @@ -8,8 +8,8 @@ #include "group.h" #include #include -#include #include +#include #define MAX_IDS 64 enum d_color_scheme { dc_normal, dc_alert }; @@ -31,6 +31,9 @@ typedef struct { void init_dialogue(dialogue_t *d, const char title[], const char text[], coordinates_t *p_coordinates); +void vinit_dialogue(dialogue_t *dialogue, const char title[], + coordinates_t *p_coordinates, const char fmt[], + va_list *v_args); int32_t draw_dialogue(dialogue_t *d); void destroy_dialogue(dialogue_t *d, void *app); void dialogue_default_callback(callback_args_t *args); diff --git a/src/widget/file_list.c b/src/widget/file_list.c index 7f9b593..4389fe7 100644 --- a/src/widget/file_list.c +++ b/src/widget/file_list.c @@ -202,7 +202,7 @@ void draw_file_list(ui_file_list_t *fui) { char p_info[64]; - /* Draw file info */ + /* Draw file info [right side] */ { uint32_t p_y = 1; p_x = 1; @@ -216,8 +216,10 @@ void draw_file_list(ui_file_list_t *fui) { mvwprintw(i_win, p_y++, p_x, "Size: %s", size_text); mvwprintw(i_win, p_y++, p_x, "Owner: %s", active_el->owner); - mvwprintw(i_win, p_y++, p_x, "Description: "); - print_multiline_text(i_win, active_el->description, sz_x, p_y, p_x, 0); + if (active_el->description != NULL) { + mvwprintw(i_win, p_y++, p_x, "Description: "); + print_multiline_text(i_win, active_el->description, sz_x, p_y, p_x, 0); + } } }