diff --git a/TestList.csv b/TestList.csv index 884d7cd..7431a1d 100644 --- a/TestList.csv +++ b/TestList.csv @@ -1,17 +1,17 @@ 1,Klara,Novotna,3010,1,0,0 2,Jan,Novak,3011,1,0,0 3,Petr,Horak,3012,1,0,0 -4,Lucie,Kralova,3013,1,0,0 +4,Lucie,Kralova,3013,1747515862,416,0 5,Martin,Prochazka,3014,1,0,0 -6,Anna,Urbanova,3015,1,0,0 +6,Anna,Urbanova,3015,1747515857,0,1 7,David,Svoboda,3016,1,0,0 8,Veronika,Kucerova,3017,1,0,0 -9,Tomas,Blaha,3018,1,0,0 +9,Tomas,Blaha,3018,1747515450,0,1 10,Barbora,Kratochvilova,3019,1,0,0 -11,Jakub,Pospisil,3020,1,0,0 +11,Jakub,Pospisil,3020,1747515462,0,1 12,Eliska,Valkova,3021,1,0,0 -13,Ondrej,Stepanek,3022,1,0,0 -14,Simona,Holubova,3023,1,0,0 +13,Ondrej,Stepanek,3022,1747515457,0,1 +14,Simona,Holubova,3023,1747515453,0,1 15,Filip,Navratil,3024,1,0,0 16,Adela,Slavikova,3025,1,0,0 17,Radek,Polak,3026,1,0,0 diff --git a/include/tui.h b/include/tui.h index f04e866..43620f6 100644 --- a/include/tui.h +++ b/include/tui.h @@ -8,19 +8,30 @@ #include "main.h" #include "users.h" +#define STANDARD_WIN_COLOUR 1 +#define HIGHLIGHT_COLOUR 2 +#define CONTRAST_WIN_COLOUR 6 +#define ERROR_WIN_COLOUR 7 +#define WARNING_WIN_COLOUR 8 +#define SUCCESS_WIN_COLOUR 9 + +void initWindow(WINDOW *_win, int window_x, int terminal_x, int terminal_y, int window_colourpair, char *header); + +// type = 1 - success, 2 - warning, 3 - failure +void dialogWindow(BYTE _type, char *_message, 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, char *db_filename, 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 personInfo(list_t * _list ,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); +int exportDialog(list_t *_list, int terminal_x, int terminal_y); void editDatabaseMenu(list_t *_person_list, int terminal_x, int terminal_y); - #endif \ No newline at end of file diff --git a/src/file_handler.c b/src/file_handler.c index 0d0e049..95a9d9c 100644 --- a/src/file_handler.c +++ b/src/file_handler.c @@ -29,6 +29,7 @@ int loadListFromCSV(char *_filename, list_t *_list) else { // vstupni databaze je vadna + // neco jako ja exit(-1); } } @@ -42,21 +43,21 @@ int saveListToCSV(char *_filename, list_t *_list) FILE *f_out = fopen(_filename, "w"); if (f_out == NULL) { - // doprasit - exit(-1); + return 0; } - + int save_sum = 0; node_t *cursor = _list->head; while (cursor != NULL) { if (cursor->user.uuid != 0) { fprintf(f_out, "%hu,%s,%s,%u,%ld,%ld,%hhu\n", cursor->user.uuid, cursor->user.name, cursor->user.surname, cursor->user.department, cursor->user.last_time_event, cursor->user.total, cursor->user.available); + save_sum++; } cursor = cursor->next; } - int save_sum = 0; + fclose(f_out); return save_sum; diff --git a/src/main.c b/src/main.c index 03575f4..99ddb5e 100644 --- a/src/main.c +++ b/src/main.c @@ -22,7 +22,7 @@ int main(int argc, char const **argv) } int y_max, x_max; - BYTE uart_status = false; + BYTE uart_status = FALSE; char uart_address[51]; pthread_t uart_thread; @@ -49,7 +49,13 @@ int main(int argc, char const **argv) person_list.head = person_list_head; pthread_mutex_init(&person_list.lock, NULL); - loadListFromCSV(argv[1], &person_list); + if (loadListFromCSV(argv[1], &person_list) <= 0) + { + endwin(); + clear(); + puts("Corrupted and/or empty list. Exiting...\n"); + return EXIT_FAILURE; + } noecho(); noraw(); @@ -57,11 +63,14 @@ int main(int argc, char const **argv) start_color(); curs_set(0); - init_pair(1, COLOR_BLACK, COLOR_WHITE); - init_pair(2, COLOR_WHITE, COLOR_RED); + init_pair(STANDARD_WIN_COLOUR, COLOR_BLACK, COLOR_WHITE); + init_pair(HIGHLIGHT_COLOUR, COLOR_WHITE, COLOR_RED); init_pair(3, COLOR_GREEN, COLOR_BLUE); init_pair(4, COLOR_RED, COLOR_BLUE); - init_pair(6, COLOR_BLUE, COLOR_BLACK); + init_pair(CONTRAST_WIN_COLOUR, COLOR_BLUE, COLOR_BLACK); + init_pair(ERROR_WIN_COLOUR, COLOR_WHITE, COLOR_RED); + init_pair(WARNING_WIN_COLOUR, COLOR_WHITE, COLOR_YELLOW); + init_pair(SUCCESS_WIN_COLOUR, COLOR_WHITE, COLOR_GREEN); assume_default_colors(COLOR_WHITE, COLOR_BLUE); erase(); @@ -97,10 +106,10 @@ int main(int argc, char const **argv) attroff(A_BOLD); refresh(); - mainMenu(&person_list, x_max, y_max); - // + mainMenu(&person_list, argv[1], x_max, y_max); pthread_join(uart_thread, NULL); + free(args); clear(); endwin(); diff --git a/src/tui.c b/src/tui.c index 23584d4..53ab4cb 100644 --- a/src/tui.c +++ b/src/tui.c @@ -8,6 +8,61 @@ #include "file_handler.h" #include "tui.h" +void initWindow(WINDOW *_win, int window_x, int terminal_x, int terminal_y, int window_colourpair, char *header) +{ + start_color(); + wbkgd(_win, COLOR_PAIR(window_colourpair)); + wattron(_win, COLOR_PAIR(window_colourpair)); + box(_win, 0, 0); + wattron(_win, A_BOLD); + mvwprintw(_win, 0, 2, "%s", header); + wattroff(_win, A_BOLD); + wattroff(_win, COLOR_PAIR(window_colourpair)); + keypad(_win, true); + wrefresh(_win); +} + +void dialogWindow(BYTE _type, char *_message, int terminal_x, int terminal_y) +{ + int win_height = 5; + int win_width = 80; + int start_y = (terminal_y - win_height) / 2; + int start_x = (terminal_x - win_width) / 2; + + WINDOW *dialog = newwin(win_height, win_width, start_y, start_x); + + if (_type == 1) + { + initWindow(dialog, win_height, terminal_x, terminal_y, SUCCESS_WIN_COLOUR, "Success"); + wattron(dialog, COLOR_PAIR(SUCCESS_WIN_COLOUR)); + mvwprintw(dialog, 2, (win_width - strlen(_message)) / 2, "%s", _message); + wattroff(dialog, COLOR_PAIR(SUCCESS_WIN_COLOUR)); + } + else if (_type == 2) + { + initWindow(dialog, win_height, terminal_x, terminal_y, WARNING_WIN_COLOUR, "Warning"); + wattron(dialog, COLOR_PAIR(WARNING_WIN_COLOUR)); + mvwprintw(dialog, 2, (win_width - strlen(_message)) / 2, "%s", _message); + wattroff(dialog, COLOR_PAIR(WARNING_WIN_COLOUR)); + } + else if (_type == 3) + { + initWindow(dialog, win_height, terminal_x, terminal_y, ERROR_WIN_COLOUR, "Failure"); + wattron(dialog, COLOR_PAIR(ERROR_WIN_COLOUR)); + mvwprintw(dialog, 2, (win_width - strlen(_message)) / 2, "%s", _message); + wattroff(dialog, COLOR_PAIR(ERROR_WIN_COLOUR)); + } + + flushinp(); + wgetch(dialog); + + delwin(dialog); + touchwin(stdscr); + refresh(); + + return; +} + void uartDialog(char *uart_adress, int terminal_x, int terminal_y) { char *prompt = "Please enter UART device address for RFID reader (probably /dev/ttyUSB0): "; @@ -22,14 +77,9 @@ void uartDialog(char *uart_adress, int terminal_x, int terminal_y) start_color(); WINDOW *uartwin = newwin(win_height, win_width, start_y, start_x); + initWindow(uartwin, win_width, terminal_x, terminal_y, STANDARD_WIN_COLOUR, header); - wbkgd(uartwin, COLOR_PAIR(1)); - - box(uartwin, 0, 0); wattron(uartwin, COLOR_PAIR(1)); - wattron(uartwin, A_BOLD); - mvwprintw(uartwin, 0, (win_width - strlen(header)) / 2, "%s", header); - wattroff(uartwin, A_BOLD); mvwprintw(uartwin, 2, (win_width - strlen(prompt)) / 2, "%s", prompt); wattroff(uartwin, COLOR_PAIR(1)); wrefresh(uartwin); @@ -40,7 +90,6 @@ void uartDialog(char *uart_adress, int terminal_x, int terminal_y) int text_x = (win_width - 52) / 2; int button_x = (win_width - 7) / 2; - keypad(uartwin, true); while (1) { // Input array @@ -99,10 +148,7 @@ void uartDialog(char *uart_adress, int terminal_x, int terminal_y) { if (choice == '\n') { - wclear(uartwin); - wrefresh(uartwin); touchwin(stdscr); - refresh(); delwin(uartwin); refresh(); strcpy(uart_adress, input); @@ -113,7 +159,7 @@ 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, char *db_filename, int terminal_x, int terminal_y) { int win_height = 10; int win_width = 50; @@ -129,22 +175,9 @@ void mainMenu(list_t *person_list, int terminal_x, int terminal_y) WINDOW *mainmenu = newwin(win_height, win_width, start_y, start_x); - wbkgd(mainmenu, COLOR_PAIR(1)); - - keypad(mainmenu, true); - - int text_x = (win_width - 52) / 2; - - wrefresh(mainmenu); - 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)); + initWindow(mainmenu, win_width, terminal_x, terminal_y, STANDARD_WIN_COLOUR, "Administration"); for (int i = 0; i < 6; i++) { if (i == highlight) @@ -183,9 +216,18 @@ void mainMenu(list_t *person_list, int terminal_x, int terminal_y) case 1: personSearch(person_list, terminal_x, terminal_y); break; + case 3: + if (!saveListToCSV(db_filename, person_list)) + dialogWindow(3, "Unknown error while saving.", terminal_x, terminal_y); + else + dialogWindow(1, "Database state succesfully saved.", terminal_x, terminal_y); + break; case 4: - exportDialog(person_list, terminal_x, terminal_y); + if (!exportDialog(person_list, terminal_x, terminal_y)) + dialogWindow(3, "Unknown error while saving.", terminal_x, terminal_y); + else + dialogWindow(1, "Database succesfully exported.", terminal_x, terminal_y); break; case 5: @@ -212,10 +254,7 @@ void personListing(list_t *person_list, int terminal_x, int terminal_y) int start_y = (terminal_y - win_height) / 2; int start_x = (terminal_x - win_width) / 2; - start_color(); - WINDOW *personlisting = newwin(win_height, win_width, start_y, start_x); - wbkgd(personlisting, COLOR_PAIR(1)); int max_rows = win_height - 2; int total_count = 0; @@ -232,20 +271,7 @@ void personListing(list_t *person_list, int terminal_x, int terminal_y) 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); - 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)); + initWindow(personlisting, win_width, terminal_x, terminal_y, STANDARD_WIN_COLOUR, "Filtered list"); wattron(personlisting, A_BOLD); mvwprintw(personlisting, 1, 2, "UUID Surname Name Available"); @@ -326,10 +352,7 @@ void personSearch(list_t *person_list, int terminal_x, int terminal_y) char buttons[2][10] = {"", ""}; char prompts[2][10] = {"Name ", "Surname "}; - start_color(); - WINDOW *searchdialog = newwin(win_height, win_width, start_y, start_x); - wbkgd(searchdialog, COLOR_PAIR(1)); char keyboard_input; int highlight = 0; @@ -347,12 +370,7 @@ void personSearch(list_t *person_list, int terminal_x, int terminal_y) { 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); + initWindow(searchdialog, win_width, terminal_x, terminal_y, STANDARD_WIN_COLOUR, "Search"); for (int i = 0; i < 2; i++) { @@ -470,18 +488,9 @@ void personInfo(list_t *_list, node_t *_person, int terminal_x, int terminal_y) 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[3][20] = {"", "", ""}; char prompts[6][32] = {"Name", "Surname", "Department", "Last time event", "Total time", "Available"}; @@ -496,12 +505,7 @@ void personInfo(list_t *_list, node_t *_person, int terminal_x, int terminal_y) 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); + initWindow(personinfodialog, win_width, terminal_x, terminal_y, CONTRAST_WIN_COLOUR, "Person Information"); char last_time_event_str[64]; printDateAndTimeInString(_person->user.last_time_event, last_time_event_str); @@ -509,12 +513,13 @@ void personInfo(list_t *_list, node_t *_person, int terminal_x, int terminal_y) char total_time_str[64]; printTimeInString(_person->user.total, total_time_str); + wattron(personinfodialog, COLOR_PAIR(6)); 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"); + mvwprintw(personinfodialog, field_y + 7, field_x, "%s: %s", prompts[5], _person->user.available ? "Yes" : "No "); wattroff(personinfodialog, COLOR_PAIR(6)); @@ -544,7 +549,7 @@ void personInfo(list_t *_list, node_t *_person, int terminal_x, int terminal_y) if (highlight != 2) highlight++; else - highlight = 1; + highlight = 0; } else if (keyboard_input == '\n') @@ -555,7 +560,10 @@ void personInfo(list_t *_list, node_t *_person, int terminal_x, int terminal_y) } else if (highlight == 1) { - addTimeEvent(_person, _list); + if (addTimeEvent(_person, _list)) + dialogWindow(1, "Time event succesfully logged.", terminal_x, terminal_y); + else + dialogWindow(3, "Unknown error", terminal_x, terminal_y); } else if (highlight == 2) { @@ -575,7 +583,7 @@ 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) +int exportDialog(list_t *_list, int terminal_x, int terminal_y) { int win_height = 8; int win_width = 80; @@ -583,20 +591,12 @@ void exportDialog(list_t *_list, int terminal_x, int terminal_y) int start_x = (terminal_x - win_width) / 2; WINDOW *exportdialog = newwin(win_height, win_width, start_y, start_x); - - wbkgd(exportdialog, COLOR_PAIR(6)); + initWindow(exportdialog, win_width, terminal_x, terminal_y, STANDARD_WIN_COLOUR, "Copy Database And Save"); int max_rows = win_height - 2; int total_count = 0; box(exportdialog, 0, 0); - wattron(exportdialog, COLOR_PAIR(1)); - wattron(exportdialog, A_BOLD); - mvwprintw(exportdialog, 0, (win_width - strlen("Copy database and save")) / 2, "%s", "Copy database and save"); - wattroff(exportdialog, A_BOLD); - wattroff(exportdialog, COLOR_PAIR(1)); - keypad(exportdialog, TRUE); - wattron(exportdialog, COLOR_PAIR(1)); mvwprintw(exportdialog, 2, (win_width - strlen("Enter name of exported database")) / 2, "%s", "Enter name of exported database"); wattroff(exportdialog, COLOR_PAIR(1)); @@ -670,16 +670,20 @@ void exportDialog(list_t *_list, int terminal_x, int terminal_y) if (choice == '\n') { - saveListToCSV(filename, _list); + BYTE check = saveListToCSV(filename, _list); wclear(exportdialog); wrefresh(exportdialog); touchwin(stdscr); refresh(); delwin(exportdialog); refresh(); - return; + return check; } } } } +} + +void editDatabaseMenu(list_t *_person_list, int terminal_x, int terminal_y) +{ } \ No newline at end of file