swift
Loading...
Searching...
No Matches
artifact.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2017 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
4#include "misc/db/artifact.h"
5
6#include <QRegularExpression>
7#include <QStringBuilder>
8
10#include "misc/stringutils.h"
11
12using namespace swift::config;
13using namespace swift::misc::network;
14
15SWIFT_DEFINE_VALUEOBJECT_MIXINS(swift::misc::db, CArtifact)
16
17namespace swift::misc::db
18{
19 CArtifact::CArtifact(const QString &name, const QString &version, const QString &md5, CArtifact::ArtifactType type,
20 int size, bool existing, const CPlatform &platform)
21 : m_name(name.trimmed()), m_md5(md5), m_type(static_cast<int>(type)), m_size(size), m_existing(existing),
22 m_platform(platform)
23 {
24 this->setVersion(trimVersionString(version));
25 if (!name.isEmpty() && version.isEmpty())
26 {
27 m_version = versionNumberFromFilename(name);
28 }
29 }
30
32 {
33 const bool m =
34 (cs == Qt::CaseInsensitive) ? caseInsensitiveStringCompare(this->getName(), name) : name == this->getName();
35 if (m)
36 {
37 return true;
38 }
39 return name.startsWith(this->getName(), cs);
40 }
41
43 {
44 if (m_size < 0)
45 {
46 return {};
47 }
49 }
50
51 bool CArtifact::matchesAnyPlatform(const CPlatform &platform) const { return m_platform.matchesAny(platform); }
52
53 bool CArtifact::hasUnrestrictedDistribution() const { return m_distributions.containsUnrestricted(); }
54
55 bool CArtifact::isWithDistribution(const CDistribution &distribution, bool acceptMoreStableDistributions) const
56 {
57 if (distribution.isEmpty() || !this->hasDistributions())
58 {
59 return false;
60 }
61 return std::any_of(
62 this->getDistributions().cbegin(), this->getDistributions().cend(), [&](const CDistribution &dist) {
63 return dist == distribution || (acceptMoreStableDistributions && dist.isStabilityBetter(distribution));
64 });
65 }
66
68 {
69 if (!this->hasDistributions())
70 {
71 return {};
72 }
73 CRemoteFile rf(this->getName(), this->getFileSize());
75 const CUrl url = d.getDownloadUrl();
76 if (url.isEmpty())
77 {
78 return {};
79 }
81 rf.setUrl(url);
82 rf.setDescription(this->getPlatform().toQString() + " " + d.getChannel());
83 return rf;
84 }
85
87 {
88 if (this->isUnknown())
89 {
90 return false;
91 }
92 if (!this->hasVersion())
93 {
94 return false;
95 }
96 return this->getQVersion() > CBuildConfig::getVersion();
97 }
98
99 QString CArtifact::convertToQString(bool i18n) const { return this->convertToQString(", ", i18n); }
100
101 QString CArtifact::convertToQString(const QString &separator, bool i18n) const
102 {
103 Q_UNUSED(i18n);
104 return u"name: " % this->getName() % separator % u"size: " % this->getFileSizeHumanReadable() % separator %
105 u"OS: " % this->getPlatform().toQString(i18n) % separator % u"timestamp: " %
107 }
108
110 {
111 if (index.isMyself())
112 {
113 return QVariant::fromValue(*this);
114 }
116 {
118 }
119
120 const auto i = index.frontCasted<ColumnIndex>();
121 switch (i)
122 {
123 case IndexName: return QVariant::fromValue(m_name);
124 case IndexMd5: return QVariant::fromValue(m_md5);
125 case IndexPlatform: return m_platform.propertyByIndex(index.copyFrontRemoved());
126 case IndexType: return QVariant::fromValue(m_type);
127 case IndexSize: return QVariant::fromValue(m_size);
128 case IndexSizeHumanReadable: return QVariant::fromValue(this->getFileSizeHumanReadable());
129 case IndexVersionString: return QVariant::fromValue(m_version);
130 case IndexQVersion: return QVariant::fromValue(this->getQVersion());
131 case IndexDistributions: return QVariant::fromValue(m_distributions);
132 default: return CValueObject::propertyByIndex(index);
133 }
134 }
135
137 {
138 if (index.isMyself())
139 {
140 (*this) = variant.value<CArtifact>();
141 return;
142 }
144 {
146 return;
147 }
148
149 const auto i = index.frontCasted<ColumnIndex>();
150 switch (i)
151 {
152 case IndexName: this->setName(variant.toString()); break;
153 case IndexMd5: m_md5 = variant.toString(); break;
154 case IndexPlatform: m_platform.setPropertyByIndex(index.copyFrontRemoved(), variant); break;
155 case IndexType: m_type = variant.toInt(); break;
156 case IndexSize: m_size = variant.toInt(); break;
157 case IndexVersionString: m_version = variant.toString(); break;
158 case IndexDistributions: m_distributions = variant.value<CDistributionList>(); break;
159 default: CValueObject::setPropertyByIndex(index, variant); break;
160 }
161 }
162
164 {
165 Q_UNUSED(prefix); // not nested
166
167 const QString name = json.value("name").toString();
168 const QString md5 = json.value("md5").toString();
169 const QString version = json.value("version").toString();
170 const CPlatform platform = CPlatform(json.value("os").toString());
171 const CArtifact::ArtifactType type = stringToType(json.value("type").toString());
172 const int size = json.value("size").toInt(-1);
173 const bool existing = json.value("existing").toBool();
174
175 CArtifact artifact(name, version, md5, type, size, existing, platform);
177 if (json.contains("distributions"))
178 {
179 const QJsonObject distJson = json.value("distributions").toObject();
180 if (!distJson.isEmpty() && distJson.contains("distributionArray"))
181 {
182 const CDistributionList distributions =
183 CDistributionList::fromDatabaseJson(distJson.value("distributionArray").toArray());
184 artifact.setDistributions(distributions);
185 }
186 }
187 return artifact;
188 }
189
191 {
192 static const QString xswb("xswiftbus");
193 static const QString installer("pilot client installer");
194 static const QString symbols("symbols");
195 static const QString unknown("unknown");
196
197 switch (type)
198 {
199 case XSwiftBus: return xswb;
200 case PilotClientInstaller: return installer;
201 case Symbols: return symbols;
202 case UnknownArtifact:
203 default: break;
204 }
205 return unknown;
206 }
207
209 {
210 if (name.isEmpty())
211 {
212 return CPlatform::unknownOs();
213 }
214 const QString n(name.toLower().trimmed());
215 if (n.contains("-windows-") || n.endsWith(".exe"))
216 {
217 if (n.contains("-64-"))
218 {
220 }
221 if (n.contains("-32-"))
222 {
224 }
225 return CPlatform::unknownOs();
226 }
227
228 if (n.contains("-macos-") || n.endsWith(".dmg"))
229 {
231 }
232 if (n.contains("-linux-") || n.endsWith(".run"))
233 {
235 }
236 if (n.contains("-allos-"))
237 {
238 return CPlatform::allOs();
239 }
240
241 return CPlatform::unknownOs();
242 }
243
244 QString CArtifact::versionNumberFromFilename(const QString &filename)
245 {
246 if (filename.isEmpty())
247 {
248 return {};
249 }
250
251 // swiftinstaller-linux-64-0.9.2.123.run
252 thread_local const QRegularExpression regex { R"(\d+\.\d+\.\d+\.\d+)" };
253 const QRegularExpressionMatch match = regex.match(filename);
254 return match.captured();
255 }
256
257 CArtifact::ArtifactType CArtifact::stringToType(const QString &str)
258 {
259 const QString s(str.trimmed().toLower());
260 if (s.contains("installer")) return CArtifact::PilotClientInstaller;
261 if (s.contains("client")) return CArtifact::PilotClientInstaller;
262 if (s.contains("symb")) return CArtifact::Symbols;
263 if (s.contains("bus")) return CArtifact::XSwiftBus;
264 return CArtifact::UnknownArtifact;
265 }
266
267 QString CArtifact::trimVersionString(const QString &version)
268 {
269 if (version.count('.') != 3) return version; // no 4th segment
270 QStringList parts = version.split('.');
271 const QString p4 = trim4thSegment(parts[3]);
272 if (p4 == parts[3])
273 {
274 return version;
275 } // nothing changed
276 parts[3] = p4;
277 const QString v = parts.join('.');
278 return v;
279 }
280
281 QString CArtifact::trim4thSegment(const QString &seg)
282 {
283 // old schema: yMMddHHmm (9)
284 if (seg.length() >= 9)
285 {
286 return QStringLiteral("0");
287 }
288 return seg;
289 }
290} // namespace swift::misc::db
static const QVersionNumber & getVersion()
Version as QVersionNumber.
static QString humanReadableFileSize(qint64 size)
Human readable (GB, MB, ..) file size.
Platform (i.e.
Definition platform.h:24
static const CPlatform & linuxPlatform()
Linux.
Definition platform.cpp:223
static const CPlatform & macOSPlatform()
Mac OS.
Definition platform.cpp:229
static const CPlatform & unknownOs()
Unknown OS.
Definition platform.cpp:235
static const CPlatform & win32Platform()
Win32.
Definition platform.cpp:211
static const CPlatform & win64Platform()
Win64.
Definition platform.cpp:217
static const CPlatform & allOs()
All OS.
Definition platform.cpp:241
Non-owning reference to a CPropertyIndex with a subset of its features.
Q_REQUIRED_RESULT CPropertyIndexRef copyFrontRemoved() const
Copy with first element removed.
CastType frontCasted() const
First element casted to given type, usually the PropertIndex enum.
bool isMyself() const
Myself index, used with nesting.
QString getFormattedUtcTimestampYmdhms() const
As yyyy MM dd HH mm ss.
QDateTime getUtcTimestamp() const
Get timestamp.
void setUtcTimestamp(const QDateTime &timestamp)
Set timestamp.
ColumnIndex
Properties by index.
Definition artifact.h:27
bool hasUnrestrictedDistribution() const
Has unrestricted distribution.
Definition artifact.cpp:53
bool isWithDistribution(const CDistribution &distribution, bool acceptMoreStableDistributions) const
Is distributed with given distribution?
Definition artifact.cpp:55
bool hasDistributions() const
Has distributions?
Definition artifact.h:107
void setName(const QString &name)
Set the name.
Definition artifact.h:62
bool matchesAnyPlatform(const CPlatform &platform) const
Matches any platform.
Definition artifact.cpp:51
void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition artifact.cpp:136
QString getFileSizeHumanReadable() const
Human readable (GB, MB, ..) file size.
Definition artifact.cpp:42
network::CRemoteFile asRemoteFile() const
Turn into remote file.
Definition artifact.cpp:67
const CPlatform & getPlatform() const
OS.
Definition artifact.h:89
const QString & getName() const
Name (i.e. installer name, symbol name).
Definition artifact.h:59
bool isUnknown() const
Unknown.
Definition artifact.h:71
int getFileSize() const
File size.
Definition artifact.h:77
QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const
Property by index.
Definition artifact.cpp:109
CArtifact()=default
Default constructor.
static CPlatform artifactNameToPlatform(const QString &name)
Name to platform.
Definition artifact.cpp:208
bool isNewerThanCurrentBuild() const
Newer than the current build.
Definition artifact.cpp:86
void setDistributions(const CDistributionList &distributions)
Related distributions.
Definition artifact.h:104
const CDistributionList & getDistributions() const
Related distributions.
Definition artifact.h:98
static const QString & typeToString(ArtifactType type)
Type as string.
Definition artifact.cpp:190
static CArtifact fromDatabaseJson(const QJsonObject &json, const QString &prefix={})
Object from database JSON format.
Definition artifact.cpp:163
bool matchesName(const QString &name, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Matching name?
Definition artifact.cpp:31
CDistribution getMostStableDistribution() const
Most stable distribution if any.
Definition artifact.h:101
QString convertToQString(bool i18n=false) const
Cast as QString.
Definition artifact.cpp:99
Distributions for channel.
bool isStabilityBetter(const CDistribution &otherDistribution) const
"this" having better stability than other distribution?
const QString & getChannel() const
Version channel (Alpha, Beta, Stable ..).
const network::CUrl & getDownloadUrl() const
Download URL, i.e. here one can download installer.
Multiple distributions for different channels:
static CDistributionList fromDatabaseJson(const QJsonArray &array)
From database JSON by array.
QVersionNumber getQVersion() const
Version as QVersion.
Definition datastore.cpp:16
QString m_version
version info
Definition datastore.h:63
bool hasVersion() const
Having a version?s.
Definition datastore.h:54
void setVersion(const QString &version)
Version info.
Definition datastore.h:57
QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const
Property by index.
static bool canHandleIndex(swift::misc::CPropertyIndexRef index)
Can given index be handled?
void setKeyVersionTimestampFromDatabaseJson(const QJsonObject &json, const QString &prefix=QString())
Set key and timestamp values.
void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant)
Set property by index.
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition mixinindex.h:167
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition mixinindex.h:180
QString toQString(bool i18n=false) const
Definition mixinstring.h:74
void setDescription(const QString &description)
Description.
Definition remotefile.h:73
void setUrl(const CUrl &url)
Set URL.
Definition remotefile.h:94
Value object encapsulating information of a location, kind of simplified CValueObject compliant versi...
Definition url.h:27
bool isEmpty() const
Empty.
Definition url.cpp:63
Plugin loaded by X-Plane which publishes a DBus service.
Definition command.h:14
SWIFT_MISC_EXPORT bool caseInsensitiveStringCompare(const QString &c1, const QString &c2)
Case insensitive string compare.
bool contains(QLatin1StringView key) const const
bool isEmpty() const const
QJsonValue value(QLatin1StringView key) const const
QJsonArray toArray() const const
QJsonObject toObject() const const
QRegularExpressionMatch match(QStringView subjectView, qsizetype offset, QRegularExpression::MatchType matchType, QRegularExpression::MatchOptions matchOptions) const const
QString captured(QAnyStringView name) const const
qsizetype count() const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool endsWith(QChar c, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
qsizetype length() const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QString toLower() const const
QString trimmed() const const
QString join(QChar separator) const const
CaseSensitivity
QVariant fromValue(T &&value)
int toInt(bool *ok) const const
QString toString() const const
T value() const &const
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition valueobject.h:67