-
Notifications
You must be signed in to change notification settings - Fork 1
/
Page.hpp
106 lines (95 loc) · 2.59 KB
/
Page.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* =====================================================================================
*
* Filename: Page.hpp
*
* Description: Helper class for loading and saving pages. Bridges mongodb and witty.
*
* Version: 1.0
* Created: 06/03/2011 09:41:13 AM
* Revision: none
* Compiler: gcc
*
* Author: Matthew Sherborne (), msherborne@gmail.com
* Company:
*
* =====================================================================================
*/
#ifndef PAGE_HPP
#define PAGE_HPP
#include "MoreAwesomeTemplate.hpp"
#include <mongo/client/connpool.h>
using mongo::BSONObj;
namespace vidanueva {
// Forward declarations
class VidaApp;
/**
* @brief Holds the data from the DB for a page
*/
class PageData {
private:
string _name;
string _title;
string _body;
public:
PageData (const string& name, const string& title, const string& body) :
_name(name), _title(title), _body(body) {}
PageData (const BSONObj& raw) :
_name(raw["name"].String()),
_title(raw["title"].String()),
_body(raw["body"].String()) {}
BSONObj toBSON() const;
const string& name() const {return _name;}
const string& title() const {return _title;}
const string& body() const {return _body;}
};
/**
* @brief This holds an actual page widget and its data.
*
*
*/
class Page : public MoreAwesomeTemplate {
public:
Page(PageData& data) : MoreAwesomeTemplate() {
setTemplateText(tr("page-template"));
bindString("title", data.title());
bindString("body", data.body());
}
};
/**
* @brief Represents a cache entry for a Page
*/
class PageCacheEntry {
private:
PageData _data;
unsigned long _hits;
time_t _lastHit;
public:
PageCacheEntry(const string& name, const string& title, const string& body) : _data(name, title, body), _hits(1), _lastHit(time(NULL)) {}
PageCacheEntry(const BSONObj& raw) : _data(raw), _hits(0), _lastHit(time(NULL)) {}
void hit() {
_lastHit = time(NULL);
_hits += 1;
}
unsigned long hits() {return _hits;}
time_t lastHit() {return _lastHit;}
Page* createPage() {return new Page(_data);}
PageData& data() {return _data;}
};
/**
* @brief Creates, loads and caches pages
*/
class PageFactory {
private:
typedef map<std::string, PageCacheEntry> CacheType;
VidaApp* app;
CacheType cache;
protected:
public:
PageFactory(VidaApp* app) : app(app) {}
static Page* LoadFromDB(const string& name);
Page* load(const string& name);
void save(const PageData& data);
};
} // namespace vidanueva
#endif // PAGE_HPP