GCC Code Coverage Report


Directory: ./
File: qtpdcom/src/MessageImpl.cpp
Date: 2023-11-12 04:06:57
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 {
38 QObject::connect(&variable, SIGNAL(valueChanged()),
39 this, SLOT(valueChanged()));
40 }
41
42 /****************************************************************************/
43
44 /** Destructor.
45 */
46 Message::Impl::~Impl()
47 {
48 }
49
50 /****************************************************************************/
51
52 /** Get the path.
53 */
54 QString Message::Impl::pathFromPlainXmlElement(
55 QDomElement elem, /**< Element. */
56 const QString &pathPrefix /**< Prefix to path (with leading /). */
57 )
58 {
59 if (!elem.hasAttribute("variable")) {
60 throw Exception("Messages has no variable attribute!");
61 }
62
63 return pathPrefix + elem.attribute("variable");
64 }
65
66 /****************************************************************************/
67
68 /** Get the index.
69 */
70 int Message::Impl::indexFromPlainXmlElement(
71 QDomElement elem /**< Element. */
72 )
73 {
74 if (not elem.hasAttribute("index")) {
75 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 void Message::Impl::fromPlainXmlElement(
92 QDomElement elem, /**< Element. */
93 const QString &pathPrefix /**< Prefix to path (with leading /). */
94 )
95 {
96 QDomNodeList children = elem.childNodes();
97
98 path = pathFromPlainXmlElement(elem, pathPrefix);
99 index = indexFromPlainXmlElement(elem);
100
101 if (!elem.hasAttribute("type")) {
102 throw Exception("Messages has no type attribute!");
103 }
104
105 type = typeFromString(elem.attribute("type"));
106
107 // find Text and Descriptions elements
108 for (int i = 0; i < children.size(); i++) {
109 QDomNode node = children.item(i);
110 if (node.isElement()) {
111 QDomElement child = node.toElement();
112 if (child.tagName() == "Text") {
113 loadTranslations(child, text);
114 }
115 else if (child.tagName() == "Description") {
116 loadTranslations(child, description);
117 }
118 }
119 }
120 }
121
122 /****************************************************************************/
123
124 /** Constructor with PdCom5 message.
125 */
126 void Message::Impl::fromPdComMessage(
127 const PdCom::Message &message
128 )
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.setTime_t(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