Older/WebApplication/Hello.cpp

108 lines
3.7 KiB
C++
Raw Normal View History

2024-10-30 23:59:59 +08:00
#include "Hello.h"
#include "BoostLog.h"
2024-10-31 23:30:34 +08:00
#include "Dialog.h"
2024-11-19 23:39:40 +08:00
#include "LoginWidget.h"
2024-10-31 19:40:29 +08:00
#include "Session.h"
2024-11-11 19:21:48 +08:00
#include "WebApplication.h"
2024-10-31 19:40:29 +08:00
#include <Wt/Auth/Identity.h>
#include <Wt/WBootstrap2Theme.h>
2024-10-30 23:59:59 +08:00
#include <Wt/WContainerWidget.h>
#include <Wt/WEnvironment.h>
#include <Wt/WLineEdit.h>
#include <Wt/WPushButton.h>
#include <Wt/WText.h>
2024-11-19 23:39:40 +08:00
Hello::Hello(const Wt::WEnvironment &env, Wt::Dbo::SqlConnectionPool &connectionPool, bool embedded)
: Wt::WApplication(env) {
messageResourceBundle().use(appRoot() + "wt");
messageResourceBundle().use(appRoot() + "auth_strings");
messageResourceBundle().use(appRoot() + "auth_css_theme");
useStyleSheet("/resources/app.css");
2024-10-31 19:40:29 +08:00
LOG(info) << "app root: " << appRoot();
2024-11-14 23:06:22 +08:00
m_session = std::make_unique<Session>(connectionPool);
2024-10-31 19:40:29 +08:00
m_session->login().changed().connect(this, &Hello::authEvent);
2024-10-30 23:59:59 +08:00
setTitle("Hello world");
if (!embedded) {
2024-11-19 00:06:01 +08:00
setTheme(std::make_shared<Wt::WBootstrap2Theme>());
2024-11-19 23:39:40 +08:00
m_root = root();
2024-10-30 23:59:59 +08:00
} else {
std::unique_ptr<Wt::WContainerWidget> topPtr = std::make_unique<Wt::WContainerWidget>();
2024-11-19 23:39:40 +08:00
m_root = topPtr.get();
2024-10-30 23:59:59 +08:00
const std::string *div = env.getParameter("div");
if (div) {
setJavaScriptClass(*div);
bindWidget(std::move(topPtr), *div);
} else {
LOG(error) << "Missing: parameter: 'div'";
}
2024-11-19 19:23:08 +08:00
internalPathChanged().connect(this, &Hello::handlePathChange);
auto externalPath = env.getParameter("path");
if (externalPath != nullptr) {
m_externalPath = *externalPath;
2024-11-19 23:39:40 +08:00
LOG(info) << "external path: " << m_externalPath;
2024-11-19 19:23:08 +08:00
} else {
auto parameters = env.getParameterMap();
for (auto &p : parameters) {
LOG(info) << p.first;
}
}
2024-10-30 23:59:59 +08:00
LOG(info) << "url: " << url();
LOG(info) << "relative resources url: " << relativeResourcesUrl();
LOG(info) << "resources url: " << resourcesUrl();
}
if (!embedded) {
root()->addWidget(std::make_unique<Wt::WText>("<p><emph>Note: you can also run this application "
"from within <a href=\"hello.html\">a web page</a>.</emph></p>"));
}
2024-11-19 23:39:40 +08:00
m_root->addWidget(std::make_unique<Wt::WText>("Your name, please ? "));
m_nameEdit = m_root->addWidget(std::make_unique<Wt::WLineEdit>());
2024-10-30 23:59:59 +08:00
m_nameEdit->setFocus();
2024-11-19 23:39:40 +08:00
auto b = m_root->addWidget(std::make_unique<Wt::WPushButton>("点击我!"));
2024-10-30 23:59:59 +08:00
b->setMargin(5, Wt::Side::Left);
2024-11-19 23:39:40 +08:00
m_root->addWidget(std::make_unique<Wt::WBreak>());
2024-10-30 23:59:59 +08:00
2024-11-19 23:39:40 +08:00
m_greeting = m_root->addWidget(std::make_unique<Wt::WText>());
2024-10-30 23:59:59 +08:00
b->clicked().connect(this, &Hello::greet);
m_nameEdit->enterPressed().connect(this, &Hello::greet);
2024-10-31 19:40:29 +08:00
2024-11-11 19:21:48 +08:00
auto app = Amass::Singleton<WebApplication>::instance();
2024-10-31 23:30:34 +08:00
2024-11-19 23:39:40 +08:00
m_root->addWidget(std::make_unique<Dialog>());
if (!m_externalPath.empty()) {
handlePathChange(m_externalPath);
}
2024-10-31 19:40:29 +08:00
}
Hello::~Hello() {
2024-10-30 23:59:59 +08:00
}
void Hello::greet() {
m_greeting->setText("Hello there, " + m_nameEdit->text());
2024-11-19 19:23:08 +08:00
setInternalPath(m_externalPath);
2024-10-30 23:59:59 +08:00
}
2024-10-31 19:40:29 +08:00
void Hello::authEvent() {
if (m_session->login().loggedIn()) {
const Wt::Auth::User &u = m_session->login().user();
LOG(info) << "User " << u.id() << " (" << u.identity(Wt::Auth::Identity::LoginName) << ")"
<< " logged in.";
} else {
LOG(info) << "User logged out.";
}
}
2024-11-19 19:23:08 +08:00
void Hello::handlePathChange(const std::string &path) {
LOG(info) << "handlePathChange: " << path;
2024-11-19 23:39:40 +08:00
if (path.starts_with("/wt/login") || path.starts_with("/wt/register")) {
m_root->clear();
m_root->setStyleClass("WtCenterContainer");
m_root->addNew<LoginWidget>(m_session->users(), m_session->login());
}
2024-11-19 19:23:08 +08:00
}