ui: progress bar for file download

This commit is contained in:
2025-12-30 19:19:54 +02:00
parent 071bc5951a
commit f1757b4ca7
16 changed files with 218 additions and 32 deletions
+5 -3
View File
@@ -1,5 +1,6 @@
client.o: src/client.c src/file_processor.h src/main.h src/types.h \
src/client.h
src/ui/widget/file_list.h src/ui/widget/../widget.h \
src/ui/widget/../../main.h src/client.h
connection.o: src/connection.c src/main.h src/types.h src/ui/app.h \
src/ui/../common.h src/ui/widget.h src/ui/../main.h \
src/ui/widget/dialogue.h src/ui/widget/../widget.h \
@@ -10,7 +11,8 @@ file_processor.o: src/file_processor.c src/client.h src/main.h \
src/ui/../main.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/ui/widget/file_list.h
src/ui/widget/dialogue.h src/ui/widget/file_list.h \
src/ui/widget/progress_bar.h
main.o: src/main.c src/connection.h src/main.h src/types.h src/ui/app.h \
src/ui/../common.h src/ui/widget.h src/ui/../main.h \
src/ui/widget/dialogue.h src/ui/widget/../widget.h \
@@ -22,6 +24,6 @@ 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/server.h
src/ui/widget/file_list.h src/server.h
server.o: src/server.c src/main.h src/types.h src/ui.h
ui.o: src/ui.c
+53 -7
View File
@@ -1,7 +1,9 @@
#include "client.h"
#include "main.h"
#include "query.h"
#include "ui/widget/dialogue.h"
#include "ui/widget/file_list.h"
#include "ui/widget/progress_bar.h"
#include <arpa/inet.h>
#include <fcntl.h>
#include <netinet/in.h>
@@ -19,7 +21,7 @@
#include <unistd.h>
static void fl_add(fl_item_t **cur, fl_item_t **start, char *fname);
static fl_item_t *fl_select(fl_item_t *start, int num);
fl_item_t *fl_select(fl_item_t *start, int num);
void fl_clear(fl_item_t **start, fl_item_t **current);
static void clear_file_in_query(query_args_t *q_args);
@@ -30,7 +32,7 @@ void file_list(file_args_t *f_args, query_args_t *q_args) {
static uint32_t idx = 1;
static char qbuf[INBUFSIZE * 2];
static uint32_t qbuf_used = 0;
file_list_t *fui = (file_list_t *)q_args->file_list_ui;
ui_file_list_t *fui = (ui_file_list_t *)q_args->file_list_ui;
fui->start = &(f_args->l_start);
fui->current = &(f_args->l_current);
@@ -73,6 +75,7 @@ void file_list(file_args_t *f_args, query_args_t *q_args) {
}
}
/* file_select: old function, can be removed in the future */
void file_select(file_args_t *f_args, query_args_t *q_args) {
char *buf = q_args->buf;
fl_item_t *l_start = f_args->l_start;
@@ -131,16 +134,61 @@ void file_select(file_args_t *f_args, query_args_t *q_args) {
free(file_path);
}
int32_t ui_file_select(file_args_t *f_args, query_args_t *q_args, int32_t idx) {
fl_item_t *l_selected; /* from the list */
fl_item_t *f_selected =
&(f_args->f_selected); /* new copy of file struct (list be cleared) */
uint32_t qlen;
char send_buf[256];
struct stat st = {0};
l_selected = fl_select(f_args->l_start, idx);
if (l_selected == NULL) {
return -1;
}
memcpy(f_selected, l_selected, sizeof(fl_item_t));
if (stat(DOWNLOADS_DIR, &st) == -1) {
mkdir(DOWNLOADS_DIR, 0700);
}
char *file_path = malloc(sizeof(DOWNLOADS_DIR) + strlen(l_selected->name) +
2); // + '/' + '\0'
f_selected->name = NULL;
sprintf(file_path, "%s/%s", DOWNLOADS_DIR, l_selected->name);
f_selected->name = strdup(l_selected->name);
fl_clear(&f_args->l_start, &f_args->l_current);
f_args->file_d = open(file_path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (f_args->file_d == -1) {
qlen = sprintf(send_buf, "error: %s\n", f_selected->name);
write(q_args->sd, send_buf, qlen);
perror(f_selected->name);
print_prompt(q_args->params);
q_args->state = WAIT_CLIENT;
free(file_path);
return -2;
}
qlen = sprintf(send_buf, "file download %s\n", f_selected->name);
write(q_args->sd, send_buf, qlen);
q_args->state = S_FILE_DOWNLOAD;
free(file_path);
return OK;
}
void file_download(file_args_t *f_args, query_args_t *q_args) {
fl_item_t *f_selected = &(f_args->f_selected);
static uint32_t it_count = 0;
static size_t it_interval = 0;
if (it_interval == 0) {
it_interval = (f_selected->size / INBUFSIZE / 100) * 10; /* every 10% */
it_interval = (f_selected->size / INBUFSIZE / 100) * 1; /* every 1% */
if (it_interval == 0)
it_interval = 1;
}
static size_t size_rest = 0;
uint32_t progress = (f_selected->size - size_rest) * 100 / f_selected->size;
ui_progress_bar_t *pb = (ui_progress_bar_t *)q_args->progress_bar;
if (size_rest == 0)
size_rest = f_selected->size;
@@ -148,7 +196,7 @@ void file_download(file_args_t *f_args, query_args_t *q_args) {
if (qlen) {
it_count++;
if (!(it_count % it_interval)) {
printf("%zu / %zu\n", f_selected->size - size_rest, f_selected->size);
pb->procent = progress;
}
if (it_count % it_interval)
q_args->buf_used = 0;
@@ -161,10 +209,8 @@ void file_download(file_args_t *f_args, query_args_t *q_args) {
it_count = 0;
it_interval = 0;
f_selected->size = 0;
printf("File %s is downloaded\n", f_selected->name);
close(f_args->file_d);
free(f_selected->name);
print_prompt(q_args->params);
q_args->state = WAIT_CLIENT;
}
}
@@ -287,7 +333,7 @@ static void fl_add(fl_item_t **cur, fl_item_t **start, char *line) {
*cur = fitem;
}
static fl_item_t *fl_select(fl_item_t *start, int num) {
fl_item_t *fl_select(fl_item_t *start, int num) {
fl_item_t *current = start;
for (; num != 1; num--) {
if (current->next != NULL) {
+2
View File
@@ -1,6 +1,7 @@
#ifndef FILE_PROCESSOR_H
#define FILE_PROCESSOR_H
#include "main.h"
#include "ui/widget/file_list.h"
void file_list(file_args_t *f_args, query_args_t *q_args);
void file_download(file_args_t *f_args, query_args_t *q_args);
@@ -10,5 +11,6 @@ int file_upload_request(char *, query_args_t *q_args);
int file_upload_start(query_args_t *q_args);
int file_upload(query_args_t *q_args);
void fl_clear(fl_item_t **start, fl_item_t **arg_current);
int32_t ui_file_select(file_args_t *f_args, query_args_t *q_args, int32_t idx);
#endif
+1
View File
@@ -85,6 +85,7 @@ typedef struct query_args {
uint64_t size;
} server_message;
void *file_list_ui; /* TODO: find solution to insert actual type without recursion */
void *progress_bar;
} query_args_t;
#endif
+2
View File
@@ -40,6 +40,7 @@ void query_loop(app_t *app) {
init_file_args(&file_args);
query_args->sd = sd;
app->file_args = &file_args;
for (;;) {
/* update screen */
@@ -260,6 +261,7 @@ void init_query_args(query_args_t *q_args, params_t *params) {
q_args->server_message.capacity = 0;
q_args->server_message.size = 0;
q_args->next_server_command = NULL;
q_args->progress_bar = NULL;
}
void user_request_description(query_args_t *q_args) {
+5 -2
View File
@@ -1,9 +1,10 @@
#include "app.h"
#include "modals/ask_server_addr.h"
#include "modals/download_pr.h"
#include "modals/login_credentials.h"
#include "modals/login_option.h"
#include "modals/server_message.h"
#include "widget/file_list.h"
#include "widget/progress_bar.h"
// #include "modals/server_message.h"
// #include "action.h"
#include <ncurses.h>
@@ -151,6 +152,9 @@ void app_draw_modal(app_t *app) {
case S_PRINT_SERVER_MESSAGE:
init_server_message_modal(app);
break;
case S_FILE_DOWNLOAD:
init_dwn_pr_modal(app);
break;
case WAIT_SERVER_INIT:
case WAIT_SERVER:
case WAIT_REGISTER:
@@ -159,7 +163,6 @@ void app_draw_modal(app_t *app) {
case UPLOAD_FILE:
case S_FILE_LIST:
case S_FILE_SELECT:
case S_FILE_DOWNLOAD:
case S_UPLOAD_PARAMS:
case S_UPLOAD_FILE:
case S_UPLOAD_REQUESTED:
+35
View File
@@ -0,0 +1,35 @@
#include "../app.h"
#include "../widget/dialogue.h"
#include "../widget/group.h"
#include <ncurses.h>
#include <stdint.h>
#include <stdio.h>
void init_dwn_pr_modal_cb(callback_args_t *args) {
}
dialogue_t *init_dwn_pr_modal(app_t *app) {
if (app == NULL)
return NULL;
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);
group_el_init_t actions[] = {
{.type = w_button, .label = "Cancel", .is_default = 0}, {.type = w_end}};
app->modal.w.parent_win = &app->win;
init_dialogue(&(app->modal), "Download", text, &(app->coordinates));
dialogue_t *d = &(app->modal);
d->w.callback = init_dwn_pr_modal_cb;
d->g_content = init_group(&(d->win), &(d->w), content, horizontal);
d->g_action = init_group(&(d->win), &(d->w), actions, horizontal);
app->query_args->progress_bar = d->g_content->elements[0].element;
dialogue_init_active_id(d);
return NULL;
}
+9
View File
@@ -0,0 +1,9 @@
#ifndef DOWNLOAD_PR_H
#define DOWNLOAD_PR_H
#include "../widget/dialogue.h"
#include "../app.h"
dialogue_t *init_dwn_pr_modal(app_t *app);
#endif
+1
View File
@@ -11,6 +11,7 @@ enum w_type {
w_box,
w_group,
w_input,
w_progress,
};
typedef struct {
+18 -12
View File
@@ -1,6 +1,7 @@
#include "file_list.h"
#include "../../file_processor.h"
#include "../app.h"
#include "progress_bar.h"
#include <ncurses.h>
#include <stdbool.h>
#include <stdint.h>
@@ -9,14 +10,16 @@
#include <sys/types.h>
#include <unistd.h>
void reset_file_list(file_list_t *fl_ui);
void reset_file_list(ui_file_list_t *fl_ui);
void file_list_cb(callback_args_t *args) {
app_t *app = args->app;
file_list_t *fui = app->query_args->file_list_ui;
ui_file_list_t *fui = app->query_args->file_list_ui;
int32_t key = *((int32_t *)args->data);
char switch_page_query[10];
int32_t spq_len = 0;
char query[256];
int32_t q_len = 0;
fl_item_t *f_item;
ui_progress_bar_t *pb;
switch (key) {
case KEY_DOWN:
if (fui->current_idx < fui->current_count - 1) {
@@ -25,8 +28,8 @@ void file_list_cb(callback_args_t *args) {
} else if (fui->current_page < fui->pages) {
fl_clear(fui->start, fui->current);
app->query_args->state = S_FILE_LIST;
sprintf(switch_page_query, "p%u\n%n", fui->current_page + 1, &spq_len);
write(app->query_args->sd, switch_page_query, spq_len);
sprintf(query, "p%u\n%n", fui->current_page + 1, &q_len);
write(app->query_args->sd, query, q_len);
reset_file_list(fui);
}
break;
@@ -37,17 +40,20 @@ void file_list_cb(callback_args_t *args) {
} else if (fui->current_page > 1) {
fl_clear(fui->start, fui->current);
app->query_args->state = S_FILE_LIST;
sprintf(switch_page_query, "p%u\n%n", fui->current_page - 1, &spq_len);
write(app->query_args->sd, switch_page_query, spq_len);
sprintf(query, "p%u\n%n", fui->current_page - 1, &q_len);
write(app->query_args->sd, query, q_len);
reset_file_list(fui);
fui->activate_last = true;
}
break;
case '\n':
ui_file_select(app->file_args, app->query_args, fui->current_idx+1);
break;
}
}
file_list_t *init_file_list(WINDOW **win) {
file_list_t *fl_ui = malloc(sizeof(file_list_t));
ui_file_list_t *init_file_list(WINDOW **win) {
ui_file_list_t *fl_ui = malloc(sizeof(ui_file_list_t));
init_widget(&(fl_ui->w), NULL, win, "");
fl_ui->current_idx = 0;
fl_ui->current_page = 0;
@@ -58,7 +64,7 @@ file_list_t *init_file_list(WINDOW **win) {
return fl_ui;
}
void reset_file_list(file_list_t *fl_ui) {
void reset_file_list(ui_file_list_t *fl_ui) {
fl_ui->current_idx = 0;
fl_ui->current_page = 0;
fl_ui->pages = 0;
@@ -67,7 +73,7 @@ void reset_file_list(file_list_t *fl_ui) {
fl_ui->activate_last = false;
}
void draw_file_list(file_list_t *fl_ui) {
void draw_file_list(ui_file_list_t *fl_ui) {
int32_t sz_y, sz_x;
int32_t p_y, p_x;
getmaxyx(*(fl_ui->w.parent_win), sz_y, sz_x);
+4 -4
View File
@@ -12,10 +12,10 @@ typedef struct {
uint32_t pages;
uint32_t current_page;
uint32_t activate_last : 1;
} file_list_t;
} ui_file_list_t;
void draw_file_list(file_list_t *fl_ui);
void redraw_file_list(file_list_t *fl_ui);
file_list_t *init_file_list(WINDOW **win);
void draw_file_list(ui_file_list_t *fl_ui);
void redraw_file_list(ui_file_list_t *fl_ui);
ui_file_list_t *init_file_list(WINDOW **win);
void file_list_cb(callback_args_t *args);
#endif
+11
View File
@@ -1,6 +1,7 @@
#include "group.h"
#include "button.h"
#include "input.h"
#include "progress_bar.h"
#include <ncurses.h>
#include <stdint.h>
#include <stdlib.h>
@@ -87,6 +88,10 @@ group_t *init_group(WINDOW **win, widget_t *w_parent, group_el_init_t *children,
init_input(win, &(group->w), children[i].label, children[i].length);
w = &(((input_t *)elements[i].element)->w);
break;
case w_progress:
elements[i].element = init_ui_progress_bar(win, &(group->w));
w = &(((ui_progress_bar_t *)elements[i].element)->w);
break;
}
// set dimensions
elements[i].id = w->id;
@@ -125,6 +130,9 @@ void draw_group(WINDOW *win, group_t *group, int32_t active_id,
case w_input:
draw_input((input_t *)el->element, active_id);
break;
case w_progress:
draw_ui_progress_bar((ui_progress_bar_t *)el->element);
break;
}
}
}
@@ -144,6 +152,9 @@ void destroy_group(group_t *group) {
case w_input:
destroy_input(el->element);
break;
case w_progress:
destroy_ui_progress_bar(el->element);
break;
}
}
if (group->elements != NULL) {
-2
View File
@@ -45,8 +45,6 @@ int32_t draw_input(input_t *input, uint32_t active_id) {
mvwvline(win, margin_y + 1, margin_x + input->w.x - 1, 0, 1);
mvwvline(win, margin_y + 2, margin_x + input->w.x - 1, ACS_LRCORNER, 1);
// TODO: correct sizes (x)
margin_x++;
if (input->w.id == active_id) {
+58
View File
@@ -0,0 +1,58 @@
#include "progress_bar.h"
#include "../app.h"
#include <ncurses.h>
#include <stdint.h>
#include <stdlib.h>
ui_progress_bar_t *init_ui_progress_bar(WINDOW **win, widget_t *w_parent) {
WINDOW *win_par = *(w_parent->w_parent->parent_win);
ui_progress_bar_t *pb = malloc(sizeof(ui_progress_bar_t));
init_widget(&pb->w, w_parent, win, "");
pb->procent = 0;
pb->text[0] = '\0';
pb->w.y = 4;
pb->w.x = getmaxx(win_par) / 10 * 9;
return pb;
}
void draw_ui_progress_bar(ui_progress_bar_t *pb) {
WINDOW *win = *(pb->w.w_parent->parent_win);
uint32_t margin_y = pb->w.m_y + pb->w.w_parent->m_y;
uint32_t margin_x = pb->w.m_x + pb->w.w_parent->m_x + 1;
wattrset(win, COLOR_PAIR(modal_color_pair));
mvwhline(win, margin_y, margin_x, 0, pb->w.x - 1);
float procent = pb->procent;
procent /= 100;
uint32_t filled_num = procent * pb->w.x;
if (filled_num > 2) {
filled_num -= 2;
}
for (int i = 1; i < 3; i++) {
wattrset(win, COLOR_PAIR(1) | A_REVERSE);
mvwprintw(win, margin_y + i, margin_x + 1, "%*s", filled_num, "");
}
wattrset(win, COLOR_PAIR(modal_color_pair));
// bottom line
mvwhline(win, margin_y + 3, margin_x, 0, pb->w.x - 1);
// left
mvwvline(win, margin_y, margin_x, ACS_ULCORNER, 1);
mvwvline(win, margin_y + 1, margin_x, 0, 1);
mvwvline(win, margin_y + 2, margin_x, 0, 1);
mvwvline(win, margin_y + 3, margin_x, ACS_LLCORNER, 1);
// right
mvwvline(win, margin_y, margin_x + pb->w.x - 2, ACS_URCORNER, 1);
mvwvline(win, margin_y + 1, margin_x + pb->w.x - 2, 0, 1);
mvwvline(win, margin_y + 2, margin_x + pb->w.x - 2, 0, 1);
mvwvline(win, margin_y + 3, margin_x + pb->w.x - 2, ACS_LRCORNER, 1);
}
void destroy_ui_progress_bar(void *_pb) {
ui_progress_bar_t *pb = (ui_progress_bar_t *)_pb;
free(pb);
}
+12
View File
@@ -0,0 +1,12 @@
#ifndef PROGRESS_BAR_H
#define PROGRESS_BAR_H
#include "../widget.h"
typedef struct {
widget_t w;
uint32_t procent;
char text[24];
} ui_progress_bar_t;
void destroy_ui_progress_bar(void *_pb);
void draw_ui_progress_bar(ui_progress_bar_t *pb);
ui_progress_bar_t *init_ui_progress_bar(WINDOW **win, widget_t *w_parent);
#endif