Open Lighting Architecture  0.9.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
30 namespace ola {
31 namespace web {
32 
42 class GenericItem {
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  */
75 class 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  */
98 class 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  }
108 
109  void SetMin(unsigned int min) {
110  m_min_set = true;
111  m_min = min;
112  }
113  void SetMax(unsigned int max) {
114  m_max_set = true;
115  m_max = max;
116  }
117 
118  protected:
119  void SetExtraProperties(JsonObject *item) const;
120  std::string Type() const { return "uint"; }
121  void SetValue(JsonObject *item) const {
122  item->Add("value", m_value);
123  }
124 
125  private:
126  unsigned int m_value;
127  bool m_min_set, m_max_set;
128  unsigned int m_min;
129  unsigned int m_max;
130 };
131 
132 
133 class BoolItem: public GenericItem {
134  public:
135  BoolItem(const std::string &description,
136  bool value,
137  const std::string &id):
138  GenericItem(description, id),
139  m_value(value) {
140  }
141 
142  protected:
143  std::string Type() const { return "bool"; }
144  void SetValue(JsonObject *item) const {
145  item->Add("value", m_value);
146  }
147 
148  private:
149  bool m_value;
150 };
151 
152 
153 class HiddenItem: public GenericItem {
154  public:
155  HiddenItem(const std::string &value, const std::string &id):
156  GenericItem("", id),
157  m_value(value) {
158  }
159 
160  protected:
161  std::string Type() const { return "hidden"; }
162  void SetValue(JsonObject *item) const {
163  item->Add("value", m_value);
164  }
165 
166  private:
167  std::string m_value;
168 };
169 
170 
171 /*
172  * An item which is a select list
173  */
174 class SelectItem: public GenericItem {
175  public:
176  SelectItem(const std::string &description,
177  const std::string &id = ""):
178  GenericItem(description, id),
179  m_selected_offset(0) {
180  }
181 
182  void SetSelectedOffset(unsigned int offset) { m_selected_offset = offset; }
183  void AddItem(const std::string &label, const std::string &value);
184  // helper method which converts ints to strings
185  void AddItem(const std::string &label, unsigned int value);
186 
187  protected:
188  void SetExtraProperties(JsonObject *item) const {
189  item->Add("selected_offset", m_selected_offset);
190  }
191  std::string Type() const { return "select"; }
192  void SetValue(JsonObject *item) const;
193 
194  private:
195  std::vector<std::pair<std::string, std::string> > m_values;
196  unsigned int m_selected_offset;
197 };
198 
199 
200 class JsonSection {
201  public:
202  explicit JsonSection(bool allow_refresh = true);
203  ~JsonSection();
204 
205  void SetSaveButton(const std::string &text) { m_save_button_text = text; }
206  void SetError(const std::string &error) { m_error = error; }
207 
208  void AddItem(const GenericItem *item);
209  std::string AsString() const;
210 
211  private:
212  bool m_allow_refresh;
213  std::string m_error;
214  std::string m_save_button_text;
215  std::vector<const GenericItem*> m_items;
216 };
217 } // namespace web
218 } // namespace ola
219 #endif // INCLUDE_OLA_WEB_JSONSECTIONS_H_