From 113719b26f0290effb7f0127aea4f9606a921741 Mon Sep 17 00:00:00 2001 From: Reim-developer Date: Sun, 17 Aug 2025 16:49:00 +0700 Subject: [PATCH 1/4] Implement FFI enum `InitDatabaseStatus` & FFI function `raw_init_clipboard_cache` with wrapper namespace `Lazyboard::ffi` and wrapper function `init_clipboard_cache` --- src/ffi/namespace/include/sqlite.hxx | 13 +++++++++++++ src/ffi/raw/sqlite.hxx | 24 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 src/ffi/namespace/include/sqlite.hxx create mode 100644 src/ffi/raw/sqlite.hxx diff --git a/src/ffi/namespace/include/sqlite.hxx b/src/ffi/namespace/include/sqlite.hxx new file mode 100644 index 0000000..12be185 --- /dev/null +++ b/src/ffi/namespace/include/sqlite.hxx @@ -0,0 +1,13 @@ +#ifndef SQLITE_HXX +#define SQLITE_HXX +#include "../../raw/sqlite.hxx" + +namespace Lazyboard::ffi { +inline auto init_clipboard_cache(const char *path) -> InitDatabaseStatus { + auto status = raw_init_clipboard_cache(path); + + return status; +} +} // namespace Lazyboard::ffi + +#endif // SQLITE_HXX \ No newline at end of file diff --git a/src/ffi/raw/sqlite.hxx b/src/ffi/raw/sqlite.hxx new file mode 100644 index 0000000..b44f626 --- /dev/null +++ b/src/ffi/raw/sqlite.hxx @@ -0,0 +1,24 @@ +#ifndef RAW_SQLITE_HXX +#define RAW_SQLITE_HXX + +#if defined(__cplusplus) +extern "C" { +#endif + +#include +using std::uint8_t; + +enum class InitDatabaseStatus : uint8_t { + OK, + CREATE_DATABASE_FAILED, + C_STR_CONVERT_FAILED, + EXECUTE_SQL_FAILED +}; + +auto raw_init_clipboard_cache(const char *path) -> InitDatabaseStatus; + +#if defined(__cplusplus) +} +#endif + +#endif // RAW_SQLITE_HXX \ No newline at end of file From 7c39038965b4790d936716717ecc3b02578d6c10 Mon Sep 17 00:00:00 2001 From: Reim-developer Date: Sun, 17 Aug 2025 17:03:37 +0700 Subject: [PATCH 2/4] Implement function `error_dialog_show` to error handling friendly --- src/front_end/theme_manager.cxx | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/front_end/theme_manager.cxx b/src/front_end/theme_manager.cxx index 8b118d7..24f2069 100644 --- a/src/front_end/theme_manager.cxx +++ b/src/front_end/theme_manager.cxx @@ -19,6 +19,7 @@ using std::cout; #endif using namespace Lazyboard::ffi; +using Lazyboard::front_end_utils::error_dialog_show; using Lazyboard::front_end_utils::error_to_string; using Lazyboard::front_end_utils::ErrorTypes; using Lazyboard::front_end_utils::is_valid_hex_color; @@ -51,14 +52,10 @@ auto Self::gui_settings(RawAppConfig *raw_app_config) noexcept -> GuiSettings { } void Self::on_invalid_hex_color_error(QMainWindow *main_window) { - // clang-format off - const auto error_name = error_to_string(ErrorTypes::INVALID_HEX_COLOR); - const auto error_message = QString(R"( -Invalid HEX color, please check your TOML configuration -Error name: %1 - )").arg(error_name.data()); // clang-format on + using E = ErrorTypes; - QMessageBox::critical(main_window, "Error", error_message); + const auto message = "Invalid HEX Color in your TOML configuration"; + error_dialog_show(main_window, message, E::INVALID_HEX_COLOR); } void Self::set_main_window_theme(QMainWindow *main_window, From e461f09e0fd6838fad09f4140606a2817b5c1e6b Mon Sep 17 00:00:00 2001 From: Reim-developer Date: Sun, 17 Aug 2025 17:03:57 +0700 Subject: [PATCH 3/4] Defined `errror_dialog_show` & add `noexcept` to some utils function --- src/front_end_utils/include/utils.hxx | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/front_end_utils/include/utils.hxx b/src/front_end_utils/include/utils.hxx index 604d311..7e96fd4 100644 --- a/src/front_end_utils/include/utils.hxx +++ b/src/front_end_utils/include/utils.hxx @@ -2,9 +2,12 @@ #define FRONT_END_UTILS_HXX #include +#include #include +#include #include #include +#include #include #include @@ -37,7 +40,7 @@ inline constexpr bool is_valid_hex_color(const Args&... args) noexcept { ...); } -inline QIcon image_from_bytes(const initializer_list& data) { +inline QIcon image_from_bytes(const initializer_list& data) noexcept { QByteArray bytes_array; bytes_array.reserve(static_cast(data.size())); @@ -53,6 +56,14 @@ inline QIcon image_from_bytes(const initializer_list& data) { return QIcon(); } +inline void error_dialog_show(QWidget* parent, const QString& message, + ErrorTypes error_types) noexcept { + auto error_string = error_to_string(error_types).data(); + auto fmt_msg = QString("%1\nError name: %2").arg(message).arg(error_string); + + QMessageBox::critical(parent, "Error", fmt_msg); +} + } // namespace Lazyboard::front_end_utils #endif \ No newline at end of file From fa7ae734fc338e784f13e13182ef966800e39e2e Mon Sep 17 00:00:00 2001 From: Reim-developer Date: Sun, 17 Aug 2025 17:27:08 +0700 Subject: [PATCH 4/4] Implement new error handling via `error_types.hxx` --- src/front_end/main_window_preload.cxx | 45 +++++++++------------ src/front_end/theme_manager.cxx | 3 +- src/front_end_utils/include/error_types.hxx | 27 ++++++++++--- src/front_end_utils/include/utils.hxx | 7 ++-- 4 files changed, 46 insertions(+), 36 deletions(-) diff --git a/src/front_end/main_window_preload.cxx b/src/front_end/main_window_preload.cxx index 1d190db..780ce25 100644 --- a/src/front_end/main_window_preload.cxx +++ b/src/front_end/main_window_preload.cxx @@ -20,6 +20,7 @@ using std::cout; #endif #include "../front_end_utils/include/error_types.hxx" +#include "../front_end_utils/include/utils.hxx" using Lazyboard::front_end::MainWindowPreload; using Self = MainWindowPreload; @@ -28,38 +29,36 @@ using std::make_unique; using std::string; using std::stringstream; using namespace Lazyboard::ffi; -using Lazyboard::front_end_utils::error_types_map; +using Lazyboard::front_end_utils::error_dialog_show; +using Lazyboard::front_end_utils::ErrorTypes; void Self::on_gen_default_cfg_error(WriteConfigStatus status, QMainWindow *main_window) { switch (status) { + using E = ErrorTypes; + case WriteConfigStatus::OK: break; case WriteConfigStatus::CREATE_DIR_FAILED: - QMessageBox::critical(main_window, "Error", - "Could not create config directory"); + error_dialog_show(main_window, E::CREATE_DIR_FAILED); break; case WriteConfigStatus::GET_CONFIG_DIR_FAILED: - QMessageBox::critical(main_window, "Error", - "Could not get application local data"); + error_dialog_show(main_window, E::GET_CONFIG_DIR_FAILED); break; case WriteConfigStatus::CREATE_FILE_FAILED: - QMessageBox::critical(main_window, "Error", - "Could not create configuration file"); + error_dialog_show(main_window, E::CREATE_FILE_FAILED); break; case WriteConfigStatus::WRITE_FILE_FAILED: - QMessageBox::critical(main_window, "Error", - "Could not write configuration"); + error_dialog_show(main_window, E::WRITE_FILE_FAILED); break; case WriteConfigStatus::TOML_TO_STRING_FAILED: - QMessageBox::critical(main_window, "Error", - "Could not convert TOML data to string"); + error_dialog_show(main_window, E::TOML_TO_STRING_FAILED); break; } } @@ -67,35 +66,30 @@ void Self::on_gen_default_cfg_error(WriteConfigStatus status, void Self::on_read_exists_cfg_error(RawReadAppConfigStatus status, QMainWindow *main_window) { using Status = RawReadAppConfigStatus; + using E = ErrorTypes; switch (status) { case Status::READ_OK: break; case Status::CONVERT_TO_MUT_FAILED: - QMessageBox::critical(main_window, "Error", - "Could not convert to *mut c_char"); + error_dialog_show(main_window, E::CONVERT_TO_MUT_FAILED); break; case Status::PARSE_TOML_FAILED: - QMessageBox::critical(main_window, "Error", - "Parse TOML configuration failed"); + error_dialog_show(main_window, E::PARSE_TOML_FAILED); break; case Status::READ_FILE_FAILED: - QMessageBox::critical(main_window, "Error", - "Could not read configuration file"); + error_dialog_show(main_window, E::READ_FILE_FAILED); break; case Status::UTF_8_ERROR: - QMessageBox::critical(main_window, "Error", - "UTF-8 error in configuration file"); + error_dialog_show(main_window, E::UTF_8_ERROR); break; case Status::CONVERT_TO_C_STR_FAILED: - QMessageBox::critical( - main_window, "Error", - "Could not convert value configuration to C string"); + error_dialog_show(main_window, E::CONVERT_TO_C_STR_FAILED); break; } } @@ -122,9 +116,8 @@ void Self::create_default_config(QMainWindow *main_window) { // clang-format off #if defined(LAZY_DEBUG) - cout << "[DEBUG] " << "Config path not found, generate at: " << config_path - << "\n"; - #endif + cout << "[DEBUG] " << "Config path not found, generate at: " << config_path << "\n"; + #endif return; } @@ -137,8 +130,8 @@ void Self::create_default_config(QMainWindow *main_window) { void Self::read_if_exists_config(QMainWindow *main_window) { raw_app_config = make_unique(); theme_manager = make_unique(); - auto config_path = this->application_config(); + auto config_path = this->application_config(); auto status = exists_config(config_path.data(), raw_app_config.get()); this->on_read_exists_cfg_error(status, main_window); diff --git a/src/front_end/theme_manager.cxx b/src/front_end/theme_manager.cxx index 24f2069..752239e 100644 --- a/src/front_end/theme_manager.cxx +++ b/src/front_end/theme_manager.cxx @@ -54,8 +54,7 @@ auto Self::gui_settings(RawAppConfig *raw_app_config) noexcept -> GuiSettings { void Self::on_invalid_hex_color_error(QMainWindow *main_window) { using E = ErrorTypes; - const auto message = "Invalid HEX Color in your TOML configuration"; - error_dialog_show(main_window, message, E::INVALID_HEX_COLOR); + error_dialog_show(main_window, E::INVALID_HEX_COLOR); } void Self::set_main_window_theme(QMainWindow *main_window, diff --git a/src/front_end_utils/include/error_types.hxx b/src/front_end_utils/include/error_types.hxx index 422646a..07c38f2 100644 --- a/src/front_end_utils/include/error_types.hxx +++ b/src/front_end_utils/include/error_types.hxx @@ -23,6 +23,12 @@ enum class ErrorTypes : uint8_t { GET_CONFIG_DIR_FAILED, CREATE_FILE_FAILED, TOML_TO_STRING_FAILED, + WRITE_FILE_FAILED, + CONVERT_TO_MUT_FAILED, + CONVERT_TO_C_STR_FAILED, + PARSE_TOML_FAILED, + READ_FILE_FAILED, + UTF_8_ERROR, }; using Types = initializer_list>; @@ -30,11 +36,22 @@ inline constexpr Types error_types_map() noexcept { using E = ErrorTypes; const Types error_types = { - pair{E::INVALID_HEX_COLOR, "INVALID_HEX_COLOR"}, - pair{E::CREATE_DIR_FAILED, "CREATE_DIR_FAILED"}, - pair{E::GET_CONFIG_DIR_FAILED, "GET_CONFIG_DIR_FAILED"}, - pair{E::CREATE_FILE_FAILED, "CREATE_FILE_FAILED"}, - pair{E::TOML_TO_STRING_FAILED, "TOML_TO_STRING_FAILED"}, + {E::INVALID_HEX_COLOR, + "Invalid HEX color, please check your TOML configuration and try " + "again"}, + {E::CREATE_DIR_FAILED, "Create directory failed"}, + {E::GET_CONFIG_DIR_FAILED, "Get configuration directory failed"}, + {E::CREATE_FILE_FAILED, "Create file faled"}, + {E::TOML_TO_STRING_FAILED, "Could not convert TOML to string"}, + {E::WRITE_FILE_FAILED, "Could not write file"}, + {E::CONVERT_TO_MUT_FAILED, "Could not convert value to '*mut c_char'"}, + {E::CONVERT_TO_C_STR_FAILED, "Could not convert value to 'c_str'"}, + {E::PARSE_TOML_FAILED, + "Could not parse TOML, please check your configuration and try " + "again"}, + {E::READ_FILE_FAILED, "Could not read file"}, + {E::UTF_8_ERROR, "UTF-8 error"}, + }; return error_types; diff --git a/src/front_end_utils/include/utils.hxx b/src/front_end_utils/include/utils.hxx index 7e96fd4..174709f 100644 --- a/src/front_end_utils/include/utils.hxx +++ b/src/front_end_utils/include/utils.hxx @@ -15,6 +15,7 @@ #include "error_types.hxx" +using std::abort; using std::initializer_list; using std::is_same_v; using std::stringstream; @@ -56,12 +57,12 @@ inline QIcon image_from_bytes(const initializer_list& data) noexcept { return QIcon(); } -inline void error_dialog_show(QWidget* parent, const QString& message, +inline void error_dialog_show(QWidget* parent, ErrorTypes error_types) noexcept { auto error_string = error_to_string(error_types).data(); - auto fmt_msg = QString("%1\nError name: %2").arg(message).arg(error_string); - QMessageBox::critical(parent, "Error", fmt_msg); + QMessageBox::critical(parent, "Error", error_string); + abort(); } } // namespace Lazyboard::front_end_utils