diff --git a/src/app/browser/main/tab.cpp b/src/app/browser/main/tab.cpp index 466ce48e..2f4984ba 100644 --- a/src/app/browser/main/tab.cpp +++ b/src/app/browser/main/tab.cpp @@ -70,12 +70,7 @@ int Tab::restore() while (sqlite3_step(statement) == SQLITE_ROW) { const int PAGE_NUMBER = append( - reinterpret_cast( - sqlite3_column_text( - statement, - DB::SESSION::LABEL_TEXT - ) - ), + _("Restore.."), sqlite3_column_int( statement, DB::SESSION::IS_CURRENT @@ -83,6 +78,15 @@ int Tab::restore() ); // Restore children components + get_tabLabel( + PAGE_NUMBER + )->restore( + sqlite3_column_int64( + statement, + DB::SESSION::ID + ) + ); + get_tabPage( PAGE_NUMBER )->restore( @@ -94,11 +98,9 @@ int Tab::restore() } } - sqlite3_finalize( + return sqlite3_finalize( statement ); - - return PREPARE_STATUS; } void Tab::clean() // @TODO menu action? @@ -122,18 +124,24 @@ void Tab::save() // Save current tab session for (int page_number = 0; page_number < get_n_pages(); page_number++) { - // Delegate save actions to child page component + // Create new session + const sqlite3_int64 APP_BROWSER_MAIN_TAB__SESSION__ID = DB::SESSION::add( + db, + page_number, + page_number == get_current_page() ? 1 : 0 + ); + + // Delegate save actions to children components + get_tabLabel( + page_number + )->save( + APP_BROWSER_MAIN_TAB__SESSION__ID + ); + get_tabPage( page_number )->save( - DB::SESSION::add( - db, - page_number, - page_number == get_current_page() ? 1 : 0, - get_tabLabel( - page_number - )->get_text() // @TODO use separated table for children widget - ) + APP_BROWSER_MAIN_TAB__SESSION__ID ); } } @@ -152,7 +160,7 @@ void Tab::update( TAB_PAGE->get_title() ); - TAB_PAGE->update(); + // TAB_PAGE->update(); @TODO meant refresh? action__tab_close_active->set_enabled( get_n_pages() > 0 @@ -167,7 +175,7 @@ int Tab::append( const Glib::ustring & LABEL_TEXT, const bool & IS_CURRENT ) { - const auto TAB_PAGE = new tab::Page( + const auto TAB_PAGE = new tab::Page( // @TODO manage db, tab::Page::MIME::UNDEFINED, LABEL_TEXT, @@ -179,7 +187,8 @@ int Tab::append( action__tab_page_navigation_update ); - const auto TAB_LABEL = new tab::Label( // @TODO managed + const auto TAB_LABEL = new tab::Label( // @TODO manage + db, action__tab_close_active ); @@ -354,8 +363,7 @@ int Tab::DB::SESSION::init( `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `time` INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP, `page_number` INTEGER NOT NULL, - `is_current` INTEGER NOT NULL, - `label_text` VARCHAR(1024) + `is_current` INTEGER NOT NULL ) )SQL", nullptr, @@ -407,6 +415,11 @@ int Tab::DB::SESSION::clean( // Delegate children dependencies cleanup if (EXEC_STATUS == SQLITE_OK) { + tab::Label::DB::SESSION::clean( + db, + APP_BROWSER_MAIN_TAB__SESSION__ID + ); + tab::Page::DB::SESSION::clean( db, APP_BROWSER_MAIN_TAB__SESSION__ID @@ -423,8 +436,7 @@ int Tab::DB::SESSION::clean( sqlite3_int64 Tab::DB::SESSION::add( sqlite3 * db, const int & PAGE_NUMBER, - const bool & IS_CURRENT, - const Glib::ustring & LABEL_TEXT + const bool & IS_CURRENT ) { char * error; // @TODO @@ -434,17 +446,14 @@ sqlite3_int64 Tab::DB::SESSION::add( R"SQL( INSERT INTO `app_browser_main_tab__session` ( `page_number`, - `is_current`, - `label_text` + `is_current` ) VALUES ( - '%d', - '%d', - '%s' + %d, + %d ) )SQL", PAGE_NUMBER, - IS_CURRENT, - LABEL_TEXT + IS_CURRENT ).c_str(), nullptr, nullptr, diff --git a/src/app/browser/main/tab.hpp b/src/app/browser/main/tab.hpp index 916eef1b..83513253 100644 --- a/src/app/browser/main/tab.hpp +++ b/src/app/browser/main/tab.hpp @@ -35,8 +35,7 @@ namespace app::browser::main ID, TIME, PAGE_NUMBER, - IS_CURRENT, - LABEL_TEXT + IS_CURRENT }; // table fields index static int init( @@ -50,8 +49,7 @@ namespace app::browser::main static sqlite3_int64 add( sqlite3 * db, const int & PAGE_NUMBER, - const bool & IS_CURRENT, - const Glib::ustring & LABEL_TEXT + const bool & IS_CURRENT ); // return sqlite3_last_insert_rowid }; @@ -123,7 +121,7 @@ namespace app::browser::main const int & PAGE_NUMBER ); - int restore(); + int restore(); // return sqlite3_finalize status code void clean(); diff --git a/src/app/browser/main/tab/label.cpp b/src/app/browser/main/tab/label.cpp index 5ab17c8d..f6d2ffd1 100644 --- a/src/app/browser/main/tab/label.cpp +++ b/src/app/browser/main/tab/label.cpp @@ -3,8 +3,14 @@ using namespace app::browser::main::tab; Label::Label( + sqlite3 * db, const Glib::RefPtr & ACTION__CLOSE_ACTIVE ) { + // Init database + DB::SESSION::init( + this->db = db + ); + // Init actions action__close_active = ACTION__CLOSE_ACTIVE; @@ -29,4 +35,173 @@ Label::Label( add_controller( GtkGestureClick ); +} + +// Actions +int Label::restore( + const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID +) { + sqlite3_stmt * statement; + + const int PREPARE_STATUS = sqlite3_prepare_v3( + db, + Glib::ustring::sprintf( + R"SQL( + SELECT * FROM `app_browser_main_tab_label__session` + WHERE `app_browser_main_tab__session__id` = %d + ORDER BY `id` DESC LIMIT 1 + )SQL", + APP_BROWSER_MAIN_TAB__SESSION__ID + ).c_str(), + -1, + SQLITE_PREPARE_NORMALIZE, + &statement, + nullptr + ); + + if (PREPARE_STATUS == SQLITE_OK) + { + // Use latest record as order + while (sqlite3_step(statement) == SQLITE_ROW) + { + // Restore widget data + set_text( + reinterpret_cast( + sqlite3_column_text( + statement, + DB::SESSION::TEXT + ) + ) + ); + + // Restore children components here (on available) + } + } + + return sqlite3_finalize( + statement + ); +} + +int Label::save( + const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID +) { + // Delegate save action to child components (on available) + + // Save label session + return DB::SESSION::add( + db, + APP_BROWSER_MAIN_TAB__SESSION__ID, + get_text() + ); +} + +// Database model +int Label::DB::SESSION::init( + sqlite3 * db +) { + char * error; + + return sqlite3_exec( + db, + R"SQL( + CREATE TABLE IF NOT EXISTS `app_browser_main_tab_label__session` + ( + `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `app_browser_main_tab__session__id` INTEGER NOT NULL, + `time` INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP, + `text` VARCHAR (1024) NOT NULL + ) + )SQL", + nullptr, + nullptr, + &error + ); +} + +int Label::DB::SESSION::clean( + sqlite3 * db, + const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID +) { + char * error; // @TODO + sqlite3_stmt * statement; + + const int PREPARE_STATUS = sqlite3_prepare_v3( + db, + Glib::ustring::sprintf( + R"SQL( + SELECT * FROM `app_browser_main_tab_label__session` + WHERE `app_browser_main_tab__session__id` = %d + )SQL", + APP_BROWSER_MAIN_TAB__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( + db, + Glib::ustring::sprintf( + R"SQL( + DELETE FROM `app_browser_main_tab_label__session` WHERE `id` = %d + )SQL", + sqlite3_column_int64( + statement, + DB::SESSION::ID + ) + ).c_str(), + nullptr, + nullptr, + &error + ); + + // Delegate children dependencies cleanup + if (EXEC_STATUS == SQLITE_OK) + { + // nothing here. + } + } + } + + return sqlite3_finalize( + statement + ); +} + +sqlite3_int64 Label::DB::SESSION::add( + sqlite3 * db, + const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID, + const Glib::ustring & TEXT +) { + char * error; // @TODO + + sqlite3_exec( + db, + Glib::ustring::sprintf( + R"SQL( + INSERT INTO `app_browser_main_tab_label__session` ( + `app_browser_main_tab__session__id`, + `text` + ) VALUES ( + '%d', + '%s' + ) + )SQL", + APP_BROWSER_MAIN_TAB__SESSION__ID, + TEXT + ).c_str(), + nullptr, + nullptr, + &error + ); + + return sqlite3_last_insert_rowid( + db + ); } \ No newline at end of file diff --git a/src/app/browser/main/tab/label.hpp b/src/app/browser/main/tab/label.hpp index 05ce8586..13550902 100644 --- a/src/app/browser/main/tab/label.hpp +++ b/src/app/browser/main/tab/label.hpp @@ -7,18 +7,78 @@ #include #include #include +#include namespace app::browser::main::tab { class Label : public Gtk::Label { - Glib::RefPtr action__close_active; + public: + + /* + * Class database + * + * Allowed parental access to enums and relationship methods + */ + struct DB + { + // APP_BROWSER_MAIN_TAB_LABEL__* + struct SESSION + { + enum + { + ID, + APP_BROWSER_MAIN_TAB__SESSION__ID, + TIME, + TEXT + }; // table fields index + + static int init( + sqlite3 * db + ); // return sqlite3_exec status code + + static int clean( + sqlite3 * db, + const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID + ); // return sqlite3_finalize status code + + static sqlite3_int64 add( + sqlite3 * db, + const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID, + const Glib::ustring & TEXT + ); // return sqlite3_last_insert_rowid + }; + }; + /* + * Internal members + */ + private: + + // Database + sqlite3 * db; + + // Actions + Glib::RefPtr action__close_active; + + /* + * Class API + */ public: Label( + sqlite3 * db, const Glib::RefPtr & ACTION__CLOSE_ACTIVE ); + + // Actions + int restore( + const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID + ); // return sqlite3_finalize status code + + int save( + const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID + ); // return sqlite3_finalize status code }; } diff --git a/src/app/browser/main/tab/page.cpp b/src/app/browser/main/tab/page.cpp index 6444610b..5a3ab984 100644 --- a/src/app/browser/main/tab/page.cpp +++ b/src/app/browser/main/tab/page.cpp @@ -63,13 +63,6 @@ Page::Page( } // Actions -void Page::update() -{ - pageNavigation->update( - progress_fraction - ); -} - int Page::restore( const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID ) { @@ -106,11 +99,9 @@ int Page::restore( } } - sqlite3_finalize( + return sqlite3_finalize( statement ); - - return PREPARE_STATUS; } int Page::save( @@ -140,6 +131,11 @@ void Page::update( description = DESCRIPTION; progress_fraction = PROGRESS_FRACTION; + // Refresh children components + pageNavigation->update( + progress_fraction + ); + // Refresh parent window action__refresh->activate(); } diff --git a/src/app/browser/main/tab/page.hpp b/src/app/browser/main/tab/page.hpp index a8cbbd2c..d8522316 100644 --- a/src/app/browser/main/tab/page.hpp +++ b/src/app/browser/main/tab/page.hpp @@ -118,11 +118,9 @@ namespace app::browser::main::tab ); // Actions - void update(); - int restore( const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID - ); + ); // return sqlite3_finalize status code int save( const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID diff --git a/src/app/browser/main/tab/page/navigation.cpp b/src/app/browser/main/tab/page/navigation.cpp index 8299887f..4bb7cbba 100644 --- a/src/app/browser/main/tab/page/navigation.cpp +++ b/src/app/browser/main/tab/page/navigation.cpp @@ -144,11 +144,9 @@ int Navigation::restore( } } - sqlite3_finalize( + return sqlite3_finalize( statement ); - - return PREPARE_STATUS; } int Navigation::save( diff --git a/src/app/browser/main/tab/page/navigation.hpp b/src/app/browser/main/tab/page/navigation.hpp index 8af4f538..290a16e4 100644 --- a/src/app/browser/main/tab/page/navigation.hpp +++ b/src/app/browser/main/tab/page/navigation.hpp @@ -95,7 +95,7 @@ namespace app::browser::main::tab::page int restore( const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID - ); + ); // return sqlite3_finalize status code int save( const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID diff --git a/src/app/browser/main/tab/page/navigation/request.cpp b/src/app/browser/main/tab/page/navigation/request.cpp index afcc6fa5..4d8247d5 100644 --- a/src/app/browser/main/tab/page/navigation/request.cpp +++ b/src/app/browser/main/tab/page/navigation/request.cpp @@ -124,11 +124,9 @@ int Request::restore( } } - sqlite3_finalize( + return sqlite3_finalize( statement ); - - return PREPARE_STATUS; } int Request::save( diff --git a/src/app/browser/main/tab/page/navigation/request.hpp b/src/app/browser/main/tab/page/navigation/request.hpp index 932665d7..cfb46cb8 100644 --- a/src/app/browser/main/tab/page/navigation/request.hpp +++ b/src/app/browser/main/tab/page/navigation/request.hpp @@ -98,7 +98,7 @@ namespace app::browser::main::tab::page::navigation int restore( const sqlite3_int64 & APP_BROWSER_MAIN_TAB_PAGE_NAVIGATION__SESSION__ID - ); + ); // return sqlite3_finalize status code int save( const sqlite3_int64 & APP_BROWSER_MAIN_TAB_PAGE_NAVIGATION__SESSION__ID