From e2931714c9c31722b238dab243b0ac0a9ac91b3e Mon Sep 17 00:00:00 2001 From: Alexander Zhylin Date: Thu, 19 Feb 2026 14:16:38 +0200 Subject: [PATCH] ui: notification; finishes registration process --- TODO.md | 3 +- src/main.c | 10 ++++-- src/main.h | 4 +-- src/modals/alert.c | 29 ++++++++++++---- src/modals/alert.h | 5 ++- src/modals/login_credentials.c | 3 +- src/modals/notification.c | 2 ++ src/modals/register.c | 7 ++-- src/query.c | 62 ++++++++++++++++------------------ src/query.h | 4 +-- src/server.c | 20 ++++++++--- src/server.h | 3 +- src/widget/dialogue.c | 6 +++- 13 files changed, 99 insertions(+), 59 deletions(-) diff --git a/TODO.md b/TODO.md index bc2fe67..7325492 100644 --- a/TODO.md +++ b/TODO.md @@ -3,7 +3,8 @@ ## Features: - [x] Search files - [x] Correct spacing in the search bar -- [ ] Finish registration +- [x] Finish registration +- [ ] Get rid of old notification mechanism - [ ] Privilege settings for files - [ ] Files removal - [ ] Encryption diff --git a/src/main.c b/src/main.c index 53e2654..6fe37e7 100644 --- a/src/main.c +++ b/src/main.c @@ -8,12 +8,13 @@ #include #include #include +#include #include "connection.h" -#include "main.h" #include "file_list.h" +#include "main.h" #include "query.h" -#include +#include "modals/alert.h" uint32_t m_id = 0; @@ -33,6 +34,9 @@ int main(int argc, char **argv) { app->params = ¶ms; analyze_args(argc, argv, ¶ms); + /* init alert */ + init_alert(app); + wrefresh(app->left_win); wrefresh(app->right_win); @@ -62,7 +66,7 @@ int main(int argc, char **argv) { int32_t process_user_input(app_t *app, callback_args_t *d_args) { int32_t c; - ui_file_list_t *fui = (ui_file_list_t *) app->query_args->file_list_ui; + ui_file_list_t *fui = (ui_file_list_t *)app->query_args->file_list_ui; c = wgetch(app->win); switch (c) { case KEY_F(9): diff --git a/src/main.h b/src/main.h index 3a82932..679cf68 100644 --- a/src/main.h +++ b/src/main.h @@ -1,6 +1,7 @@ #ifndef MAIN_H #define MAIN_H +#include #include #include @@ -80,9 +81,8 @@ typedef struct query_args { int32_t buf_used; p_file_t *file; enum state state; - enum state next_state; char *buf; - uint32_t from_server : 1; + bool from_server; params_t *params; char *next_server_command; struct { diff --git a/src/modals/alert.c b/src/modals/alert.c index 3fc0c36..7863d4f 100644 --- a/src/modals/alert.c +++ b/src/modals/alert.c @@ -5,11 +5,22 @@ #include #include -void init_alert_cb(callback_args_t *args) { +/* 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) { // } -void alert(app_t *app, const char *message) { +void init_alert(app_t *_app) { app = _app; } + +void alert(const char *message) { notification("Alert", message, dc_alert); } + +void notification(const char *title, const char *message, + enum d_color_scheme color) { if (app == NULL) return; @@ -18,25 +29,29 @@ void alert(app_t *app, const char *message) { dialogue_t *d = malloc(sizeof(dialogue_t)); - init_dialogue(d, "Alert", message, &(app->coordinates)); + init_dialogue(d, title, message, &(app->coordinates)); - d->w.callback = init_alert_cb; + d->w.callback = alert_cb; d->g_content = NULL; d->g_action = init_group(&(d->win), &(d->w), actions, horizontal); dialogue_init_active_id(d); - d->color_scheme = dc_alert; + d->color_scheme = color; draw_dialogue(d); wrefresh(d->win); int c = 0; while ((c = wgetch(d->win))) { - if (c == '\n') break; + if (c == '\n') + break; } - + destroy_group(d->g_action); delwin(d->win); free(d); app_refresh(app); + if (app->modal.win != NULL) { + app->modal.needs_update = true; + } } \ No newline at end of file diff --git a/src/modals/alert.h b/src/modals/alert.h index 80283e4..99a20d6 100644 --- a/src/modals/alert.h +++ b/src/modals/alert.h @@ -1,5 +1,8 @@ #ifndef ALERT_H #define ALERT_H #include "app.h" -void alert(app_t *app, const char *message); +void init_alert(app_t *app); +void alert(char *message); +void notification(const char *title, const char *message, + enum d_color_scheme color); #endif \ No newline at end of file diff --git a/src/modals/login_credentials.c b/src/modals/login_credentials.c index ed2e00f..e844040 100644 --- a/src/modals/login_credentials.c +++ b/src/modals/login_credentials.c @@ -38,7 +38,8 @@ void init_login_credentials_modal_cb(callback_args_t *args) { print_bars(app); break; case 1: - destroy_app(app, 0); + app->query_args->state = S_ASK_LOGIN_TYPE; + app->modal.needs_destroy = true; } } } diff --git a/src/modals/notification.c b/src/modals/notification.c index 43cce0d..f0cdd05 100644 --- a/src/modals/notification.c +++ b/src/modals/notification.c @@ -5,6 +5,8 @@ #include +/* TODO: get rid of this mechanism */ + typedef struct { button_t *element; enum w_type type; diff --git a/src/modals/register.c b/src/modals/register.c index 2386b91..bac1657 100644 --- a/src/modals/register.c +++ b/src/modals/register.c @@ -33,7 +33,7 @@ void init_register_modal_cb(callback_args_t *args) { switch (response) { case 0: if (strcmp(in_pass->value, in_pass_r->value)) { - alert(app, "Your passwords do not match"); + alert("Your passwords do not match"); in_pass->value[0] = '\0'; in_pass->value_len = 0; in_pass_r->value[0] = '\0'; @@ -50,11 +50,10 @@ void init_register_modal_cb(callback_args_t *args) { &qlen); write(app->params->sd, query, qlen); app->query_args->state = S_WAIT_REGISTER_CONFIRMATION; - /* d->needs_destroy = true; - print_bars(app); */ break; case 1: - destroy_app(app, 0); + app->query_args->state = S_ASK_LOGIN_TYPE; + app->modal.needs_destroy = true; } } } diff --git a/src/query.c b/src/query.c index 46f8d22..6c6fd15 100644 --- a/src/query.c +++ b/src/query.c @@ -16,19 +16,28 @@ #include #include +#include "app.h" #include "connection.h" +#include "draw_modal.h" #include "file_processor.h" #include "main.h" +#include "modals/alert.h" #include "server.h" -#include "app.h" -#include "draw_modal.h" #include "types.h" +#include "widget/app.h" -static void wait_side(query_args_t *q_args); +static void wait_side(app_t *app); void user_request_description(query_args_t *q_args); -int process_query(query_args_t *query_args, file_args_t *file_args); +int process_query(app_t *app); int32_t process_user_input(app_t *app, callback_args_t *d_args); +#define EXIT_APP(message, app, exit_code) \ + { \ + alert(message); \ + close_session(app->params->sd); \ + destroy_app(app, exit_code); \ + } + void query_loop(app_t *app) { query_args_t *query_args = app->query_args; fd_set readfds; @@ -67,16 +76,15 @@ void query_loop(app_t *app) { free(query_args->next_server_command); query_args->next_server_command = NULL; query_args->from_server = TRUE; - if (ERR == process_query(query_args, &file_args)) { - close_session(sd); - exit(1); + if (ERR == process_query(app)) { + EXIT_APP("*** error while processing query ***", app, 1) } continue; } else { sr = select(maxfd + 1, &readfds, NULL, NULL, NULL); if (sr == -1) { - perror("select"); - exit(3); + /* perror("select"); */ + EXIT_APP("*** error while processing 'select' call ***", app, 3) } } @@ -84,15 +92,12 @@ void query_loop(app_t *app) { /* process request from the server */ qlen = read(sd, query_args->buf, INBUFSIZE); if (qlen == 0) { - close_session(sd); - printf("\n*** server closed the connection. ***\n"); - exit(1); + EXIT_APP("*** server closed the connection ***", app, 1) } else { query_args->buf_used = qlen; query_args->from_server = TRUE; - if (ERR == process_query(query_args, &file_args)) { - close_session(sd); - exit(1); + if (ERR == process_query(app)) { + EXIT_APP("*** error while processing query ***", app, 1) } } } @@ -102,22 +107,12 @@ void query_loop(app_t *app) { /* process upload/download */ query_args->buf_used = read(query_args->file->fd, query_args->buf, INBUFSIZE); - process_query(query_args, &file_args); + process_query(app); } if (FD_ISSET(STDIN_FILENO, &readfds)) { - /* process request from the client */ - /* qlen = read(STDIN_FILENO, buf, INBUFSIZE); */ - /* if (qlen == 0) { - close_session(sd); - exit(0); - } else { */ - /* query_args->buf_used = qlen; - query_args->from_server = 0; */ - // if (-1 == process_query(&query_args, &file_args)) { if (ERR == process_user_input(app, &d_args)) { - close_session(sd); - exit(1); + EXIT_APP("*** error while processing user input ***", app, 4) } } } @@ -133,7 +128,9 @@ void wait_register(query_args_t *q_args) { write(q_args->sd, r_buf, strlen(r_buf) - 1); } -int process_query(query_args_t *query_args, file_args_t *file_args) { +int process_query(app_t *app) { + query_args_t *query_args = app->query_args; + file_args_t *file_args = app->file_args; query_args->buf[query_args->buf_used] = 0; int32_t res = 0; switch (query_args->state) { @@ -144,7 +141,7 @@ int process_query(query_args_t *query_args, file_args_t *file_args) { case S_WAIT_SERVER: case WAIT_REGISTER_CONFIRMATION: case S_WAIT_REGISTER_CONFIRMATION: - wait_side(query_args); + wait_side(app); break; case S_ERR: break; @@ -153,7 +150,7 @@ int process_query(query_args_t *query_args, file_args_t *file_args) { break; case S_FILE_SELECT: if (query_args->from_server) { - wait_side(query_args); + wait_side(app); } else { file_select(file_args, query_args); } @@ -233,13 +230,14 @@ int query_extract_from_buf(char *buf, int *buf_used, char **output_line) { return pos + 1; } -static void wait_side(query_args_t *q_args) { +static void wait_side(app_t *app) { + query_args_t *q_args = app->query_args; int qlen; int buf_used = q_args->buf_used; char *query = NULL; while ((qlen = query_extract_from_buf(q_args->buf, &buf_used, &query))) { - process_server_command(query, qlen, q_args); + process_server_command(query, qlen, app); free(query); query = NULL; } diff --git a/src/query.h b/src/query.h index d3af3dc..7517c0b 100644 --- a/src/query.h +++ b/src/query.h @@ -2,11 +2,11 @@ #define QUERY_H #include "main.h" -#include "app.h" +#include void query_loop(app_t *); void wait_register(query_args_t *q_args); -int process_query(query_args_t *query_args, file_args_t *file_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); diff --git a/src/server.c b/src/server.c index 0b8abfe..4389f93 100644 --- a/src/server.c +++ b/src/server.c @@ -1,6 +1,8 @@ +#include "app.h" +#include "dialogue.h" #include "main.h" -#include "widget/file_list.h" #include "modals/alert.h" +#include "widget/file_list.h" #include #include #include @@ -16,6 +18,7 @@ void ask_uname_and_password(params_t *params); void ask_register(params_t *params, char *email); +/* TODO: questionable solution. Do better */ #define PRINT_SRV_MESSAGE(q_args, l_len, line) \ if (q_args->server_message.size > 0) { \ q_args->state = S_PRINT_SERVER_MESSAGE; \ @@ -24,8 +27,9 @@ void ask_register(params_t *params, char *email); return 0; \ } -int process_server_command(char *line, int l_len, query_args_t *q_args) { +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; params_t *params = q_args->params; uint64_t new_capacity; ui_file_list_t *fui = (ui_file_list_t *)q_args->file_list_ui; @@ -71,14 +75,22 @@ int process_server_command(char *line, int l_len, query_args_t *q_args) { /* REGISTER CONFIRMATION */ if (q_args->state == S_WAIT_REGISTER_CONFIRMATION) { - if (!strcmp(line, "ok")) { + if (!strcmp(line, "ok\n")) { sprintf(query, "file list %u %u\n%n", fui->max_lines, fui->current_page, &q_len); write(q_args->sd, query, q_len); q_args->state = S_FILE_LIST; + destroy_dialogue(&(app->modal), app); + sprintf(query, + "You've been successfully registered.\n" + "Welcome, %s!", + app->params->uname); + notification("Registration", query, dc_normal); + print_bars(app); return 0; } else { - PRINT_SRV_MESSAGE(q_args, l_len, line) + q_args->state = S_ASK_REGISTER; + alert(line); return 1; } } diff --git a/src/server.h b/src/server.h index 6d6f1f1..b78f644 100644 --- a/src/server.h +++ b/src/server.h @@ -1,8 +1,9 @@ #ifndef SERVER_H #define SERVER_H +#include "app.h" #include "main.h" -int process_server_command(char *line, int l_len, query_args_t *q_args); +int process_server_command(char *line, int l_len, app_t *app); void ask_register(params_t *params, char *email); #endif \ No newline at end of file diff --git a/src/widget/dialogue.c b/src/widget/dialogue.c index 5959e2f..8de483f 100644 --- a/src/widget/dialogue.c +++ b/src/widget/dialogue.c @@ -164,6 +164,7 @@ void dialogue_default_callback(callback_args_t *args) { void init_dialogue(dialogue_t *dialogue, const char title[], const char text[], coordinates_t *p_coordinates) { + uint32_t t_size = 0; dialogue->win = 0; dialogue->g_content = NULL; dialogue->w.x = 0; @@ -175,7 +176,10 @@ void init_dialogue(dialogue_t *dialogue, const char title[], const char text[], dialogue->needs_destroy = false; dialogue->color_scheme = dc_normal; strcpy(dialogue->w.title, title); + t_size = strlen(text); strcpy(dialogue->text, text); + /* trim the last new line symbol */ + if (t_size && dialogue->text[t_size-1] == '\n') dialogue->text[t_size-1] = '\0'; } void dialogue_init_active_id(dialogue_t *dialogue) { @@ -290,7 +294,7 @@ int32_t draw_dialogue(dialogue_t *d) { wattrset(d->win, COLOR_PAIR(0) | A_BOLD | A_REVERSE); break; case dc_alert: - wattrset(d->win, COLOR_PAIR(4) | A_BOLD | A_REVERSE); + wattrset(d->win, COLOR_PAIR(4) | A_BOLD | A_REVERSE); break; }