ui: finished (kinda) file upload

This commit is contained in:
2026-01-22 16:26:36 +02:00
parent 346ded71bc
commit 50952f324d
10 changed files with 88 additions and 30 deletions
+1 -1
View File
@@ -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
+20 -3
View File
@@ -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;
+8 -2
View File
@@ -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:
+8 -9
View File
@@ -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;
+4 -2
View File
@@ -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 {
+10 -8
View File
@@ -5,22 +5,24 @@
#include <stdint.h>
#include <stdio.h>
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;
+1 -1
View File
@@ -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
+1
View File
@@ -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;
+34 -3
View File
@@ -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; \