diff --git a/src/editor_easy.cpp b/src/editor_easy.cpp index 2922f10..f066090 100644 --- a/src/editor_easy.cpp +++ b/src/editor_easy.cpp @@ -236,5 +236,6 @@ std::vector editor_easy_allocated; std::string complaint = std::vformat(loc_strings->criminal_complaint_template, comp_args); + delete_all(&editor_easy_allocated); return complaint; } diff --git a/src/menu.cpp b/src/menu.cpp index bb0fdf2..b4799e3 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -37,6 +38,97 @@ std::array get_name_date_from_item(ITEM* item) { return name_date; } +void reload_menu_from_directory(complete_menu& main_menu) { + // Clean up existing menu items and allocated memory + if (main_menu.menu != nullptr) { + unpost_menu(main_menu.menu); + free_menu(main_menu.menu); + main_menu.menu = nullptr; + } + + // Free existing menu items + if (main_menu.items != nullptr) { + for (size_t i = 0; i < main_menu.items_size; i++) { + if (main_menu.items[i] != nullptr) { + free_item(main_menu.items[i]); + } + } + delete[] main_menu.items; + main_menu.items = nullptr; + } + + // Clean up allocated memory (excluding the complete_menu itself) + for (auto it = main_menu_allocated.begin(); + it != main_menu_allocated.end();) { + if (it->type == GENERIC_TYPE) { + delete[] static_cast(it->ptr); + it = main_menu_allocated.erase(it); + } else { + ++it; + } + } + + // Recreate items from directory (same logic as initialization) + std::vector items; + + if (std::filesystem::exists(COMPLAINTS_DIR)) { + for (const std::filesystem::directory_entry& directroy_entry : + std::filesystem::directory_iterator(COMPLAINTS_DIR)) { + if (directroy_entry.is_regular_file()) { + std::ifstream file(directroy_entry.path()); + std::string name_date[2]; + std::stringstream ssfn(directroy_entry.path().filename().string()); + + for (uint8_t i = 0; i < ARRAY_SIZE(name_date); i++) { + std::getline(ssfn, name_date[i], '_'); + } + + json complaint_json = json::parse(file); + if (complaint_json["status"].is_null() || + complaint_json["status"].get() >= STATUS_COUNT) { + std::cerr << "Invalid status in file: " + << directroy_entry.path().filename().string() << std::endl; + safe_exit(EINVAL); + } + + name_date[1].append(" "); + name_date[1].append( + loc_strings + ->status_strings[complaint_json["status"].get()]); + + char* name = new char[name_date[0].length() + 1]; + main_menu_allocated.push_back({GENERIC_TYPE, name, 1}); + char* date = new char[name_date[1].length() + 1]; + main_menu_allocated.push_back({GENERIC_TYPE, date, 1}); + + strcpy(name, name_date[0].c_str()); + strcpy(date, name_date[1].c_str()); + items.push_back(new_item(name, date)); + } + } + } + + // Add null terminator + items.push_back(nullptr); + + // Create new menu items array + main_menu.items = new ITEM*[items.size()]; + main_menu.items_size = items.size() - 1; + memcpy(main_menu.items, items.data(), (items.size() * sizeof(ITEM*))); + + // Create new menu + main_menu.menu = new_menu(main_menu.items); + + // Restore menu settings + set_menu_win(main_menu.menu, main_menu.win); + set_menu_sub(main_menu.menu, derwin(main_menu.win, 6, 38, 3, 1)); + set_menu_mark(main_menu.menu, " * "); + + // Post the menu + post_menu(main_menu.menu); + wrefresh(main_menu.win); +} + void menu() { current_allocated = &main_menu_allocated; /* Initialize curses */ @@ -143,11 +235,30 @@ void menu() { case ':': switch (hash_djb2a(spawncmd())) { case "print"_sh: - case "p"_sh: + case "p"_sh: { // DONT FORGET TO PRINT ACTUAL DOCUMENT - printDocument("test"); + std::array name_date = + get_name_date_from_item(current_item(main_menu.menu)); + std::ifstream selected_file(COMPLAINTS_DIR "/" + name_date[0] + + '_' + name_date[1]); + if (!selected_file.is_open()) { + std::clog << selected_file.rdstate() << "\n"; + std::cerr << RED "[ERROR] " RESET << loc_strings->invalid_input + << "\n" + << "File: " + << COMPLAINTS_DIR "/" + name_date[0] + '_' + + name_date[1] + << "\n"; + exit(EINVAL); + } + + json selected_file_json = json::parse(selected_file); + + printDocument( + selected_file_json["complaint_text"].get()); current_allocated = &main_menu_allocated; break; + } case "easy_edit"_sh: case "ee"_sh: { const std::string name = spawncmd(loc_strings->reference_name); @@ -173,7 +284,9 @@ void menu() { {"complaint_text", complaint_text}}; complaint_file << complaint_json; - + complaint_file.close(); + current_allocated = &main_menu_allocated; + reload_menu_from_directory(main_menu); break; } case "eh"_sh: { @@ -214,6 +327,7 @@ void menu() { } break; case 'm': { + int current_item_index = item_index(current_item(main_menu.menu)); std::array name_date = get_name_date_from_item(current_item(main_menu.menu)); @@ -302,6 +416,7 @@ void menu() { set_menu_sub(main_menu.menu, old_win_sub); set_menu_format(main_menu.menu, 7, 1); set_menu_mark(main_menu.menu, " * "); + set_current_item(main_menu.menu, main_menu.items[current_item_index]); post_menu(main_menu.menu); break; }