diff --git a/TODO.md b/TODO.md index 7f08a4f..ea721d1 100644 --- a/TODO.md +++ b/TODO.md @@ -14,6 +14,6 @@ TODO: UPLOAD: - [ ] Progress bar -- [ ] Show notifications and messages from the server (universal way) +- [x] Show notifications and messages from the server (universal way) use notification or something diff --git a/src/deps.mk b/src/deps.mk index 2737a6c..3c35080 100644 --- a/src/deps.mk +++ b/src/deps.mk @@ -25,5 +25,5 @@ query.o: src/query.c src/client.h src/main.h src/types.h src/connection.h \ src/ui/widget/dialogue.h src/ui/widget/../widget.h \ src/ui/widget/../../common.h src/ui/widget/button.h \ src/ui/widget/../../main.h src/ui/widget/group.h src/file_processor.h \ - src/ui/widget/file_list.h src/server.h + src/ui/widget/file_list.h src/server.h src/ui/widget/dialogue.h server.o: src/server.c src/main.h src/types.h diff --git a/src/file_processor.c b/src/file_processor.c index 3c30ae3..4041bc5 100644 --- a/src/file_processor.c +++ b/src/file_processor.c @@ -204,9 +204,9 @@ void file_download(file_args_t *f_args, query_args_t *q_args) { if (!(it_count % it_interval)) { pb->procent = progress; d->needs_update = true; - } - if (it_count % it_interval) + } else { q_args->buf_used = 0; + } if (size_rest < qlen) { size_rest = 0; } else { @@ -292,18 +292,35 @@ int32_t file_upload_start(query_args_t *q_args) { } int32_t file_upload(query_args_t *q_args) { + static uint32_t it_count = 0; + static size_t it_interval = 0; + if (it_interval == 0) { + it_interval = (q_args->file->size / INBUFSIZE / 100) * 5; /* every 1% */ + if (it_interval == 0) + it_interval = 1; + } + uint32_t progress = + (q_args->file->size - q_args->file->rest) * 100 / q_args->file->size; + ui_progress_bar_t *pb = (ui_progress_bar_t *)q_args->progress_bar; + dialogue_t *d = (dialogue_t *)q_args->active_dialogue; if (q_args->buf_used > 0) { int wlen = write(q_args->sd, q_args->buf, q_args->buf_used); if (q_args->buf_used != wlen) { /* TODO: ошибка */ return -1; } else { + it_count++; + if (!(it_count % it_interval)) { + pb->procent = progress; + d->needs_update = true; + } q_args->buf[0] = 0; q_args->buf_used = 0; q_args->file->rest -= wlen; } } else { - /* TODO: уведомление пользователю */ + it_count = 0; + it_interval = 0; return 1; } return 0; diff --git a/src/query.c b/src/query.c index 138390b..9e9dd93 100644 --- a/src/query.c +++ b/src/query.c @@ -22,6 +22,7 @@ #include "main.h" #include "server.h" #include "ui/app.h" +#include "ui/widget/dialogue.h" static void wait_side(query_args_t *q_args); void user_request_description(query_args_t *q_args); @@ -44,6 +45,9 @@ void query_loop(app_t *app) { for (;;) { /* update screen */ + if (query_args->state == S_WAIT_SERVER && app->modal.is_initiated) { + destroy_dialogue(&(app->modal), app); + } app_draw_modal(app); app_refresh(app); @@ -134,7 +138,7 @@ void wait_register(query_args_t *q_args) { int process_query(query_args_t *query_args, file_args_t *file_args) { query_args->buf[query_args->buf_used] = 0; - + int32_t res = 0; switch (query_args->state) { case WAIT_SERVER: case WAIT_SERVER_INIT: @@ -168,11 +172,13 @@ int process_query(query_args_t *query_args, file_args_t *file_args) { break; case S_UPLOAD_PARAMS: case S_UPLOAD_FILE: - if (file_upload(query_args)) { + 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; + } else if (res == -1) { + /* TODO: Error */ } break; case S_ASK_USER_BEFORE_LOGIN: diff --git a/src/server.c b/src/server.c index b7b51a4..947b948 100644 --- a/src/server.c +++ b/src/server.c @@ -14,7 +14,7 @@ void ask_uname_and_password(params_t *params); void ask_register(params_t *params, char *email); -#define PRINT_SRV_MESSAGE(q_args) \ +#define PRINT_SRV_MESSAGE(q_args, l_len, line) \ if (q_args->server_message.size > 0) { \ q_args->state = S_PRINT_SERVER_MESSAGE; \ q_args->next_server_command = malloc(l_len + 1); \ @@ -36,7 +36,7 @@ int process_server_command(char *line, int l_len, query_args_t *q_args) { /* LOGIN */ if (!strncmp(line, "login>", ws_pos)) { if (q_args->server_message.size > 0) { - PRINT_SRV_MESSAGE(q_args); + PRINT_SRV_MESSAGE(q_args, l_len, line); } if (q_args->params->uname != NULL && q_args->params->pass != NULL) { write(params->sd, q_args->params->uname, strlen(q_args->params->uname)); @@ -110,13 +110,12 @@ int process_server_command(char *line, int l_len, query_args_t *q_args) { strncat(q_args->server_message.text, line, l_len); q_args->server_message.text[q_args->server_message.size + l_len] = 0; } - } else { - /* char buf[INBUFSIZE + sizeof("server: ")] = "server: "; - strcat(buf, line); - write(STDOUT_FILENO, buf, l_len + sizeof("server: ")); - if (q_args->state == WAIT_CLIENT) { - print_prompt(q_args->params); - } */ + char *end_char = strchr(q_args->server_message.text, '\04'); /* Search for the end of the notification */ + if (end_char != NULL) { + *end_char = '\0'; /* Don't show this symbol */ + q_args->state = S_PRINT_SERVER_MESSAGE; + q_args->server_message.size--; + } } return 0; diff --git a/src/ui/app.c b/src/ui/app.c index d4c7417..1441f16 100644 --- a/src/ui/app.c +++ b/src/ui/app.c @@ -155,7 +155,10 @@ void app_draw_modal(app_t *app) { init_server_message_modal(app); break; case S_FILE_DOWNLOAD: - init_dwn_pr_modal(app); + init_dwn_pr_modal(app, false); + break; + case S_UPLOAD_FILE: + init_dwn_pr_modal(app, true); break; case S_UPLOAD_FILE_SELECT: init_upload_dialogue_modal(app); @@ -176,7 +179,6 @@ void app_draw_modal(app_t *app) { case S_WAIT_SERVER: case S_N_D: case S_NEXT_ACTION: - case S_UPLOAD_FILE: if (app->query_args->notification != NULL) { init_notification_modal(app); } else { diff --git a/src/ui/modals/download_pr.c b/src/ui/modals/download_pr.c index e32de93..bfc3606 100644 --- a/src/ui/modals/download_pr.c +++ b/src/ui/modals/download_pr.c @@ -5,22 +5,24 @@ #include #include -void dwn_pr_modal_cb(callback_args_t *args) { -} +void dwn_pr_modal_cb(callback_args_t *args) {} -dialogue_t *init_dwn_pr_modal(app_t *app) { +dialogue_t *init_dwn_pr_modal(app_t *app, bool is_upload) { if (app == NULL) return NULL; - group_el_init_t content[] = {{.type = w_progress, .label = "", .length = 30}, /* length is ignored */ - {.type = w_end}}; + group_el_init_t content[] = { + {.type = w_progress, .label = "", .length = 30}, /* length is ignored */ + {.type = w_end}}; char text[256]; - sprintf(text, "Downloading file: %s", app->file_args->f_selected.name); + sprintf(text, "%s file: %s", is_upload ? "Uploading" : "Downloading", + is_upload ? app->query_args->file->name : app->file_args->f_selected.name); group_el_init_t actions[] = { - {.type = w_button, .label = "Cancel", .is_default = 0}, {.type = w_end}}; + {.type = w_button, .label = "Cancel", .is_default = 1}, {.type = w_end}}; app->modal.w.parent_win = &app->win; - init_dialogue(&(app->modal), "Download", text, &(app->coordinates)); + init_dialogue(&(app->modal), is_upload ? "Upload" : "Download", text, + &(app->coordinates)); dialogue_t *d = &(app->modal); d->w.callback = dwn_pr_modal_cb; diff --git a/src/ui/modals/download_pr.h b/src/ui/modals/download_pr.h index adc5c5e..b5274e7 100644 --- a/src/ui/modals/download_pr.h +++ b/src/ui/modals/download_pr.h @@ -4,6 +4,6 @@ #include "../widget/dialogue.h" #include "../app.h" -dialogue_t *init_dwn_pr_modal(app_t *app); +dialogue_t *init_dwn_pr_modal(app_t *app, bool is_upload); #endif \ No newline at end of file diff --git a/src/ui/modals/server_message.c b/src/ui/modals/server_message.c index 53ec6b5..9b0e6f2 100644 --- a/src/ui/modals/server_message.c +++ b/src/ui/modals/server_message.c @@ -28,6 +28,7 @@ void server_message_modal_cb(callback_args_t *args) { destroy_dialogue(d, app); /* TODO: macros */ free(app->query_args->server_message.text); + app->query_args->server_message.text = NULL; app->query_args->server_message.capacity = 0; app->query_args->server_message.size = 0; app->query_args->state = S_NEXT_ACTION; diff --git a/src/ui/widget/dialogue.c b/src/ui/widget/dialogue.c index 2dc038a..08d4217 100644 --- a/src/ui/widget/dialogue.c +++ b/src/ui/widget/dialogue.c @@ -187,10 +187,12 @@ void dialogue_init_active_id(dialogue_t *dialogue) { dialogue->active.type = g_content; widget_t *w = (widget_t *)dialogue->g_content->elements[i].element; dialogue->active.id = w->id; - break; + return; + } } - } else if (dialogue->g_action != NULL) { + } + if (dialogue->g_action != NULL) { for (int i = 0; i < dialogue->g_action->count; i++) { widget_type = dialogue->g_action->elements[i].type; if (widget_type == w_button || widget_type == w_input || @@ -198,12 +200,41 @@ void dialogue_init_active_id(dialogue_t *dialogue) { dialogue->active.type = g_action; widget_t *w = (widget_t *)dialogue->g_action->elements[i].element; dialogue->active.id = w->id; - break; + return; + } } } } +// void dialogue_init_active_id(dialogue_t *dialogue) { +// enum w_type widget_type; + +// if (dialogue->g_content != NULL) { +// for (int i = 0; i < dialogue->g_content->count; i++) { +// widget_type = dialogue->g_content->elements[i].type; +// if (widget_type == w_button || widget_type == w_input || +// widget_type == w_fs_file_list) { /* Add here new types */ +// dialogue->active.type = g_content; +// widget_t *w = (widget_t *)dialogue->g_content->elements[i].element; +// dialogue->active.id = w->id; +// break; +// } +// } +// } else if (dialogue->g_action != NULL) { +// for (int i = 0; i < dialogue->g_action->count; i++) { +// widget_type = dialogue->g_action->elements[i].type; +// if (widget_type == w_button || widget_type == w_input || +// widget_type == w_fs_file_list) { +// dialogue->active.type = g_action; +// widget_t *w = (widget_t *)dialogue->g_action->elements[i].element; +// dialogue->active.id = w->id; +// break; +// } +// } +// } +// } + #define DETECT_GROUP_SIZE(group, line_max_len, y, x) \ if (group) { \ group->w.m_y = y; \