Skip to content

Commit 4908c3d

Browse files
Merge pull request #58 from Reim-developer/dev
Implement `SQLiteManager` class
2 parents ba7c985 + e96a94e commit 4908c3d

8 files changed

Lines changed: 137 additions & 5 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 3.19)
22
project(Lazyboard)
33

4-
set(CMAKE_CXX_STANDARD 20)
4+
set(CMAKE_CXX_STANDARD 23)
55
set(CMAKE_CXX_STANDARD_REQUIRED ON)
66

77
set(CMAKE_AUTOMOC ON)

src/ffi/namespace/include/utils.hxx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@ inline bool is_exists_path(char *path) {
2121
return result;
2222
}
2323

24+
inline char *cache_dir() {
25+
auto cache_dir = raw_cache_dir();
26+
27+
return cache_dir;
28+
}
29+
2430
inline void free_c_str(char *c_str) { raw_free_c_str(c_str); }
31+
2532
} // namespace Lazyboard::ffi
2633

2734
#endif // UTILS_HXX

src/ffi/raw/utils.hxx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ enum class CreateFileSystemStatus : uint8_t {
2323

2424
OpenBrowserStatus raw_open_browser(const char *url);
2525
CreateFileSystemStatus raw_new_folder(const char *path);
26+
char *raw_cache_dir();
2627
bool raw_is_path_exists(const char *path);
2728
void raw_free_c_str(char *c_str);
2829

src/front_end/include/main_window.hxx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
#include <memory>
99
#include <string>
1010

11+
#include "../../front_end_db/include/sqlite_manager.hxx"
1112
#include "about_widget.hxx"
1213
#include "main_window_preload.hxx"
1314
#include "setting_widget.hxx"
1415
#include "table_widget.hxx"
1516

17+
using Lazyboard::front_end_db::SQLiteManager;
1618
using std::string;
1719
using std::unique_ptr;
1820

@@ -40,6 +42,7 @@ class MainWindow {
4042
unique_ptr<SettingWidget> setting_widget;
4143
unique_ptr<AboutWidget> about_widget;
4244
unique_ptr<MainWindowPreload> main_window_preload;
45+
unique_ptr<SQLiteManager> sqlite_manager;
4346

4447
public:
4548
Self *init_main_window();

src/front_end/main_window.cxx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ using Self = MainWindow;
2323
Self::MainWindow() {
2424
main_window = make_unique<QMainWindow>();
2525
main_window_preload = make_unique<MainWindowPreload>();
26+
sqlite_manager = make_unique<SQLiteManager>();
27+
2628
auto central_w = make_unique<QWidget>();
2729
auto layout_w = make_unique<QGridLayout>(central_w.get());
2830

@@ -36,8 +38,11 @@ Self *Self::init_main_window() {
3638
main_window->setMinimumSize(MIN_WIDTH, MIN_HEIGHT);
3739
main_window->setWindowTitle("Lazyboard");
3840
main_window->setWindowIcon(image_from_bytes(image_bytes));
41+
3942
main_window_preload->create_default_config(main_window.get());
4043
main_window_preload->read_if_exists_config(main_window.get());
44+
sqlite_manager->create_clipboard_cache(main_window.get());
45+
4146
this->front_end_show();
4247
main_window->show();
4348

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef SQLITE_MANAGER_HXX
2+
#define SQLITE_MANAGER_HXX
3+
#include <qmainwindow.h>
4+
5+
#include "../../ffi/raw/sqlite.hxx"
6+
#include "../../ffi/raw/utils.hxx"
7+
8+
namespace Lazyboard::front_end_db {
9+
class SQLiteManager {
10+
private:
11+
using InitDataResult = InitDatabaseStatus;
12+
using MkdirResult = CreateFileSystemStatus;
13+
14+
void on_create_folder_error(const MkdirResult &result) noexcept;
15+
void on_create_clipboard_cache_error(const InitDataResult &status) noexcept;
16+
17+
public:
18+
void create_clipboard_cache(QMainWindow *main_window);
19+
20+
private:
21+
QMainWindow *_main_window;
22+
};
23+
} // namespace Lazyboard::front_end_db
24+
25+
#endif // SQLITE_MANAGER_HXX
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include "include/sqlite_manager.hxx"
2+
3+
#include <qmainwindow.h>
4+
5+
#include <format>
6+
#include <string>
7+
8+
#include "../ffi/namespace/include/sqlite.hxx"
9+
#include "../ffi/namespace/include/utils.hxx"
10+
#include "../front_end_utils/include/utils.hxx"
11+
12+
#if defined(LAZY_DEBUG)
13+
#include <iostream>
14+
using std::cout;
15+
#endif
16+
17+
using Lazyboard::ffi::cache_dir;
18+
using Lazyboard::ffi::free_c_str;
19+
using Lazyboard::ffi::init_clipboard_cache;
20+
using Lazyboard::ffi::new_folder;
21+
using Lazyboard::front_end_db::SQLiteManager;
22+
using Lazyboard::front_end_utils::error_dialog_show;
23+
using Lazyboard::front_end_utils::ErrorTypes;
24+
using Self = SQLiteManager;
25+
using std::format;
26+
using std::string;
27+
using Status = InitDatabaseStatus;
28+
29+
void Self::on_create_clipboard_cache_error(
30+
const InitDataResult& result) noexcept {
31+
using E = ErrorTypes;
32+
33+
switch (result) {
34+
case InitDataResult::OK:
35+
break;
36+
37+
case InitDataResult::C_STR_CONVERT_FAILED:
38+
error_dialog_show(this->_main_window, E::CONVERT_TO_C_STR_FAILED);
39+
break;
40+
41+
case InitDataResult::CREATE_DATABASE_FAILED:
42+
error_dialog_show(this->_main_window, E::CREATE_DATABASE_ERR);
43+
break;
44+
45+
case InitDataResult::EXECUTE_SQL_FAILED:
46+
error_dialog_show(this->_main_window, E::EXECUTE_SQL_ERR);
47+
}
48+
}
49+
50+
void Self::on_create_folder_error(const MkdirResult& result) noexcept {
51+
using E = ErrorTypes;
52+
using Result = MkdirResult;
53+
54+
switch (result) {
55+
case Result::OK:
56+
break;
57+
58+
case Result::WRAP_RAW_C_FAILED:
59+
error_dialog_show(this->_main_window, E::WRAP_C_STR_ERR);
60+
break;
61+
62+
case Result::FAILED:
63+
error_dialog_show(this->_main_window, E::CREATE_DIR_FAILED);
64+
break;
65+
}
66+
}
67+
68+
void Self::create_clipboard_cache(QMainWindow* main_window) {
69+
this->_main_window = main_window;
70+
71+
auto raw_cache_directory = cache_dir();
72+
string cache_dir_string = format("{}/Lazyboard", raw_cache_directory);
73+
auto database_path = format("{}/clipboard_cache.db", cache_dir_string);
74+
free_c_str(raw_cache_directory);
75+
76+
const auto create_dir_result = new_folder(cache_dir_string.data());
77+
on_create_folder_error(create_dir_result);
78+
79+
const auto status = init_clipboard_cache(database_path.data());
80+
on_create_clipboard_cache_error(status);
81+
82+
// clang-format off
83+
#if defined (LAZY_DEBUG)
84+
cout << "Found cache directory path: " << database_path << "\n";
85+
#endif // clang-format on
86+
}

src/front_end_utils/include/error_types.hxx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,16 @@ enum class ErrorTypes : uint8_t {
2929
PARSE_TOML_FAILED,
3030
READ_FILE_FAILED,
3131
UTF_8_ERROR,
32+
CREATE_DATABASE_ERR,
33+
EXECUTE_SQL_ERR,
34+
WRAP_C_STR_ERR,
3235
};
3336

3437
using Types = initializer_list<pair<ErrorTypes, string_view>>;
3538
inline constexpr Types error_types_map() noexcept {
3639
using E = ErrorTypes;
3740

38-
const Types error_types = {
41+
static constexpr Types ERROR_TYPES = {
3942
{E::INVALID_HEX_COLOR,
4043
"Invalid HEX color, please check your TOML configuration and try "
4144
"again"},
@@ -51,10 +54,12 @@ inline constexpr Types error_types_map() noexcept {
5154
"again"},
5255
{E::READ_FILE_FAILED, "Could not read file"},
5356
{E::UTF_8_ERROR, "UTF-8 error"},
54-
57+
{E::EXECUTE_SQL_ERR, "Could not execute SQLite query"},
58+
{E::CREATE_DATABASE_ERR, "Could not create database"},
59+
{E::WRAP_C_STR_ERR, "Could not wrap 'c_str'"},
5560
};
5661

57-
return error_types;
62+
return ERROR_TYPES;
5863
}
5964

6065
inline constexpr string_view error_to_string(ErrorTypes error) noexcept {
@@ -64,7 +69,7 @@ inline constexpr string_view error_to_string(ErrorTypes error) noexcept {
6469
}
6570
}
6671

67-
return "UNKOWN_ERROR";
72+
return "UNKNOWN_ERROR";
6873
}
6974

7075
} // namespace Lazyboard::front_end_utils

0 commit comments

Comments
 (0)