Directory: | ./ |
---|---|
File: | src/MessageImpl.cpp |
Date: | 2024-11-19 17:00:39 |
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 | currentItem {nullptr}, | ||
37 |
1/2✓ Branch 5 taken 19 times.
✗ Branch 6 not taken.
|
19 | announced {false} |
38 | { | ||
39 |
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( |
40 | &variable, | ||
41 | SIGNAL(valueChanged()), | ||
42 | this, | ||
43 | SLOT(valueChanged())); | ||
44 | 19 | } | |
45 | |||
46 | /****************************************************************************/ | ||
47 | |||
48 | /** Destructor. | ||
49 | */ | ||
50 | 38 | Message::Impl::~Impl() | |
51 | 38 | {} | |
52 | |||
53 | /****************************************************************************/ | ||
54 | |||
55 | /** Get the path. | ||
56 | */ | ||
57 | 12 | QString Message::Impl::pathFromPlainXmlElement( | |
58 | QDomElement elem, /**< Element. */ | ||
59 | const QString &pathPrefix /**< Prefix to path (with leading /). */ | ||
60 | ) | ||
61 | { | ||
62 |
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")) { |
63 | ✗ | throw Exception("Messages has no variable attribute!"); | |
64 | } | ||
65 | |||
66 |
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"); |
67 | } | ||
68 | |||
69 | /****************************************************************************/ | ||
70 | |||
71 | /** Get the index. | ||
72 | */ | ||
73 | 12 | int Message::Impl::indexFromPlainXmlElement(QDomElement elem /**< Element. */ | |
74 | ) | ||
75 | { | ||
76 |
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")) { |
77 | 12 | 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 | 6 | void Message::Impl::fromPlainXmlElement( | |
94 | QDomElement elem, /**< Element. */ | ||
95 | const QString &pathPrefix /**< Prefix to path (with leading /). */ | ||
96 | ) | ||
97 | { | ||
98 |
1/2✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
12 | QDomNodeList children = elem.childNodes(); |
99 | |||
100 |
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); |
101 |
2/4✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 6 times.
✗ Branch 6 not taken.
|
6 | index = indexFromPlainXmlElement(elem); |
102 | |||
103 |
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")) { |
104 | ✗ | throw Exception("Messages has no type attribute!"); | |
105 | } | ||
106 | |||
107 |
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")); |
108 | |||
109 | // find Text and Descriptions elements | ||
110 |
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++) { |
111 |
1/2✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
|
24 | QDomNode node = children.item(i); |
112 |
2/4✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
|
12 | if (node.isElement()) { |
113 |
1/2✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
|
24 | QDomElement child = node.toElement(); |
114 |
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") { |
115 |
2/4✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 6 times.
✗ Branch 6 not taken.
|
6 | loadTranslations(child, text); |
116 | } | ||
117 |
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") { |
118 |
2/4✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 6 times.
✗ Branch 6 not taken.
|
6 | loadTranslations(child, description); |
119 | } | ||
120 | } | ||
121 | } | ||
122 | 6 | } | |
123 | |||
124 | /****************************************************************************/ | ||
125 | |||
126 | /** Constructor with PdCom5 message. | ||
127 | */ | ||
128 | 13 | void Message::Impl::fromPdComMessage(const PdCom::Message &message) | |
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 |