From d66692a42afcacc134d5226620d6fb3af007d72f Mon Sep 17 00:00:00 2001 From: yggverse Date: Tue, 17 Sep 2024 08:29:45 +0300 Subject: [PATCH] implement label title object storage --- src/app/browser/main/tab/label.cpp | 46 ++++-- src/app/browser/main/tab/label.hpp | 4 +- src/app/browser/main/tab/label/title.cpp | 196 ++++++++++++++++++++++- src/app/browser/main/tab/label/title.hpp | 61 ++++++- 4 files changed, 287 insertions(+), 20 deletions(-) diff --git a/src/app/browser/main/tab/label.cpp b/src/app/browser/main/tab/label.cpp index 81f7b94b..f2f3d3ae 100644 --- a/src/app/browser/main/tab/label.cpp +++ b/src/app/browser/main/tab/label.cpp @@ -37,7 +37,9 @@ Label::Label( * labelPin ); - labelTitle = Gtk::make_managed(); + labelTitle = Gtk::make_managed( + database + ); set_tooltip_text( labelTitle->get_text() @@ -129,8 +131,13 @@ int Label::session_restore( ) == 1 ); - // Restore children components here (on available) - // @TODO + // Restore children components + labelTitle->session_restore( + sqlite3_column_int64( + statement, + Database::Session::ID + ) + ); } } @@ -139,17 +146,23 @@ int Label::session_restore( ); } -int Label::session_save( +sqlite3_int64 Label::session_save( const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID ) { - // Delegate save action to child components (on available) - - // Save label session - return Database::Session::add( + // Create new session + const sqlite3_int64 APP_BROWSER_MAIN_TAB_LABEL__SESSION__ID = Database::Session::add( database, APP_BROWSER_MAIN_TAB__SESSION__ID, is_pinned ); + + // Delegate save action to child components + labelTitle->session_save( + APP_BROWSER_MAIN_TAB_LABEL__SESSION__ID + ); + + // Return ID + return APP_BROWSER_MAIN_TAB_LABEL__SESSION__ID; } void Label::pin() @@ -185,7 +198,7 @@ void Label::update( TITLE ); - labelTitle->set_text( + labelTitle->update( TITLE ); } @@ -251,6 +264,11 @@ int Label::Database::Session::clean( { while (sqlite3_step(statement) == SQLITE_ROW) { + const sqlite3_int64 APP_BROWSER_MAIN_TAB_LABEL__SESSION__ID = sqlite3_column_int64( + statement, + Database::Session::ID + ); + // Delete record const int EXEC_STATUS = sqlite3_exec( database, @@ -258,10 +276,7 @@ int Label::Database::Session::clean( R"SQL( DELETE FROM `app_browser_main_tab_label__session` WHERE `id` = %d )SQL", - sqlite3_column_int64( - statement, - Database::Session::ID - ) + APP_BROWSER_MAIN_TAB_LABEL__SESSION__ID ).c_str(), nullptr, nullptr, @@ -271,7 +286,10 @@ int Label::Database::Session::clean( // Delegate children dependencies cleanup if (EXEC_STATUS == SQLITE_OK) { - // nothing here. + label::Title::Database::Session::clean( + database, + APP_BROWSER_MAIN_TAB_LABEL__SESSION__ID + ); } } } diff --git a/src/app/browser/main/tab/label.hpp b/src/app/browser/main/tab/label.hpp index a859c9d0..69c72192 100644 --- a/src/app/browser/main/tab/label.hpp +++ b/src/app/browser/main/tab/label.hpp @@ -90,9 +90,9 @@ namespace app::browser::main::tab const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID ); // return sqlite3_finalize status code - int session_save( + sqlite3_int64 session_save( const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID - ); // return sqlite3_finalize status code + ); // return sqlite3_last_insert_rowid void pin(); diff --git a/src/app/browser/main/tab/label/title.cpp b/src/app/browser/main/tab/label/title.cpp index 010e2a8a..bb8f666b 100644 --- a/src/app/browser/main/tab/label/title.cpp +++ b/src/app/browser/main/tab/label/title.cpp @@ -2,10 +2,20 @@ using namespace app::browser::main::tab::label; -Title::Title() -{ +Title::Title( + sqlite3 * database +) { + // Init database + Database::Session::init( + this->database = database + ); + + // Init extras + text = _("New page"); + + // Init widget set_text( - _("New page") + text ); set_ellipsize( @@ -19,4 +29,184 @@ Title::Title() set_single_line_mode( true ); +} + +// Actions +int Title::session_restore( + const sqlite3_int64 & APP_BROWSER_MAIN_TAB_LABEL__SESSION__ID +) { + sqlite3_stmt * statement; + + const int PREPARE_STATUS = sqlite3_prepare_v3( + database, + Glib::ustring::sprintf( + R"SQL( + SELECT * FROM `app_browser_main_tab_label_title__session` + WHERE `app_browser_main_tab_label__session__id` = %d + ORDER BY `id` DESC LIMIT 1 + )SQL", + APP_BROWSER_MAIN_TAB_LABEL__SESSION__ID + ).c_str(), + -1, + SQLITE_PREPARE_NORMALIZE, + &statement, + nullptr + ); + + if (PREPARE_STATUS == SQLITE_OK) + { + // Restore label text from latest database record + while (sqlite3_step(statement) == SQLITE_ROW) + { + // Restore widget data + update( + reinterpret_cast( + sqlite3_column_text( + statement, + Database::Session::TEXT + ) + ) + ); + + // Restore children components here (on available) + } + } + + return sqlite3_finalize( + statement + ); +} + +sqlite3_int64 Title::session_save( + const sqlite3_int64 & APP_BROWSER_MAIN_TAB_LABEL__SESSION__ID +) { + // Create new session record + const sqlite3_int64 APP_BROWSER_MAIN_TAB_LABEL_TITLE__SESSION__ID = Database::Session::add( + database, + APP_BROWSER_MAIN_TAB_LABEL__SESSION__ID, + text + ); + + // Delegate save action to child components (on available) + + // Return ID + return APP_BROWSER_MAIN_TAB_LABEL_TITLE__SESSION__ID; +} + +void Title::update( + const Glib::ustring & TEXT +) { + set_text( + text = TEXT + ); +} + +// Database model +int Title::Database::Session::init( + sqlite3 * database +) { + char * error; + + return sqlite3_exec( + database, + R"SQL( + CREATE TABLE IF NOT EXISTS `app_browser_main_tab_label_title__session` + ( + `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `app_browser_main_tab_label__session__id` INTEGER NOT NULL, + `time` INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP, + `text` VARCHAR(1024) NOT NULL + ) + )SQL", + nullptr, + nullptr, + &error + ); +} + +int Title::Database::Session::clean( + sqlite3 * database, + const sqlite3_int64 & APP_BROWSER_MAIN_TAB_LABEL__SESSION__ID +) { + char * error; // @TODO + sqlite3_stmt * statement; + + const int PREPARE_STATUS = sqlite3_prepare_v3( + database, + Glib::ustring::sprintf( + R"SQL( + SELECT * FROM `app_browser_main_tab_label_title__session` + WHERE `app_browser_main_tab_label__session__id` = %d + )SQL", + APP_BROWSER_MAIN_TAB_LABEL__SESSION__ID + ).c_str(), + -1, + SQLITE_PREPARE_NORMALIZE, + &statement, + nullptr + ); + + if (PREPARE_STATUS == SQLITE_OK) + { + while (sqlite3_step(statement) == SQLITE_ROW) + { + // Delete record + const int EXEC_STATUS = sqlite3_exec( + database, + Glib::ustring::sprintf( + R"SQL( + DELETE FROM `app_browser_main_tab_label_title__session` WHERE `id` = %d + )SQL", + sqlite3_column_int64( + statement, + Database::Session::ID + ) + ).c_str(), + nullptr, + nullptr, + &error + ); + + // Delegate children dependencies cleanup + if (EXEC_STATUS == SQLITE_OK) + { + // nothing here. + } + } + } + + return sqlite3_finalize( + statement + ); +} + +sqlite3_int64 Title::Database::Session::add( + sqlite3 * database, + const sqlite3_int64 & APP_BROWSER_MAIN_TAB_LABEL__SESSION__ID, + Glib::ustring & TEXT +) { + char * error; // @TODO + + sqlite3_exec( + database, + Glib::ustring::sprintf( + R"SQL( + INSERT INTO `app_browser_main_tab_label_title__session` ( + `app_browser_main_tab_label__session__id`, + `text` + ) VALUES ( + '%d', + '%s' + ) + )SQL", + APP_BROWSER_MAIN_TAB_LABEL__SESSION__ID, + TEXT + ).c_str(), + nullptr, + nullptr, + &error + ); + + return sqlite3_last_insert_rowid( + database + ); } \ No newline at end of file diff --git a/src/app/browser/main/tab/label/title.hpp b/src/app/browser/main/tab/label/title.hpp index 8f5d55ec..268bfb03 100644 --- a/src/app/browser/main/tab/label/title.hpp +++ b/src/app/browser/main/tab/label/title.hpp @@ -5,16 +5,60 @@ #include #include #include +#include namespace app::browser::main::tab::label { class Title : public Gtk::Label { + public: + + /* + * Class database + * + * Allowed parental access to enums and relationship methods + */ + struct Database + { + // app_browser_main_tab_label_title__* + struct Session + { + enum + { + ID, + APP_BROWSER_MAIN_TAB_LABEL__SESSION__ID, + TIME, + TEXT + }; // table fields index + + static int init( + sqlite3 * database + ); // return sqlite3_exec status code + + static int clean( + sqlite3 * database, + const sqlite3_int64 & APP_BROWSER_MAIN_TAB_LABEL__SESSION__ID + ); // return sqlite3_finalize status code + + static sqlite3_int64 add( + sqlite3 * database, + const sqlite3_int64 & APP_BROWSER_MAIN_TAB_LABEL__SESSION__ID, + Glib::ustring & TEXT + ); // return sqlite3_last_insert_rowid + }; + }; + /* * Internal members */ private: + // Database + sqlite3 * database; + + // Extras + Glib::ustring text; + // Defaults static const int WIDTH_CHARS = 16; @@ -23,7 +67,22 @@ namespace app::browser::main::tab::label */ public: - Title(); + Title( + sqlite3 * database + ); + + // Actions + int session_restore( + const sqlite3_int64 & APP_BROWSER_MAIN_TAB_LABEL__SESSION__ID + ); // return sqlite3_finalize status code + + sqlite3_int64 session_save( + const sqlite3_int64 & APP_BROWSER_MAIN_TAB_LABEL__SESSION__ID + ); // return sqlite3_last_insert_rowid + + void update( + const Glib::ustring & TEXT + ); }; }