Project

General

Profile

RE: WT3 to WT4 Port » main.C

Sebastian Fett, 06/17/2019 11:51 AM

 
/**************************************************************
* FPMWebGui Main-Application *
* -------------------------- *
* Handles the authentication screen and in advance sessions. *
**************************************************************/



// +------------+
// | Inclusions |
// +------------+

// - Wt-Library-Inclusions
// - - General
#include <Wt/WString> // https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1WString.html
// - - Basic Widgets
#include <Wt/WApplication> // https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1WApplication.html
#include <Wt/WEnvironment> // https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1WEnvironment.html
#include <Wt/WBootstrapTheme> // https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1WBootstrapTheme.html
#include <Wt/WCssTheme> // https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1WCssTheme.html
#include <Wt/WContainerWidget> // https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1WContainerWidget.html
#include <Wt/WHBoxLayout> // https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1WHBoxLayout.html
#include <Wt/WImage> // https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1WImage.html
// - - Web Application Server
#include <Wt/WServer> // https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1WServer.html
// - - Database
#include <Wt/Dbo/Dbo> // https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Dbo_1_1Dbo.html
// - - Authentication
#include <Wt/Auth/AuthModel> // https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Auth_1_1AuthModel.html
#include <Wt/Auth/AuthWidget> // https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Auth_1_1AuthWidget.html
#include <Wt/Auth/PasswordService> // https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Auth_1_1PasswordService.html
// - Custom Inclusions
#include "main.h" // Prototype definition of class "AuthApplication" and its functions.
#include "model/Session.h" //
#include "BaseScreen.h" //



// +--------------------------------+
// | Class-Function-Implementations |
// +--------------------------------+

AuthApplication::AuthApplication(const Wt::WEnvironment& env) :
Wt::WApplication(env),
session_() {
session_.login().changed().connect(this, &AuthApplication::authEvent);
createLoginScreen();
}

// Creates the Login-Screen.
void AuthApplication::createLoginScreen() {
root()->addStyleClass("container");
setTheme(new Wt::WBootstrapTheme());
//WApplication::requireJQuery(), solves the jQuery version mismatch bug at my personal machine.
requireJQuery("resources/jquery.min.js");
//This need not be checked in.
Wt::WContainerWidget *container_logo = new Wt::WContainerWidget();
Wt::WImage *image = new Wt::WImage(Wt::WLink("icons/wt_powered.jpg"),container_logo);
Wt::Auth::AuthWidget *authWidget = new Wt::Auth::AuthWidget(Session::auth(), session_.users(), session_.login());
authWidget->model()->addPasswordAuth(&Session::passwordAuth());
authWidget->setRegistrationEnabled(true);
authWidget->processEnvironment();
root()->addWidget(container_logo);
root()->addWidget(authWidget);
}

// Handles user-logins and user-logouts.
void AuthApplication::authEvent() {
//std::cout<<"---------INSIDE the authEvent() function block------------\n"; // -> von Rajveer; auskommentiert von Stefan
if (session_.login().loggedIn()) {
const Wt::Auth::User& u = session_.login().user();
Wt::log("notice")
<< "User " << u.id()
<< " (" << u.identity(Wt::Auth::Identity::LoginName) << ")"
<< " logged in.";
//populating the User table with details of the newly created user // "newly created"?
session_.addNames();
// session_.addDataFilling();
// dbo::Transaction transaction(session_);
// Wt::Dbo::ptr<User> user = session_.user(u);
// // user.modify()->name = valueText(u.identity(Wt::Auth::Identity::LoginName)).toUTF8();
// user.modify()->name = (u.identity(Wt::Auth::Identity::LoginName)).toUTF8();
createApplicationTree();
} else {
Wt::log("notice") << "User logged out.";
//createApplicationTree();
root()->clear();
createLoginScreen();
}
}

void AuthApplication::createApplicationTree() {
if (appRoot().empty()) {
std::cerr << "!!!!!!!!!!" << std::endl
<< "!! Warning: read the README.md file for hints on deployment,"
<< " the approot looks suspect!" << std::endl
<< "!!!!!!!!!!" << std::endl;
}
root()->removeStyleClass("container");
// root()->setTheme(new Wt::WBootstrapTheme(app));

// load text bundles (for the tr() function)
messageResourceBundle().use(appRoot() + "text");
// app->messageResourceBundle().use(app->appRoot() + "src");
Wt::WHBoxLayout *layout = new Wt::WHBoxLayout(root());
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(new BaseScreen(session_));

setTitle("FPMWeb");

useStyleSheet("style/everywidget.css");
useStyleSheet("style/dragdrop.css");
}



// +----------------------+
// | Actual Main-Function |
// +----------------------+

Wt::WApplication *createApplication(const Wt::WEnvironment& env) {
return new AuthApplication(env);
}

int main(int argc, char **argv) {
try {
Wt::WServer server(argc, argv, WTHTTP_CONFIGURATION);

server.addEntryPoint(Wt::Application, createApplication);

Session::configureAuth();

server.run();

Wt::WApplication *app = Wt::WApplication::instance();

app->internalPathChanged().connect(std::bind([=] () {
createApplication(app->environment());
}));
} catch (Wt::WServer::Exception& e) {
std::cerr << "main.C::main::WServer-Exception: " << e.what() << std::endl;
} catch (std::exception &e) {
std::cerr << "main.C::main::Exception: " << e.what() << std::endl;
}
}
(3-3/5)