GCC Code Coverage Report


Directory: ./
File: src/MessageImpl.cpp
Date: 2024-01-26 10:40:55
Exec Total Coverage
Lines: 69 103 67.0%
Branches: 76 180 42.2%

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 19 Message::Impl::Impl(Message *message):
33 parent{message},
34 type{Information},
35 index{-1},
36
1/2
✓ Branch 5 taken 19 times.
✗ Branch 6 not taken.
19 currentItem{nullptr}
37 {
38
3/6
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 19 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 19 times.
✗ Branch 9 not taken.
19 QObject::connect(&variable, SIGNAL(valueChanged()),
39 this, SLOT(valueChanged()));
40 19 }
41
42 /****************************************************************************/
43
44 /** Destructor.
45 */
46 38 Message::Impl::~Impl()
47 {
48 38 }
49
50 /****************************************************************************/
51
52 /** Get the path.
53 */
54 12 QString Message::Impl::pathFromPlainXmlElement(
55 QDomElement elem, /**< Element. */
56 const QString &pathPrefix /**< Prefix to path (with leading /). */
57 )
58 {
59
2/4
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 12 times.
12 if (!elem.hasAttribute("variable")) {
60 throw Exception("Messages has no variable attribute!");
61 }
62
63
3/6
✓ Branch 5 taken 12 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 12 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 12 times.
✗ Branch 12 not taken.
12 return pathPrefix + elem.attribute("variable");
64 }
65
66 /****************************************************************************/
67
68 /** Get the index.
69 */
70 12 int Message::Impl::indexFromPlainXmlElement(
71 QDomElement elem /**< Element. */
72 )
73 {
74
3/6
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 12 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 12 times.
✗ Branch 10 not taken.
12 if (not elem.hasAttribute("index")) {
75 12 return -1;
76 }
77
78 bool ok{false};
79 int ret = elem.attribute("index").toInt(&ok);
80 if (not ok or ret < -1) {
81 throw Exception("Message has invalid index value!");
82 }
83
84 return ret;
85 }
86
87 /****************************************************************************/
88
89 /** Constructor with XML element.
90 */
91 6 void Message::Impl::fromPlainXmlElement(
92 QDomElement elem, /**< Element. */
93 const QString &pathPrefix /**< Prefix to path (with leading /). */
94 )
95 {
96
1/2
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
12 QDomNodeList children = elem.childNodes();
97
98
2/4
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
6 path = pathFromPlainXmlElement(elem, pathPrefix);
99
2/4
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 6 times.
✗ Branch 6 not taken.
6 index = indexFromPlainXmlElement(elem);
100
101
3/6
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 6 times.
✗ Branch 6 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 6 times.
6 if (!elem.hasAttribute("type")) {
102 throw Exception("Messages has no type attribute!");
103 }
104
105
3/6
✓ Branch 5 taken 6 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 6 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 6 times.
✗ Branch 12 not taken.
6 type = typeFromString(elem.attribute("type"));
106
107 // find Text and Descriptions elements
108
3/4
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 6 times.
18 for (int i = 0; i < children.size(); i++) {
109
1/2
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
24 QDomNode node = children.item(i);
110
2/4
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
12 if (node.isElement()) {
111
1/2
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
24 QDomElement child = node.toElement();
112
4/6
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 12 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 6 times.
✓ Branch 10 taken 6 times.
12 if (child.tagName() == "Text") {
113
2/4
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 6 times.
✗ Branch 6 not taken.
6 loadTranslations(child, text);
114 }
115
3/6
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 6 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 6 times.
✗ Branch 10 not taken.
6 else if (child.tagName() == "Description") {
116
2/4
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 6 times.
✗ Branch 6 not taken.
6 loadTranslations(child, description);
117 }
118 }
119 }
120 6 }
121
122 /****************************************************************************/
123
124 /** Constructor with PdCom5 message.
125 */
126 13 void Message::Impl::fromPdComMessage(
127 const PdCom::Message &message
128 )
129 {
130
3/10
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✓ Branch 5 taken 3 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
13 switch (message.level) {
131 1 case PdCom::LogLevel::Reset:
132 1 type = Information;
133 1 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 9 case PdCom::LogLevel::Error:
144 9 type = Error;
145 9 break;
146 3 case PdCom::LogLevel::Warn:
147 3 type = Warning;
148 3 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 13 path = message.path.c_str();
161 13 index = message.index;
162
2/4
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 13 times.
✗ Branch 8 not taken.
13 text[""] = message.text.c_str();
163 13 }
164
165 /****************************************************************************/
166
167 /** Returns the message time as a string.
168 */
169 37 QString Message::Impl::timeString(quint64 time_ns)
170 {
171 quint64 sec, nsec, usec;
172 74 QDateTime dt;
173 74 QString usecStr;
174
175 37 sec = time_ns / 1000000000U;
176 37 nsec = time_ns % 1000000000U;
177 37 usec = nsec / 1000U;
178
1/2
✓ Branch 1 taken 37 times.
✗ Branch 2 not taken.
37 dt.setSecsSinceEpoch(sec);
179
2/4
✓ Branch 3 taken 37 times.
✗ Branch 4 not taken.
✓ Branch 10 taken 37 times.
✗ Branch 11 not taken.
37 usecStr = QString(",%1").arg(usec, 6, 10, QLatin1Char('0'));
180
3/6
✓ Branch 3 taken 37 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 37 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 37 times.
✗ Branch 10 not taken.
74 return dt.toString("yyyy-MM-dd hh:mm:ss") + usecStr;
181 }
182
183 /****************************************************************************/
184
185 /** Processes a TextNode XML element.
186 */
187 12 void Message::Impl::loadTranslations(
188 QDomElement elem, /**< Element. */
189 TranslationMap &map /**< Translation map. */
190 )
191 {
192
1/2
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
24 QDomNodeList children = elem.childNodes();
193
194
3/4
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
✓ Branch 4 taken 12 times.
36 for (int i = 0; i < children.size(); i++) {
195
2/4
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 24 times.
✗ Branch 7 not taken.
48 QDomNode node = children.item(i);
196
2/4
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 24 times.
24 if (!node.isElement()) {
197 continue;
198 }
199
2/4
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 24 times.
✗ Branch 7 not taken.
48 QDomElement child = node.toElement();
200
3/6
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 24 times.
✗ Branch 6 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 24 times.
24 if (child.tagName() != "Translation") {
201 throw Exception(QString("Expected Translation element, got %1!")
202 .arg(child.tagName()));
203 }
204
3/6
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 24 times.
✗ Branch 6 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 24 times.
24 if (!child.hasAttribute("lang")) {
205 throw Exception("Translation missing lang attribute!");
206 }
207
5/10
✓ Branch 3 taken 24 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 24 times.
✗ Branch 7 not taken.
✓ Branch 13 taken 24 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 24 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 24 times.
✗ Branch 20 not taken.
24 map[child.attribute("lang")] = child.text().simplified();
208 }
209 12 }
210
211 /****************************************************************************/
212
213 /** Converts a message type string to the appropriate #Type.
214 */
215 6 Message::Type Message::Impl::typeFromString(const QString &str)
216 {
217
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (str == "Information") {
218 return Information;
219 }
220
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (str == "Warning") {
221 return Warning;
222 }
223
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 if (str == "Error") {
224 6 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 7 void Message::Impl::valueChanged()
238 {
239 7 emit parent->stateChanged();
240 7 }
241
242 /****************************************************************************/
243