File: WebClientCounter.cpp

package info (click to toggle)
railcontrol 24%2Bdfsg1-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,636 kB
  • sloc: cpp: 40,819; javascript: 2,509; makefile: 144; php: 97; sh: 60
file content (242 lines) | stat: -rw-r--r-- 9,293 bytes parent folder | download
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
RailControl - Model Railway Control Software

Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org

RailControl 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, or (at your option) any
later version.

RailControl 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 RailControl; see the file LICENCE. If not see
<http://www.gnu.org/licenses/>.
*/

#include <vector>

#include "DataModel/Counter.h"
#include "Utils/Utils.h"
#include "Server/Web/HtmlTag.h"
#include "Server/Web/HtmlTagButtonCancel.h"
#include "Server/Web/HtmlTagButtonCommandWide.h"
#include "Server/Web/HtmlTagButtonOK.h"
#include "Server/Web/HtmlTagButtonPopupWide.h"
#include "Server/Web/HtmlTagInputHidden.h"
#include "Server/Web/HtmlTagInputIntegerWithLabel.h"
#include "Server/Web/HtmlTagInputTextWithLabel.h"
#include "Server/Web/HtmlTagCounter.h"
#include "Server/Web/WebClient.h"
#include "Server/Web/WebClientCounter.h"

using namespace DataModel;
using LayoutPosition = DataModel::LayoutItem::LayoutPosition;
using LayoutItemSize = DataModel::LayoutItem::LayoutItemSize;
using LayoutRotation = DataModel::LayoutItem::LayoutRotation;
using std::map;
using std::string;
using std::to_string;
using std::vector;

namespace Server { namespace Web
{
	void WebClientCounter::HandleCounterEdit(const map<string, string>& arguments)
	{
		HtmlTag content;
		const CounterID counterID = Utils::Utils::GetIntegerMapEntry(arguments, "counter", CounterNone);
		string name = Languages::GetText(Languages::TextNew);
		int max = Utils::Utils::GetIntegerMapEntry(arguments, "max", 1);
		int min = Utils::Utils::GetIntegerMapEntry(arguments, "min", 0);
		LayoutPosition posx = Utils::Utils::GetIntegerMapEntry(arguments, "posx", 0);
		LayoutPosition posy = Utils::Utils::GetIntegerMapEntry(arguments, "posy", 0);
		LayoutPosition posz = Utils::Utils::GetIntegerMapEntry(arguments, "posz", LayerUndeletable);
		LayoutRotation rotation = Utils::Utils::GetIntegerMapEntry(arguments, "rotation", DataModel::LayoutItem::Rotation0);

		if (counterID > CounterNone)
		{
			const DataModel::Counter* counter = manager.GetCounter(counterID);
			if (counter != nullptr)
			{
				name = counter->GetName();
				max = counter->GetMax();
				min = counter->GetMin();
				posx = counter->GetPosX();
				posy = counter->GetPosY();
				posz = counter->GetPosZ();
				rotation = counter->GetRotation();
			}
		}

		content.AddChildTag(HtmlTag("h1").AddContent(name).AddId("popup_title"));
		HtmlTag tabMenu("div");
		tabMenu.AddChildTag(WebClientStatic::HtmlTagTabMenuItem("main", Languages::TextBasic, true));
		tabMenu.AddChildTag(WebClientStatic::HtmlTagTabMenuItem("position", Languages::TextPosition));

		content.AddChildTag(tabMenu);

		HtmlTag formContent;
		formContent.AddChildTag(HtmlTagInputHidden("cmd", "countersave"));
		formContent.AddChildTag(HtmlTagInputHidden("counter", to_string(counterID)));

		HtmlTag mainContent("div");
		mainContent.AddId("tab_main");
		mainContent.AddClass("tab_content");
		mainContent.AddChildTag(HtmlTagInputTextWithLabel("name", Languages::TextName, name).AddAttribute("onkeyup", "updateName();"));
		mainContent.AddChildTag(HtmlTagInputIntegerWithLabel("max", Languages::TextMax, max, 0, 255));
		mainContent.AddChildTag(HtmlTagInputIntegerWithLabel("min", Languages::TextMin, min, -255, 0));
		formContent.AddChildTag(mainContent);

		formContent.AddChildTag(client.HtmlTagTabPosition(posx, posy, posz, rotation));

		content.AddChildTag(HtmlTag("div").AddClass("popup_content").AddChildTag(HtmlTag("form").AddId("editform").AddChildTag(formContent)));
		content.AddChildTag(HtmlTagButtonCancel());
		content.AddChildTag(HtmlTagButtonOK());
		client.ReplyHtmlWithHeader(content);
	}

	void WebClientCounter::HandleCounterSave(const map<string, string>& arguments)
	{
		const CounterID counterID = Utils::Utils::GetIntegerMapEntry(arguments, "counter", CounterNone);
		const string name = Utils::Utils::GetStringMapEntry(arguments, "name");
		const int max = Utils::Utils::GetIntegerMapEntry(arguments, "max", 1);
		const int min = Utils::Utils::GetIntegerMapEntry(arguments, "min", 0);
		const LayoutPosition posX = Utils::Utils::GetIntegerMapEntry(arguments, "posx", 0);
		const LayoutPosition posY = Utils::Utils::GetIntegerMapEntry(arguments, "posy", 0);
		const LayoutPosition posZ = Utils::Utils::GetIntegerMapEntry(arguments, "posz", 0);
		const LayoutRotation rotation = Utils::Utils::GetIntegerMapEntry(arguments, "rotation", DataModel::LayoutItem::Rotation0);
		string result;
		if (!manager.CounterSave(counterID,
			name,
			max,
			min,
			posX,
			posY,
			posZ,
			rotation,
			result))
		{
			client.ReplyResponse(WebClient::ResponseError, result);
			return;
		}

		client.ReplyResponse(WebClient::ResponseInfo, Languages::TextCounterSaved, name);
	}

