Debian Codegraph @

Server/Web/WebClientText.cpp

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 RailControl - Model Railway Control Software
3
4 Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org
5
6 RailControl is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 RailControl is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with RailControl; see the file LICENCE. If not see
18 <http://www.gnu.org/licenses/>.
19 */
20
21 #include <vector>
22
23 #include "DataModel/Text.h"
24 #include "Utils/Utils.h"
25 #include "Server/Web/HtmlTag.h"
26 #include "Server/Web/HtmlTagButtonCancel.h"
27 #include "Server/Web/HtmlTagButtonCommandWide.h"
28 #include "Server/Web/HtmlTagButtonOK.h"
29 #include "Server/Web/HtmlTagButtonPopupWide.h"
30 #include "Server/Web/HtmlTagInputHidden.h"
31 #include "Server/Web/HtmlTagInputIntegerWithLabel.h"
32 #include "Server/Web/HtmlTagInputTextWithLabel.h"
33 #include "Server/Web/HtmlTagText.h"
34 #include "Server/Web/WebClient.h"
35 #include "Server/Web/WebClientText.h"
36
37 using namespace DataModel;
38 using LayoutPosition = DataModel::LayoutItem::LayoutPosition;
39 using LayoutItemSize = DataModel::LayoutItem::LayoutItemSize;
40 using LayoutRotation = DataModel::LayoutItem::LayoutRotation;
41 using std::map;
42 using std::string;
43 using std::to_string;
44 using std::vector;
45
46 namespace Server { namespace Web
47 {
48 void WebClientText::HandleTextEdit(const map<string, string>& arguments)
49 {
50 HtmlTag content;
51 TextID textID = Utils::Utils::GetIntegerMapEntry(arguments, "text", TextNone);
52 string name = Languages::GetText(Languages::TextNew);
53 LayoutPosition posx = Utils::Utils::GetIntegerMapEntry(arguments, "posx", 0);
54 LayoutPosition posy = Utils::Utils::GetIntegerMapEntry(arguments, "posy", 0);
55 LayoutPosition posz = Utils::Utils::GetIntegerMapEntry(arguments, "posz", LayerUndeletable);
56
57 LayoutItemSize width = Utils::Utils::GetIntegerMapEntry(arguments, "width", 1);
58
59 LayoutRotation rotation = Utils::Utils::GetIntegerMapEntry(arguments, "rotation", DataModel::LayoutItem::Rotation0);
60
61 if (textID > TextNone)
62 {
63 const DataModel::Text* text = manager.GetText(textID);
•64 if (text)
65 {
66 name = text->GetName();
67 posx = text->GetPosX();
68 posy = text->GetPosY();
69 posz = text->GetPosZ();
70 width = text->GetWidth();
71 rotation = text->GetRotation();
72 }
73 }
74
75 content.AddChildTag(HtmlTag("h1").AddContent(name).AddId("popup_title"));
76 HtmlTag tabMenu("div");
77 tabMenu.AddChildTag(WebClientStatic::HtmlTagTabMenuItem("main", Languages::TextBasic, true));
78 tabMenu.AddChildTag(WebClientStatic::HtmlTagTabMenuItem("position", Languages::TextPosition));
79
80 content.AddChildTag(tabMenu);
81
82 HtmlTag formContent;
83 formContent.AddChildTag(HtmlTagInputHidden("cmd", "textsave"));
84 formContent.AddChildTag(HtmlTagInputHidden("text", to_string(textID)));
85
86 HtmlTag mainContent("div");
87 mainContent.AddId("tab_main");
88 mainContent.AddClass("tab_content");
89 mainContent.AddChildTag(HtmlTagInputTextWithLabel("name", Languages::TextName, name).AddAttribute("onkeyup", "updateName();"));
90 mainContent.AddChildTag(HtmlTagInputIntegerWithLabel("width", Languages::TextWidth, width, DataModel::Text::MinWidth, DataModel::Text::MaxWidth));
91 formContent.AddChildTag(mainContent);
92
93 formContent.AddChildTag(client.HtmlTagTabPosition(posx, posy, posz, rotation));
94
95 content.AddChildTag(HtmlTag("div").AddClass("popup_content").AddChildTag(HtmlTag("form").AddId("editform").AddChildTag(formContent)));
96 content.AddChildTag(HtmlTagButtonCancel());
97 content.AddChildTag(HtmlTagButtonOK());
98 client.ReplyHtmlWithHeader(content);
99 }
100
101 void WebClientText::HandleTextSave(const map<string, string>& arguments)
102 {
103 TextID textID = Utils::Utils::GetIntegerMapEntry(arguments, "text", TextNone);
104 string name = Utils::Utils::GetStringMapEntry(arguments, "name");
105 LayoutPosition posX = Utils::Utils::GetIntegerMapEntry(arguments, "posx", 0);
106 LayoutPosition posY = Utils::Utils::GetIntegerMapEntry(arguments, "posy", 0);
107 LayoutPosition posZ = Utils::Utils::GetIntegerMapEntry(arguments, "posz", 0);
108 LayoutItemSize width = Utils::Utils::GetIntegerMapEntry(arguments, "width", 1);
109 LayoutRotation rotation = Utils::Utils::GetIntegerMapEntry(arguments, "rotation", DataModel::LayoutItem::Rotation0);
110 string result;
111 if (!manager.TextSave(textID,
112 name,
113 posX,
114 posY,
115 posZ,
116 width,
117 rotation,
118 result))
119 {
120 client.ReplyResponse(WebClient::ResponseError, result);
121 return;
122 }
123
124 client.ReplyResponse(WebClient::ResponseInfo, Languages::TextTextSaved, name);
125 }
126
127 void WebClientText::HandleTextList()
128 {
129 HtmlTag content;
130 content.AddChildTag(HtmlTag("h1").AddContent(Languages::TextTexts));
131 HtmlTag table("table");
132 const map<string,DataModel::Text*> textList = manager.TextListByName();
133 map<string,string> textArgument;
134 for (auto& text : textList)
135 {
136 HtmlTag row("tr");
137 row.AddChildTag(HtmlTag("td").AddContent(text.first));
138 const string& textIdString = to_string(text.second->GetID());
139 textArgument["text"] = textIdString;
140 row.AddChildTag(HtmlTag("td").AddChildTag(HtmlTagButtonPopupWide(Languages::TextEdit, "textedit_list_" + textIdString, textArgument)));
141 row.AddChildTag(HtmlTag("td").AddChildTag(HtmlTagButtonPopupWide(Languages::TextDelete, "textaskdelete_" + textIdString, textArgument)));
142 table.AddChildTag(row);
143 }
144 content.AddChildTag(HtmlTag("div").AddClass("popup_content").AddChildTag(table));
145 content.AddChildTag(HtmlTagButtonCancel());
146 content.AddChildTag(HtmlTagButtonPopupWide(Languages::TextNew, "textedit_0"));
147 client.ReplyHtmlWithHeader(content);
148 }
149
150 void WebClientText::HandleTextAskDelete(const map<string, string>& arguments)
151 {
152 TextID textID = Utils::Utils::GetIntegerMapEntry(arguments, "text", TextNone);
153
154 if (textID == TextNone)
155 {
156 client.ReplyHtmlWithHeaderAndParagraph(Languages::TextTextDoesNotExist);
157 return;
158 }
159
160 const DataModel::Text* text = manager.GetText(textID);
•161 if (!text)
162 {
163 client.ReplyHtmlWithHeaderAndParagraph(Languages::TextTextDoesNotExist);
164 return;
165 }
166
167 HtmlTag content;
168 const string& textName = text->GetName();
169 content.AddContent(HtmlTag("h1").AddContent(Languages::TextDeleteText));
170 content.AddContent(HtmlTag("p").AddContent(Languages::TextAreYouSureToDelete, textName));
171 content.AddContent(HtmlTag("form").AddId("editform")
172 .AddContent(HtmlTagInputHidden("cmd", "textdelete"))
173 .AddContent(HtmlTagInputHidden("text", to_string(textID))
174 ));
175 content.AddContent(HtmlTagButtonCancel());
176 content.AddContent(HtmlTagButtonOK());
177 client.ReplyHtmlWithHeader(content);
178 }
179
180 void WebClientText::HandleTextDelete(const map<string, string>& arguments)
181 {
182 TextID textID = Utils::Utils::GetIntegerMapEntry(arguments, "text", TextNone);
183 const DataModel::Text* text = manager.GetText(textID);
•184 if (!text)
185 {
186 client.ReplyResponse(WebClient::ResponseError, Languages::TextTextDoesNotExist);
187 return;
188 }
189
190 string name = text->GetName();
191 string result;
192 if (!manager.TextDelete(textID, result))
193 {
194 client.ReplyResponse(WebClient::ResponseError, result);
195 return;
196 }
197
198 client.ReplyResponse(WebClient::ResponseInfo, Languages::TextTextDeleted, name);
199 }
200
201 void WebClientText::HandleTextGet(const map<string, string>& arguments)
202 {
203 TextID textID = Utils::Utils::GetIntegerMapEntry(arguments, "text");
204 const DataModel::Text* text = manager.GetText(textID);
•205 if (!text)
206 {
207 client.ReplyHtmlWithHeader(HtmlTag());
208 return;
209 }
210 client.ReplyHtmlWithHeader(HtmlTagText(text));
211 }
212 }} // namespace Server::Web