ui: file list -> fixes opening links
This commit is contained in:
@@ -302,20 +302,23 @@ void init_file_args(file_args_t *f_args) {
|
||||
|
||||
/* work with file list */
|
||||
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;
|
||||
char *name_begin = strchr(line, '[') + 1;
|
||||
char *name_end = strrchr(line, ']');
|
||||
int name_len = name_end - name_begin;
|
||||
fl_item_t *fitem = malloc(sizeof(fl_item_t));
|
||||
sscanf(line, "%s %zu %s%n", fname, &(fitem->size), fowner, &h_len);
|
||||
sscanf(name_end, "] %zu %s%n", &(fitem->size), fowner, &h_len);
|
||||
/* file name */
|
||||
fitem->name = malloc((sizeof(char)) * (strlen(fname) + 1));
|
||||
strcpy(fitem->name, fname);
|
||||
fitem->name = malloc((sizeof(char)) * (name_len + 1));
|
||||
strncpy(fitem->name, name_begin, name_len);
|
||||
fitem->name[name_len] = 0;
|
||||
/* file owner */
|
||||
fitem->owner = malloc((sizeof(char)) * (strlen(fowner) + 1));
|
||||
strcpy(fitem->owner, fowner);
|
||||
/* file description */
|
||||
descr_begin = line + h_len + 1;
|
||||
descr_begin = name_end + h_len + 1;
|
||||
if (strcmp(descr_begin, "\n")) {
|
||||
int d_len = strlen(line) - h_len;
|
||||
fitem->description = malloc((sizeof(char)) * (d_len));
|
||||
|
||||
+1
-1
@@ -78,7 +78,7 @@ int32_t process_user_input(app_t *app, callback_args_t *d_args) {
|
||||
return OK;
|
||||
case 'U':
|
||||
case 'u':
|
||||
if (!app->modal.is_initiated) {
|
||||
if (!app->modal.is_initiated && !fui->active_search) {
|
||||
app->query_args->state = S_UPLOAD_FILE_SELECT;
|
||||
break;
|
||||
}
|
||||
|
||||
+4
-1
@@ -136,11 +136,14 @@ void wait_register(query_args_t *q_args) {
|
||||
|
||||
int upload_confirm_cb(app_t *app, char *query) {
|
||||
if (!strncmp("finished\n", query, sizeof("finished\n") - 1)) {
|
||||
ui_file_list_t *fui = (ui_file_list_t *)app->query_args->file_list_ui;
|
||||
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->query_args->state = S_FILE_LIST;
|
||||
app->modal.needs_destroy = true;
|
||||
reset_file_list(fui);
|
||||
server_send_string(app->query_args, "file list %u %u\n", fui->max_lines, 1);
|
||||
return 0;
|
||||
}
|
||||
return 1; /* TODO: Error case */
|
||||
|
||||
+14
-3
@@ -1,12 +1,12 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
/* Copyright (c) 2026 Oleksandr Zhylin */
|
||||
|
||||
#include "app.h"
|
||||
#include "dialogue.h"
|
||||
#include "main.h"
|
||||
#include <modals.h>
|
||||
#include <widget.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <fcntl.h>
|
||||
#include <modals.h>
|
||||
#include <netinet/in.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
@@ -16,6 +16,7 @@
|
||||
#include <sys/un.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
#include <widget.h>
|
||||
|
||||
void ask_uname_and_password(params_t *params);
|
||||
void ask_register(params_t *params, char *email);
|
||||
@@ -29,7 +30,17 @@ void ask_register(params_t *params, char *email);
|
||||
return 0; \
|
||||
}
|
||||
|
||||
/* TODO: refactor to process callbacks */
|
||||
void server_send_string(query_args_t *q, const char *fmt, ...) {
|
||||
char buf[INBUFSIZE];
|
||||
size_t b_len = 0;
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
b_len = vsprintf(buf, fmt, args);
|
||||
write(q->sd, buf, b_len);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
/* 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;
|
||||
|
||||
+1
-1
@@ -8,5 +8,5 @@
|
||||
#include "main.h"
|
||||
int process_server_command(char *line, int l_len, app_t *app);
|
||||
void ask_register(params_t *params, char *email);
|
||||
|
||||
void server_send_string(query_args_t *q, const char *fmt, ...);
|
||||
#endif
|
||||
@@ -38,7 +38,6 @@ void file_list_cb(callback_args_t *args) {
|
||||
}
|
||||
} else if (key == '\n' || key == '\33') {
|
||||
fui->active_search = false;
|
||||
fl_clear(fui->start, fui->current);
|
||||
app->query_args->state = S_FILE_LIST;
|
||||
if (key == '\33') {
|
||||
fui->search_key->data[0] = '\0';
|
||||
@@ -65,7 +64,6 @@ void file_list_cb(callback_args_t *args) {
|
||||
fui->current_idx++;
|
||||
draw_file_list(fui);
|
||||
} else if (fui->current_page < fui->pages) {
|
||||
fl_clear(fui->start, fui->current);
|
||||
app->query_args->state = S_FILE_LIST;
|
||||
sprintf(query, "%s %u %u\n%n", q_prefix, fui->max_lines,
|
||||
fui->current_page + 1, &q_len);
|
||||
@@ -78,7 +76,6 @@ void file_list_cb(callback_args_t *args) {
|
||||
fui->current_idx--;
|
||||
draw_file_list(fui);
|
||||
} else if (fui->current_page > 1) {
|
||||
fl_clear(fui->start, fui->current);
|
||||
app->query_args->state = S_FILE_LIST;
|
||||
sprintf(query, "%s %u %u\n%n", q_prefix, fui->max_lines,
|
||||
fui->current_page - 1, &q_len);
|
||||
@@ -92,7 +89,6 @@ void file_list_cb(callback_args_t *args) {
|
||||
fui->search_key->data[0] = '\0';
|
||||
fui->search_key->slen = 0;
|
||||
sprintf(q_prefix, "file list");
|
||||
fl_clear(fui->start, fui->current);
|
||||
app->query_args->state = S_FILE_LIST;
|
||||
sprintf(query, "%s %u %u\n%n", q_prefix, fui->max_lines, 1, &q_len);
|
||||
write(app->query_args->sd, query, q_len);
|
||||
@@ -123,6 +119,7 @@ ui_file_list_t *init_file_list(WINDOW **win, WINDOW *const *info_win) {
|
||||
}
|
||||
|
||||
void reset_file_list(ui_file_list_t *fl_ui) {
|
||||
fl_clear(fl_ui->start, fl_ui->current);
|
||||
fl_ui->current_idx = 0;
|
||||
fl_ui->current_page = 0;
|
||||
fl_ui->pages = 0;
|
||||
|
||||
@@ -27,4 +27,5 @@ 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, WINDOW *const *info_win);
|
||||
void file_list_cb(callback_args_t *args);
|
||||
void reset_file_list(ui_file_list_t *fl_ui);
|
||||
#endif
|
||||
@@ -22,6 +22,7 @@ void reset_file_list(ui_fs_file_list_t *fl_ui);
|
||||
|
||||
struct fl_args {
|
||||
char *name;
|
||||
char *path;
|
||||
u_char d_type;
|
||||
};
|
||||
|
||||
@@ -32,6 +33,10 @@ static void fl_add(fs_fl_item_t **f_start, fs_fl_item_t **f_current,
|
||||
strcpy(c_item->name, fargs->name);
|
||||
c_item->next = NULL;
|
||||
c_item->d_type = fargs->d_type;
|
||||
if (fargs->path != NULL) {
|
||||
c_item->path = malloc(strlen(fargs->path) + 1);
|
||||
strcpy(c_item->path, fargs->path);
|
||||
}
|
||||
if (*f_start == NULL) {
|
||||
c_item->prev = NULL;
|
||||
*f_start = c_item;
|
||||
@@ -58,21 +63,24 @@ static void fl_clear(fs_fl_item_t **start, fs_fl_item_t **arg_current) {
|
||||
*arg_current = NULL;
|
||||
}
|
||||
|
||||
fs_fl_item_t *get_files_from_fs(char *path) {
|
||||
fs_fl_item_t *get_files_from_fs(char *path_prefix) {
|
||||
DIR *dir;
|
||||
struct dirent *dent;
|
||||
char name[257];
|
||||
char l_path[INBUFSIZE];
|
||||
struct fl_args f_args;
|
||||
ssize_t r;
|
||||
|
||||
fs_fl_item_t *d_start = NULL, *d_current = NULL; /* directories */
|
||||
fs_fl_item_t *f_start = NULL, *f_current = NULL; /* files */
|
||||
|
||||
dir = opendir(path);
|
||||
dir = opendir(path_prefix);
|
||||
if (!dir) {
|
||||
return NULL;
|
||||
errno = 1;
|
||||
}
|
||||
while ((dent = readdir(dir)) != NULL) {
|
||||
f_args.path = NULL;
|
||||
if ((f_args.d_type = dent->d_type) == DT_DIR) {
|
||||
if (!strcmp(dent->d_name, "..")) {
|
||||
fs_fl_item_t *t_st = NULL, *t_cur = NULL;
|
||||
@@ -92,6 +100,13 @@ fs_fl_item_t *get_files_from_fs(char *path) {
|
||||
sprintf(name, "/%s", dent->d_name);
|
||||
f_args.name = name;
|
||||
fl_add(&d_start, &d_current, &f_args);
|
||||
} else if (f_args.d_type == DT_LNK) {
|
||||
sprintf(name, "%s/%s", path_prefix, dent->d_name);
|
||||
r = readlink(name, l_path, INBUFSIZE);
|
||||
sprintf(name, "->/%s", dent->d_name);
|
||||
f_args.name = name;
|
||||
f_args.path = l_path;
|
||||
fl_add(&d_start, &d_current, &f_args);
|
||||
} else {
|
||||
/* Hide files with dot-beginnings */
|
||||
if (!strncmp(dent->d_name, ".", 1))
|
||||
@@ -136,6 +151,15 @@ void select_item(ui_fs_file_list_t *fui, cb_resp_data *resp_data) {
|
||||
resp_data->code = cbrp_val;
|
||||
resp_data->val.type = val_num;
|
||||
resp_data->val.val.num = 1;
|
||||
} else if (fui->current->d_type == DT_LNK) {
|
||||
fui->d_path = realloc(fui->d_path, strlen(fui->current->path) + 1);
|
||||
strcpy(fui->d_path, fui->current->path);
|
||||
fl_clear(&(fui->start), &(fui->current));
|
||||
fui->start = get_files_from_fs(fui->d_path);
|
||||
fui->current = fui->start;
|
||||
fui->current_idx = 0;
|
||||
resp_data->code = cbrc_none;
|
||||
draw_fs_file_list(fui);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
typedef struct fs_fl_item {
|
||||
char *name;
|
||||
char *path;
|
||||
struct fs_fl_item *next;
|
||||
struct fs_fl_item *prev;
|
||||
u_char d_type;
|
||||
|
||||
Reference in New Issue
Block a user