From f98b0e55e948349cc9256dfe18262e18cf45d917 Mon Sep 17 00:00:00 2001 From: yggverse Date: Sun, 8 Sep 2024 22:13:52 +0300 Subject: [PATCH] delegate specific database operations to widgets, use SQLiteCpp wrapper --- Makefile | 6 ++---- README.md | 6 ++++-- po/POTFILES.in | 6 ++---- src/app.cpp | 40 +++++++++++++++++++++++++++++++++++ src/{main.hpp => app.hpp} | 1 + src/app/browser.cpp | 3 ++- src/app/browser.hpp | 3 ++- src/app/browser/main.cpp | 2 ++ src/app/browser/main.hpp | 2 ++ src/app/browser/main/tab.cpp | 13 ++++++++++++ src/app/browser/main/tab.hpp | 2 ++ src/lib/database.cpp | 17 --------------- src/lib/database.hpp | 31 --------------------------- src/lib/database/session.cpp | 22 ------------------- src/lib/database/session.hpp | 24 --------------------- src/main.cpp | 41 ------------------------------------ 16 files changed, 72 insertions(+), 147 deletions(-) create mode 100644 src/app.cpp rename src/{main.hpp => app.hpp} (74%) delete mode 100644 src/lib/database.cpp delete mode 100644 src/lib/database.hpp delete mode 100644 src/lib/database/session.cpp delete mode 100644 src/lib/database/session.hpp delete mode 100644 src/main.cpp diff --git a/Makefile b/Makefile index 49696069..7e909053 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ LDFLAGS = `pkg-config --libs gio-2.0 glibmm-2.68 gtkmm-4.0 pangomm-2.48 sqlite3` # Define target executable and source files TARGET = bin/Yoda -SRCS = src/main.cpp\ +SRCS = src/app.cpp\ src/app/browser.cpp\ src/app/browser/header.cpp\ src/app/browser/header/main.cpp\ @@ -27,9 +27,7 @@ SRCS = src/main.cpp\ src/app/browser/main/tab/page/navigation/history/forward.cpp\ src/app/browser/main/tab/page/navigation/request.cpp\ src/app/browser/main/tab/page/navigation/update.cpp\ - src/app/browser/main/tab/label.cpp\ - src/lib/database.cpp\ - src/lib/database/session.cpp + src/app/browser/main/tab/label.cpp OBJS = $(SRCS:.cpp=.o) diff --git a/README.md b/README.md index 10c905ed..94d15b16 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,8 @@ apt install git\ libglibmm-2.68-dev\ libgtkmm-4.0-dev\ libpangomm-2.48-dev\ - libsqlite3-dev + libsqlite3-dev\ + libsqlitecpp-dev ``` * `git clone https://github.com/YGGverse/Yoda.git` @@ -67,4 +68,5 @@ pkg-config --cflags --libs gio-2.0\ * [GTK](https://gtk.org) - free and open-source cross-platform widget toolkit * [gtkmm](https://gtkmm.org) - official C++ interface for GTK -* [SQLite](https://sqlite.org) - profile database \ No newline at end of file +* [SQLite](https://sqlite.org) - profile database + * [SQLiteCpp](https://github.com/SRombauts/SQLiteCpp) - SQLite3 C++ wrapper \ No newline at end of file diff --git a/po/POTFILES.in b/po/POTFILES.in index b27b62e1..35842d24 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -1,3 +1,4 @@ +src/app.cpp src/app/browser.cpp src/app/browser/header.cpp src/app/browser/header/main.cpp @@ -21,7 +22,4 @@ src/app/browser/main/tab/page/navigation/history/back.cpp src/app/browser/main/tab/page/navigation/history/forward.cpp src/app/browser/main/tab/page/navigation/request.cpp src/app/browser/main/tab/page/navigation/update.cpp -src/app/browser/main/tab/label.cpp -src/lib/database.cpp -src/lib/database/session.cpp -src/main.cpp +src/app/browser/main/tab/label.cpp \ No newline at end of file diff --git a/src/app.cpp b/src/app.cpp new file mode 100644 index 00000000..26284d11 --- /dev/null +++ b/src/app.cpp @@ -0,0 +1,40 @@ +#include "app.hpp" +#include "app/browser.hpp" + +int main( + int argc, + char * argv[] +) { + // Init database + SQLite::Database db( + "database.sqlite3", + SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE + ); + + // Init application + auto app = Gtk::Application::create( + "io.github.yggverse.Yoda" + ); + + // Init actions + app->add_action( + "quit", + [app] + { + app->quit(); + } + ); + + app->set_accel_for_action( + "app.quit", + "q" + ); + + // Launch browser component + return app->make_window_and_run( + argc, + argv, + db, + app + ); +} \ No newline at end of file diff --git a/src/main.hpp b/src/app.hpp similarity index 74% rename from src/main.hpp rename to src/app.hpp index 223189ae..7d28e39b 100644 --- a/src/main.hpp +++ b/src/app.hpp @@ -1,5 +1,6 @@ #include #include +#include int main( int argc, diff --git a/src/app/browser.cpp b/src/app/browser.cpp index ed9de345..673833b7 100644 --- a/src/app/browser.cpp +++ b/src/app/browser.cpp @@ -5,8 +5,8 @@ using namespace app; Browser::Browser( + SQLite::Database & db, const Glib::RefPtr & APP - //const std::shared_ptr & db ) { // Init window actions const auto ACTION__REFRESH = add_action( @@ -177,6 +177,7 @@ Browser::Browser( ); browserMain = Gtk::make_managed( + db, ACTION__REFRESH, ACTION__MAIN_TAB_CLOSE_ACTIVE, ACTION__MAIN_TAB_CLOSE_ALL, diff --git a/src/app/browser.hpp b/src/app/browser.hpp index bf508060..33c3cfa2 100644 --- a/src/app/browser.hpp +++ b/src/app/browser.hpp @@ -7,6 +7,7 @@ #include #include #include +#include namespace lib { @@ -34,8 +35,8 @@ namespace app public: Browser( + SQLite::Database & db, const Glib::RefPtr & APP - //const std::shared_ptr & db ); }; } diff --git a/src/app/browser/main.cpp b/src/app/browser/main.cpp index b40ca349..e7b86967 100644 --- a/src/app/browser/main.cpp +++ b/src/app/browser/main.cpp @@ -4,6 +4,7 @@ using namespace app::browser; Main::Main( + SQLite::Database & db, const Glib::RefPtr & ACTION__REFRESH, const Glib::RefPtr & ACTION__MAIN_TAB_CLOSE_ACTIVE, const Glib::RefPtr & ACTION__MAIN_TAB_CLOSE_ALL, @@ -22,6 +23,7 @@ Main::Main( // Init components mainTab = Gtk::make_managed( + db, ACTION__REFRESH, ACTION__MAIN_TAB_CLOSE_ACTIVE, ACTION__MAIN_TAB_CLOSE_ALL, diff --git a/src/app/browser/main.hpp b/src/app/browser/main.hpp index c4fe9cf8..d5b15301 100644 --- a/src/app/browser/main.hpp +++ b/src/app/browser/main.hpp @@ -7,6 +7,7 @@ #include #include #include +#include namespace app::browser { @@ -26,6 +27,7 @@ namespace app::browser public: Main( + SQLite::Database & db, const Glib::RefPtr & ACTION__REFRESH, const Glib::RefPtr & ACTION__MAIN_TAB_CLOSE_ACTIVE, const Glib::RefPtr & ACTION__MAIN_TAB_CLOSE_ALL, diff --git a/src/app/browser/main/tab.cpp b/src/app/browser/main/tab.cpp index 852b4457..e0c7c68c 100644 --- a/src/app/browser/main/tab.cpp +++ b/src/app/browser/main/tab.cpp @@ -5,6 +5,7 @@ using namespace app::browser::main; Tab::Tab( + SQLite::Database & db, const Glib::RefPtr & ACTION__REFRESH, const Glib::RefPtr & ACTION__TAB_CLOSE_ACTIVE, const Glib::RefPtr & ACTION__MAIN_TAB_CLOSE_ALL, @@ -12,6 +13,18 @@ Tab::Tab( const Glib::RefPtr & ACTION__TAB_PAGE_NAVIGATION_HISTORY_FORWARD, const Glib::RefPtr & ACTION__TAB_PAGE_NAVIGATION_UPDATE ) { + // Init database + db.exec( + R"SQL( + CREATE TABLE IF NOT EXISTS `app_browser_tab` + ( + `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + `time` INTEGER NOT NULL, + `request` VARCHAR(1024) + ) + )SQL" + ); + // Init actions action__refresh = ACTION__REFRESH; action__tab_close_active = ACTION__TAB_CLOSE_ACTIVE; diff --git a/src/app/browser/main/tab.hpp b/src/app/browser/main/tab.hpp index 2641d29c..e1f013a8 100644 --- a/src/app/browser/main/tab.hpp +++ b/src/app/browser/main/tab.hpp @@ -6,6 +6,7 @@ #include #include #include +#include namespace app::browser::main { @@ -41,6 +42,7 @@ namespace app::browser::main public: Tab( + SQLite::Database & db, const Glib::RefPtr & ACTION__REFRESH, const Glib::RefPtr & ACTION__TAB_CLOSE_ACTIVE, const Glib::RefPtr & ACTION__MAIN_TAB_CLOSE_ALL, diff --git a/src/lib/database.cpp b/src/lib/database.cpp deleted file mode 100644 index 4ed74f18..00000000 --- a/src/lib/database.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include "database.hpp" -#include "database/session.hpp" - -using namespace lib; - -Database::Database( - const char * filename -) { - status = sqlite3_open( - filename, - &connection - ); - - session = new database::Session( - connection - ); -} \ No newline at end of file diff --git a/src/lib/database.hpp b/src/lib/database.hpp deleted file mode 100644 index a379605b..00000000 --- a/src/lib/database.hpp +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef LIB_DATABASE_HPP -#define LIB_DATABASE_HPP - -#include - -namespace lib -{ - namespace database - { - class Session; - } - - class Database - { - int status; - - char * error; - - sqlite3 * connection; - - public: - - database::Session * session; - - Database( - const char * filename - ); - }; -} - -#endif // LIB_DATABASE_HPP \ No newline at end of file diff --git a/src/lib/database/session.cpp b/src/lib/database/session.cpp deleted file mode 100644 index 3a79d406..00000000 --- a/src/lib/database/session.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include "session.hpp" - -using namespace lib::database; - -Session::Session( - sqlite3 * connection -) { - status = sqlite3_exec( - connection, - R"SQL( - CREATE TABLE IF NOT EXISTS `session` - ( - `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, - `time` INTEGER NOT NULL, - `request` VARCHAR(1024) - ) - )SQL", - nullptr, - nullptr, - &error - ); -} \ No newline at end of file diff --git a/src/lib/database/session.hpp b/src/lib/database/session.hpp deleted file mode 100644 index 1f61413b..00000000 --- a/src/lib/database/session.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef LIB_DATABASE_SESSION_HPP -#define LIB_DATABASE_SESSION_HPP - -#include - -namespace lib::database -{ - class Session - { - int status; - - char * error; - - sqlite3 * connection; - - public: - - Session( - sqlite3 * connection - ); - }; -} - -#endif // LIB_DATABASE_SESSION_HPP \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp deleted file mode 100644 index c956f201..00000000 --- a/src/main.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include "main.hpp" -#include "app/browser.hpp" -#include "lib/database.hpp" - -int main( - int argc, - char * argv[] -) { - // Init profile database - const std::shared_ptr DB( // @TODO - new lib::Database( - "database.sqlite3" - ) - ); - - // Init app - const Glib::RefPtr APP = Gtk::Application::create( - "io.github.yggverse.Yoda" - ); - - APP->add_action( - "quit", - [APP] - { - APP->quit(); - } - ); - - APP->set_accel_for_action( - "app.quit", - "q" - ); - - // Launch browser component - return APP->make_window_and_run( - argc, - argv, - APP - // DB - ); -} \ No newline at end of file