Browse Source

delegate specific database operations to widgets, use SQLiteCpp wrapper

CPP-GTK4
yggverse 4 months ago
parent
commit
f98b0e55e9
  1. 6
      Makefile
  2. 4
      README.md
  3. 4
      po/POTFILES.in
  4. 40
      src/app.cpp
  5. 1
      src/app.hpp
  6. 3
      src/app/browser.cpp
  7. 3
      src/app/browser.hpp
  8. 2
      src/app/browser/main.cpp
  9. 2
      src/app/browser/main.hpp
  10. 13
      src/app/browser/main/tab.cpp
  11. 2
      src/app/browser/main/tab.hpp
  12. 17
      src/lib/database.cpp
  13. 31
      src/lib/database.hpp
  14. 22
      src/lib/database/session.cpp
  15. 24
      src/lib/database/session.hpp
  16. 41
      src/main.cpp

6
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 # Define target executable and source files
TARGET = bin/Yoda TARGET = bin/Yoda
SRCS = src/main.cpp\ SRCS = src/app.cpp\
src/app/browser.cpp\ src/app/browser.cpp\
src/app/browser/header.cpp\ src/app/browser/header.cpp\
src/app/browser/header/main.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/history/forward.cpp\
src/app/browser/main/tab/page/navigation/request.cpp\ src/app/browser/main/tab/page/navigation/request.cpp\
src/app/browser/main/tab/page/navigation/update.cpp\ src/app/browser/main/tab/page/navigation/update.cpp\
src/app/browser/main/tab/label.cpp\ src/app/browser/main/tab/label.cpp
src/lib/database.cpp\
src/lib/database/session.cpp
OBJS = $(SRCS:.cpp=.o) OBJS = $(SRCS:.cpp=.o)

4
README.md

