forked from SimulPiscator/AirSane
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpage.h
188 lines (166 loc) · 4.47 KB
/
webpage.h
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
AirSane Imaging Daemon
Copyright (C) 2018-2022 Simul Piscator
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef WEBPAGE_H
#define WEBPAGE_H
#include "basic/dictionary.h"
#include "httpserver.h"
#include <iostream>
#include <string>
#include <vector>
class WebPage
{
WebPage(const WebPage&) = delete;
WebPage& operator=(const WebPage&) = delete;
public:
WebPage();
virtual ~WebPage() {}
WebPage& setTitle(const std::string& s)
{
mTitle = s;
return *this;
}
const std::string& title() const { return mTitle; }
WebPage& setFavicon(const std::string& type, const std::string& url);
WebPage& clearFavicon();
WebPage& addStyle(const std::string&);
const std::string& style() const { return mStyle; }
WebPage& clearStyle();
WebPage& render(const HttpServer::Request&, HttpServer::Response&);
static std::string htmlEscape(const std::string&);
static std::string numtostr(double);
class element
{
public:
explicit element(const std::string& tag)
: mTag(tag)
{}
virtual ~element() {}
element& addText(const std::string& s) { return addContent(htmlEscape(s)); }
element& addText(double d) { return addText(numtostr(d)); }
element& addContent(const std::string& s)
{
mText += s;
return *this;
}
element& setAttribute(const std::string&, const std::string&);
element& setAttribute(const std::string& s, double d)
{
return setAttribute(s, numtostr(d));
}
const Dictionary& attributes() const { return mAttributes; }
virtual std::string toString() const;
private:
Dictionary mAttributes;
std::string mTag, mText;
};
struct br : element
{
br()
: element("br")
{}
std::string toString() const override { return element::toString() + "\n"; }
};
struct heading : element
{
explicit heading(int level)
: element("h" + numtostr(level))
{}
};
struct paragraph : element
{
paragraph()
: element("p")
{}
std::string toString() const override { return element::toString() + "\n"; }
};
struct list : element
{
list()
: element("ul")
{}
list& addItem(const std::string&);
list& addItem(const element&);
};
struct anchor : element
{
explicit anchor(const std::string& href = "")
: element("a")
{
setAttribute("href", href);
}
};
struct formField : element
{
explicit formField(const std::string& tag)
: element(tag)
{}
formField& setName(const std::string& s)
{
setAttribute("name", s);
return *this;
}
formField& setValue(const std::string& s)
{
setAttribute("value", s);
return *this;
}
formField& setLabel(const std::string& s)
{
mLabel = s;
return *this;
}
std::string toString() const override;
std::string labelHtml() const;
private:
std::string mLabel;
};
struct formInput : formField
{
explicit formInput(const std::string& type)
: formField("input")
{
setAttribute("type", type);
}
};
struct formSelect : formField
{
formSelect()
: formField("select")
{}
formSelect& addOption(const std::string& value,
const std::string& text = "");
formSelect& addOptions(const Dictionary&);
formSelect& addOptions(const std::vector<std::string>&);
std::string toString() const override;
private:
Dictionary mOptions;
};
protected:
virtual void onRender() = 0;
std::ostream& out() const { return *mpOut; }
const HttpServer::Request& request() const { return *mpRequest; }
HttpServer::Response& response() const { return *mpResponse; }
private:
std::string mTitle, mStyle, mFaviconType, mFaviconUrl;
std::ostream* mpOut;
const HttpServer::Request* mpRequest;
HttpServer::Response* mpResponse;
};
inline std::ostream&
operator<<(std::ostream& os, const WebPage::element& el)
{
return os << el.toString();
}
#endif // WEBPAGE_H