ui: file upload basic process (need improvement)
This commit is contained in:
+4
-4
@@ -7,12 +7,12 @@ connection.o: src/connection.c src/main.h src/types.h src/ui/app.h \
|
||||
src/ui/widget/../../common.h src/ui/widget/button.h \
|
||||
src/ui/widget/../../main.h src/ui/widget/group.h
|
||||
file_processor.o: src/file_processor.c src/client.h src/main.h \
|
||||
src/types.h src/query.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 \
|
||||
src/types.h src/file_processor.h src/ui/widget/file_list.h \
|
||||
src/ui/widget/../widget.h src/ui/widget/../../main.h src/query.h \
|
||||
src/ui/app.h src/ui/../common.h src/ui/widget.h src/ui/widget/dialogue.h \
|
||||
src/ui/widget/../../common.h src/ui/widget/button.h \
|
||||
src/ui/widget/../../main.h src/ui/widget/group.h \
|
||||
src/ui/widget/dialogue.h src/ui/widget/file_list.h \
|
||||
src/ui/widget/progress_bar.h
|
||||
src/ui/widget/dialogue.h src/ui/widget/progress_bar.h
|
||||
fs.o: src/fs.c
|
||||
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 \
|
||||
|
||||
+7
-15
@@ -15,6 +15,7 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include "client.h"
|
||||
#include "file_processor.h"
|
||||
#include "main.h"
|
||||
#include "query.h"
|
||||
#include "ui/widget/dialogue.h"
|
||||
@@ -25,8 +26,6 @@ static void fl_add(fl_item_t **cur, fl_item_t **start, char *fname);
|
||||
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);
|
||||
|
||||
void file_list(file_args_t *f_args, query_args_t *q_args) {
|
||||
uint32_t qlen;
|
||||
char *query = NULL;
|
||||
@@ -229,7 +228,7 @@ 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];
|
||||
@@ -277,6 +276,7 @@ int32_t file_upload_open(char *dpath, char *fname, query_args_t *q_args) {
|
||||
strcpy(q_args->file->name, fname);
|
||||
q_args->file->size = fsize;
|
||||
q_args->file->rest = fsize;
|
||||
q_args->file->description = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -292,13 +292,6 @@ int32_t file_upload_start(query_args_t *q_args) {
|
||||
}
|
||||
|
||||
int32_t file_upload(query_args_t *q_args) {
|
||||
/* static size_t it_int = 0;
|
||||
static int it_count = 0;
|
||||
|
||||
if (it_int == 0) {
|
||||
it_int = (q_args->file->size / INBUFSIZE / 100) * 10; // every 10%
|
||||
} */
|
||||
|
||||
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) {
|
||||
@@ -310,17 +303,16 @@ int32_t file_upload(query_args_t *q_args) {
|
||||
q_args->file->rest -= wlen;
|
||||
}
|
||||
} else {
|
||||
/* file upload is finished */
|
||||
printf("Upload of %s is finished.\n", q_args->file->name);
|
||||
/* TODO: ждать ответа от сервера */
|
||||
clear_file_in_query(q_args);
|
||||
/* TODO: уведомление пользователю */
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void clear_file_in_query(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);
|
||||
free(q_args->file);
|
||||
q_args->file = NULL;
|
||||
}
|
||||
|
||||
@@ -13,4 +13,5 @@ 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);
|
||||
void clear_file_in_query(query_args_t *q_args);
|
||||
#endif
|
||||
+3
-1
@@ -71,8 +71,10 @@ int32_t process_user_input(app_t *app, callback_args_t *d_args) {
|
||||
return OK;
|
||||
case 'U':
|
||||
case 'u':
|
||||
app->query_args->state = S_UPLOAD_FILE;
|
||||
if (!app->modal.is_initiated) {
|
||||
app->query_args->state = S_UPLOAD_FILE_SELECT;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
d_args->data = (void *)&c;
|
||||
app->active_callback(d_args);
|
||||
|
||||
@@ -39,6 +39,7 @@ enum state {
|
||||
S_FILE_DOWNLOAD,
|
||||
S_UPLOAD_PARAMS,
|
||||
S_UPLOAD_FILE,
|
||||
S_UPLOAD_FILE_SELECT,
|
||||
S_UPLOAD_REQUESTED,
|
||||
S_ASK_USER_BEFORE_LOGIN,
|
||||
S_ERR,
|
||||
@@ -53,6 +54,7 @@ enum state {
|
||||
typedef struct p_file {
|
||||
char *name;
|
||||
char *path;
|
||||
char *description;
|
||||
size_t size;
|
||||
size_t rest;
|
||||
int fd;
|
||||
|
||||
+5
-29
@@ -163,7 +163,6 @@ int process_query(query_args_t *query_args, file_args_t *file_args) {
|
||||
if (!file_upload_start(query_args)) {
|
||||
query_args->state = S_UPLOAD_FILE;
|
||||
} else {
|
||||
print_prompt(query_args->params);
|
||||
query_args->state = WAIT_CLIENT;
|
||||
}
|
||||
break;
|
||||
@@ -172,8 +171,8 @@ int process_query(query_args_t *query_args, file_args_t *file_args) {
|
||||
if (file_upload(query_args)) {
|
||||
/* upload finishes */
|
||||
user_request_description(query_args);
|
||||
print_prompt(query_args->params);
|
||||
query_args->state = WAIT_CLIENT;
|
||||
clear_file_in_query(query_args);
|
||||
query_args->state = S_WAIT_SERVER;
|
||||
}
|
||||
break;
|
||||
case S_ASK_USER_BEFORE_LOGIN:
|
||||
@@ -266,30 +265,7 @@ void init_query_args(query_args_t *q_args, params_t *params) {
|
||||
}
|
||||
|
||||
void user_request_description(query_args_t *q_args) {
|
||||
printf("File description > ");
|
||||
fflush(stdout);
|
||||
char buf[INBUFSIZE]; /* пока ограничение на буфер */
|
||||
char *line_buf = NULL;
|
||||
int blen = 0;
|
||||
int line_len = 0;
|
||||
size_t ll;
|
||||
while (blen < INBUFSIZE - 6) { /* reserve space for ":END:\n" */
|
||||
line_len = getline(&line_buf, &ll, stdin);
|
||||
if (line_len == 1) {
|
||||
/* free(line); -> не работает */
|
||||
break;
|
||||
}
|
||||
if (blen == 0) {
|
||||
strcpy(buf, line_buf);
|
||||
} else {
|
||||
strcat(buf, line_buf);
|
||||
}
|
||||
free(line_buf);
|
||||
line_buf = NULL;
|
||||
blen += line_len;
|
||||
}
|
||||
|
||||
strcat(buf, ":END:\n");
|
||||
blen += 6;
|
||||
write(q_args->sd, buf, blen);
|
||||
strcat(q_args->file->description, "\n\n:END:\n");
|
||||
write(q_args->sd, q_args->file->description,
|
||||
strlen(q_args->file->description)-1);
|
||||
}
|
||||
+2
-1
@@ -157,7 +157,7 @@ void app_draw_modal(app_t *app) {
|
||||
case S_FILE_DOWNLOAD:
|
||||
init_dwn_pr_modal(app);
|
||||
break;
|
||||
case S_UPLOAD_FILE:
|
||||
case S_UPLOAD_FILE_SELECT:
|
||||
init_upload_dialogue_modal(app);
|
||||
break;
|
||||
case S_UPLOAD_PARAMS:
|
||||
@@ -176,6 +176,7 @@ 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 {
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include <string.h>
|
||||
|
||||
void upload_dialogue_modal_cb(callback_args_t *args) {
|
||||
int32_t response;
|
||||
int32_t response = -1;
|
||||
callback_args_t d_args;
|
||||
app_t *app = args->app;
|
||||
dialogue_t *d = (dialogue_t *)app->active_widget;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "../app.h"
|
||||
#include "../widget/dialogue.h"
|
||||
#include "../widget/fs_file_list.h"
|
||||
#include "../widget/group.h"
|
||||
#include "../widget/input.h"
|
||||
#include <ncurses.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
@@ -10,8 +10,7 @@
|
||||
#include <unistd.h>
|
||||
|
||||
void upload_props_dialogue_modal_cb(callback_args_t *args) {
|
||||
/* TODO: finish */
|
||||
int32_t response;
|
||||
int32_t response = -1;
|
||||
callback_args_t d_args;
|
||||
app_t *app = args->app;
|
||||
dialogue_t *d = (dialogue_t *)app->active_widget;
|
||||
@@ -22,9 +21,21 @@ void upload_props_dialogue_modal_cb(callback_args_t *args) {
|
||||
dialogue_default_callback(&d_args);
|
||||
char query[256];
|
||||
int32_t query_len = 0;
|
||||
input_t *desc_input = (input_t *)d->g_content->elements[0].element;
|
||||
if (response > -1) {
|
||||
switch (response) {
|
||||
case 0:
|
||||
query_len =
|
||||
sprintf(query, "file upload \"%s\" %zu 1\n",
|
||||
app->query_args->file->name, app->query_args->file->size);
|
||||
app->query_args->file->description =
|
||||
malloc(desc_input->value_len + 9); /* reserve space for \n:END:\n */
|
||||
strcpy(app->query_args->file->description, desc_input->value);
|
||||
destroy_dialogue(d, app);
|
||||
app->query_args->state = S_UPLOAD_REQUESTED;
|
||||
write(app->query_args->sd, query, query_len);
|
||||
break;
|
||||
case 1:
|
||||
if (app->query_args->file->name != NULL)
|
||||
free(app->query_args->file->name);
|
||||
if (app->query_args->file->path != NULL)
|
||||
@@ -33,12 +44,6 @@ void upload_props_dialogue_modal_cb(callback_args_t *args) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -51,7 +56,10 @@ dialogue_t *init_upload_props_dialogue_modal(app_t *app) {
|
||||
{.type = w_end}};
|
||||
|
||||
group_el_init_t actions[] = {
|
||||
{.type = w_button, .label = "Cancel", .is_default = 0}, {.type = w_end}};
|
||||
{.type = w_button, .label = "Upload", .is_default = 1},
|
||||
{.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",
|
||||
|
||||
Reference in New Issue
Block a user