GCC Code Coverage Report


Directory: ./
File: qtpdcom/src/MessageImpl.cpp
Date: 2026-11-08 04:08:44
Exec Total Coverage
Lines: 0 97 0.0%
Branches: 0 178 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(
74 QDomElement elem /**< Element. */
75 )
76 {
77 ✗ if (not elem.hasAttribute("index")) {
78 ✗ return -1;
79 }
80
81 ✗ bool ok {false};
82 ✗ int ret = elem.attribute("index").toInt(&ok);
83 ✗ if (not ok or ret < -1) {
84 ✗ throw Exception("Message has invalid index value!");
85 }
86
87 ✗ return ret;
88 }
89
90 /****************************************************************************/
91
92 /** Constructor with XML element.
93 */
94 ✗ void Message::Impl::fromPlainXmlElement(
95 QDomElement elem, /**< Element. */
96 const QString &pathPrefix /**< Prefix to path (with leading /). */
97 )
98 {
99 ✗ QDomNodeList children = elem.childNodes();
100
101 ✗ path = pathFromPlainXmlElement(elem, pathPrefix);
102 ✗ index = indexFromPlainXmlElement(elem);
103
104 ✗ if (!elem.hasAttribute("type")) {
105 ✗ throw Exception("Messages has no type attribute!");
106 }
107
108 ✗ type = typeFromString(elem.attribute("type"));
109
110 // find Text and Descriptions elements
111 ✗ for (int i = 0; i < children.size(); i++) {
112 ✗ QDomNode node = children.item(i);
113 ✗ if (node.isElement()) {
114 ✗ QDomElement child = node.toElement();
115 ✗ if (child.tagName() == "Text") {
116 ✗ loadTranslations(child, text);
117 }
118 ✗ else if (child.tagName() == "Description") {
119 ✗ loadTranslations(child, description);
120 }
121 }
122 }
123 }
124
125 /****************************************************************************/
126
127 /** Constructor with PdCom5 message.
128 */
129 ✗ void Message::Impl::fromPdComMessage(const PdCom::Message &message)
130 {
131 ✗ switch (message.level) {
132 ✗ case PdCom::LogLevel::Reset:
133 ✗ type = Information;
134 ✗ break;
135 ✗ case PdCom::LogLevel::Emergency:
136 ✗ type = Error;
137 ✗ break;
138 ✗ case PdCom::LogLevel::Alert:
139 ✗ type = Error;
140 ✗ break;
141 ✗ case PdCom::LogLevel::Critical:
142 ✗ type = Error;
143 ✗ break;
144 ✗ case PdCom::LogLevel::Error:
145 ✗ type = Error;
146 ✗ break;
147 ✗ case PdCom::LogLevel::Warn:
148 ✗ type = Warning;
149 ✗ break;
150 ✗ case PdCom::LogLevel::Info:
151 ✗ type = Information;
152 ✗ break;
153 ✗ case PdCom::LogLevel::Debug:
154 ✗ type = Information;
155 ✗ break;
156 ✗ case PdCom::LogLevel::Trace:
157 ✗ type = Information;
158 ✗ break;
159 }
160
161 ✗ path = message.path.c_str();
162 ✗ index = message.index;
163 ✗ serverText = message.text.c_str();
164 }
165
166 /****************************************************************************/
167
168 /** Returns the message time as a string.
169 */
170 ✗ QString Message::Impl::timeString(quint64 time_ns)
171 {
172 quint64 sec, nsec, usec;
173 ✗ QDateTime dt;
174 ✗ QString usecStr;
175
176 ✗ sec = time_ns / 1000000000U;
177 ✗ nsec = time_ns % 1000000000U;
178 ✗ usec = nsec / 1000U;
179 ✗ dt.setSecsSinceEpoch(sec);
180 ✗ usecStr = QString(",%1").arg(usec, 6, 10, QLatin1Char('0'));
181 ✗ return dt.toString("yyyy-MM-dd hh:mm:ss") + usecStr;
182 }
183
184 /****************************************************************************/
185
186 /** Processes a TextNode XML element.
187 */
188 ✗ void Message::Impl::loadTranslations(
189 QDomElement elem, /**< Element. */
190 TranslationMap &map /**< Translation map. */
191 )
192 {
193 ✗ QDomNodeList children = elem.childNodes();
194
195 ✗ for (int i = 0; i < children.size(); i++) {
196 ✗ QDomNode node = children.item(i);
197 ✗ if (!node.isElement()) {
198 ✗ continue;
199 }
200 ✗ QDomElement child = node.toElement();
201 ✗ if (child.tagName() != "Translation") {
202 ✗ throw Exception(QString("Expected Translation element, got %1!")
203 ✗ .arg(child.tagName()));
204 }
205 ✗ if (!child.hasAttribute("lang")) {
206 ✗ throw Exception("Translation missing lang attribute!");
207 }
208 ✗ map[child.attribute("lang")] = child.text().simplified();
209 }
210 }
211
212 /****************************************************************************/
213
214 /** Converts a message type string to the appropriate #Type.
215 */
216 ✗ Message::Type Message::Impl::typeFromString(const QString &str)
217 {
218 ✗ if (str == "Information") {
219 ✗ return Information;
220 }
221 ✗ if (str == "Warning") {
222 ✗ return Warning;
223 }
224 ✗ if (str == "Error") {
225 ✗ return Error;
226 }
227 ✗ if (str == "Critical") {
228 ✗ return Critical;
229 }
230
231 ✗ throw Message::Exception(QString("Invalid message type '%1'").arg(str));
232 }
233
234 /****************************************************************************/
235
236 /** Variable value changed.
237 */
238 ✗ void Message::Impl::valueChanged()
239 {
240 ✗ emit parent->stateChanged();
241 }
242
243 /****************************************************************************/
244