From 625bbfc5ea18e5cef163423198c0418034a97828 Mon Sep 17 00:00:00 2001 From: Wieczorek Bartosz Date: Tue, 14 Mar 2017 15:08:43 +0100 Subject: [PATCH] refactor --- CMakeLists.txt | 2 +- src/CMakeLists.txt | 2 - src/app/CMakeLists.txt | 2 - src/app/Session.cpp | 0 src/app/Session.hpp | 0 src/app/main.cpp | 4 +- src/eedb/Application.cpp | 71 --------------------------------- src/eedb/Application.hpp | 17 -------- src/eedb/CMakeLists.txt | 1 + src/eedb/EEDB.cpp | 60 ++++++++++++++++++++++++++++ src/eedb/EEDB.hpp | 32 +++++++++++++++ src/eedb/widgets/AuthWidget.hpp | 1 + src/eedb/widgets/MainWindow.cpp | 7 +++- src/eedb/widgets/MainWindow.hpp | 8 +++- 14 files changed, 109 insertions(+), 98 deletions(-) delete mode 100644 src/app/Session.cpp delete mode 100644 src/app/Session.hpp delete mode 100644 src/eedb/Application.cpp delete mode 100644 src/eedb/Application.hpp create mode 100644 src/eedb/EEDB.cpp create mode 100644 src/eedb/EEDB.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 58ebeee..62d318f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ project(eedb) add_definitions( -std=c++17 ) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "/usr/share/cmake/Modules/") -find_package(Wt REQUIRED) +find_package(Wt 4.0.0 REQUIRED) find_package(Sqlpp11 REQUIRED) find_package(PostgreSQL REQUIRED) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7472b05..2fc7868 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,8 +1,6 @@ project( eedb_app ) INCLUDE_DIRECTORIES(.) - - add_subdirectory(utils) add_subdirectory(eedb) add_subdirectory(app) diff --git a/src/app/CMakeLists.txt b/src/app/CMakeLists.txt index 810a1ac..d078d4a 100644 --- a/src/app/CMakeLists.txt +++ b/src/app/CMakeLists.txt @@ -1,7 +1,5 @@ set(SOURCES - Application.cpp DatabaseConnection.cpp - Session.cpp main.cpp) find_package( Boost 1.54.0 REQUIRED system ) diff --git a/src/app/Session.cpp b/src/app/Session.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/src/app/Session.hpp b/src/app/Session.hpp deleted file mode 100644 index e69de29..0000000 diff --git a/src/app/main.cpp b/src/app/main.cpp index 59c8f7b..f25b3b0 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -1,6 +1,6 @@ -#include "Application.hpp" #include "DatabaseConnection.hpp" +#include #include #include @@ -9,7 +9,7 @@ Wt::WApplication * createApplication(const Wt::WEnvironment & env) { auto dbConnection = eedb::DbConnection{}.create(env); - return eedb::Application{}.create(env, std::move(dbConnection)); + return new eedb::EEDB{env, std::move(dbConnection)}; } int main(int argc, char ** argv) { diff --git a/src/eedb/Application.cpp b/src/eedb/Application.cpp deleted file mode 100644 index 939c71d..0000000 --- a/src/eedb/Application.cpp +++ /dev/null @@ -1,71 +0,0 @@ -#include "Application.hpp" - -#include -#include - -#include -#include -#include -#include -#include -#include - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -class AuthApplication : public Wt::WApplication { - public: - AuthApplication(const Wt::WEnvironment & env, std::unique_ptr< eedb::db::PgConnection > _database) - : Wt::WApplication(env), _dbConnection(std::move(_database)) { - _login.changed().connect(this, &AuthApplication::authEvent); - _userDatabase = std::make_unique< eedb::auth::PgUserAuth >(*_dbConnection, env); - - root()->addStyleClass("container"); - useStyleSheet("/resources/style.css"); - - setTheme(eedb::BootstrapTheme(this).create()); - - _authWidget = new eedb::AuthWidget(eedb::auth::Services{}, *_userDatabase, _login); - root()->addWidget(_authWidget); - } - - void authEvent() { - using namespace Wt; - - if(_login.loggedIn()) { - Wt::WContainerWidget * container = root(); - setInternalPath("/app"); - root()->removeStyleClass("container"); - new eedb::Home(container); - - } else { - root()->addWidget(_authWidget); - Wt::log("notice") << "User logged out."; - } - } - - private: - std::unique_ptr< eedb::db::PgConnection > _dbConnection; - std::unique_ptr< eedb::auth::PgUserAuth > _userDatabase; - Wt::Auth::Login _login; - eedb::AuthWidget * _authWidget; -}; - -namespace eedb { - -Wt::WApplication * Application::create(const Wt::WEnvironment & env, std::unique_ptr< eedb::db::PgConnection > connection) const { - return new AuthApplication(env, std::move(connection)); -} -} diff --git a/src/eedb/Application.hpp b/src/eedb/Application.hpp deleted file mode 100644 index 4aac96c..0000000 --- a/src/eedb/Application.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#include - -namespace Wt { -class WApplication; -class WEnvironment; -} - -namespace eedb::db { -class PgConnection; -} - -namespace eedb { -class Application { - public: - Wt::WApplication * create(const Wt::WEnvironment & env, std::unique_ptr< eedb::db::PgConnection > connection) const; -}; -} diff --git a/src/eedb/CMakeLists.txt b/src/eedb/CMakeLists.txt index a9063f7..8b44649 100644 --- a/src/eedb/CMakeLists.txt +++ b/src/eedb/CMakeLists.txt @@ -1,4 +1,5 @@ file(GLOB SOURCE + EEDB.cpp auth/PgUserAuth.cpp auth/Services.cpp data/* diff --git a/src/eedb/EEDB.cpp b/src/eedb/EEDB.cpp new file mode 100644 index 0000000..cf1fcb3 --- /dev/null +++ b/src/eedb/EEDB.cpp @@ -0,0 +1,60 @@ +#include "EEDB.hpp" + +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace eedb { + +EEDB::EEDB(const Wt::WEnvironment & env, std::unique_ptr< eedb::db::PgConnection > _database) + : Wt::WApplication(env), _dbConnection(std::move(_database)) { + _login.changed().connect(this, &EEDB::authEvent); + _userDatabase = std::make_unique< eedb::auth::PgUserAuth >(*_dbConnection, env); + + root()->addStyleClass("container"); + useStyleSheet("/resources/style.css"); + + setTheme(eedb::BootstrapTheme(this).create()); + + _authWidget = new eedb::AuthWidget(eedb::auth::Services{}, *_userDatabase, _login); + root()->addWidget(_authWidget); +} + +void EEDB::authEvent() { + using namespace Wt; + + if(_login.loggedIn()) { + Wt::WContainerWidget * container = root(); + setInternalPath("/app"); + root()->removeStyleClass("container"); + Wt::log("notice") << "Clearing root and creating widgets"; + new eedb::Home(container, &_authWidget->login()); + + } else { + Wt::log("notice") << "User logged out."; + redirect(url()); + quit(); + } +} +} diff --git a/src/eedb/EEDB.hpp b/src/eedb/EEDB.hpp new file mode 100644 index 0000000..e30864b --- /dev/null +++ b/src/eedb/EEDB.hpp @@ -0,0 +1,32 @@ +#include + +#include + +#include +#include + +// remove later +#include + +namespace Wt { +class WEnvironment; +} + +namespace eedb::db { +class PgConnection; +} + +namespace eedb { +class EEDB : public Wt::WApplication { + public: + EEDB(const Wt::WEnvironment & env, std::unique_ptr< eedb::db::PgConnection > _database); + + void authEvent(); + + private: + std::unique_ptr< eedb::db::PgConnection > _dbConnection; + std::unique_ptr< eedb::auth::PgUserAuth > _userDatabase; + Wt::Auth::Login _login; + eedb::AuthWidget * _authWidget; +}; +} diff --git a/src/eedb/widgets/AuthWidget.hpp b/src/eedb/widgets/AuthWidget.hpp index 2887e48..8477d02 100644 --- a/src/eedb/widgets/AuthWidget.hpp +++ b/src/eedb/widgets/AuthWidget.hpp @@ -1,3 +1,4 @@ +#pragma once #include namespace eedb::auth { diff --git a/src/eedb/widgets/MainWindow.cpp b/src/eedb/widgets/MainWindow.cpp index 8beaf56..373e42c 100644 --- a/src/eedb/widgets/MainWindow.cpp +++ b/src/eedb/widgets/MainWindow.cpp @@ -1,5 +1,6 @@ #include +#include #include #include #include @@ -15,7 +16,7 @@ namespace eedb { -eedb::Home::Home(Wt::WContainerWidget * container) { +eedb::Home::Home(Wt::WContainerWidget * container, Wt::Auth::Login * login) : _login(login) { // Create a navigation bar with a link to a web page. auto navigation = new Wt::WNavigationBar(container); navigation->setTitle("eedb", "http://eedb.pl"); @@ -45,7 +46,9 @@ eedb::Home::Home(Wt::WContainerWidget * container) { // Create a popup submenu for the Help menu. auto sessionMenuPopup = new Wt::WPopupMenu(); sessionMenuPopup->addItem("Logout"); -// sessionMenuPopup->itemSelected().connect([=](auto...) { _authWidget->login().logout(); }); + sessionMenuPopup->setId("session.menu"); + + sessionMenuPopup->itemSelected().connect([=](auto...) { const_cast< Wt::Auth::Login * >(_login)->logout(); }); auto sessionItem = new Wt::WMenuItem("Session"); sessionItem->setMenu(sessionMenuPopup); diff --git a/src/eedb/widgets/MainWindow.hpp b/src/eedb/widgets/MainWindow.hpp index 55c0a85..930200c 100644 --- a/src/eedb/widgets/MainWindow.hpp +++ b/src/eedb/widgets/MainWindow.hpp @@ -1,10 +1,16 @@ namespace Wt { class WContainerWidget; } +namespace Wt::Auth { +class Login; +} namespace eedb { class Home { public: - Home(Wt::WContainerWidget * container); + Home(Wt::WContainerWidget * container, Wt::Auth::Login * login); + + private: + const Wt::Auth::Login * _login; }; }