ui: next step of file upload (not finished)

This commit is contained in:
2026-01-20 19:30:01 +02:00
parent 83157a55f3
commit 975f4b9dcd
8 changed files with 117 additions and 17 deletions
+24 -3
View File
@@ -219,11 +219,12 @@ void file_download(file_args_t *f_args, query_args_t *q_args) {
f_selected->size = 0;
close(f_args->file_d);
d->needs_destroy = true;
sprintf(answer, "File %s is downloaded from the server!", f_selected->name);
q_args->notification = malloc(strlen(answer)+1);
sprintf(answer, "File %s is downloaded from the server!",
f_selected->name);
q_args->notification = malloc(strlen(answer) + 1);
strcpy(q_args->notification, answer);
free(f_selected->name);
write(q_args->sd, "file list\n", sizeof("file list\n")-1);
write(q_args->sd, "file list\n", sizeof("file list\n") - 1);
q_args->state = S_FILE_LIST;
}
}
@@ -259,6 +260,26 @@ int32_t file_upload_request(char *query, query_args_t *q_args) {
return 0;
}
int32_t file_upload_open(char *dpath, char *fname, query_args_t *q_args) {
char a_perm;
q_args->file = malloc(sizeof(p_file_t));
q_args->file->path = malloc(strlen(dpath) + strlen(fname) + 2);
sprintf(q_args->file->path, "%s/%s", dpath, fname);
int fd = open(q_args->file->path, O_RDONLY);
if (fd == -1) {
perror(q_args->file->path);
return -1;
}
size_t fsize = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
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;
return 0;
}
int32_t file_upload_start(query_args_t *q_args) {
char *query;
query_extract_from_buf(q_args->buf, &(q_args->buf_used), &query);
+1
View File
@@ -10,6 +10,7 @@ void init_file_args(file_args_t *fargs);
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);
int32_t file_upload_open(char *dpath, char *fname, 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
+4 -2
View File
@@ -6,6 +6,7 @@
#include "modals/notification.h"
#include "modals/server_message.h"
#include "modals/upload_dialogue.h"
#include "modals/upload_props_dialogue.h"
#include "widget/dialogue.h"
#include <ncurses.h>
#include <netinet/in.h>
@@ -138,7 +139,6 @@ void app_refresh(app_t *app) {
void app_draw_modal(app_t *app) {
if (app->modal.needs_destroy) {
destroy_dialogue(&(app->modal), app);
app_refresh(app);
}
if (!app->modal.is_initiated) {
switch (app->query_args->state) {
@@ -160,6 +160,9 @@ void app_draw_modal(app_t *app) {
case S_UPLOAD_FILE:
init_upload_dialogue_modal(app);
break;
case S_UPLOAD_PARAMS:
init_upload_props_dialogue_modal(app);
break;
case WAIT_SERVER_INIT:
case WAIT_SERVER:
case WAIT_REGISTER:
@@ -167,7 +170,6 @@ void app_draw_modal(app_t *app) {
case WAIT_CLIENT:
case S_FILE_LIST:
case S_FILE_SELECT:
case S_UPLOAD_PARAMS:
case S_UPLOAD_REQUESTED:
case S_ASK_USER_BEFORE_LOGIN:
case S_ERR:
+6 -8
View File
@@ -1,11 +1,10 @@
#include "../app.h"
#include "../widget/dialogue.h"
#include "../widget/group.h"
#include "../widget/fs_file_list.h"
#include "../widget/group.h"
#include "../../file_processor.h"
#include <ncurses.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void upload_dialogue_modal_cb(callback_args_t *args) {
@@ -18,17 +17,16 @@ void upload_dialogue_modal_cb(callback_args_t *args) {
d_args.resp_data = &response;
d_args.element = app->active_widget;
dialogue_default_callback(&d_args);
ui_fs_file_list_t *fui = (ui_fs_file_list_t *) d->g_content->elements[0].element;
ui_fs_file_list_t *fui =
(ui_fs_file_list_t *)d->g_content->elements[0].element;
if (response > -1) {
switch (response) {
case 0:
app->query_args->state = S_WAIT_SERVER;
destroy_dialogue(d, app);
break;
case 1:
app->query_args->file = malloc(sizeof(p_file_t));
app->query_args->file->path = malloc(strlen(fui->d_path) + strlen(fui->current->name) + 2);
strcpy(app->query_args->file->path, fui->current->name);
sprintf(app->query_args->file->path, "%s/%s", fui->d_path, fui->current->name);
file_upload_open(fui->d_path, fui->current->name, app->query_args);
destroy_dialogue(d, app);
app->query_args->state = S_UPLOAD_PARAMS;
break;
+70
View File
@@ -0,0 +1,70 @@
#include "../app.h"
#include "../widget/dialogue.h"
#include "../widget/fs_file_list.h"
#include "../widget/group.h"
#include <ncurses.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void upload_props_dialogue_modal_cb(callback_args_t *args) {
/* TODO: finish */
int32_t response;
callback_args_t d_args;
app_t *app = args->app;
dialogue_t *d = (dialogue_t *)app->active_widget;
memcpy(&d_args, args, sizeof(callback_args_t));
d_args.app = NULL;
d_args.resp_data = &response;
d_args.element = app->active_widget;
dialogue_default_callback(&d_args);
char query[256];
int32_t query_len = 0;
if (response > -1) {
switch (response) {
case 0:
if (app->query_args->file->name != NULL)
free(app->query_args->file->name);
if (app->query_args->file->path != NULL)
free(app->query_args->file->path);
free(app->query_args->file);
app->query_args->file = NULL;
app->query_args->state = S_WAIT_SERVER;
break;
case 1:
query_len = sprintf(query, "file upload \"%s\" %zu 1\n", app->query_args->file->name, app->query_args->file->size);
destroy_dialogue(d, app);
app->query_args->state = S_UPLOAD_REQUESTED;
write(app->query_args->sd, query, query_len+1);
break;
}
}
}
dialogue_t *init_upload_props_dialogue_modal(app_t *app) {
if (app == NULL)
return NULL;
group_el_init_t content[] = {
{.type = w_input, .label = "File description", .length = 30},
{.type = w_end}};
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), "Description for the file",
"Enter essential data", &(app->coordinates));
dialogue_t *d = &(app->modal);
d->w.callback = upload_props_dialogue_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->active_dialogue = d;
dialogue_init_active_id(d);
return NULL;
}
+9
View File
@@ -0,0 +1,9 @@
#ifndef UPLOAD_PROPS_DIALOGUE_H
#define UPLOAD_PROPS_DIALOGUE_H
#include "../widget/dialogue.h"
#include "../app.h"
dialogue_t *init_upload_props_dialogue_modal(app_t *app);
#endif
+2 -2
View File
@@ -49,11 +49,10 @@ widget_t *get_active_widget(dialogue_t *d) {
} else {
g = d->g_content;
}
widget_t *w = (widget_t *) g->elements[d->active.id - g->first_id].element;
widget_t *w = (widget_t *)g->elements[d->active.id - g->first_id].element;
return w;
}
void dialogue_default_callback(callback_args_t *args) {
dialogue_t *d = (void *)args->element;
int32_t key = *((int32_t *)args->data);
@@ -324,4 +323,5 @@ void destroy_dialogue(dialogue_t *d, void *_app) {
app->active_win = app->left_win;
app->active_callback = file_list_cb;
app->query_args->active_dialogue = NULL;
app_refresh(app);
}
-1
View File
@@ -1,6 +1,5 @@
#include "fs_file_list.h"
#include "progress_bar.h"
#include "../app.h"
#include <dirent.h>
#include <errno.h>
#include <ncurses.h>