swift
Loading...
Searching...
No Matches
datastoreutility.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#include <QJsonArray>
7#include <QJsonDocument>
8#include <QJsonObject>
9#include <QJsonValue>
10#include <QTimeZone>
11
12#include "misc/logcategories.h"
14#include "misc/statusmessage.h"
16#include "misc/stringutils.h"
17
18using namespace swift::misc;
19using namespace swift::misc::network;
20
21namespace swift::misc::db
22{
24 {
25 static const QStringList cats({ CLogCategories::swiftDbWebservice() });
26 return cats;
27 }
28
30
32 {
33 static const QString y("Y");
34 static const QString n("N");
35 return v ? y : n;
36 }
37
39 {
40 QString ks(stringWithKey.trimmed());
41 if (ks.isEmpty())
42 {
43 return -1;
44 }
45 bool ok = false;
46 int key = ks.toInt(&ok);
47 if (ok)
48 {
49 return key;
50 } // only a number
51
52 // key in string with ()
53 int i1 = ks.lastIndexOf('(');
54 if (i1 < 0)
55 {
56 return -1;
57 }
58 int i2 = ks.lastIndexOf(')');
59 if (i2 <= i1 + 1)
60 {
61 return -1;
62 }
63 const QString n(ks.mid(i1 + 1, i2 - i1 - 1));
64 ok = false;
65 key = n.toInt(&ok);
66 return ok ? key : -1;
67 }
68
70 {
71 const int i = valueWithKey.indexOf('(');
72 if (i < 0)
73 {
74 return valueWithKey.trimmed();
75 }
76 if (i < 1)
77 {
78 return {};
79 }
80 return valueWithKey.left(i - 1).trimmed();
81 }
82
84 {
85 if (timestamp.isEmpty())
86 {
87 return {};
88 }
90 }
91
93 {
94 if (jsonResponse.isEmpty())
95 {
97 u"Empty JSON data for published models"));
98 return false;
99 }
100
101 const QJsonDocument jsonDoc(QJsonDocument::fromJson(jsonResponse.toUtf8()));
102
103 // array of messages only
104 if (jsonDoc.isArray())
105 {
107 messages.push_back(msgs);
108 return true;
109 }
110
111 // no object -> most likely some fucked up HTML string with the PHP error
112 if (!jsonDoc.isObject())
113 {
115 messages.push_back(
116 CStatusMessage(static_cast<CDatastoreUtility *>(nullptr), CStatusMessage::SeverityError, phpError));
117 return false;
118 }
119
120 QJsonObject json(jsonDoc.object());
121 if (json.contains("msgs"))
122 {
123 const QJsonValue msgJson(json.take("msgs"));
125 if (!msgs.isEmpty())
126 {
127 messages.push_back(msgs);
128 return !messages.hasErrorMessages();
129 }
130 }
131 return false;
132 }
133} // namespace swift::misc::db
static const QString & swiftDbWebservice()
Webservice with swift DB.
void push_back(const T &value)
Appends an element at the end of the sequence.
Definition sequence.h:308
bool isEmpty() const
Synonym for empty.
Definition sequence.h:285
Streamable status message, e.g.
static constexpr auto SeverityError
Status severities.
Status messages, e.g. from Core -> GUI.
bool hasErrorMessages() const
Error messages.
static CStatusMessageList fromDatabaseJson(const QJsonArray &array)
From our database JSON format.
CDatastoreUtility()=delete
No constructor.
static const QString & boolToDbYN(bool v)
Bool to DB yes/no.
static const QStringList & getLogCategories()
Log.categories.
static int extractIntegerKey(const QString &stringWithKey)
Extract key from string like "MyAircraft (33)".
static QString stripKeyInParentheses(const QString &valueWithKey)
Strip the parentheses part, e.g. "foo (123)" => "foo".
static QDateTime parseTimestamp(const QString &timestamp)
Parse a timestamp object.
static bool dbBoolStringToBool(const QString &dbBool)
DB Bool value to bool.
static bool parseAutoPublishResponse(const QString &jsonResponse, CStatusMessageList &messages)
Auto publish response.
static QString removeHtmlPartsFromPhpErrorMessage(const QString &errorMessage)
Remove the HTML formatting from a PHP error message.
Free functions in swift::misc.
SWIFT_MISC_EXPORT QString removeDateTimeSeparators(const QString &s)
Remove the typical separators such as "-", " ".
SWIFT_MISC_EXPORT bool stringToBool(const QString &boolString)
Convert string to bool.
SWIFT_MISC_EXPORT QDateTime parseDateTimeStringOptimized(const QString &dateTimeString)
Parse yyyyMMddHHmmsszzz strings optimized.
QJsonArray array() const const
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
bool isArray() const const
bool isObject() const const
QJsonObject object() const const
QJsonArray toArray() const const
qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
qsizetype lastIndexOf(QChar ch, Qt::CaseSensitivity cs) const const
QString left(qsizetype n) &&
QString mid(qsizetype position, qsizetype n) &&
int toInt(bool *ok, int base) const const
QByteArray toUtf8() const const
QString trimmed() const const