Refactor personInfo function: update parameters to include person list, enhance memory handling, and improve user interaction in TUI.
This commit is contained in:
@@ -255,20 +255,20 @@ void personListing(list_t *person_list, int terminal_x, int terminal_y)
|
||||
int idx = 0, row = 2;
|
||||
while (current && row < win_height)
|
||||
{
|
||||
if (current->user.uuid)
|
||||
if (idx >= offset && row < win_height - 1)
|
||||
{
|
||||
if (idx == highlight)
|
||||
wattron(personlisting, COLOR_PAIR(2));
|
||||
mvwprintw(personlisting, row, 2, "%-10hu %-20s %-20s %s",
|
||||
current->user.uuid,
|
||||
current->user.surname,
|
||||
current->user.name,
|
||||
current->user.available ? "Yes " : "No ");
|
||||
if (idx == highlight)
|
||||
wattroff(personlisting, COLOR_PAIR(2));
|
||||
row++;
|
||||
}
|
||||
|
||||
if (idx >= offset && row < win_height - 1)
|
||||
{
|
||||
if (idx == highlight)
|
||||
wattron(personlisting, COLOR_PAIR(2));
|
||||
mvwprintw(personlisting, row, 2, "%-10hu %-20s %-20s %s",
|
||||
current->user.uuid,
|
||||
current->user.surname,
|
||||
current->user.name,
|
||||
current->user.available ? "Yes " : "No ");
|
||||
if (idx == highlight)
|
||||
wattroff(personlisting, COLOR_PAIR(2));
|
||||
row++;
|
||||
}
|
||||
idx++;
|
||||
current = current->next;
|
||||
}
|
||||
@@ -278,7 +278,7 @@ void personListing(list_t *person_list, int terminal_x, int terminal_y)
|
||||
keyboard_input = wgetch(personlisting);
|
||||
if (keyboard_input == KEY_UP)
|
||||
{
|
||||
if (highlight > 1)
|
||||
if (highlight > 0)
|
||||
{
|
||||
highlight--;
|
||||
if (highlight < offset)
|
||||
@@ -303,7 +303,7 @@ void personListing(list_t *person_list, int terminal_x, int terminal_y)
|
||||
selected = selected->next;
|
||||
idx++;
|
||||
}
|
||||
personInfo(selected, terminal_x, terminal_y);
|
||||
personInfo(person_list, selected, terminal_x, terminal_y);
|
||||
}
|
||||
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_width = 50;
|
||||
int win_width = 60;
|
||||
int start_y = (terminal_y - win_height) / 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));
|
||||
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 keyboard_input;
|
||||
@@ -520,7 +520,7 @@ void personInfo(node_t *_person, int terminal_x, int terminal_y)
|
||||
|
||||
wrefresh(personinfodialog);
|
||||
|
||||
for (int i = 0; i < 2; i++)
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
if (highlight == i)
|
||||
wattron(personinfodialog, COLOR_PAIR(2));
|
||||
@@ -541,8 +541,12 @@ void personInfo(node_t *_person, int terminal_x, int terminal_y)
|
||||
|
||||
if (keyboard_input == '\t')
|
||||
{
|
||||
highlight = !highlight;
|
||||
if (highlight != 2)
|
||||
highlight++;
|
||||
else
|
||||
highlight = 1;
|
||||
}
|
||||
|
||||
else if (keyboard_input == '\n')
|
||||
{
|
||||
if (highlight == 0)
|
||||
@@ -550,6 +554,10 @@ void personInfo(node_t *_person, int terminal_x, int terminal_y)
|
||||
exportPersonInfo(_person);
|
||||
}
|
||||
else if (highlight == 1)
|
||||
{
|
||||
addTimeEvent(_person, _list);
|
||||
}
|
||||
else if (highlight == 2)
|
||||
{
|
||||
delwin(personinfodialog);
|
||||
touchwin(stdscr);
|
||||
|
||||
+50
-17
@@ -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)
|
||||
{
|
||||
node_t *new_node = (node_t *)malloc(sizeof(node_t));
|
||||
if (new_node == NULL)
|
||||
return 0;
|
||||
|
||||
if (!strlen(_name) || !strlen(_surname))
|
||||
{
|
||||
free(new_node);
|
||||
new_node = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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.surname);
|
||||
free(new_node);
|
||||
pthread_mutex_unlock(&_list->lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -188,16 +229,6 @@ int loadPersonToList(list_t *_list, uint16_t _uuid, char *_name, char *_surname,
|
||||
|
||||
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;
|
||||
while (current->next != NULL)
|
||||
{
|
||||
@@ -223,7 +254,9 @@ int addTimeEvent(
|
||||
pthread_mutex_lock(&_list->lock);
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user