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
2 changes: 1 addition & 1 deletion .github/workflows/linux_dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
sudo apt-get update
sudo apt-get install -y \
git cmake make ccache \
clang
clang sqlite3

- name: Install Qt
uses: jurplel/install-qt-action@v4
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
compile_commands.json
build
.cache
.cache
*.db
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set(CMAKE_C_STANDARD_REQUIRED 99)

set(CMAKE_AUTOMOC ON)
find_package(Qt6 REQUIRED COMPONENTS Widgets Gui Core)
find_package(SQLite3 REQUIRED)

set(RUST_BACKEND_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/back_end)

Expand Down Expand Up @@ -35,6 +36,7 @@ target_link_libraries(
PRIVATE Qt6::Widgets
PRIVATE Qt6::Core
PRIVATE Qt6::Gui
PRIVATE SQLite::SQLite3
PRIVATE ${RUST_BACKEND_LIB}
)

Expand Down
2 changes: 1 addition & 1 deletion scripts/linux_deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ nproc=$(nproc)
install_dependency() {
sudo apt-get update
sudo apt-get install -y git cmake \
clang
clang sqlite3
}

function check() {
Expand Down
50 changes: 50 additions & 0 deletions src/back_end/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/back_end/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ panic = "abort"
[dependencies]
dirs = "6.0.0"
serde = { version = "1.0.219", features = ["derive"] }
sqlite = "0.37.0"
toml = "0.9.5"
webbrowser = "1.0.5"
1 change: 1 addition & 0 deletions src/back_end/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod utils {
pub mod browser;
pub mod fs_utils;
pub mod memory;
pub mod sqlite;
}

pub mod internal {
Expand Down
48 changes: 48 additions & 0 deletions src/back_end/src/utils/sqlite.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use sqlite::Connection;
use std::ffi::{CStr, c_char};

#[repr(C)]
pub enum InitDatabaseStatus {
Ok,
CreateDatabaseFailed,
CStrConvertFailed,
ExecuteSqlFailed,
}

const INIT_QUERY: &str = r"--sql
CREATE TABLE IF NOT EXISTS clipboard_cache (
id INTEGER PRIMARY KEY AUTOINCREMENT,
content_hash TEXT NOT NULL UNIQUE,
time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
content TEXT NOT NULL,
type TEXT NOT NULL,
is_pinned BOOLEAN NOT NULL DEFAULT FALSE
);
";

#[unsafe(no_mangle)]
/// # Safety
/// Careful with unsafe context & raw pointers.
pub unsafe extern "C" fn raw_init_clipboard_cache(
file_path: *const c_char,
) -> InitDatabaseStatus {
unsafe {
use InitDatabaseStatus as Status;

let Ok(file_path_cstr) = CStr::from_ptr(file_path).to_str() else {
return Status::CStrConvertFailed;
};

let Ok(sql) = Connection::open(file_path_cstr) else {
return Status::CreateDatabaseFailed;
};

let is_err = sql.execute(INIT_QUERY).is_err();

if is_err {
return Status::ExecuteSqlFailed;
}

Status::Ok
}
}
27 changes: 27 additions & 0 deletions tests-ffi/sqlite.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

#include <cassert>
#include <cstdint>
using std::uint8_t;

extern "C" enum class InitDatabaseStatus : uint8_t {
OK,
CREATE_DATABASE_FAILED,
C_STR_CONVERT_FAILED,
EXECUTE_SQL_FAILED
};

extern "C" InitDatabaseStatus raw_init_clipboard_cache(const char* path);

int main() {
using Status = InitDatabaseStatus;

const char* test_path = "test_init.db";
auto status = raw_init_clipboard_cache(test_path);

assert(status != Status::CREATE_DATABASE_FAILED);
assert(status != Status::C_STR_CONVERT_FAILED);
assert(status != Status::EXECUTE_SQL_FAILED);
assert(status == Status::OK);

return 0;
}
3 changes: 2 additions & 1 deletion tests-ffi/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function run_ffi_test() {
local clang_cxx="clang++"
local static_lib_path="../src/back_end/target/debug/libback_end.a"
local build_dir="build"
local sqlite_lib="sqlite3"
check "$clang_cxx"

echo -e "\e[0;32m[+] Build backend:\e[0;37m"
Expand All @@ -46,7 +47,7 @@ function run_ffi_test() {
build_output=$(basename "$file" ".cxx")
file_name=$(basename "$file")

$clang_cxx "$file" "$static_lib_path" -o "$build_dir/$build_output"
$clang_cxx "$file" -l"$sqlite_lib" "$static_lib_path" -o "$build_dir/$build_output"
echo
echo -e -n "\e[0;32m[+] Test for $file_name:\e[0;37m"

Expand Down