swift
Loading...
Searching...
No Matches
distribution.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
5
6#include <QRegularExpression>
7#include <QStringBuilder>
8
9using namespace swift::config;
10using namespace swift::misc;
11using namespace swift::misc::network;
12
13SWIFT_DEFINE_VALUEOBJECT_MIXINS(swift::misc::db, CDistribution)
14
15namespace swift::misc::db
16{
17 CDistribution::CDistribution(const QString &channel, int stability, bool restricted)
18 : m_channel(channel.trimmed().toUpper()), m_stability(stability), m_restricted(restricted)
19 {}
20
21 void CDistribution::setChannel(const QString &channel) { m_channel = channel.trimmed().toUpper(); }
22
24 {
25 if (url.isEmpty())
26 {
27 return;
28 }
29 m_downloadUrl = url;
30 }
31
32 bool CDistribution::hasDownloadUrl() const { return !m_downloadUrl.isEmpty(); }
33
35 {
36 // StandardIconLockOpen16 is hard to distinguish from closed
37 return m_restricted ? CIcons::StandardIconLockClosed16 : CIcons::StandardIconGlobe16;
38 }
39
40 bool CDistribution::isStabilitySameOrBetter(const CDistribution &otherDistribution) const
41 {
42 return m_stability >= otherDistribution.m_stability;
43 }
44
45 bool CDistribution::isStabilityBetter(const CDistribution &otherDistribution) const
46 {
47 return m_stability > otherDistribution.m_stability;
48 }
49
50 QString CDistribution::convertToQString(bool i18n) const { return convertToQString(", ", i18n); }
51
52 QString CDistribution::convertToQString(const QString &separator, bool i18n) const
53 {
54 return u"channel: " % this->getChannel() % separator % u"download URL: " % getDownloadUrl().toQString(i18n) %
55 separator % u"timestamp: " % this->getFormattedUtcTimestampYmdhms();
56 }
57
59
61 {
62 if (index.isMyself())
63 {
64 return QVariant::fromValue(*this);
65 }
67 {
69 }
70
71 const auto i = index.frontCasted<ColumnIndex>();
72 switch (i)
73 {
74 case IndexChannel: return QVariant::fromValue(m_channel);
75 case IndexStability: return QVariant::fromValue(m_stability);
76 case IndexDownloadUrl: return QVariant::fromValue(m_downloadUrl);
77 case IndexRestricted: return QVariant::fromValue(m_restricted);
78 default: return CValueObject::propertyByIndex(index);
79 }
80 }
81
83 {
84 if (index.isMyself())
85 {
86 (*this) = variant.value<CDistribution>();
87 return;
88 }
90 {
92 return;
93 }
94
95 const auto i = index.frontCasted<ColumnIndex>();
96 switch (i)
97 {
98 case IndexChannel: this->setChannel(variant.value<QString>()); break;
99 case IndexStability: m_stability = variant.toInt(); break;
100 case IndexDownloadUrl: m_downloadUrl = variant.value<CUrl>(); break;
101 case IndexRestricted: m_restricted = variant.toBool(); break;
102 default: CValueObject::setPropertyByIndex(index, variant); break;
103 }
104 }
105
107 {
108 static const CDistribution d = [] {
109 CDistribution ld("local developer", 0, true);
111 return ld;
112 }();
113 return d;
114 }
115
117 {
118 Q_UNUSED(prefix); // not nested
119
120 const QString channel(json.value("channel").toString());
121 const bool restricted = json.value("restricted").toBool();
122 const int stability = json.value("stability").toInt();
123 CDistribution distribution(channel, stability, restricted);
125
126 // add the URLs
127 for (int i = 0; i < 5; i++)
128 {
129 const QString key = "url" + QString::number(i);
130 const QString url = json.value(key).toString();
131 if (url.isEmpty())
132 {
133 continue;
134 }
135 distribution.setDownloadUrl(CUrl(url));
136 }
137 return distribution;
138 }
139} // namespace swift::misc::db
IconIndex
Index for each icon, allows to send them via DBus, efficiently store them, etc.
Definition icons.h:32
Non-owning reference to a CPropertyIndex with a subset of its features.
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.
void setCurrentUtcTime()
Set the current time as timestamp.
bool isStabilityBetter(const CDistribution &otherDistribution) const
"this" having better stability than other distribution?
const QString & getChannel() const
Version channel (Alpha, Beta, Stable ..).
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
CDistribution()=default
Default constructor.
CIcons::IconIndex toIcon() const
Representing icon.
ColumnIndex
Properties by index.
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
CIcons::IconIndex getRestrictionIcon() const
Get the restrict icon.
const network::CUrl & getDownloadUrl() const
Download URL, i.e. here one can download installer.
QString convertToQString(bool i18n=false) const
Cast as QString.
void setChannel(const QString &channel)
Set the channel.
static const CDistribution & localDeveloperBuild()
Pseudo distribution for local builds.
bool isStabilitySameOrBetter(const CDistribution &otherDistribution) const
"this" having same or better stability than other distribution?
void setDownloadUrl(const swift::misc::network::CUrl &url)
Set URL, ignored if empty.
static CDistribution fromDatabaseJson(const QJsonObject &json, const QString &prefix={})
Object from database JSON format.
bool hasDownloadUrl() const
Has a download URL?
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
Cast as QString.
Definition mixinstring.h:74
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
Free functions in swift::misc.
bool isEmpty() const const
QString number(double n, char format, int precision)
QString toUpper() const const
QString trimmed() const const
QVariant fromValue(T &&value)
bool toBool() const const
int toInt(bool *ok) 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