#include "Hello.h" #include "BoostLog.h" #include "Dialog.h" #include "LoginWidget.h" #include "Session.h" #include "WebApplication.h" #include #include #include #include #include #include #include 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"); LOG(info) << "app root: " << appRoot(); m_session = std::make_unique(connectionPool); m_session->login().changed().connect(this, &Hello::authEvent); setTitle("Hello world"); if (!embedded) { setTheme(std::make_shared()); m_root = root(); } else { std::unique_ptr topPtr = std::make_unique(); m_root = topPtr.get(); const std::string *div = env.getParameter("div"); if (div) { setJavaScriptClass(*div); bindWidget(std::move(topPtr), *div); } else { LOG(error) << "Missing: parameter: 'div'"; } internalPathChanged().connect(this, &Hello::handlePathChange); auto externalPath = env.getParameter("path"); if (externalPath != nullptr) { m_externalPath = *externalPath; LOG(info) << "external path: " << m_externalPath; } else { auto parameters = env.getParameterMap(); for (auto &p : parameters) { LOG(info) << p.first; } } LOG(info) << "url: " << url(); LOG(info) << "relative resources url: " << relativeResourcesUrl(); LOG(info) << "resources url: " << resourcesUrl(); } if (!embedded) { root()->addWidget(std::make_unique("

Note: you can also run this application " "from within a web page.

")); } m_root->addWidget(std::make_unique("Your name, please ? ")); m_nameEdit = m_root->addWidget(std::make_unique()); m_nameEdit->setFocus(); auto b = m_root->addWidget(std::make_unique("点击我!")); b->setMargin(5, Wt::Side::Left); m_root->addWidget(std::make_unique()); m_greeting = m_root->addWidget(std::make_unique()); b->clicked().connect(this, &Hello::greet); m_nameEdit->enterPressed().connect(this, &Hello::greet); auto app = Amass::Singleton::instance(); m_root->addWidget(std::make_unique()); if (!m_externalPath.empty()) { handlePathChange(m_externalPath); } } Hello::~Hello() { } void Hello::greet() { m_greeting->setText("Hello there, " + m_nameEdit->text()); setInternalPath(m_externalPath); } 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."; } } void Hello::handlePathChange(const std::string &path) { LOG(info) << "handlePathChange: " << path; if (path.starts_with("/wt/login") || path.starts_with("/wt/register")) { m_root->clear(); m_root->setStyleClass("WtCenterContainer"); m_root->addNew(m_session->users(), m_session->login()); } }