Refactor personInfo function: update parameters to include person list, enhance memory handling, and improve user interaction in TUI.

This commit is contained in:
2025-05-17 21:44:45 +02:00
parent c311dce195
commit 3a0fd9fe65
4 changed files with 113 additions and 71 deletions
+32 -32
View File
@@ -1,32 +1,32 @@
17,Klara,Novotna,3010,1,0,1 1,Klara,Novotna,3010,1,0,0
18,Jan,Novak,3011,1,0,1 2,Jan,Novak,3011,1,0,0
19,Petr,Horak,3012,1,0,1 3,Petr,Horak,3012,1,0,0
20,Lucie,Kralova,3013,1,0,1 4,Lucie,Kralova,3013,1,0,0
21,Martin,Prochazka,3014,1,0,1 5,Martin,Prochazka,3014,1,0,0
22,Anna,Urbanova,3015,1,0,1 6,Anna,Urbanova,3015,1,0,0
23,David,Svoboda,3016,1,0,1 7,David,Svoboda,3016,1,0,0
24,Veronika,Kucerova,3017,1,0,1 8,Veronika,Kucerova,3017,1,0,0
25,Tomas,Blaha,3018,1,0,1 9,Tomas,Blaha,3018,1,0,0
26,Barbora,Kratochvilova,3019,1,0,1 10,Barbora,Kratochvilova,3019,1,0,0
27,Jakub,Pospisil,3020,1,0,1 11,Jakub,Pospisil,3020,1,0,0
28,Eliska,Valkova,3021,1,0,1 12,Eliska,Valkova,3021,1,0,0
29,Ondrej,Stepanek,3022,1,0,1 13,Ondrej,Stepanek,3022,1,0,0
30,Simona,Holubova,3023,1,0,1 14,Simona,Holubova,3023,1,0,0
31,Filip,Navratil,3024,1,0,1 15,Filip,Navratil,3024,1,0,0
32,Adela,Slavikova,3025,1,0,1 16,Adela,Slavikova,3025,1,0,0
33,Radek,Polak,3026,1,0,1 17,Radek,Polak,3026,1,0,0
34,Monika,Adamcova,3027,1,0,1 18,Monika,Adamcova,3027,1,0,0
35,Jaroslav,Kolar,3028,1,0,1 19,Jaroslav,Kolar,3028,1,0,0
36,Petra,Malikova,3029,1,0,1 20,Petra,Malikova,3029,1,0,0
37,Daniel,Richter,3030,1,0,1 21,Daniel,Richter,3030,1,0,0
38,Kristyna,Stastna,3031,1,0,1 22,Kristyna,Stastna,3031,1,0,0
39,Stepan,Musil,3032,1,0,1 23,Stepan,Musil,3032,1,0,0
40,Lenka,Jurickova,3033,1,0,1 24,Lenka,Jurickova,3033,1,0,0
41,Patrik,Vanek,3034,1,0,1 25,Patrik,Vanek,3034,1,0,0
42,Karolina,Vitkova,3035,1,0,1 26,Karolina,Vitkova,3035,1,0,0
43,Stanislav,Sevcik,3036,1,0,1 27,Stanislav,Sevcik,3036,1,0,0
44,Alena,Matouskova,3037,1,0,1 28,Alena,Matouskova,3037,1,0,0
45,Josef,Suchy,3038,1,0,1 29,Josef,Suchy,3038,1,0,0
46,Marketa,Rezacova,3039,1,0,1 30,Marketa,Rezacova,3039,1,0,0
47,Libor,Hanak,3040,1,0,1 31,Libor,Hanak,3040,1,0,0
48,Libor,Hanak,3040,1,0,1 32,Libor,Hanak,3040,1,0,0
1 17 1 Klara Novotna 3010 1 0 0
2 18 1 2 Jan Novak 3011 1 0 0
3 19 1 3 Petr Horak 3012 1 0 0
4 20 1 4 Lucie Kralova 3013 1 0 0
5 21 1 5 Martin Prochazka 3014 1 0 0
6 22 1 6 Anna Urbanova 3015 1 0 0
7 23 1 7 David Svoboda 3016 1 0 0
8 24 1 8 Veronika Kucerova 3017 1 0 0
9 25 1 9 Tomas Blaha 3018 1 0 0
10 26 1 10 Barbora Kratochvilova 3019 1 0 0
11 27 1 11 Jakub Pospisil 3020 1 0 0
12 28 1 12 Eliska Valkova 3021 1 0 0
13 29 1 13 Ondrej Stepanek 3022 1 0 0
14 30 1 14 Simona Holubova 3023 1 0 0
15 31 1 15 Filip Navratil 3024 1 0 0
16 32 1 16 Adela Slavikova 3025 1 0 0
17 33 1 17 Radek Polak 3026 1 0 0
18 34 1 18 Monika Adamcova 3027 1 0 0
19 35 1 19 Jaroslav Kolar 3028 1 0 0
20 36 1 20 Petra Malikova 3029 1 0 0
21 37 1 21 Daniel Richter 3030 1 0 0
22 38 1 22 Kristyna Stastna 3031 1 0 0
23 39 1 23 Stepan Musil 3032 1 0 0
24 40 1 24 Lenka Jurickova 3033 1 0 0
25 41 1 25 Patrik Vanek 3034 1 0 0
26 42 1 26 Karolina Vitkova 3035 1 0 0
27 43 1 27 Stanislav Sevcik 3036 1 0 0
28 44 1 28 Alena Matouskova 3037 1 0 0
29 45 1 29 Josef Suchy 3038 1 0 0
30 46 1 30 Marketa Rezacova 3039 1 0 0
31 47 1 31 Libor Hanak 3040 1 0 0
32 48 1 32 Libor Hanak 3040 1 0 0
+2 -1
View File
@@ -16,10 +16,11 @@ 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 personInfo(list_t * _list ,node_t *_person, int terminal_x, int terminal_y);
void exportDialog(list_t *_list, int terminal_x, int terminal_y); void exportDialog(list_t *_list, int terminal_x, int terminal_y);
void editDatabaseMenu(list_t *_person_list, int terminal_x, int terminal_y); void editDatabaseMenu(list_t *_person_list, int terminal_x, int terminal_y);
#endif #endif
+29 -21
View File
@@ -255,20 +255,20 @@ void personListing(list_t *person_list, int terminal_x, int terminal_y)
int idx = 0, row = 2; int idx = 0, row = 2;
while (current && row < win_height) while (current && row < win_height)
{ {
if (current->user.uuid)
if (idx >= offset && row < win_height - 1) if (idx >= offset && row < win_height - 1)
{ {
if (idx == highlight) if (idx == highlight)
wattron(personlisting, COLOR_PAIR(2)); wattron(personlisting, COLOR_PAIR(2));
mvwprintw(personlisting, row, 2, "%-10hu %-20s %-20s %s", mvwprintw(personlisting, row, 2, "%-10hu %-20s %-20s %s",
current->user.uuid, current->user.uuid,
current->user.surname, current->user.surname,
current->user.name, current->user.name,
current->user.available ? "Yes " : "No "); current->user.available ? "Yes " : "No ");
if (idx == highlight) if (idx == highlight)
wattroff(personlisting, COLOR_PAIR(2)); wattroff(personlisting, COLOR_PAIR(2));
row++; row++;
} }
idx++; idx++;
current = current->next; current = current->next;
} }
@@ -278,7 +278,7 @@ void personListing(list_t *person_list, int terminal_x, int terminal_y)
keyboard_input = wgetch(personlisting); keyboard_input = wgetch(personlisting);
if (keyboard_input == KEY_UP) if (keyboard_input == KEY_UP)
{ {
if (highlight > 1) if (highlight > 0)
{ {
highlight--; highlight--;
if (highlight < offset) if (highlight < offset)
@@ -303,7 +303,7 @@ 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); personInfo(person_list, selected, terminal_x, terminal_y);
} }
else if (keyboard_input == 27 || keyboard_input == 'q') else if (keyboard_input == 27 || keyboard_input == 'q')
{ {
@@ -460,10 +460,10 @@ void personSearch(list_t *person_list, int terminal_x, int terminal_y)
} }
} }
void personInfo(node_t *_person, int terminal_x, int terminal_y) void personInfo(list_t *_list, node_t *_person, int terminal_x, int terminal_y)
{ {
int win_height = 14; int win_height = 14;
int win_width = 50; int win_width = 60;
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;
@@ -483,7 +483,7 @@ void personInfo(node_t *_person, int terminal_x, int terminal_y)
wattroff(personinfodialog, COLOR_PAIR(6)); wattroff(personinfodialog, COLOR_PAIR(6));
keypad(personinfodialog, TRUE); keypad(personinfodialog, TRUE);
char buttons[2][20] = {"<Print to file...>", "<Back>"}; char buttons[3][20] = {"<Print to file...>", "<Add Time Event>", "<Back>"};
char prompts[6][32] = {"Name", "Surname", "Department", "Last time event", "Total time", "Available"}; char prompts[6][32] = {"Name", "Surname", "Department", "Last time event", "Total time", "Available"};
char keyboard_input; char keyboard_input;
@@ -520,7 +520,7 @@ void personInfo(node_t *_person, int terminal_x, int terminal_y)
wrefresh(personinfodialog); wrefresh(personinfodialog);
for (int i = 0; i < 2; i++) for (int i = 0; i < 3; i++)
{ {
if (highlight == i) if (highlight == i)
wattron(personinfodialog, COLOR_PAIR(2)); wattron(personinfodialog, COLOR_PAIR(2));
@@ -541,8 +541,12 @@ void personInfo(node_t *_person, int terminal_x, int terminal_y)
if (keyboard_input == '\t') if (keyboard_input == '\t')
{ {
highlight = !highlight; if (highlight != 2)
highlight++;
else
highlight = 1;
} }
else if (keyboard_input == '\n') else if (keyboard_input == '\n')
{ {
if (highlight == 0) if (highlight == 0)
@@ -550,6 +554,10 @@ void personInfo(node_t *_person, int terminal_x, int terminal_y)
exportPersonInfo(_person); exportPersonInfo(_person);
} }
else if (highlight == 1) else if (highlight == 1)
{
addTimeEvent(_person, _list);
}
else if (highlight == 2)
{ {
delwin(personinfodialog); delwin(personinfodialog);
touchwin(stdscr); touchwin(stdscr);
+50 -17
View File
@@ -156,14 +156,54 @@ int addPersonToList(
int loadPersonToList(list_t *_list, uint16_t _uuid, char *_name, char *_surname, uint32_t _department, time_t _last_event, time_t _total, BYTE _available) int loadPersonToList(list_t *_list, uint16_t _uuid, char *_name, char *_surname, uint32_t _department, time_t _last_event, time_t _total, BYTE _available)
{ {
node_t *new_node = (node_t *)malloc(sizeof(node_t));
if (new_node == NULL)
return 0;
if (!strlen(_name) || !strlen(_surname)) if (!strlen(_name) || !strlen(_surname))
{ {
free(new_node); return 0;
new_node = NULL; }
pthread_mutex_lock(&_list->lock);
if (_list->head == NULL)
{
_list->head = (node_t *)malloc(sizeof(node_t));
if (_list->head == NULL) {
pthread_mutex_unlock(&_list->lock);
return 0;
}
_list->head->next = NULL;
_list->head->user.name = NULL;
_list->head->user.surname = NULL;
}
if (_list->head->user.name == NULL && _list->head->user.surname == NULL)
{
node_t *target = _list->head;
target->user.uuid = _uuid;
target->user.name = (char *)malloc(strlen(_name) + 1);
target->user.surname = (char *)malloc(strlen(_surname) + 1);
if (target->user.name == NULL || target->user.surname == NULL)
{
free(target->user.name);
free(target->user.surname);
pthread_mutex_unlock(&_list->lock);
return 0;
}
strcpy(target->user.name, _name);
strcpy(target->user.surname, _surname);
target->user.department = _department;
target->user.last_time_event = _last_event;
target->user.total = _total;
target->user.available = _available;
pthread_mutex_unlock(&_list->lock);
return 1;
}
node_t *new_node = (node_t *)malloc(sizeof(node_t));
if (new_node == NULL)
{
pthread_mutex_unlock(&_list->lock);
return 0; return 0;
} }
@@ -176,6 +216,7 @@ int loadPersonToList(list_t *_list, uint16_t _uuid, char *_name, char *_surname,
free(new_node->user.name); free(new_node->user.name);
free(new_node->user.surname); free(new_node->user.surname);
free(new_node); free(new_node);
pthread_mutex_unlock(&_list->lock);
return 0; return 0;
} }
@@ -188,16 +229,6 @@ int loadPersonToList(list_t *_list, uint16_t _uuid, char *_name, char *_surname,
new_node->next = NULL; new_node->next = NULL;
pthread_mutex_lock(&_list->lock);
if (_list->head == NULL)
{
_list->head = new_node;
pthread_mutex_unlock(&_list->lock);
return 0;
}
node_t *current = _list->head; node_t *current = _list->head;
while (current->next != NULL) while (current->next != NULL)
{ {
@@ -223,7 +254,9 @@ int addTimeEvent(
pthread_mutex_lock(&_list->lock); pthread_mutex_lock(&_list->lock);
if (_person->user.available) if (_person->user.available)
{ {
_person->user.total += (current_time - _person->user.last_time_event); if (current_time > _person->user.last_time_event) {
_person->user.total += (current_time - _person->user.last_time_event);
}
} }
_person->user.available = !_person->user.available; _person->user.available = !_person->user.available;