Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/ffi/namespace/include/sqlite.hxx
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions src/ffi/raw/sqlite.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef RAW_SQLITE_HXX
#define RAW_SQLITE_HXX

#if defined(__cplusplus)
extern "C" {
#endif

#include <cstdint>
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
45 changes: 19 additions & 26 deletions src/front_end/main_window_preload.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,74 +29,67 @@ 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;
}
}

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;
}
}
Expand All @@ -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;
}
Expand All @@ -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<RawAppConfig>();
theme_manager = make_unique<ThemeManager>();
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);

Expand Down
10 changes: 3 additions & 7 deletions src/front_end/theme_manager.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,14 +52,9 @@ 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);
error_dialog_show(main_window, E::INVALID_HEX_COLOR);
}

void Self::set_main_window_theme(QMainWindow *main_window,
Expand Down
27 changes: 22 additions & 5 deletions src/front_end_utils/include/error_types.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,35 @@ 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<pair<ErrorTypes, string_view>>;
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;
Expand Down
14 changes: 13 additions & 1 deletion src/front_end_utils/include/utils.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@
#define FRONT_END_UTILS_HXX

#include <qcolor.h>
#include <qcontainerfwd.h>
#include <qicon.h>
#include <qmessagebox.h>
#include <qpixmap.h>
#include <qstringview.h>
#include <qwidget.h>

#include <initializer_list>
#include <type_traits>
#include <vector>

#include "error_types.hxx"

using std::abort;
using std::initializer_list;
using std::is_same_v;
using std::stringstream;
Expand All @@ -37,7 +41,7 @@ inline constexpr bool is_valid_hex_color(const Args&... args) noexcept {
...);
}

inline QIcon image_from_bytes(const initializer_list<uint8_t>& data) {
inline QIcon image_from_bytes(const initializer_list<uint8_t>& data) noexcept {
QByteArray bytes_array;
bytes_array.reserve(static_cast<int>(data.size()));

Expand All @@ -53,6 +57,14 @@ inline QIcon image_from_bytes(const initializer_list<uint8_t>& data) {
return QIcon();
}

inline void error_dialog_show(QWidget* parent,
ErrorTypes error_types) noexcept {
auto error_string = error_to_string(error_types).data();

QMessageBox::critical(parent, "Error", error_string);
abort();
}

} // namespace Lazyboard::front_end_utils

#endif