changes upload method
This commit is contained in:
+15
-38
@@ -214,36 +214,6 @@ void file_download(file_args_t *f_args, query_args_t *q_args) {
|
||||
}
|
||||
}
|
||||
}
|
||||
/* obsolete */
|
||||
int32_t file_upload_request(char *query, query_args_t *q_args) {
|
||||
char qbuf[INBUFSIZE];
|
||||
char fpath[512];
|
||||
char a_perm;
|
||||
sscanf(query, "file upload \"%s %c", fpath, &a_perm);
|
||||
fpath[strlen(fpath) - 1] = 0; // remove \"
|
||||
int fd = open(fpath, O_RDONLY);
|
||||
if (fd == -1) {
|
||||
perror(fpath);
|
||||
return -1;
|
||||
}
|
||||
size_t fsize = lseek(fd, 0, SEEK_END);
|
||||
lseek(fd, 0, SEEK_SET);
|
||||
char *fname = strrchr(fpath, '/') + 1;
|
||||
int qlen = snprintf(qbuf, INBUFSIZE, "file upload \"%s\" %zu %c", fname,
|
||||
fsize, a_perm);
|
||||
if (qlen > INBUFSIZE) {
|
||||
fprintf(stderr, "query is too large to be sent\n");
|
||||
return -1;
|
||||
}
|
||||
q_args->file = malloc(sizeof(p_file_t));
|
||||
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;
|
||||
write(q_args->sd, qbuf, qlen);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t file_upload_open(char *dpath, char *fname, query_args_t *q_args) {
|
||||
q_args->file = malloc(sizeof(p_file_t));
|
||||
@@ -314,7 +284,9 @@ int32_t file_upload(query_args_t *q_args) {
|
||||
void clear_file_in_query(query_args_t *q_args) {
|
||||
free(q_args->file->name);
|
||||
free(q_args->file->path);
|
||||
free(q_args->file->description);
|
||||
if (q_args->file->description != NULL) {
|
||||
free(q_args->file->description);
|
||||
}
|
||||
free(q_args->file);
|
||||
q_args->file = NULL;
|
||||
}
|
||||
@@ -332,6 +304,7 @@ void init_file_args(file_args_t *f_args) {
|
||||
static void fl_add(fl_item_t **cur, fl_item_t **start, char *line) {
|
||||
char fname[128];
|
||||
char fowner[32];
|
||||
char *descr_begin = NULL;
|
||||
int h_len;
|
||||
fl_item_t *fitem = malloc(sizeof(fl_item_t));
|
||||
sscanf(line, "%s %zu %s%n", fname, &(fitem->size), fowner, &h_len);
|
||||
@@ -342,14 +315,18 @@ static void fl_add(fl_item_t **cur, fl_item_t **start, char *line) {
|
||||
fitem->owner = malloc((sizeof(char)) * (strlen(fowner) + 1));
|
||||
strcpy(fitem->owner, fowner);
|
||||
/* file description */
|
||||
int d_len = strlen(line) - h_len;
|
||||
fitem->description = malloc((sizeof(char)) * (d_len));
|
||||
strcpy(fitem->description, line + h_len + 1); // +1 to escape the dividing \32
|
||||
int i;
|
||||
for (i = 0; i < d_len; i++) {
|
||||
if (fitem->description[i] == '\a') {
|
||||
fitem->description[i] = '\n';
|
||||
descr_begin = line + h_len + 1;
|
||||
if (strcmp(descr_begin, "\n")) {
|
||||
int d_len = strlen(line) - h_len;
|
||||
fitem->description = malloc((sizeof(char)) * (d_len));
|
||||
strcpy(fitem->description, descr_begin); // +1 to escape the dividing \32
|
||||
for (int i = 0; i < d_len; i++) {
|
||||
if (fitem->description[i] == '\a') {
|
||||
fitem->description[i] = '\n';
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fitem->description = NULL;
|
||||
}
|
||||
/* fitem->description[strlen(line + line_pos)] = 0; */
|
||||
fitem->next = NULL;
|
||||
|
||||
@@ -43,6 +43,7 @@ enum state {
|
||||
S_UPLOAD_FILE,
|
||||
S_UPLOAD_FILE_SELECT,
|
||||
S_UPLOAD_REQUESTED,
|
||||
S_UPLOAD_SERVER_FINISHES,
|
||||
S_ASK_USER_BEFORE_LOGIN,
|
||||
S_ERR,
|
||||
S_NEXT_ACTION,
|
||||
|
||||
+11
-8
@@ -1,13 +1,13 @@
|
||||
#include <widget.h>
|
||||
#include "alert.h"
|
||||
#include <stdarg.h>
|
||||
#include <ncursesw/ncurses.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <widget.h>
|
||||
|
||||
/* 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) {
|
||||
//
|
||||
@@ -15,10 +15,10 @@ void alert_cb(callback_args_t *args) {
|
||||
|
||||
void init_alert(app_t *_app) { app = _app; }
|
||||
|
||||
void alert(const char *message) { notification("Alert", message, dc_alert); }
|
||||
void alert(const char *message) { notification("Alert", dc_alert, message); }
|
||||
|
||||
void notification(const char *title, const char *message,
|
||||
enum d_color_scheme color) {
|
||||
void notification(const char *title, enum d_color_scheme color,
|
||||
const char *f_message, ...) {
|
||||
if (app == NULL)
|
||||
return;
|
||||
|
||||
@@ -26,12 +26,15 @@ void notification(const char *title, const char *message,
|
||||
{.type = w_button, .label = "OK", .is_default = true}, {.type = w_end}};
|
||||
|
||||
dialogue_t *d = malloc(sizeof(dialogue_t));
|
||||
va_list args;
|
||||
va_start(args, f_message);
|
||||
|
||||
init_dialogue(d, title, message, &(app->coordinates));
|
||||
vinit_dialogue(d, title, &(app->coordinates), f_message, &args);
|
||||
|
||||
d->w.callback = alert_cb;
|
||||
d->g_content = NULL;
|
||||
d->g_action = init_group(&(d->win), &(d->w), actions,&(d->id_map), horizontal, g_action);
|
||||
d->g_action = init_group(&(d->win), &(d->w), actions, &(d->id_map),
|
||||
horizontal, g_action);
|
||||
|
||||
dialogue_init_active_id(d);
|
||||
d->color_scheme = color;
|
||||
|
||||
+4
-3
@@ -1,8 +1,9 @@
|
||||
#ifndef ALERT_H
|
||||
#define ALERT_H
|
||||
#include "app.h"
|
||||
#include <stdarg.h>
|
||||
void init_alert(app_t *app);
|
||||
void alert(char *message);
|
||||
void notification(const char *title, const char *message,
|
||||
enum d_color_scheme color);
|
||||
void alert(const char *message);
|
||||
void notification(const char *title, enum d_color_scheme color,
|
||||
const char *f_message, ...);
|
||||
#endif
|
||||
+38
-15
@@ -1,6 +1,7 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
/* Copyright (c) 2026 Oleksandr Zhylin */
|
||||
|
||||
#include "query.h"
|
||||
#include <arpa/inet.h>
|
||||
#include <fcntl.h>
|
||||
#include <ncursesw/ncurses.h>
|
||||
@@ -19,17 +20,19 @@
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "connection.h"
|
||||
#include "draw_modal.h"
|
||||
#include "file_processor.h"
|
||||
#include "main.h"
|
||||
#include "server.h"
|
||||
#include "types.h"
|
||||
#include <modals.h>
|
||||
#include <widget.h>
|
||||
#include <widget_core.h>
|
||||
|
||||
static void wait_side(app_t *app);
|
||||
#include "alert.h"
|
||||
#include "connection.h"
|
||||
#include "dialogue.h"
|
||||
#include "file_processor.h"
|
||||
#include "main.h"
|
||||
#include "server.h"
|
||||
#include "types.h"
|
||||
|
||||
static int wait_side(app_t *app, wait_server_cb *cb);
|
||||
void user_request_description(query_args_t *q_args);
|
||||
int process_query(app_t *app);
|
||||
int32_t process_user_input(app_t *app, callback_args_t *d_args);
|
||||
@@ -131,6 +134,18 @@ void wait_register(query_args_t *q_args) {
|
||||
write(q_args->sd, r_buf, strlen(r_buf) - 1);
|
||||
}
|
||||
|
||||
int upload_confirm_cb(app_t *app, char *query) {
|
||||
if (!strncmp("finished\n", query, sizeof("finished\n") - 1)) {
|
||||
notification("File upload", dc_normal, "File %s is uploaded to the server!",
|
||||
app->query_args->file->name);
|
||||
clear_file_in_query(app->query_args);
|
||||
app->query_args->state = WAIT_CLIENT;
|
||||
app->modal.needs_destroy = true;
|
||||
return 0;
|
||||
}
|
||||
return 1; /* TODO: Error case */
|
||||
}
|
||||
|
||||
int process_query(app_t *app) {
|
||||
query_args_t *query_args = app->query_args;
|
||||
file_args_t *file_args = app->file_args;
|
||||
@@ -144,7 +159,7 @@ int process_query(app_t *app) {
|
||||
case S_WAIT_SERVER:
|
||||
case WAIT_REGISTER_CONFIRMATION:
|
||||
case S_WAIT_REGISTER_CONFIRMATION:
|
||||
wait_side(app);
|
||||
wait_side(app, NULL);
|
||||
break;
|
||||
case S_ERR:
|
||||
break;
|
||||
@@ -153,7 +168,7 @@ int process_query(app_t *app) {
|
||||
break;
|
||||
case S_FILE_SELECT:
|
||||
if (query_args->from_server) {
|
||||
wait_side(app);
|
||||
wait_side(app, NULL);
|
||||
} else {
|
||||
file_select(file_args, query_args);
|
||||
}
|
||||
@@ -171,14 +186,16 @@ int process_query(app_t *app) {
|
||||
case S_UPLOAD_PARAMS:
|
||||
case S_UPLOAD_FILE:
|
||||
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;
|
||||
/* wait server for finish upload */
|
||||
query_args->state = S_UPLOAD_SERVER_FINISHES;
|
||||
} else if (res == -1) {
|
||||
/* TODO: Error */
|
||||
}
|
||||
break;
|
||||
case S_UPLOAD_SERVER_FINISHES:
|
||||
wait_side(app, upload_confirm_cb);
|
||||
/* user_request_description(query_args); */
|
||||
break;
|
||||
case S_ASK_USER_BEFORE_LOGIN:
|
||||
break;
|
||||
case WAIT_REGISTER:
|
||||
@@ -228,17 +245,23 @@ int query_extract_from_buf(char *buf, int *buf_used, char **output_line) {
|
||||
return pos + 1;
|
||||
}
|
||||
|
||||
static void wait_side(app_t *app) {
|
||||
static int wait_side(app_t *app, wait_server_cb *callback) {
|
||||
query_args_t *q_args = app->query_args;
|
||||
int qlen;
|
||||
int buf_used = q_args->buf_used;
|
||||
char *query = NULL;
|
||||
int res = 0;
|
||||
|
||||
while ((qlen = query_extract_from_buf(q_args->buf, &buf_used, &query))) {
|
||||
process_server_command(query, qlen, app);
|
||||
if (callback == NULL) {
|
||||
process_server_command(query, qlen, app);
|
||||
} else {
|
||||
res = callback(app, query);
|
||||
}
|
||||
free(query);
|
||||
query = NULL;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void init_query_args(query_args_t *q_args, params_t *params) {
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#ifndef QUERY_H
|
||||
#define QUERY_H
|
||||
|
||||
#include "app.h"
|
||||
#include "main.h"
|
||||
#include <widget.h>
|
||||
|
||||
@@ -12,5 +13,6 @@ void wait_register(query_args_t *q_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);
|
||||
typedef int wait_server_cb(app_t *app, char *query);
|
||||
|
||||
#endif
|
||||
+3
-1
@@ -29,6 +29,7 @@ void ask_register(params_t *params, char *email);
|
||||
return 0; \
|
||||
}
|
||||
|
||||
/* TODO: refactor to process callbacks */
|
||||
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;
|
||||
@@ -87,7 +88,7 @@ int process_server_command(char *line, int l_len, app_t *app) {
|
||||
"You've been successfully registered.\n"
|
||||
"Welcome, %s!",
|
||||
app->params->uname);
|
||||
notification("Registration", query, dc_normal);
|
||||
notification("Registration", dc_normal, query);
|
||||
print_bars(app);
|
||||
return 0;
|
||||
} else {
|
||||
@@ -111,6 +112,7 @@ int process_server_command(char *line, int l_len, app_t *app) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* TODO: Hide details */
|
||||
if (q_args->state == S_WAIT_SERVER) {
|
||||
if (q_args->server_message.text == NULL) {
|
||||
q_args->server_message.text = malloc(l_len + 1);
|
||||
|
||||
+2
-2
@@ -36,8 +36,8 @@ typedef struct {
|
||||
void *active_widget;
|
||||
} app_t;
|
||||
|
||||
app_t *init_app();
|
||||
void init_nc();
|
||||
app_t *init_app(void);
|
||||
void init_nc(void);
|
||||
void destroy_app(app_t *app, int32_t exit_code);
|
||||
void draw_borders(app_t *app);
|
||||
void print_bars(app_t *app);
|
||||
|
||||
+12
-8
@@ -1,20 +1,16 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
/* Copyright (c) 2026 Oleksandr Zhylin */
|
||||
|
||||
#include <ncursesw/ncurses.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "app.h"
|
||||
#include "button.h"
|
||||
#include "checkbox.h"
|
||||
#include "dialogue.h"
|
||||
#include "file_list.h"
|
||||
#include "group.h"
|
||||
#include "input.h"
|
||||
#include <widget.h>
|
||||
#include <widget_core.h>
|
||||
|
||||
#include "file_list.h"
|
||||
|
||||
void incr_active_id(dialogue_t *d) {
|
||||
group_el_t *active_el = NULL;
|
||||
while (true) {
|
||||
@@ -170,6 +166,14 @@ void init_dialogue(dialogue_t *dialogue, const char title[], const char text[],
|
||||
dialogue->text[t_size - 1] = '\0';
|
||||
}
|
||||
|
||||
/* variadic */
|
||||
void vinit_dialogue(dialogue_t *dialogue, const char title[],
|
||||
coordinates_t *p_coordinates, const char fmt[], va_list *v_args) {
|
||||
char f_text[DIALOGUE_TEXT];
|
||||
vsprintf(f_text, fmt, *v_args);
|
||||
init_dialogue(dialogue, title, f_text, p_coordinates);
|
||||
}
|
||||
|
||||
int group_init_active_id(group_t *g, dialogue_t *d) {
|
||||
enum w_type wt;
|
||||
|
||||
|
||||
@@ -8,8 +8,8 @@
|
||||
#include "group.h"
|
||||
#include <ncursesw/ncurses.h>
|
||||
#include <stdint.h>
|
||||
#include <widget_core.h>
|
||||
#include <utils.h>
|
||||
#include <widget_core.h>
|
||||
#define MAX_IDS 64
|
||||
|
||||
enum d_color_scheme { dc_normal, dc_alert };
|
||||
@@ -31,6 +31,9 @@ typedef struct {
|
||||
|
||||
void init_dialogue(dialogue_t *d, const char title[], const char text[],
|
||||
coordinates_t *p_coordinates);
|
||||
void vinit_dialogue(dialogue_t *dialogue, const char title[],
|
||||
coordinates_t *p_coordinates, const char fmt[],
|
||||
va_list *v_args);
|
||||
int32_t draw_dialogue(dialogue_t *d);
|
||||
void destroy_dialogue(dialogue_t *d, void *app);
|
||||
void dialogue_default_callback(callback_args_t *args);
|
||||
|
||||
@@ -202,7 +202,7 @@ void draw_file_list(ui_file_list_t *fui) {
|
||||
|
||||
char p_info[64];
|
||||
|
||||
/* Draw file info */
|
||||
/* Draw file info [right side] */
|
||||
{
|
||||
uint32_t p_y = 1;
|
||||
p_x = 1;
|
||||
@@ -216,8 +216,10 @@ void draw_file_list(ui_file_list_t *fui) {
|
||||
|
||||
mvwprintw(i_win, p_y++, p_x, "Size: %s", size_text);
|
||||
mvwprintw(i_win, p_y++, p_x, "Owner: %s", active_el->owner);
|
||||
mvwprintw(i_win, p_y++, p_x, "Description: ");
|
||||
print_multiline_text(i_win, active_el->description, sz_x, p_y, p_x, 0);
|
||||
if (active_el->description != NULL) {
|
||||
mvwprintw(i_win, p_y++, p_x, "Description: ");
|
||||
print_multiline_text(i_win, active_el->description, sz_x, p_y, p_x, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user