swift
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 
4 #include "misc/db/distribution.h"
5 
6 #include <QRegularExpression>
7 #include <QStringBuilder>
8 
9 using namespace swift::config;
10 using namespace swift::misc;
11 using namespace swift::misc::network;
12 
13 SWIFT_DEFINE_VALUEOBJECT_MIXINS(swift::misc::db, CDistribution)
14 
15 namespace swift::misc::db
16 {
17  CDistribution::CDistribution() {}
18 
19  CDistribution::CDistribution(const QString &channel, int stability, bool restricted)
20  : m_channel(channel.trimmed().toUpper()), m_stability(stability), m_restricted(restricted)
21  {}
22 
23  void CDistribution::setChannel(const QString &channel) { m_channel = channel.trimmed().toUpper(); }
24 
26  {
27  if (url.isEmpty()) { return; }
28  m_downloadUrl = url;
29  }
30 
31  bool CDistribution::hasDownloadUrl() const { return !m_downloadUrl.isEmpty(); }
32 
34  {
35  // StandardIconLockOpen16 is hard to distinguish from closed
36  return m_restricted ? CIcons::StandardIconLockClosed16 : CIcons::StandardIconGlobe16;
37  }
38 
39  bool CDistribution::isStabilitySameOrBetter(const CDistribution &otherDistribution) const
40  {
41  return m_stability >= otherDistribution.m_stability;
42  }
43 
44  bool CDistribution::isStabilityBetter(const CDistribution &otherDistribution) const
45  {
46  return m_stability > otherDistribution.m_stability;
47  }
48 
49  QString CDistribution::convertToQString(bool i18n) const { return convertToQString(", ", i18n); }
50 
51  QString CDistribution::convertToQString(const QString &separator, bool i18n) const
52  {
53  return u"channel: " % this->getChannel() % separator % u"download URL: " % getDownloadUrl().toQString(i18n) %
54  separator % u"timestamp: " % this->getFormattedUtcTimestampYmdhms();
55  }
56 
58 
60  {
61  if (index.isMyself()) { return QVariant::fromValue(*this); }
63  {
65  }
66 
67  const ColumnIndex i = index.frontCasted<ColumnIndex>();
68  switch (i)
69  {
70  case IndexChannel: return QVariant::fromValue(m_channel);
71  case IndexStability: return QVariant::fromValue(m_stability);
72  case IndexDownloadUrl: return QVariant::fromValue(m_downloadUrl);
73  case IndexRestricted: return QVariant::fromValue(m_restricted);
74  default: return CValueObject::propertyByIndex(index);
75  }
76  }
77 
78  void CDistribution::setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
79  {
80  if (index.isMyself())
81  {
82  (*this) = variant.value<CDistribution>();
83  return;
84  }
86  {
88  return;
89  }
90 
91  const ColumnIndex i = index.frontCasted<ColumnIndex>();
92  switch (i)
93  {
94  case IndexChannel: this->setChannel(variant.value<QString>()); break;
95  case IndexStability: m_stability = variant.toInt(); break;
96  case IndexDownloadUrl: m_downloadUrl = variant.value<CUrl>(); break;
97  case IndexRestricted: m_restricted = variant.toBool(); break;
98  default: CValueObject::setPropertyByIndex(index, variant); break;
99  }
100  }
101 
103  {
104  static const CDistribution d = [] {
105  CDistribution ld("local developer", 0, true);
106  ld.setCurrentUtcTime();
107  return ld;
108  }();
109  return d;
110  }
111 
112  CDistribution CDistribution::fromDatabaseJson(const QJsonObject &json, const QString &prefix)
113  {
114  Q_UNUSED(prefix); // not nested
115 
116  const QString channel(json.value("channel").toString());
117  const bool restricted = json.value("restricted").toBool();
118  const int stability = json.value("stability").toInt();
119  CDistribution distribution(channel, stability, restricted);
120  distribution.setKeyVersionTimestampFromDatabaseJson(json);
121 
122  // add the URLs
123  for (int i = 0; i < 5; i++)
124  {
125  const QString key = "url" + QString::number(i);
126  const QString url = json.value(key).toString();
127  if (url.isEmpty()) { continue; }
128  distribution.setDownloadUrl(CUrl(url));
129  }
130  return distribution;
131  }
132 } // 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.
Distributions for channel.
Definition: distribution.h:27
bool isStabilityBetter(const CDistribution &otherDistribution) const
"this" having better stability than other distribution?
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
CIcons::IconIndex toIcon() const
Representing icon.
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
CIcons::IconIndex getRestrictionIcon() const
Get the restrict icon.
const QString & getChannel() const
Version channel (Alpha, Beta, Stable ..)
Definition: distribution.h:45
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?
const network::CUrl & getDownloadUrl() const
Download URL, i.e. here one can download installer.
Definition: distribution.h:57
QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const
Property by index.
Definition: datastore.cpp:94
static bool canHandleIndex(swift::misc::CPropertyIndexRef index)
Can given index be handled?
Definition: datastore.cpp:147
void setKeyVersionTimestampFromDatabaseJson(const QJsonObject &json, const QString &prefix=QString())
Set key and timestamp values.
Definition: datastore.cpp:79
void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition: datastore.cpp:110
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition: mixinindex.h:160
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition: mixinindex.h:167
QString toQString(bool i18n=false) const
Cast as QString.
Definition: mixinstring.h:76
Value object encapsulating information of a location, kind of simplified CValueObject compliant versi...
Definition: url.h:27
bool isEmpty() const
Empty.
Definition: url.cpp:54
Free functions in swift::misc.
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition: valueobject.h:67