Open Lighting Architecture
 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 
33 using std::string;
34 
44 class GenericItem {
45  public:
46  GenericItem(const string &description, const string &id):
47  m_description(description),
48  m_id(id),
49  m_button_text("") {
50  }
51  virtual ~GenericItem() {}
52 
53  // Sets the text for the button associated
54  void SetButtonText(const string &text) {
55  m_button_text = text;
56  }
57 
58  void PopulateItem(JsonObject *item) const;
59 
60  protected:
61  virtual string Type() const = 0;
62  virtual void SetValue(JsonObject *item) const = 0;
63  virtual void SetExtraProperties(JsonObject *item) const {
64  (void) item;
65  }
66 
67  private:
68  string m_description;
69  string m_id;
70  string m_button_text;
71 };
72 
73 
74 /*
75  * This is a item that contains a string value
76  */
77 class StringItem: public GenericItem {
78  public:
79  StringItem(const string &description,
80  const string &value,
81  const string &id = ""):
82  GenericItem(description, id),
83  m_value(value) {
84  }
85 
86  protected:
87  string Type() const { return "string"; }
88  void SetValue(JsonObject *item) const {
89  item->Add("value", m_value);
90  }
91 
92  private:
93  string m_value;
94 };
95 
96 
97 /*
98  * An item that contains a unsigned int
99  */
100 class UIntItem: public GenericItem {
101  public:
102  UIntItem(const string &description,
103  unsigned int value,
104  const string &id = ""):
105  GenericItem(description, id),
106  m_value(value),
107  m_min_set(false),
108  m_max_set(false) {
109  }
110 
111  void SetMin(unsigned int min) {
112  m_min_set = true;
113  m_min = min;
114  }
115  void SetMax(unsigned int max) {
116  m_max_set = true;
117  m_max = max;
118  }
119 
120  protected:
121  void SetExtraProperties(JsonObject *item) const;
122  string Type() const { return "uint"; }
123  void SetValue(JsonObject *item) const {
124  item->Add("value", m_value);
125  }
126 
127  private:
128  unsigned int m_value;
129  bool m_min_set, m_max_set;
130  unsigned int m_min;
131  unsigned int m_max;
132 };
133 
134 
135 class BoolItem: public GenericItem {
136  public:
137  BoolItem(const string &description,
138  bool value,
139  const string &id):
140  GenericItem(description, id),
141  m_value(value) {
142  }
143 
144  protected:
145  string Type() const { return "bool"; }
146  void SetValue(JsonObject *item) const {
147  item->Add("value", m_value);
148  }
149 
150  private:
151  bool m_value;
152 };
153 
154 
155 class HiddenItem: public GenericItem {
156  public:
157  HiddenItem(const string &value, const string &id):
158  GenericItem("", id),
159  m_value(value) {
160  }
161 
162  protected:
163  string Type() const { return "hidden"; }
164  void SetValue(JsonObject *item) const {
165  item->Add("value", m_value);
166  }
167 
168  private:
169  string m_value;
170 };
171 
172 
173 /*
174  * An item which is a select list
175  */
176 class SelectItem: public GenericItem {
177  public:
178  SelectItem(const string &description,
179  const string &id = ""):
180  GenericItem(description, id),
181  m_selected_offset(0) {
182  }
183 
184  void SetSelectedOffset(unsigned int offset) { m_selected_offset = offset; }
185  void AddItem(const string &label, const string &value);
186  // helper method which converts ints to strings
187  void AddItem(const string &label, unsigned int value);
188 
189  protected:
190  void SetExtraProperties(JsonObject *item) const {
191  item->Add("selected_offset", m_selected_offset);
192  }
193  string Type() const { return "select"; }
194  void SetValue(JsonObject *item) const;
195 
196  private:
197  std::vector<std::pair<string, string> > m_values;
198  unsigned int m_selected_offset;
199 };
200 
201 
202 class JsonSection {
203  public:
204  explicit JsonSection(bool allow_refresh = true);
205  ~JsonSection();
206 
207  void SetSaveButton(const string &text) { m_save_button_text = text; }
208  void SetError(const string &error) { m_error = error; }
209 
210  void AddItem(const GenericItem *item);
211  string AsString() const;
212 
213  private:
214  bool m_allow_refresh;
215  string m_error;
216  string m_save_button_text;
217  std::vector<const GenericItem*> m_items;
218 };
219 } // namespace web
220 } // namespace ola
221 #endif // INCLUDE_OLA_WEB_JSONSECTIONS_H_