MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / onServerReply

Method onServerReply

app/src/Licensing/Trial.cpp:244–311  ·  view source on GitHub ↗

* @brief Handles the server response for trial activation; the returned expiry * is clamped to a 14-day cap before the trial state and capability token are * derived from it. */

Source from the content-addressed store, hash-verified

242 * derived from it.
243 */
244void Licensing::Trial::onServerReply(QNetworkReply* reply)
245{
246 m_busy = false;
247 Q_EMIT busyChanged();
248
249 if (reply->error() != QNetworkReply::NoError) {
250 Misc::Utilities::showMessageBox(QObject::tr("Network error"),
251 reply->errorString(),
252 QMessageBox::Critical,
253 QObject::tr("Trial Activation Error"));
254 reply->deleteLater();
255 Q_EMIT enabledChanged();
256 return;
257 }
258
259 const QByteArray data = reply->readAll();
260 reply->deleteLater();
261
262 QJsonParseError parseError;
263 const QJsonDocument document = QJsonDocument::fromJson(data, &parseError);
264 if (parseError.error != QJsonParseError::NoError || !document.isObject()) {
265 Misc::Utilities::showMessageBox(
266 QObject::tr("Invalid server response"),
267 QObject::tr("The server returned malformed data: %1").arg(parseError.errorString()),
268 QMessageBox::Warning,
269 QObject::tr("Trial Activation Error"));
270 Q_EMIT enabledChanged();
271 return;
272 }
273
274 m_trialEnabled = false;
275 m_deviceRegistered = false;
276 m_trialExpiry = QDateTime::currentDateTimeUtc();
277
278 const QJsonObject object = document.object();
279 const auto expiryVal = object.value("expireAt");
280 const auto enabledVal = object.value("trialEnabled");
281 const auto deviceRegistered = object.value("registered");
282
283 if (expiryVal.isString() && enabledVal.isBool() && deviceRegistered.isBool()) {
284 const QString expiryStr = expiryVal.toString();
285 QDateTime expiry = QDateTime::fromString(expiryStr, Qt::ISODate).toUTC();
286 if (expiry.isValid()) {
287 const QDateTime now = QDateTime::currentDateTimeUtc();
288 if (expiry > now.addDays(14))
289 expiry = now.addDays(14);
290
291 m_trialExpiry = expiry;
292 m_trialEnabled = enabledVal.toBool() && (expiry > now);
293 m_deviceRegistered = deviceRegistered.toBool();
294 }
295 }
296
297 else {
298 Misc::Utilities::showMessageBox(QObject::tr("Unexpected server response"),
299 QObject::tr("The server response is missing required fields."),
300 QMessageBox::Warning,
301 QObject::tr("Trial Activation Error"));

Callers

nothing calls this directly

Calls 6

fromJsonFunction · 0.85
installTrialTokenFunction · 0.85
errorMethod · 0.80
errorStringMethod · 0.80
valueMethod · 0.45
isValidMethod · 0.45

Tested by

no test coverage detected