diff options
| author | Aldrik Ramaekers <aldrikboy@gmail.com> | 2025-08-23 11:18:44 +0200 |
|---|---|---|
| committer | Aldrik Ramaekers <aldrikboy@gmail.com> | 2025-08-23 11:18:44 +0200 |
| commit | 359422c97cce93bbb27051f9df3efb45bd0b9052 (patch) | |
| tree | 2e352bb852a25390d40d45e199f835d218ad497f /src | |
| parent | 8ea59863c5d13e68e080cf7612047ea4c655292c (diff) | |
settings file writing
Diffstat (limited to 'src')
| -rw-r--r-- | src/administration.cpp | 11 | ||||
| -rw-r--r-- | src/administration_writer.cpp | 78 | ||||
| -rw-r--r-- | src/locales/en.cpp | 4 | ||||
| -rw-r--r-- | src/ui/helpers.cpp | 58 | ||||
| -rw-r--r-- | src/ui/ui_main.cpp | 3 | ||||
| -rw-r--r-- | src/ui/ui_settings.cpp | 15 |
6 files changed, 167 insertions, 2 deletions
diff --git a/src/administration.cpp b/src/administration.cpp index 68714d3..1d09e69 100644 --- a/src/administration.cpp +++ b/src/administration.cpp @@ -298,6 +298,8 @@ static void administration_create_debug_data() strops_copy(_c.description, _name, sizeof(_c.description));\ administration_project_add(_c);}; + strops_copy(g_administration.path, "C:\\Users\\aldri\\Downloads\\test.openbooks", sizeof(g_administration.path)); + ADD_CONSUMER("Emma Müller", "Hauptstraße 12", "10115 Berlin", "DE"); ADD_CONSUMER("Luca Rossi", "Via Roma 45", "00184 Roma", "IT"); ADD_CONSUMER("Sofia Garcia", "Calle Mayor 7", "28013 Madrid", "ES"); @@ -349,8 +351,8 @@ static void administration_create_debug_data() #define ADD_INVOICE(_outgoing)\ {\ invoice inv = administration_invoice_create_empty();\ - inv.supplier = administration_company_info_get();\ - inv.customer = *administation_get_random_contact();\ + if (_outgoing) inv.supplier = administration_company_info_get(); else inv.supplier = *administation_get_random_contact();\ + if (_outgoing) inv.customer = *administation_get_random_contact(); else inv.customer = administration_company_info_get();\ administation_get_random_project(inv.project_id);\ administation_get_random_cost_center(inv.cost_center_id);\ inv.is_outgoing = _outgoing;\ @@ -429,6 +431,11 @@ void administration_company_info_set(contact data) g_administration.company_info = data; } +administration* administation_get() +{ + return &g_administration; +} + // Contact functions. // ======================= bool administration_contact_add(contact data) diff --git a/src/administration_writer.cpp b/src/administration_writer.cpp new file mode 100644 index 0000000..5f23d78 --- /dev/null +++ b/src/administration_writer.cpp @@ -0,0 +1,78 @@ +#define _CRT_SECURE_NO_WARNINGS + +#include <zip.h> +#include <xml.h> +#include <stdlib.h> + +#include "administration_writer.hpp" + +bool administration_writer_save_cost_centers() +{ + return false; +} + +bool administration_writer_save_tax_brackets() +{ + struct zip_t *zip = zip_open(administration_file_path_get(), 0, 'w'); + if (!zip) { + zip = zip_open(administration_file_path_get(), 0, 'a'); + if (!zip) { + return false; + } + } + + if (zip_entry_open(zip, ADMIN_FILE_SETTINGS) < 0) { + return false; + } + + // Get all data. + u32 num_brackets = administration_tax_bracket_count(); + u32 buffer_size = sizeof(country_tax_bracket) * num_brackets; + country_tax_bracket* bracket_buffer = (country_tax_bracket*)malloc(buffer_size); + num_brackets = administration_tax_bracket_get_all(bracket_buffer); + + int buf_length = num_brackets * 500; // Ballpark file content size. + char* settings_content = (char*)malloc(buf_length); + memset(settings_content, 0, buf_length); + + char* orig_content = settings_content; + + //// Tax brackets. + settings_content += sprintf(settings_content, "<country_tax_brackets>\n"); + + for (u32 i = 0; i < num_brackets; i++) { + country_tax_bracket c = bracket_buffer[i]; + + settings_content += sprintf(settings_content, "\t<country_tax_bracket>\n"); + + settings_content += sprintf(settings_content, "\t\t<id>%s</id>\n", c.id); + settings_content += sprintf(settings_content, "\t\t<country_code>%s</country_code>\n", c.country_code); + settings_content += sprintf(settings_content, "\t\t<rate>%.2f</rate>\n", c.rate); + settings_content += sprintf(settings_content, "\t\t<description>%s</description>\n", c.description); + + settings_content += sprintf(settings_content, "\t</country_tax_bracket>\n"); + } + + settings_content += sprintf(settings_content, "</country_tax_brackets>"); + + int final_length = (int)strlen(orig_content); + + bool result = true; + if (!xml_parse_document((uint8_t*)orig_content, final_length)) { + result = false; + } + + + if (zip_entry_write(zip, orig_content, final_length) < 0) { + result = false; + } + + zip_entry_close(zip); + + zip_close(zip); + + free(bracket_buffer); + free(orig_content); + + return result; +}
\ No newline at end of file diff --git a/src/locales/en.cpp b/src/locales/en.cpp index c2b0738..c965edd 100644 --- a/src/locales/en.cpp +++ b/src/locales/en.cpp @@ -5,6 +5,10 @@ locale_entry en_locales[] = { {"ui.unsavedProject", "[unsaved project]"}, {"ui.workingOn", "Working on"}, + // Status strings. + {"status.saved", "[Saved to disk]"}, + {"status.saveFailed", "[Save failed]"}, + // General form buttons. {"form.create", "+ Create"}, {"form.back", "Back"}, diff --git a/src/ui/helpers.cpp b/src/ui/helpers.cpp index 6ca1bd2..c15528e 100644 --- a/src/ui/helpers.cpp +++ b/src/ui/helpers.cpp @@ -1,8 +1,66 @@ +#include <tinyfiledialogs.h> + #include "ui.hpp" #include "imgui.h" #include "locales.hpp" #include "strops.hpp" +static ui_status current_status; + +void ui_draw_status() +{ + float region_width = ImGui::GetContentRegionAvail().x; + float text_width = ImGui::CalcTextSize(current_status.text).x; + + // Move cursor so that the text ends at the right edge + if (current_status.visible) + { + ImGui::SetCursorPosX(ImGui::GetCursorPosX() + region_width - text_width); + ImGui::PushStyleColor(ImGuiCol_Text, current_status.color); + ImGui::TextUnformatted(current_status.text); + ImGui::PopStyleColor(); + } + + ImGuiIO& io = ImGui::GetIO(); + current_status.time += io.DeltaTime; + + if (current_status.time >= STATUS_FLASH_INTERVAL && current_status.flash_count < STATUS_MAX_FLASHES) + { + current_status.visible = !current_status.visible; + if (current_status.visible) current_status.flash_count++; + current_status.time = 0.0f; + } + + if (current_status.time >= STATUS_DURATION) + { + current_status.text[0] = 0; + } +} + +void ui_set_status_ex(const char* txt, int color) +{ + current_status.flash_count = 0; + current_status.visible = true; + current_status.time = 0.0f; + current_status.color = color; + strops_copy(current_status.text, txt, STATUS_TEXT_LEN); +} + +void ui_set_status_error(const char* txt) +{ + ui_set_status_ex(txt, COLOR_ERROR); +} + +void ui_set_status(const char* txt) +{ + ui_set_status_ex(txt, COLOR_DEFAULT); +} + +ui_status ui_get_status() +{ + return current_status; +} + void ui_helper_draw_required_tag() { ImDrawList* draw_list = ImGui::GetWindowDrawList(); diff --git a/src/ui/ui_main.cpp b/src/ui/ui_main.cpp index f6b179e..33747f1 100644 --- a/src/ui/ui_main.cpp +++ b/src/ui/ui_main.cpp @@ -143,6 +143,9 @@ void ui_draw_main() ImGui::Text("%s: %s", localize("ui.workingOn"), administration_file_path_get()); } + ImGui::SameLine(); + ui_draw_status(); + ImGui::End(); ImGui::PopStyleVar(); }
\ No newline at end of file diff --git a/src/ui/ui_settings.cpp b/src/ui/ui_settings.cpp index 3f9ab6d..9cc2cd9 100644 --- a/src/ui/ui_settings.cpp +++ b/src/ui/ui_settings.cpp @@ -6,6 +6,7 @@ #include "imgui.h" #include "administration.hpp" #include "locales.hpp" +#include "administration_writer.hpp" extern void draw_contact_form(contact* buffer, bool viewing_only = false); @@ -124,6 +125,12 @@ static void ui_draw_vat_rates() is_adding_item = false; administration_tax_bracket_update(new_tax_bracket); + if (administration_writer_save_tax_brackets()) { + ui_set_status(localize("status.saved")); + } + else { + ui_set_status_error(localize("status.saveFailed")); + } ui_destroy_settings(); ui_setup_settings(); @@ -170,6 +177,12 @@ static void ui_draw_vat_rates() is_adding_item = false; administration_tax_bracket_add(new_tax_bracket); + if (administration_writer_save_tax_brackets()) { + ui_set_status(localize("status.saved")); + } + else { + ui_set_status_error(localize("status.saveFailed")); + } ui_destroy_settings(); ui_setup_settings(); @@ -226,6 +239,7 @@ static void ui_draw_cost_centers() is_adding_item = false; administration_cost_center_update(new_cost_center); + administration_writer_save_cost_centers(); memset(&new_cost_center, 0, sizeof(new_cost_center)); ui_destroy_settings(); @@ -280,6 +294,7 @@ static void ui_draw_cost_centers() is_adding_item = false; is_editing_item = false; administration_cost_center_add(new_cost_center); + administration_writer_save_cost_centers(); ui_destroy_settings(); ui_setup_settings(); |
