Enhance user information handling: add exportPersonInfo function, improve TUI dialogs, and refactor related code for better clarity and functionality.

This commit is contained in:
2025-05-17 20:22:54 +02:00
parent c84987239e
commit 1923b91ff4
10 changed files with 299 additions and 89 deletions
+12 -1
View File
@@ -9,7 +9,18 @@
"file_operations.h": "c", "file_operations.h": "c",
"string.h": "c", "string.h": "c",
"termios.h": "c", "termios.h": "c",
"typeinfo": "c" "typeinfo": "c",
"stdio.h": "c",
"array": "c",
"hash_map": "c",
"bitset": "c",
"string_view": "c",
"hash_set": "c",
"format": "c",
"initializer_list": "c",
"ranges": "c",
"span": "c",
"valarray": "c"
}, },
"C_Cpp.errorSquiggles": "enabled" "C_Cpp.errorSquiggles": "enabled"
} }
+2
View File
@@ -15,4 +15,6 @@ int loadListFromCSV(char* _filename, list_t * _list);
int saveListToCSV(char* _filename, list_t * _list); int saveListToCSV(char* _filename, list_t * _list);
int exportPersonInfo(node_t * _person);
#endif #endif
+24 -1
View File
@@ -1,13 +1,36 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <stdint.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include <ncurses.h>
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <string.h>
#include <signal.h>
#include "utils.h"
#ifndef __MAIN_H__ #ifndef __MAIN_H__
#define __MAIN_H__ #define __MAIN_H__
#define TRUE !0 #ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0 #define FALSE 0
#endif
#ifndef BYTE
#define BYTE uint8_t #define BYTE uint8_t
#endif
#define MIN_X_TERMINAL_SIZE 50 #define MIN_X_TERMINAL_SIZE 50
#define MIN_Y_TERMINAL_SIZE 20 #define MIN_Y_TERMINAL_SIZE 20
+8 -3
View File
@@ -4,15 +4,20 @@
#include <ncurses.h> #include <ncurses.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h>
#include "main.h" #include "main.h"
#include "users.h" #include "users.h"
void uartDialog(char *uart_adress, int terminal_x, int terminal_y); void uartDialog(char *uart_adress, int terminal_x, int terminal_y);
void mainMenu(list_t * person_list, int terminal_x, int terminal_y); void mainMenu(list_t *person_list, int terminal_x, int terminal_y);
void personListing(list_t * person_list, int terminal_x, int terminal_y); void personListing(list_t *person_list, int terminal_x, int terminal_y);
void personSearch(list_t * person_list, int terminal_x, int terminal_y); void personSearch(list_t *person_list, int terminal_x, int terminal_y);
void personInfo(node_t *_person, int terminal_x, int terminal_y);
void exportDialog(list_t _list, int terminal_x, int terminal_y)
#endif #endif
-5
View File
@@ -1,10 +1,5 @@
#include "main.h" #include "main.h"
#include <stdint.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#ifndef __USERS_H__ #ifndef __USERS_H__
#define __USERS_H__ #define __USERS_H__
+10
View File
@@ -0,0 +1,10 @@
#ifndef __UTILS_H__
#define __UTILS_H__
#include "main.h"
void printTimeInString(time_t _input, char * _output);
void printDateAndTimeInString(time_t _input, char * _output);
#endif
+24
View File
@@ -60,4 +60,28 @@ int saveListToCSV(char *_filename, list_t *_list)
fclose(f_out); fclose(f_out);
return save_sum; return save_sum;
}
int exportPersonInfo(node_t *_person)
{
char filename[64];
sprintf(filename, "person_uuid%d.txt", _person->user.uuid);
FILE *f_out = fopen(filename, "w");
if (f_out == NULL)
{
return 0;
}
char last_time_event_str[64];
printDateAndTimeInString(_person->user.last_time_event, last_time_event_str);
char total_time_str[64];
printTimeInString(_person->user.total, total_time_str);
fprintf(f_out, "NAME: %s\nSURNAME: %s\nDEPARTMENT: %d\nLAST TIME EVENT: %s\nTOTAL TIME: %s\n", _person->user.name, _person->user.surname, _person->user.department, last_time_event_str, total_time_str);
fclose(f_out);
return 1;
} }
+1 -11
View File
@@ -1,14 +1,3 @@
#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <string.h>
#include <signal.h>
#include "main.h" #include "main.h"
#include "users.h" #include "users.h"
#include "tui.h" #include "tui.h"
@@ -72,6 +61,7 @@ int main(int argc, char const **argv)
init_pair(2, COLOR_WHITE, COLOR_RED); init_pair(2, COLOR_WHITE, COLOR_RED);
init_pair(3, COLOR_GREEN, COLOR_BLUE); init_pair(3, COLOR_GREEN, COLOR_BLUE);
init_pair(4, COLOR_RED, COLOR_BLUE); init_pair(4, COLOR_RED, COLOR_BLUE);
init_pair(6, COLOR_BLUE, COLOR_BLACK);
assume_default_colors(COLOR_WHITE, COLOR_BLUE); assume_default_colors(COLOR_WHITE, COLOR_BLUE);
erase(); erase();
+190 -68
View File
@@ -4,6 +4,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h> #include <ctype.h>
#include "main.h" #include "main.h"
#include "db_handler.h"
#include "tui.h" #include "tui.h"
void uartDialog(char *uart_adress, int terminal_x, int terminal_y) void uartDialog(char *uart_adress, int terminal_x, int terminal_y)
@@ -118,9 +119,9 @@ void mainMenu(list_t *person_list, int terminal_x, int terminal_y)
int start_y = (terminal_y - win_height) / 2; int start_y = (terminal_y - win_height) / 2;
int start_x = (terminal_x - win_width) / 2; int start_x = (terminal_x - win_width) / 2;
char choices[6][30] = {"List all employees", "Find employee", "Edit database", "Save", "Save as...", "Exit"}; char choices[6][30] = {"List all employees", "Find employee", "Edit database", "Save", "Export", "Exit"};
char keboard_input; char keyboard_input;
int highlight = 0; int highlight = 0;
start_color(); start_color();
@@ -129,12 +130,6 @@ void mainMenu(list_t *person_list, int terminal_x, int terminal_y)
wbkgd(mainmenu, COLOR_PAIR(1)); wbkgd(mainmenu, COLOR_PAIR(1));
box(mainmenu, 0, 0);
wattron(mainmenu, COLOR_PAIR(1));
wattron(mainmenu, A_BOLD);
mvwprintw(mainmenu, 0, (win_width - strlen("Administration")) / 2, "%s", "Administration");
wattroff(mainmenu, A_BOLD);
wattroff(mainmenu, COLOR_PAIR(1));
keypad(mainmenu, true); keypad(mainmenu, true);
int text_x = (win_width - 52) / 2; int text_x = (win_width - 52) / 2;
@@ -143,6 +138,12 @@ void mainMenu(list_t *person_list, int terminal_x, int terminal_y)
while (1) while (1)
{ {
box(mainmenu, 0, 0);
wattron(mainmenu, COLOR_PAIR(1));
wattron(mainmenu, A_BOLD);
mvwprintw(mainmenu, 0, (win_width - strlen("Administration")) / 2, "%s", "Administration");
wattroff(mainmenu, A_BOLD);
wattroff(mainmenu, COLOR_PAIR(1));
for (int i = 0; i < 6; i++) for (int i = 0; i < 6; i++)
{ {
if (i == highlight) if (i == highlight)
@@ -160,9 +161,9 @@ void mainMenu(list_t *person_list, int terminal_x, int terminal_y)
} }
wrefresh(mainmenu); wrefresh(mainmenu);
flushinp(); flushinp();
keboard_input = wgetch(mainmenu); keyboard_input = wgetch(mainmenu);
if (keboard_input == '\t') if (keyboard_input == '\t')
{ {
if (highlight < 5) if (highlight < 5)
highlight++; highlight++;
@@ -170,7 +171,7 @@ void mainMenu(list_t *person_list, int terminal_x, int terminal_y)
highlight = 0; highlight = 0;
} }
else if (keboard_input == '\n') else if (keyboard_input == '\n')
{ {
switch (highlight) switch (highlight)
{ {
@@ -180,9 +181,10 @@ void mainMenu(list_t *person_list, int terminal_x, int terminal_y)
case 1: case 1:
personSearch(person_list, terminal_x, terminal_y); personSearch(person_list, terminal_x, terminal_y);
break;
case 5: case 5:
if (keboard_input == '\n') if (keyboard_input == '\n')
{ {
wclear(mainmenu); wclear(mainmenu);
wrefresh(mainmenu); wrefresh(mainmenu);
@@ -209,14 +211,6 @@ void personListing(list_t *person_list, int terminal_x, int terminal_y)
WINDOW *personlisting = newwin(win_height, win_width, start_y, start_x); WINDOW *personlisting = newwin(win_height, win_width, start_y, start_x);
wbkgd(personlisting, COLOR_PAIR(1)); wbkgd(personlisting, COLOR_PAIR(1));
box(personlisting, 0, 0);
wattron(personlisting, COLOR_PAIR(1));
wattron(personlisting, A_BOLD);
mvwprintw(personlisting, 0, (win_width - strlen("Filtered list")) / 2, "%s", "Filtered list");
wattroff(personlisting, A_BOLD);
wattroff(personlisting, COLOR_PAIR(1));
keypad(personlisting, TRUE);
int max_rows = win_height - 2; int max_rows = win_height - 2;
int total_count = 0; int total_count = 0;
@@ -229,10 +223,17 @@ void personListing(list_t *person_list, int terminal_x, int terminal_y)
int offset = 0; int offset = 0;
int highlight = 0; int highlight = 0;
int keboard_input; int keyboard_input;
while (1) while (1)
{ {
box(personlisting, 0, 0);
wattron(personlisting, COLOR_PAIR(1));
wattron(personlisting, A_BOLD);
mvwprintw(personlisting, 0, (win_width - strlen("Filtered list")) / 2, "%s", "Filtered list");
wattroff(personlisting, A_BOLD);
wattroff(personlisting, COLOR_PAIR(1));
keypad(personlisting, TRUE);
werase(personlisting); werase(personlisting);
box(personlisting, 0, 0); box(personlisting, 0, 0);
wattron(personlisting, COLOR_PAIR(1)); wattron(personlisting, COLOR_PAIR(1));
@@ -247,7 +248,7 @@ void personListing(list_t *person_list, int terminal_x, int terminal_y)
current = person_list->head; current = person_list->head;
int idx = 0, row = 2; int idx = 0, row = 2;
while (current && row < win_height - 1) while (current && row < win_height)
{ {
if (current->user.uuid) if (current->user.uuid)
if (idx >= offset && row < win_height - 1) if (idx >= offset && row < win_height - 1)
@@ -269,17 +270,17 @@ void personListing(list_t *person_list, int terminal_x, int terminal_y)
wrefresh(personlisting); wrefresh(personlisting);
keboard_input = wgetch(personlisting); keyboard_input = wgetch(personlisting);
if (keboard_input == KEY_UP) if (keyboard_input == KEY_UP)
{ {
if (highlight > 0) if (highlight > 1)
{ {
highlight--; highlight--;
if (highlight < offset) if (highlight < offset)
offset--; offset--;
} }
} }
else if (keboard_input == KEY_DOWN) else if (keyboard_input == KEY_DOWN)
{ {
if (highlight < total_count - 1) if (highlight < total_count - 1)
{ {
@@ -288,7 +289,7 @@ void personListing(list_t *person_list, int terminal_x, int terminal_y)
offset++; offset++;
} }
} }
else if (keboard_input == '\n') else if (keyboard_input == '\n')
{ {
int idx = 0; int idx = 0;
node_t *selected = person_list->head; node_t *selected = person_list->head;
@@ -297,8 +298,9 @@ void personListing(list_t *person_list, int terminal_x, int terminal_y)
selected = selected->next; selected = selected->next;
idx++; idx++;
} }
personInfo(selected, terminal_x, terminal_y);
} }
else if (keboard_input == 27) else if (keyboard_input == 27 || keyboard_input == 'q')
{ {
break; break;
} }
@@ -311,8 +313,8 @@ void personListing(list_t *person_list, int terminal_x, int terminal_y)
void personSearch(list_t *person_list, int terminal_x, int terminal_y) void personSearch(list_t *person_list, int terminal_x, int terminal_y)
{ {
int win_height = 20; int win_height = 11;
int win_width = 76; int win_width = 50;
int start_y = (terminal_y - win_height) / 2; int start_y = (terminal_y - win_height) / 2;
int start_x = (terminal_x - win_width) / 2; int start_x = (terminal_x - win_width) / 2;
@@ -323,16 +325,8 @@ void personSearch(list_t *person_list, int terminal_x, int terminal_y)
WINDOW *searchdialog = newwin(win_height, win_width, start_y, start_x); WINDOW *searchdialog = newwin(win_height, win_width, start_y, start_x);
wbkgd(searchdialog, COLOR_PAIR(1)); wbkgd(searchdialog, COLOR_PAIR(1));
box(searchdialog, 0, 0);
wattron(searchdialog, COLOR_PAIR(1)); char keyboard_input;
wattron(searchdialog, A_BOLD);
mvwprintw(searchdialog, 0, (win_width - strlen("Search")) / 2, "%s", "Search");
wattroff(searchdialog, A_BOLD);
wattroff(searchdialog, COLOR_PAIR(1));
keypad(searchdialog, TRUE);
char choice;
int highlight = 0; int highlight = 0;
int field_y = 2; int field_y = 2;
@@ -342,10 +336,18 @@ void personSearch(list_t *person_list, int terminal_x, int terminal_y)
char *fields[2] = {name, surname}; char *fields[2] = {name, surname};
int field_lens[2] = {31, 31}; int field_lens[2] = {31, 31};
uint16_t * uuids_found; uint16_t *uuids_found;
while (1) while (1)
{ {
box(searchdialog, 0, 0);
wattron(searchdialog, COLOR_PAIR(1));
wattron(searchdialog, A_BOLD);
mvwprintw(searchdialog, 0, (win_width - strlen("Search")) / 2, "%s", "Search");
wattroff(searchdialog, A_BOLD);
wattroff(searchdialog, COLOR_PAIR(1));
keypad(searchdialog, TRUE);
for (int i = 0; i < 2; i++) for (int i = 0; i < 2; i++)
{ {
@@ -353,7 +355,12 @@ void personSearch(list_t *person_list, int terminal_x, int terminal_y)
wattron(searchdialog, COLOR_PAIR(2)); wattron(searchdialog, COLOR_PAIR(2));
else else
wattron(searchdialog, COLOR_PAIR(1)); wattron(searchdialog, COLOR_PAIR(1));
mvwprintw(searchdialog, field_y + i * 2, field_x, "%s [%30s]", prompts[i], fields[i]);
mvwprintw(searchdialog, field_y + i * 2, field_x, "%s ", prompts[i]);
mvwprintw(searchdialog, field_y + i * 2, field_x + strlen(prompts[i]) + 3, "[%30s]", fields[i]);
if (strcmp(fields[i], "\0") == 0)
mvwprintw(searchdialog, field_y + i * 2, field_x + strlen(prompts[i]) + 3, "[______________________________]");
if (highlight == i) if (highlight == i)
wattroff(searchdialog, COLOR_PAIR(2)); wattroff(searchdialog, COLOR_PAIR(2));
else else
@@ -379,9 +386,9 @@ void personSearch(list_t *person_list, int terminal_x, int terminal_y)
wrefresh(searchdialog); wrefresh(searchdialog);
choice = wgetch(searchdialog); keyboard_input = wgetch(searchdialog);
if (choice == '\t') if (keyboard_input == '\t')
{ {
if (highlight < 3) if (highlight < 3)
{ {
@@ -390,7 +397,7 @@ void personSearch(list_t *person_list, int terminal_x, int terminal_y)
else else
highlight = 0; highlight = 0;
} }
else if (choice == '\n') else if (keyboard_input == '\n')
{ {
if (highlight < 2) if (highlight < 2)
{ {
@@ -404,42 +411,157 @@ void personSearch(list_t *person_list, int terminal_x, int terminal_y)
} }
else if (highlight == 2) else if (highlight == 2)
{ {
uint16_t count = searchUUIDByName(person_list, name, surname, &uuids_found); uint16_t count = searchUUIDByName(person_list, name, surname, &uuids_found);
list_t found_pesons; list_t found_pesons;
found_pesons.head = NULL; found_pesons.head = NULL;
node_t **last_ptr = &found_pesons.head; node_t **last_ptr = &found_pesons.head;
for (uint16_t i = 0; i < count; i++) { for (uint16_t i = 0; i < count; i++)
node_t *cur = person_list->head; {
while (cur) { node_t *cur = person_list->head;
if (cur->user.uuid == uuids_found[i]) { while (cur)
node_t *new_node = malloc(sizeof(node_t)); {
if (new_node) { if (cur->user.uuid == uuids_found[i])
new_node->user = cur->user; {
new_node->next = NULL; node_t *new_node = malloc(sizeof(node_t));
*last_ptr = new_node; if (new_node)
last_ptr = &new_node->next; {
} new_node->user = cur->user;
break; new_node->next = NULL;
} *last_ptr = new_node;
cur = cur->next; last_ptr = &new_node->next;
} }
} break;
personListing(&found_pesons, terminal_x, terminal_y); }
cur = cur->next;
}
}
personListing(&found_pesons, terminal_x, terminal_y);
} }
else if (highlight == 3) else if (highlight == 3)
{ {
delwin(searchdialog); delwin(searchdialog);
touchwin(stdscr); touchwin(stdscr);
refresh(); refresh();
break; return;
} }
} }
else if (choice == 27) else if (keyboard_input == 27 || keyboard_input == 'q')
{ // ESC { // ESC
delwin(searchdialog); delwin(searchdialog);
touchwin(stdscr); touchwin(stdscr);
refresh(); refresh();
break; return;
} }
} }
}
void personInfo(node_t *_person, int terminal_x, int terminal_y)
{
int win_height = 14;
int win_width = 50;
int start_y = (terminal_y - win_height) / 2;
int start_x = (terminal_x - win_width) / 2;
start_color();
WINDOW *personinfodialog = newwin(win_height, win_width, start_y, start_x);
wbkgd(personinfodialog, COLOR_PAIR(6));
int max_rows = win_height - 2;
int total_count = 0;
box(personinfodialog, 0, 0);
wattron(personinfodialog, COLOR_PAIR(6));
wattron(personinfodialog, A_BOLD);
mvwprintw(personinfodialog, 0, (win_width - strlen("Search")) / 2, "%s", "Search");
wattroff(personinfodialog, A_BOLD);
wattroff(personinfodialog, COLOR_PAIR(6));
keypad(personinfodialog, TRUE);
char buttons[2][20] = {"<Print to file...>", "<Back>"};
char prompts[6][32] = {"Name", "Surname", "Department", "Last time event", "Total time", "Available"};
char keyboard_input;
int highlight = 0;
int field_y = 2;
int field_x = 4;
int button_y = field_y + 15;
int button_x = field_x;
while (1)
{
werase(personinfodialog);
box(personinfodialog, 0, 0);
wattron(personinfodialog, COLOR_PAIR(6));
wattron(personinfodialog, A_BOLD);
mvwprintw(personinfodialog, 0, (win_width - strlen("Person Information")) / 2, "%s", "Person Information");
wattroff(personinfodialog, A_BOLD);
char last_time_event_str[64];
printDateAndTimeInString(_person->user.last_time_event, last_time_event_str);
char total_time_str[64];
printTimeInString(_person->user.total, total_time_str);
mvwprintw(personinfodialog, field_y, field_x, "%s: %s", prompts[0], _person->user.name);
mvwprintw(personinfodialog, field_y + 1, field_x, "%s: %s", prompts[1], _person->user.surname);
mvwprintw(personinfodialog, field_y + 2, field_x, "%s: %d", prompts[2], _person->user.department);
mvwprintw(personinfodialog, field_y + 4, field_x, "%s: %s", prompts[3], last_time_event_str);
mvwprintw(personinfodialog, field_y + 5, field_x, "%s: %s", prompts[4], total_time_str);
mvwprintw(personinfodialog, field_y + 7, field_x, "%s: %s", prompts[5], _person->user.available ? "Yes" : "No");
wattroff(personinfodialog, COLOR_PAIR(6));
wrefresh(personinfodialog);
for (int i = 0; i < 2; i++)
{
if (highlight == i)
wattron(personinfodialog, COLOR_PAIR(2));
else
wattron(personinfodialog, COLOR_PAIR(6));
wattron(personinfodialog, A_BOLD);
mvwprintw(personinfodialog, field_y + 10, field_x + i * 20, "%s", buttons[i]);
wattroff(personinfodialog, A_BOLD);
if (highlight == i)
wattroff(personinfodialog, COLOR_PAIR(2));
else
wattroff(personinfodialog, COLOR_PAIR(6));
}
wrefresh(personinfodialog);
keyboard_input = wgetch(personinfodialog);
if (keyboard_input == '\t')
{
highlight = !highlight;
}
else if (keyboard_input == '\n')
{
if (highlight == 0)
{
exportPersonInfo(_person);
}
else if (highlight == 1)
{
delwin(personinfodialog);
touchwin(stdscr);
refresh();
return;
}
}
else if (keyboard_input == 27 || keyboard_input == 'q')
{
delwin(personinfodialog);
touchwin(stdscr);
refresh();
return;
}
}
}
void exportDialog(list_t _list, int terminal_x, int terminal_y){
} }
+28
View File
@@ -0,0 +1,28 @@
#include "utils.h"
#include "main.h"
void printTimeInString(time_t _input, char *_output)
{
long hours = _input / 3600;
long minutes = (_input % 3600) / 60;
long seconds = _input % 60;
snprintf(_output, 64, "%ld:%02ld:%02ld", hours, minutes, seconds);
return;
}
void printDateAndTimeInString(time_t _input, char *_output)
{
time_t raw_time = _input;
struct tm *last_time_event = localtime(&raw_time);
if (last_time_event)
{
strftime(_output, 64, "%d/%m/%Y %H:%M:%S", last_time_event);
}
else
{
strcpy(_output, "N/A");
}
return;
}