This commit is contained in:
parent
dded22125c
commit
bf02560cdd
@ -236,5 +236,6 @@ std::vector<allocation> editor_easy_allocated;
|
||||
std::string complaint =
|
||||
std::vformat(loc_strings->criminal_complaint_template, comp_args);
|
||||
|
||||
delete_all(&editor_easy_allocated);
|
||||
return complaint;
|
||||
}
|
||||
|
121
src/menu.cpp
121
src/menu.cpp
@ -1,6 +1,7 @@
|
||||
#include <curses.h>
|
||||
#include <menu.h>
|
||||
#include <ncurses.h>
|
||||
#include <array>
|
||||
#include <cerrno>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
@ -37,6 +38,97 @@ std::array<std::string, 2> 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<char*>(it->ptr);
|
||||
it = main_menu_allocated.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
// Recreate items from directory (same logic as initialization)
|
||||
std::vector<ITEM*> 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<uint8_t>() >= 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<uint8_t>()]);
|
||||
|
||||
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<std::string, 2> 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<std::string>());
|
||||
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<std::string, 2> 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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user