	void WebClientCounter::HandleCounterList()
	{
		HtmlTag content;
		content.AddChildTag(HtmlTag("h1").AddContent(Languages::TextCounters));
		HtmlTag table("table");
		const map<string,DataModel::Counter*> counterList = manager.CounterListByName();
		map<string,string> counterArgument;
		for (auto& counter : counterList)
		{
			HtmlTag row("tr");
			row.AddChildTag(HtmlTag("td").AddContent(counter.first));
			const string& counterIdString = to_string(counter.second->GetID());
			counterArgument["counter"] = counterIdString;
			row.AddChildTag(HtmlTag("td").AddChildTag(HtmlTagButtonPopupWide(Languages::TextEdit, "counteredit_list_" + counterIdString, counterArgument)));
			row.AddChildTag(HtmlTag("td").AddChildTag(HtmlTagButtonPopupWide(Languages::TextDelete, "counteraskdelete_" + counterIdString, counterArgument)));
			table.AddChildTag(row);
		}
		content.AddChildTag(HtmlTag("div").AddClass("popup_content").AddChildTag(table));
		content.AddChildTag(HtmlTagButtonCancel());
		content.AddChildTag(HtmlTagButtonPopupWide(Languages::TextNew, "counteredit_0"));
		client.ReplyHtmlWithHeader(content);
	}

	void WebClientCounter::HandleCounterAskDelete(const map<string, string>& arguments)
	{
		const CounterID counterID = Utils::Utils::GetIntegerMapEntry(arguments, "counter", CounterNone);
		if (counterID == CounterNone)
		{
			client.ReplyHtmlWithHeaderAndParagraph(Languages::TextCounterDoesNotExist);
			return;
		}

		const DataModel::Counter* counter = manager.GetCounter(counterID);
		if (!counter)
		{
			client.ReplyHtmlWithHeaderAndParagraph(Languages::TextCounterDoesNotExist);
			return;
		}

		HtmlTag content;
		const string& counterName = counter->GetName();
		content.AddContent(HtmlTag("h1").AddContent(Languages::TextDeleteCounter));
		content.AddContent(HtmlTag("p").AddContent(Languages::TextAreYouSureToDelete, counterName));
		content.AddContent(HtmlTag("form").AddId("editform")
			.AddContent(HtmlTagInputHidden("cmd", "counterdelete"))
			.AddContent(HtmlTagInputHidden("counter", to_string(counterID))
			));
		content.AddContent(HtmlTagButtonCancel());
		content.AddContent(HtmlTagButtonOK());
		client.ReplyHtmlWithHeader(content);
	}

	void WebClientCounter::HandleCounterDelete(const map<string, string>& arguments)
	{
		const CounterID counterID = Utils::Utils::GetIntegerMapEntry(arguments, "counter", CounterNone);
		const DataModel::Counter* counter = manager.GetCounter(counterID);
		if (!counter)
		{
			client.ReplyResponse(WebClient::ResponseError, Languages::TextCounterDoesNotExist);
			return;
		}

		string name = counter->GetName();
		string result;
		if (!manager.CounterDelete(counterID, result))
		{
			client.ReplyResponse(WebClient::ResponseError, result);
			return;
		}

		client.ReplyResponse(WebClient::ResponseInfo, Languages::TextCounterDeleted, name);
	}

	void WebClientCounter::HandleCounterGet(const map<string, string>& arguments)
	{
		const CounterID counterID = Utils::Utils::GetIntegerMapEntry(arguments, "counter");
		const DataModel::Counter* counter = manager.GetCounter(counterID);
		if (!counter)
		{
			client.ReplyHtmlWithHeader(HtmlTag());
			return;
		}
		client.ReplyHtmlWithHeader(HtmlTagCounter(counter));
	}

	void WebClientCounter::HandleCounterIncrement(const map<string, string>& arguments)
	{
		const CounterID counterID = Utils::Utils::GetIntegerMapEntry(arguments, "counter");
		const bool result = manager.Count(counterID, CounterTypeIncrement);
		if (result)
		{
			client.ReplyResponse(WebClient::ResponseInfo, Languages::TextCounterIncremented);
		}
		else
		{
			client.ReplyResponse(WebClient::ResponseWarning, Languages::TextCounterReachedLimit);
		}
	}

	void WebClientCounter::HandleCounterDecrement(const map<string, string>& arguments)
	{
		const CounterID counterID = Utils::Utils::GetIntegerMapEntry(arguments, "counter");
		const bool result = manager.Count(counterID, CounterTypeDecrement);
		if (result)
		{
			client.ReplyResponse(WebClient::ResponseInfo, Languages::TextCounterDecremented);
		}
		else
		{
			client.ReplyResponse(WebClient::ResponseWarning, Languages::TextCounterReachedLimit);
		}
	}
}} // namespace Server::Web