Skip to content

Commit

Permalink
Can now add pages and save them, but not see them
Browse files Browse the repository at this point in the history
  • Loading branch information
matiu committed Jun 11, 2011
1 parent 73982b9 commit 052efba
Show file tree
Hide file tree
Showing 310 changed files with 41,439 additions and 90 deletions.
64 changes: 48 additions & 16 deletions App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <Wt/WLogger>
#include <Wt/WDialog>
#include <Wt/WPushButton>
#include <Wt/WFileResource>
#include <string>
#include "LoginWindow.hpp"

Expand All @@ -35,32 +36,41 @@ using Wt::WApplication;
using Wt::WEnvironment;
using Wt::WDialog;
using Wt::WServer;
using mongo::ScopedDbConnection;

namespace vidanueva {

VidaApp::VidaApp(const WEnvironment &environment) : WApplication(environment) {
log("NOTICE") << "Resources URL: " << resourcesUrl();
// Set up our signals
_userChanged = new AppSignal(this);
// Add some styles
useStyleSheet(resourcesUrl() + "/themes/" + cssTheme() + "/forms.css");
useStyleSheet(resourcesUrl() + "/themes/" + cssTheme() + "/fonts.css");
useStyleSheet(resourcesUrl() + "/themes/" + cssTheme() + "/controlPanel.css");
// Configure our user login look up tool
std::string mongoHost, mongoDb, mongoUsersTable;
readConfigurationProperty("mongo-host", mongoHost);
readConfigurationProperty("mongo-db", mongoDb);
std::string mongoUsersTable;
readConfigurationProperty("mongo-host", _mongoHostName);
readConfigurationProperty("mongo-db", _mongoDB);
readConfigurationProperty("mongo-users-table", mongoUsersTable);
_userManager.configure(mongoHost, mongoDb, mongoUsersTable);
_userManager.configure(_mongoHostName, _mongoDB, mongoUsersTable);
// Load our message bundles
// Bundles we'll use a lot
messageResourceBundle().use(appRoot() + "messages/MainWindow");
messageResourceBundle().use(appRoot() + "messages/LoginWindow");
messageResourceBundle().use(appRoot() + "messages/ControlPanel");
messageResourceBundle().use(appRoot() + "messages/DialogButtonBar");
messageResourceBundle().use(appRoot() + "messages/misc");
messageResourceBundle().use(appRoot() + "messages/EditButtonBar");
// Bundles we won't use quite so much
messageResourceBundle().use(appRoot() + "messages/LoginWindow", false);
messageResourceBundle().use(appRoot() + "messages/ControlPanel", false);
messageResourceBundle().use(appRoot() + "messages/PageEdit", false);
// Set up the UI
setTitle(WString::tr("main-title"));
_mainWindow = new MainWindow(root());
setBodyClass("yui-skin-sam");
// Hook up the url event handlers
internalPathChanged().connect(this, &VidaApp::onURLChange);
setInternalPath("/", true); // Go home, no matter what URL they put in
}

/**
Expand All @@ -77,32 +87,52 @@ void VidaApp::onURLChange(const std::string& path) {
showLoginDialog();
} else {
log("NOTICE") << "GOING HOME 1";
setInternalPath("/"); // Back to the home page .. can't login twice
goHome(); // Back to the home page .. can't login twice
}
} else if (internalPathMatches("/page")) {
// Look up the page that belongs here
mainWindow()->setBody(new WPushButton(path)); // TODO: actually show the page
}

}

/**
* @brief Shows a modal login dialog
*/
void VidaApp::showLoginDialog() {
log("NOTICE") << "Showing login dialog";
WDialog dialog(WString::tr("login"));
LoginWindow* loginWindow = new LoginWindow(dialog);
loginWindow->setFocus();
if (dialog.exec() == WDialog::Accepted) {
dialog.hide();
// See if we can log them in
_userManager.savePassword(loginWindow->username(), loginWindow->password());
if (_userManager.checkLogin(loginWindow->username(), loginWindow->password())) {
_username = loginWindow->username();
log("NOTICE") << loginWindow->username() << " logged in";
log("NOTICE") << "GOING HOME 2";
setInternalPath("/"); // Back to the home page
log("SECURITY") << loginWindow->username() << " logged in";
goHome(); // Back to the home page
_userChanged->emit(this);
return;
} else {
mainWindow()->setStatusText(WString::tr("Wrong username or password"));
log("SECURITY") << loginWindow->username() << " failed log in";
}
log("NOTICE") << loginWindow->username() << " failed log in";
}
_username = ""; // If we make it here .. we're not logged in anymore
log("NOTICE") << "GOING HOME 3";
setInternalPath("/"); // Back to the home page
goHome(); // Back to the home page
}

/**
* @brief Insert/Update a record in the mongo DB
*
* @param tableName The name of the table to insert/update on
* @param index The index to look up the existing record
* @param data The data to replace/insert into the table
*/
void VidaApp::mongoSave(const string& tableName, mongo::BSONObj& index, mongo::BSONObj& data) {
ScopedDbConnection db(_mongoHostName);
db->update(mongoNSFor(tableName), index, data, true);
db.done();
}


Expand All @@ -124,7 +154,7 @@ WApplication *createApplication(const WEnvironment& env) { return new VidaApp(en
*/
WApplication *createRedirectApp(const WEnvironment& env) {
WApplication* app = new WApplication(env);
app->redirect("/vida");
app->redirect("/vida" + app->internalPath());
app->quit();
return app;
}
Expand All @@ -137,7 +167,7 @@ int main(int argc, char **argv) {
server.setServerConfiguration(argc, argv, WTHTTP_CONFIGURATION);

server.addEntryPoint(Wt::Application, vidanueva::createApplication, "/vida", "/css/favicon.ico");
server.addEntryPoint(Wt::Application, vidanueva::createRedirectApp, "", "/css/favicon.ico");
server.addEntryPoint(Wt::Application, vidanueva::createRedirectApp, "/", "/css/favicon.ico");

if (server.start()) {
WServer::waitForShutdown();
Expand All @@ -149,3 +179,5 @@ int main(int argc, char **argv) {
std::cerr << "exception: " << e.what() << std::endl;
}
}


15 changes: 13 additions & 2 deletions App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
#ifndef APP_HPP
#define APP_HPP

#include <Wt/WApplication>
#include <Wt/WSignal>
#include "UserManager.hpp"
#include "MainWindow.hpp"
#include <Wt/WApplication>
#include <Wt/WSignal>
#include <string>
#include <mongo/client/connpool.h>

namespace Wt {
class WEnvironment;
Expand All @@ -31,6 +33,8 @@ namespace Wt {
using Wt::WEnvironment;
using Wt::WApplication;
using Wt::Signal;
using std::string;
using mongo::BSONObj;

namespace vidanueva {

Expand All @@ -44,12 +48,19 @@ class VidaApp : public WApplication {
void onURLChange(const std::string& path);
void showLoginDialog();
AppSignal* _userChanged;
string _mongoHostName;
string _mongoDB;
public:
VidaApp(const WEnvironment &environment);
bool loggedIn() { return !_username.empty(); } /// Returns true if a user is logged in, otherwise false if current user is anonymous
std::string username() { return _username; } /// Returns the username if someone is logged in, "" otherwise
AppSignal* userChanged() { return _userChanged; } /// An event triggered when a user logs in or logs out
MainWindow* mainWindow() { return _mainWindow; } /// A pointer to the main window widget
void goHome() { setInternalPath("/", true); }
const string& mongoHostname() { return _mongoDB; } /// Returns the hostname of our mongo db
const string& mongoDB() { return _mongoDB; } /// Returns the name of the actual database inside of mongo
const string mongoNSFor(const string& tableName) { return _mongoDB + "." + tableName; } /// Returns the mongo namespace for any given tablename eg: "pages" => "vidanueva.pages"
void mongoSave(const string& tableName, mongo::BSONObj& index, mongo::BSONObj& data);
};


Expand Down
22 changes: 16 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ ENDIF()
configure_file(wt-config.xml wt-config.xml)

# Sort out our libraries
find_library(wt wt)
find_library(wthttp wthttp)
if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
find_library(wt wtd)
find_library(wthttp wthttpd)
else()
find_library(wt wt)
find_library(wthttp wthttp)
endif()
find_library(mongoclient mongoclient)
find_library(ssl ssl) # open ssl for the sha1 for the passwords

Expand All @@ -24,10 +29,15 @@ set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
#set(Boost_COMPILER gcc45)
#set(Boost_REALPATH ON)
set(Boost_REALPATH ON)
#set(DYLD_LIBRARY_PATH /opt/local/lib)
find_package(Boost 1.42.0 REQUIRED COMPONENTS system)
find_package(Boost 1.42.0 REQUIRED COMPONENTS system program_options signals date_time filesystem regex)
include_directories(BEFORE ${Boost_INCLUDE_DIRS})

# Build the executable
ADD_EXECUTABLE(${PROJECT_NAME} App.cpp MainWindow.cpp LoginWindow.cpp UserManager.cpp)
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${Boost_SYSTEM_LIBRARY} wt wthttp mongoclient ssl)
ADD_EXECUTABLE(${PROJECT_NAME} App.cpp
MainWindow.cpp LoginWindow.cpp UserManager.cpp ControlPanel.cpp PageEdit.cpp)
TARGET_LINK_LIBRARIES(${PROJECT_NAME}
${Boost_SYSTEM_LIBRARY} ${Boost_PROGRAM_OPTIONS_LIBRARY} ${Boost_SIGNALS_LIBRARY}
${Boost_DATE_TIME_LIBRARY} ${Boost_FILESYSTEM_LIBRARY} ${Boost_REGEX_LIBRARY}
${wt} ${wthttp} mongoclient ssl)
49 changes: 49 additions & 0 deletions ControlPanel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* =====================================================================================
*
* Filename: ControlPanel.cpp
*
* Description: Control Panel code goes here
*
* Version: 1.0
* Created: 06/03/2011 09:39:46 AM
* Revision: none
* Compiler: gcc
*
* Author: Matthew Sherborne (), msherborne@gmail.com
* Company:
*
* =====================================================================================
*/

#include "ControlPanel.hpp"
#include "PageEdit.hpp"
#include <Wt/WDialog>
#include"App.hpp"

using Wt::WDialog;

namespace vidanueva {

const string ControlPanel::addPageURL = "/newPage";

/**
* Called when the user hits the add page button. Shows the add a new page dialog.
*/
void ControlPanel::addNewPage() {
PageEdit* pageEdit = new PageEdit();
getApp()->mainWindow()->setBody(pageEdit);
}

/**
* @brief If someone navigates to /page/new .. show a new page dialog.
*
* @param path
*/
void ControlPanel::urlChanged(const std::string& path) {
if (path == addPageURL) {
addNewPage();
}
}

} // namespace vidanueva
23 changes: 16 additions & 7 deletions ControlPanel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,39 @@
* =====================================================================================
*/

#ifndef CONTROLS_HPP
#define CONTROLS_HPP
#ifndef CONTROL_PANEL_HPP
#define CONTROL_PANEL_HPP

#include<Wt/WContainerWidget>
#include<Wt/WPushButton>
#include<Wt/WAnchor>
#include<string>
#include"App.hpp"

using Wt::WContainerWidget;
using Wt::WPushButton;
using Wt::WAnchor;
using std::string;

namespace vidanueva {

class ControlPanel : public WContainerWidget {
public:
static const string addPageURL;
private:
WPushButton* _addPageBtn;
WAnchor* _addPageLink;
public:
ControlPanel(WContainerWidget* parent=0) : WContainerWidget(parent) {
VidaApp* app = getApp();
setId("control-panel");
_addPageBtn = new WPushButton(tr("add-page"), this);
_addPageLink = new WAnchor(this);
_addPageLink->setText(tr("add-page"));
_addPageLink->setRefInternalPath(addPageURL);
app->internalPathChanged().connect(this, &ControlPanel::urlChanged);
}
void addNewPage();
void urlChanged(const std::string& path);
};


} // namespace vidanueva

#endif // CONTROLS_HPP
#endif // CONTROL_PANEL_HPP
47 changes: 47 additions & 0 deletions DialogButtonBar.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* =====================================================================================
*
* Filename: DialogButtonBar.hpp
*
* Description: Gives you an OK and cancel button to sit at the bottom of a dialog
*
* Version: 1.0
* Created: 06/04/2011 09:49:11 AM
* Revision: none
* Compiler: gcc
*
* Author: Matthew Sherborne (), msherborne@gmail.com
* Company:
*
* =====================================================================================
*/

#ifndef DIALOG_BUTTON_BAR_HPP
#define DIALOG_BUTTON_BAR_HPP

#include <Wt/WDialog>
#include <Wt/WPushButton>
#include "MoreAwesomeTemplate.hpp"

using Wt::WPushButton;
using Wt::WDialog;

namespace vidanueva {

class DialogButtonBar : public MoreAwesomeTemplate {
private:
WPushButton* _okBtn;
WPushButton* _cancelBtn;
public:
DialogButtonBar(WDialog& dialog) : MoreAwesomeTemplate(dialog.contents()) {
setTemplateText(tr("dialog-button-bar"));
bindAndCreateWidget(_okBtn, "ok-btn", tr("ok-btn"));
bindAndCreateWidget(_cancelBtn, "cancel-btn", tr("cancel-btn"));
_okBtn->clicked().connect(&dialog, &WDialog::accept);
_cancelBtn->clicked().connect(&dialog, &WDialog::reject);
}
};

}

#endif // DIALOG_BUTTON_BAR_HPP
Loading

0 comments on commit 052efba

Please sign in to comment.