@ -16,7 +16,8 @@ apt install git\
libglibmm-2.68-dev\ libglibmm-2.68-dev\
libgtkmm-4.0-dev\ libgtkmm-4.0-dev\
libpangomm-2.48-dev\ libpangomm-2.48-dev\
libsqlite3-dev libsqlite3-dev\
libsqlitecpp-dev
``` ```
* `git clone https://github.com/YGGverse/Yoda.git` * `git clone https://github.com/YGGverse/Yoda.git`
@ -68,3 +69,4 @@ pkg-config --cflags --libs gio-2.0\
* [GTK](https://gtk.org) - free and open-source cross-platform widget toolkit * [GTK](https://gtk.org) - free and open-source cross-platform widget toolkit
* [gtkmm](https://gtkmm.org) - official C++ interface for GTK * [gtkmm](https://gtkmm.org) - official C++ interface for GTK
* [SQLite](https://sqlite.org) - profile database * [SQLite](https://sqlite.org) - profile database
* [SQLiteCpp](https://github.com/SRombauts/SQLiteCpp) - SQLite3 C++ wrapper

4
po/POTFILES.in

@ -1,3 +1,4 @@
src/app.cpp
src/app/browser.cpp src/app/browser.cpp
src/app/browser/header.cpp src/app/browser/header.cpp
src/app/browser/header/main.cpp src/app/browser/header/main.cpp
@ -22,6 +23,3 @@ 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/request.cpp
src/app/browser/main/tab/page/navigation/update.cpp src/app/browser/main/tab/page/navigation/update.cpp
src/app/browser/main/tab/label.cpp src/app/browser/main/tab/label.cpp
src/lib/database.cpp
src/lib/database/session.cpp
src/main.cpp

40
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",
"<Primary>q"
);
// Launch browser component
return app->make_window_and_run<app::Browser>(
argc,
argv,
db,
app
);
}

1
src/main.hpp → src/app.hpp

@ -1,5 +1,6 @@
#include <glibmm/refptr.h> #include <glibmm/refptr.h>
#include <gtkmm/application.h> #include <gtkmm/application.h>
#include <SQLiteCpp/SQLiteCpp.h>
int main( int main(
int argc, int argc,

3
src/app/browser.cpp

@ -5,8 +5,8 @@
using namespace app; using namespace app;
Browser::Browser( Browser::Browser(
SQLite::Database & db,
const Glib::RefPtr<Gtk::Application> & APP const Glib::RefPtr<Gtk::Application> & APP
//const std::shared_ptr<lib::Database> & db
) { ) {
// Init window actions // Init window actions
const auto ACTION__REFRESH = add_action( const auto ACTION__REFRESH = add_action(
@ -177,6 +177,7 @@ Browser::Browser(
); );
browserMain = Gtk::make_managed<browser::Main>( browserMain = Gtk::make_managed<browser::Main>(
db,
ACTION__REFRESH, ACTION__REFRESH,
ACTION__MAIN_TAB_CLOSE_ACTIVE, ACTION__MAIN_TAB_CLOSE_ACTIVE,
ACTION__MAIN_TAB_CLOSE_ALL, ACTION__MAIN_TAB_CLOSE_ALL,

3
src/app/browser.hpp

@ -7,6 +7,7 @@
#include <gtkmm/application.h> #include <gtkmm/application.h>
#include <gtkmm/applicationwindow.h> #include <gtkmm/applicationwindow.h>
#include <gtkmm/object.h> #include <gtkmm/object.h>
#include <SQLiteCpp/SQLiteCpp.h>
namespace lib namespace lib
{ {
@ -34,8 +35,8 @@ namespace app
public: public:
Browser( Browser(
SQLite::Database & db,
const Glib::RefPtr<Gtk::Application> & APP const Glib::RefPtr<Gtk::Application> & APP
//const std::shared_ptr<lib::Database> & db
); );
}; };
} }

2
src/app/browser/main.cpp

@ -4,6 +4,7 @@
using namespace app::browser; using namespace app::browser;
Main::Main( Main::Main(
SQLite::Database & db,
const Glib::RefPtr<Gio::SimpleAction> & ACTION__REFRESH, const Glib::RefPtr<Gio::SimpleAction> & ACTION__REFRESH,
const Glib::RefPtr<Gio::SimpleAction> & ACTION__MAIN_TAB_CLOSE_ACTIVE, const Glib::RefPtr<Gio::SimpleAction> & ACTION__MAIN_TAB_CLOSE_ACTIVE,
const Glib::RefPtr<Gio::SimpleAction> & ACTION__MAIN_TAB_CLOSE_ALL, const Glib::RefPtr<Gio::SimpleAction> & ACTION__MAIN_TAB_CLOSE_ALL,
@ -22,6 +23,7 @@ Main::Main(
// Init components // Init components
mainTab = Gtk::make_managed<main::Tab>( mainTab = Gtk::make_managed<main::Tab>(
db,
ACTION__REFRESH, ACTION__REFRESH,
ACTION__MAIN_TAB_CLOSE_ACTIVE, ACTION__MAIN_TAB_CLOSE_ACTIVE,
ACTION__MAIN_TAB_CLOSE_ALL, ACTION__MAIN_TAB_CLOSE_ALL,

2
src/app/browser/main.hpp

@ -7,6 +7,7 @@
#include <glibmm/ustring.h> #include <glibmm/ustring.h>
#include <gtkmm/box.h> #include <gtkmm/box.h>
#include <gtkmm/object.h> #include <gtkmm/object.h>
#include <SQLiteCpp/SQLiteCpp.h>
namespace app::browser namespace app::browser
{ {
@ -26,6 +27,7 @@ namespace app::browser
public: public:
Main( Main(
SQLite::Database & db,
const Glib::RefPtr<Gio::SimpleAction> & ACTION__REFRESH, const Glib::RefPtr<Gio::SimpleAction> & ACTION__REFRESH,
const Glib::RefPtr<Gio::SimpleAction> & ACTION__MAIN_TAB_CLOSE_ACTIVE, const Glib::RefPtr<Gio::SimpleAction> & ACTION__MAIN_TAB_CLOSE_ACTIVE,
const Glib::RefPtr<Gio::SimpleAction> & ACTION__MAIN_TAB_CLOSE_ALL, const Glib::RefPtr<Gio::SimpleAction> & ACTION__MAIN_TAB_CLOSE_ALL,

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

@ -5,6 +5,7 @@
using namespace app::browser::main; using namespace app::browser::main;
Tab::Tab( Tab::Tab(
SQLite::Database & db,
const Glib::RefPtr<Gio::SimpleAction> & ACTION__REFRESH, const Glib::RefPtr<Gio::SimpleAction> & ACTION__REFRESH,
const Glib::RefPtr<Gio::SimpleAction> & ACTION__TAB_CLOSE_ACTIVE, const Glib::RefPtr<Gio::SimpleAction> & ACTION__TAB_CLOSE_ACTIVE,
const Glib::RefPtr<Gio::SimpleAction> & ACTION__MAIN_TAB_CLOSE_ALL, const Glib::RefPtr<Gio::SimpleAction> & ACTION__MAIN_TAB_CLOSE_ALL,
@ -12,6 +13,18 @@ Tab::Tab(
const Glib::RefPtr<Gio::SimpleAction> & ACTION__TAB_PAGE_NAVIGATION_HISTORY_FORWARD, const Glib::RefPtr<Gio::SimpleAction> & ACTION__TAB_PAGE_NAVIGATION_HISTORY_FORWARD,
const Glib::RefPtr<Gio::SimpleAction> & ACTION__TAB_PAGE_NAVIGATION_UPDATE const Glib::RefPtr<Gio::SimpleAction> & 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 // Init actions
action__refresh = ACTION__REFRESH; action__refresh = ACTION__REFRESH;
action__tab_close_active = ACTION__TAB_CLOSE_ACTIVE; action__tab_close_active = ACTION__TAB_CLOSE_ACTIVE;

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

@ -6,6 +6,7 @@
#include <glibmm/refptr.h> #include <glibmm/refptr.h>
#include <glibmm/ustring.h> #include <glibmm/ustring.h>
#include <gtkmm/notebook.h> #include <gtkmm/notebook.h>
#include <SQLiteCpp/SQLiteCpp.h>
namespace app::browser::main namespace app::browser::main
{ {
@ -41,6 +42,7 @@ namespace app::browser::main
public: public:
Tab( Tab(
SQLite::Database & db,
const Glib::RefPtr<Gio::SimpleAction> & ACTION__REFRESH, const Glib::RefPtr<Gio::SimpleAction> & ACTION__REFRESH,
const Glib::RefPtr<Gio::SimpleAction> & ACTION__TAB_CLOSE_ACTIVE, const Glib::RefPtr<Gio::SimpleAction> & ACTION__TAB_CLOSE_ACTIVE,
const Glib::RefPtr<Gio::SimpleAction> & ACTION__MAIN_TAB_CLOSE_ALL, const Glib::RefPtr<Gio::SimpleAction> & ACTION__MAIN_TAB_CLOSE_ALL,

17
src/lib/database.cpp

@ -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
);
}

31
src/lib/database.hpp

@ -1,31 +0,0 @@
#ifndef LIB_DATABASE_HPP
#define LIB_DATABASE_HPP
#include <sqlite3.h>
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

22
src/lib/database/session.cpp

@ -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
);
}

24
src/lib/database/session.hpp

@ -1,24 +0,0 @@
#ifndef LIB_DATABASE_SESSION_HPP
#define LIB_DATABASE_SESSION_HPP
#include <sqlite3.h>
namespace lib::database
{
class Session
{
int status;
char * error;
sqlite3 * connection;
public:
Session(
sqlite3 * connection
);
};
}
#endif // LIB_DATABASE_SESSION_HPP

41
src/main.cpp

@ -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<lib::Database> DB( // @TODO
new lib::Database(
"database.sqlite3"
)
);
// Init app
const Glib::RefPtr<Gtk::Application> APP = Gtk::Application::create(
"io.github.yggverse.Yoda"
);
APP->add_action(
"quit",
[APP]
{
APP->quit();
}
);
APP->set_accel_for_action(
"app.quit",
"<Primary>q"
);
// Launch browser component
return APP->make_window_and_run<app::Browser>(
argc,
argv,
APP
// DB
);
}
Loading…
Cancel
Save