GCC Code Coverage Report


Directory: ./
File: qtpdcom/src/MessageImpl.cpp
Date: 2024-12-15 04:08:34
Exec Total Coverage
Lines: 0 97 0.0%
Branches: 0 182 0.0%

Line Branch Exec Source
1 /*****************************************************************************
2 *
3 * Copyright (C) 2009-2022 Florian Pose <fp@igh.de>
4 *
5 * This file is part of the QtPdCom library.
6 *
7 * The QtPdCom library is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * The QtPdCom library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with the QtPdCom Library. If not, see <http://www.gnu.org/licenses/>.
19 *
20 ****************************************************************************/
21
22 #include "MessageImpl.h"
23 #include "MessageItem.h"
24 using namespace QtPdCom;
25
26 #include <QDateTime>
27
28 /****************************************************************************/
29
30 /** Constructor.
31 */
32 Message::Impl::Impl(Message *message):
33 parent {message},
34 type {Information},
35 index {-1},
36 currentItem {nullptr},
37 announced {false}
38 {
39 QObject::connect(
40 &variable,
41 SIGNAL(valueChanged()),
42 this,
43 SLOT(valueChanged()));
44 }
45
46 /****************************************************************************/
47
48 /** Destructor.
49 */
50 Message::Impl::~Impl()
51 {}
52
53 /****************************************************************************/
54
55 /** Get the path.
56 */
57 QString Message::Impl::pathFromPlainXmlElement(
58 QDomElement elem, /**< Element. */
59 const QString &pathPrefix /**< Prefix to path (with leading /). */
60 )
61 {
62 if (!elem.hasAttribute("variable")) {
63 throw Exception("Messages has no variable attribute!");
64 }
65
66 return pathPrefix + elem.attribute("variable");
67 }
68
69 /****************************************************************************/
70
71 /** Get the index.
72 */
73 int Message::Impl::indexFromPlainXmlElement(QDomElement elem /**< Element. */
74 )
75 {
76 if (not elem.hasAttribute("index")) {
77 return -1;
78 }
79
80 bool ok {false};
81 int ret = elem.attribute("index").toInt(&ok);
82 if (not ok or ret < -1) {
83 throw Exception("Message has invalid index value!");
84 }
85
86 return ret;
87 }
88
89 /****************************************************************************/
90
91 /** Constructor with XML element.
92 */
93 void Message::Impl::fromPlainXmlElement(
94 QDomElement elem, /**< Element. */
95 const QString &pathPrefix /**< Prefix to path (with leading /). */
96 )
97 {
98 QDomNodeList children = elem.childNodes();
99
100 path = pathFromPlainXmlElement(elem, pathPrefix);
101 index = indexFromPlainXmlElement(elem);
102
103 if (!elem.hasAttribute("type")) {
104 throw Exception("Messages has no type attribute!");
105 }
106
107 type = typeFromString(elem.attribute("type"));
108
109 // find Text and Descriptions elements
110 for (int i = 0; i < children.size(); i++) {
111 QDomNode node = children.item(i);
112 if (node.isElement()) {
113 QDomElement child = node.toElement();
114 if (child.tagName() == "Text") {
115 loadTranslations(child, text);
116 }
117 else if (child.tagName() == "Description") {
118 loadTranslations(child, description);
119 }
120 }
121 }
122 }
123
124 /****************************************************************************/
125
126 /** Constructor with PdCom5 message.
127 */
128 void Message::Impl::fromPdComMessage(const PdCom::Message &message)
129 {
130 switch (message.level) {
131 case PdCom::LogLevel::Reset:
132 type = Information;
133 break;
134 case PdCom::LogLevel::Emergency:
135 type = Error;
136 break;
137 case PdCom::LogLevel::Alert:
138 type = Error;
139 break;
140 case PdCom::LogLevel::Critical:
141 type = Error;
142 break;
143 case PdCom::LogLevel::Error:
144 type = Error;
145 break;
146 case PdCom::LogLevel::Warn:
147 type = Warning;
148 break;
149 case PdCom::LogLevel::Info:
150 type = Information;
151 break;
152 case PdCom::LogLevel::Debug:
153 type = Information;
154 break;
155 case PdCom::LogLevel::Trace:
156 type = Information;
157 break;
158 }
159
160 path = message.path.c_str();
161 index = message.index;
162 text[""] = message.text.c_str();
163 }
164
165 /****************************************************************************/
166
167 /** Returns the message time as a string.
168 */
169 QString Message::Impl::timeString(quint64 time_ns)
170 {
171 quint64 sec, nsec, usec;
172 QDateTime dt;
173 QString usecStr;
174
175 sec = time_ns / 1000000000U;
176 nsec = time_ns % 1000000000U;
177 usec = nsec / 1000U;
178 dt.setSecsSinceEpoch(sec);
179 usecStr = QString(",%1").arg(usec, 6, 10, QLatin1Char('0'));
180 return dt.toString("yyyy-MM-dd hh:mm:ss") + usecStr;
181 }
182
183 /****************************************************************************/
184
185 /** Processes a TextNode XML element.
186 */
187 void Message::Impl::loadTranslations(
188 QDomElement elem, /**< Element. */
189 TranslationMap &map /**< Translation map. */
190 )
191 {
192 QDomNodeList children = elem.childNodes();
193
194 for (int i = 0; i < children.size(); i++) {
195 QDomNode node = children.item(i);
196 if (!node.isElement()) {
197 continue;
198 }
199 QDomElement child = node.toElement();
200 if (child.tagName() != "Translation") {
201 throw Exception(QString("Expected Translation element, got %1!")
202 .arg(child.tagName()));
203 }
204 if (!child.hasAttribute("lang")) {
205 throw Exception("Translation missing lang attribute!");
206 }
207 map[child.attribute("lang")] = child.text().simplified();
208 }
209 }
210
211 /****************************************************************************/
212
213 /** Converts a message type string to the appropriate #Type.
214 */
215 Message::Type Message::Impl::typeFromString(const QString &str)
216 {
217 if (str == "Information") {
218 return Information;
219 }
220 if (str == "Warning") {
221 return Warning;
222 }
223 if (str == "Error") {
224 return Error;
225 }
226 if (str == "Critical") {
227 return Critical;
228 }
229
230 throw Message::Exception(QString("Invalid message type '%1'").arg(str));
231 }
232
233 /****************************************************************************/
234
235 /** Variable value changed.
236 */
237 void Message::Impl::valueChanged()
238 {
239 emit parent->stateChanged();
240 }
241
242 /****************************************************************************/
243