Open Lighting Architecture 0.10.9
Loading...
Searching...
No Matches
JsonSections.h
1/*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15 *
16 * JsonSections.h
17 * This builds the json string for the web UI.
18 * Copyright (C) 2010 Simon Newton
19 */
20
21#ifndef INCLUDE_OLA_WEB_JSONSECTIONS_H_
22#define INCLUDE_OLA_WEB_JSONSECTIONS_H_
23
24#include <ola/StringUtils.h>
25#include <ola/web/Json.h>
26#include <string>
27#include <utility>
28#include <vector>
29
30namespace ola {
31namespace web {
32
43 public:
44 GenericItem(const std::string &description, const std::string &id):
45 m_description(description),
46 m_id(id),
47 m_button_text("") {
48 }
49 virtual ~GenericItem() {}
50
51 // Sets the text for the button associated
52 void SetButtonText(const std::string &text) {
53 m_button_text = text;
54 }
55
56 void PopulateItem(JsonObject *item) const;
57
58 protected:
59 virtual std::string Type() const = 0;
60 virtual void SetValue(JsonObject *item) const = 0;
61 virtual void SetExtraProperties(JsonObject *item) const {
62 (void) item;
63 }
64
65 private:
66 std::string m_description;
67 std::string m_id;
68 std::string m_button_text;
69};
70
71
72/*
73 * This is a item that contains a string value
74 */
75class StringItem: public GenericItem {
76 public:
77 StringItem(const std::string &description,
78 const std::string &value,
79 const std::string &id = ""):
80 GenericItem(description, id),
81 m_value(value) {
82 }
83
84 protected:
85 std::string Type() const { return "string"; }
86 void SetValue(JsonObject *item) const {
87 item->Add("value", m_value);
88 }
89
90 private:
91 std::string m_value;
92};
93
94
95/*
96 * An item that contains a unsigned int
97 */
98class UIntItem: public GenericItem {
99 public:
100 UIntItem(const std::string &description,
101 unsigned int value,
102 const std::string &id = ""):
103 GenericItem(description, id),
104 m_value(value),
105 m_min_set(false),
106 m_max_set(false),
107 m_min(0),
108 m_max(0) {
109 }
110
111 void SetMin(unsigned int min) {
112 m_min_set = true;
113 m_min = min;
114 }
115
116 void SetMax(unsigned int max) {
117 m_max_set = true;
118 m_max = max;
119 }
120
121 protected:
122 void SetExtraProperties(JsonObject *item) const;
123 std::string Type() const { return "uint"; }
124 void SetValue(JsonObject *item) const {
125 item->Add("value", m_value);
126 }
127
128 private:
129 unsigned int m_value;
130 bool m_min_set, m_max_set;
131 unsigned int m_min;
132 unsigned int m_max;
133};
134
135
136class BoolItem: public GenericItem {
137 public:
138 BoolItem(const std::string &description,
139 bool value,
140 const std::string &id):
141 GenericItem(description, id),
142 m_value(value) {
143 }
144
145 protected:
146 std::string Type() const { return "bool"; }
147 void SetValue(JsonObject *item) const {
148 item->Add("value", m_value);
149 }
150
151 private:
152 bool m_value;
153};
154
155
156class HiddenItem: public GenericItem {
157 public:
158 HiddenItem(const std::string &value, const std::string &id):
159 GenericItem("", id),
160 m_value(value) {
161 }
162
163 protected:
164 std::string Type() const { return "hidden"; }
165 void SetValue(JsonObject *item) const {
166 item->Add("value", m_value);
167 }
168
169 private:
170 std::string m_value;
171};
172
173
174/*
175 * An item which is a select list
176 */
177class SelectItem: public GenericItem {
178 public:
179 SelectItem(const std::string &description,
180 const std::string &id = ""):
181 GenericItem(description, id),
182 m_selected_offset(0) {
183 }
184
185 void SetSelectedOffset(unsigned int offset) { m_selected_offset = offset; }
186 void AddItem(const std::string &label, const std::string &value);
187 // helper method which converts ints to strings
188 void AddItem(const std::string &label, unsigned int value);
189
190 protected:
191 void SetExtraProperties(JsonObject *item) const {
192 item->Add("selected_offset", m_selected_offset);
193 }
194 std::string Type() const { return "select"; }
195 void SetValue(JsonObject *item) const;
196
197 private:
198 std::vector<std::pair<std::string, std::string> > m_values;
199 unsigned int m_selected_offset;
200};
201
202
204 public:
205 explicit JsonSection(bool allow_refresh = true);
206 ~JsonSection();
207
208 void SetSaveButton(const std::string &text) { m_save_button_text = text; }
209 void SetError(const std::string &error) { m_error = error; }
210
211 void AddItem(const GenericItem *item);
212 std::string AsString() const;
213
214 private:
215 bool m_allow_refresh;
216 std::string m_error;
217 std::string m_save_button_text;
218 std::vector<const GenericItem*> m_items;
219};
220} // namespace web
221} // namespace ola
222#endif // INCLUDE_OLA_WEB_JSONSECTIONS_H_
Basic data types used to represent elements in a JSON document.
Various string utility functions.
Definition JsonSections.h:136
Definition JsonSections.h:42
Definition JsonSections.h:156
A JSON object. JSON Objects are key : value mappings, similar to dictionaries in Python.
Definition Json.h:899
void Add(const std::string &key, const std::string &value)
Add a key to string mapping.
Definition Json.cpp:607
Definition JsonSections.h:203
JsonSection(bool allow_refresh=true)
Definition JsonSections.cpp:91
~JsonSection()
Definition JsonSections.cpp:101
void AddItem(const GenericItem *item)
Definition JsonSections.cpp:112
Definition JsonSections.h:177
Definition JsonSections.h:75
Definition JsonSections.h:98
The namespace containing all OLA symbols.
Definition Credentials.cpp:44