Browse Source

delegate db actions to label class

CPP-GTK4
yggverse 2 months ago
parent
commit
b67b9561fb
  1. 73
      src/app/browser/main/tab.cpp
  2. 8
      src/app/browser/main/tab.hpp
  3. 175
      src/app/browser/main/tab/label.cpp
  4. 62
      src/app/browser/main/tab/label.hpp
  5. 16
      src/app/browser/main/tab/page.cpp
  6. 4
      src/app/browser/main/tab/page.hpp
  7. 4
      src/app/browser/main/tab/page/navigation.cpp
  8. 2
      src/app/browser/main/tab/page/navigation.hpp
  9. 4
      src/app/browser/main/tab/page/navigation/request.cpp
  10. 2
      src/app/browser/main/tab/page/navigation/request.hpp

73
src/app/browser/main/tab.cpp

@ -70,12 +70,7 @@ int Tab::restore()
while (sqlite3_step(statement) == SQLITE_ROW) while (sqlite3_step(statement) == SQLITE_ROW)
{ {
const int PAGE_NUMBER = append( const int PAGE_NUMBER = append(
reinterpret_cast<const char*>( _("Restore.."),
sqlite3_column_text(
statement,
DB::SESSION::LABEL_TEXT
)
),
sqlite3_column_int( sqlite3_column_int(
statement, statement,
DB::SESSION::IS_CURRENT DB::SESSION::IS_CURRENT
@ -83,6 +78,15 @@ int Tab::restore()
); );
// Restore children components // Restore children components
get_tabLabel(
PAGE_NUMBER
)->restore(
sqlite3_column_int64(
statement,
DB::SESSION::ID
)
);
get_tabPage( get_tabPage(
PAGE_NUMBER PAGE_NUMBER
)->restore( )->restore(
@ -94,11 +98,9 @@ int Tab::restore()
} }
} }
sqlite3_finalize( return sqlite3_finalize(
statement statement
); );
return PREPARE_STATUS;
} }
void Tab::clean() // @TODO menu action? void Tab::clean() // @TODO menu action?
@ -122,18 +124,24 @@ void Tab::save()
// Save current tab session // Save current tab session
for (int page_number = 0; page_number < get_n_pages(); page_number++) 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( get_tabPage(
page_number page_number
)->save( )->save(
DB::SESSION::add( APP_BROWSER_MAIN_TAB__SESSION__ID
db,
page_number,
page_number == get_current_page() ? 1 : 0,
get_tabLabel(
page_number
)->get_text() // @TODO use separated table for children widget
)
); );
} }
} }
@ -152,7 +160,7 @@ void Tab::update(
TAB_PAGE->get_title() TAB_PAGE->get_title()
); );
TAB_PAGE->update(); // TAB_PAGE->update(); @TODO meant refresh?
action__tab_close_active->set_enabled( action__tab_close_active->set_enabled(
get_n_pages() > 0 get_n_pages() > 0
@ -167,7 +175,7 @@ int Tab::append(
const Glib::ustring & LABEL_TEXT, const Glib::ustring & LABEL_TEXT,
const bool & IS_CURRENT const bool & IS_CURRENT
) { ) {
const auto TAB_PAGE = new tab::Page( const auto TAB_PAGE = new tab::Page( // @TODO manage
db, db,
tab::Page::MIME::UNDEFINED, tab::Page::MIME::UNDEFINED,
LABEL_TEXT, LABEL_TEXT,
@ -179,7 +187,8 @@ int Tab::append(
action__tab_page_navigation_update 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 action__tab_close_active
); );
@ -354,8 +363,7 @@ int Tab::DB::SESSION::init(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`time` INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP, `time` INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP,
`page_number` INTEGER NOT NULL, `page_number` INTEGER NOT NULL,
`is_current` INTEGER NOT NULL, `is_current` INTEGER NOT NULL
`label_text` VARCHAR(1024)
) )
)SQL", )SQL",
nullptr, nullptr,
@ -407,6 +415,11 @@ int Tab::DB::SESSION::clean(
// Delegate children dependencies cleanup // Delegate children dependencies cleanup
if (EXEC_STATUS == SQLITE_OK) if (EXEC_STATUS == SQLITE_OK)
{ {
tab::Label::DB::SESSION::clean(
db,
APP_BROWSER_MAIN_TAB__SESSION__ID
);
tab::Page::DB::SESSION::clean( tab::Page::DB::SESSION::clean(
db, db,
APP_BROWSER_MAIN_TAB__SESSION__ID APP_BROWSER_MAIN_TAB__SESSION__ID
@ -423,8 +436,7 @@ int Tab::DB::SESSION::clean(
sqlite3_int64 Tab::DB::SESSION::add( sqlite3_int64 Tab::DB::SESSION::add(
sqlite3 * db, sqlite3 * db,
const int & PAGE_NUMBER, const int & PAGE_NUMBER,
const bool & IS_CURRENT, const bool & IS_CURRENT
const Glib::ustring & LABEL_TEXT
) { ) {
char * error; // @TODO char * error; // @TODO
@ -434,17 +446,14 @@ sqlite3_int64 Tab::DB::SESSION::add(
R"SQL( R"SQL(
INSERT INTO `app_browser_main_tab__session` ( INSERT INTO `app_browser_main_tab__session` (
`page_number`, `page_number`,
`is_current`, `is_current`
`label_text`
) VALUES ( ) VALUES (
'%d', %d,
'%d', %d
'%s'
) )
)SQL", )SQL",
PAGE_NUMBER, PAGE_NUMBER,
IS_CURRENT, IS_CURRENT
LABEL_TEXT
).c_str(), ).c_str(),
nullptr, nullptr,
nullptr, nullptr,

8
src/app/browser/main/tab.hpp

@ -35,8 +35,7 @@ namespace app::browser::main
ID, ID,
TIME, TIME,
PAGE_NUMBER, PAGE_NUMBER,
IS_CURRENT, IS_CURRENT
LABEL_TEXT
}; // table fields index }; // table fields index
static int init( static int init(
@ -50,8 +49,7 @@ namespace app::browser::main
static sqlite3_int64 add( static sqlite3_int64 add(
sqlite3 * db, sqlite3 * db,
const int & PAGE_NUMBER, const int & PAGE_NUMBER,
const bool & IS_CURRENT, const bool & IS_CURRENT
const Glib::ustring & LABEL_TEXT
); // return sqlite3_last_insert_rowid ); // return sqlite3_last_insert_rowid
}; };
@ -123,7 +121,7 @@ namespace app::browser::main
const int & PAGE_NUMBER const int & PAGE_NUMBER
); );
int restore(); int restore(); // return sqlite3_finalize status code
void clean(); void clean();

175
src/app/browser/main/tab/label.cpp

@ -3,8 +3,14 @@
using namespace app::browser::main::tab; using namespace app::browser::main::tab;
Label::Label( Label::Label(
sqlite3 * db,
const Glib::RefPtr<Gio::SimpleAction> & ACTION__CLOSE_ACTIVE const Glib::RefPtr<Gio::SimpleAction> & ACTION__CLOSE_ACTIVE
) { ) {
// Init database
DB::SESSION::init(
this->db = db
);
// Init actions // Init actions
action__close_active = ACTION__CLOSE_ACTIVE; action__close_active = ACTION__CLOSE_ACTIVE;
@ -29,4 +35,173 @@ Label::Label(
add_controller( add_controller(
GtkGestureClick 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<const char*>(
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
);
} }

62
src/app/browser/main/tab/label.hpp

@ -7,18 +7,78 @@
#include <glibmm/ustring.h> #include <glibmm/ustring.h>
#include <gtkmm/gestureclick.h> #include <gtkmm/gestureclick.h>
#include <gtkmm/label.h> #include <gtkmm/label.h>
#include <sqlite3.h>
namespace app::browser::main::tab namespace app::browser::main::tab
{ {
class Label : public Gtk::Label class Label : public Gtk::Label
{ {
Glib::RefPtr<Gio::SimpleAction> 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<Gio::SimpleAction> action__close_active;
/*
* Class API
*/
public: public:
Label( Label(
sqlite3 * db,
const Glib::RefPtr<Gio::SimpleAction> & ACTION__CLOSE_ACTIVE const Glib::RefPtr<Gio::SimpleAction> & 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
}; };
} }

16
src/app/browser/main/tab/page.cpp

@ -63,13 +63,6 @@ Page::Page(
} }
// Actions // Actions
void Page::update()
{
pageNavigation->update(
progress_fraction
);
}
int Page::restore( int Page::restore(
const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID
) { ) {
@ -106,11 +99,9 @@ int Page::restore(
} }
} }
sqlite3_finalize( return sqlite3_finalize(
statement statement
); );
return PREPARE_STATUS;
} }
int Page::save( int Page::save(
@ -140,6 +131,11 @@ void Page::update(
description = DESCRIPTION; description = DESCRIPTION;
progress_fraction = PROGRESS_FRACTION; progress_fraction = PROGRESS_FRACTION;
// Refresh children components
pageNavigation->update(
progress_fraction
);
// Refresh parent window // Refresh parent window
action__refresh->activate(); action__refresh->activate();
} }

4
src/app/browser/main/tab/page.hpp

@ -118,11 +118,9 @@ namespace app::browser::main::tab
); );
// Actions // Actions
void update();
int restore( int restore(
const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID
); ); // return sqlite3_finalize status code
int save( int save(
const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID

4
src/app/browser/main/tab/page/navigation.cpp

@ -144,11 +144,9 @@ int Navigation::restore(
} }
} }
sqlite3_finalize( return sqlite3_finalize(
statement statement
); );
return PREPARE_STATUS;
} }
int Navigation::save( int Navigation::save(

2
src/app/browser/main/tab/page/navigation.hpp

@ -95,7 +95,7 @@ namespace app::browser::main::tab::page
int restore( int restore(
const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID
); ); // return sqlite3_finalize status code
int save( int save(
const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID

4
src/app/browser/main/tab/page/navigation/request.cpp

@ -124,11 +124,9 @@ int Request::restore(
} }
} }
sqlite3_finalize( return sqlite3_finalize(
statement statement
); );
return PREPARE_STATUS;
} }
int Request::save( int Request::save(

2
src/app/browser/main/tab/page/navigation/request.hpp

@ -98,7 +98,7 @@ namespace app::browser::main::tab::page::navigation
int restore( int restore(
const sqlite3_int64 & APP_BROWSER_MAIN_TAB_PAGE_NAVIGATION__SESSION__ID const sqlite3_int64 & APP_BROWSER_MAIN_TAB_PAGE_NAVIGATION__SESSION__ID
); ); // return sqlite3_finalize status code
int save( int save(
const sqlite3_int64 & APP_BROWSER_MAIN_TAB_PAGE_NAVIGATION__SESSION__ID const sqlite3_int64 & APP_BROWSER_MAIN_TAB_PAGE_NAVIGATION__SESSION__ID

Loading…
Cancel
Save