Attica

content.cpp
1 /*
2  This file is part of KDE.
3 
4  SPDX-FileCopyrightText: 2008 Cornelius Schumacher <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8 
9 #include "content.h"
10 
11 #include <QDateTime>
12 
13 using namespace Attica;
14 
15 class Content::Private : public QSharedData
16 {
17 public:
18  QString m_id;
19  QString m_name;
20  int m_downloads;
21  int m_numberOfComments;
22  int m_rating;
23  QDateTime m_created;
24  QDateTime m_updated;
25  QList<Icon> m_icons;
26  QList<QUrl> m_videos;
27  QStringList m_tags;
28 
29  QMap<QString, QString> m_extendedAttributes;
30 
31  Private()
32  : m_downloads(0)
33  , m_numberOfComments(0)
34  , m_rating(0)
35  {
36  }
37 };
38 
40  : d(new Private)
41 {
42 }
43 
45  : d(other.d)
46 {
47 }
48 
50 {
51  d = other.d;
52  return *this;
53 }
54 
56 {
57 }
58 
59 void Content::setId(const QString &u)
60 {
61  d->m_id = u;
62 }
63 
65 {
66  return d->m_id;
67 }
68 
70 {
71  d->m_name = name;
72 }
73 
75 {
76  return d->m_name;
77 }
78 
79 void Content::setRating(int v)
80 {
81  d->m_rating = v;
82 }
83 
84 int Content::rating() const
85 {
86  return d->m_rating;
87 }
88 
90 {
91  d->m_downloads = v;
92 }
93 
94 int Content::downloads() const
95 {
96  return d->m_downloads;
97 }
98 
100 {
101  d->m_numberOfComments = v;
102 }
103 
105 {
106  return d->m_numberOfComments;
107 }
108 
110 {
111  d->m_created = date;
112 }
113 
115 {
116  return d->m_created;
117 }
118 
120 {
121  d->m_updated = date;
122 }
123 
125 {
126  return d->m_updated;
127 }
128 
129 void Content::addAttribute(const QString &key, const QString &value)
130 {
131  d->m_extendedAttributes.insert(key, value);
132 }
133 
135 {
136  return d->m_extendedAttributes.value(key);
137 }
138 
140 {
141  return d->m_extendedAttributes;
142 }
143 
144 bool Content::isValid() const
145 {
146  return !(d->m_id.isEmpty());
147 }
148 
150 {
151  return attribute(QStringLiteral("summary"));
152 }
153 
155 {
156  return attribute(QStringLiteral("description"));
157 }
158 
160 {
161  return QUrl(attribute(QStringLiteral("detailpage")));
162 }
163 
164 QString Attica::Content::changelog() const
165 {
166  return attribute(QStringLiteral("changelog"));
167 }
168 
169 QString Attica::Content::depend() const
170 {
171  return attribute(QStringLiteral("depend"));
172 }
173 
175 {
177  QMap<QString, QString>::const_iterator iter = d->m_extendedAttributes.constBegin();
178  while (iter != d->m_extendedAttributes.constEnd()) {
179  const QString &key = iter.key();
180  static const QLatin1String tag("downloadname");
181  if (key.startsWith(tag)) {
182  bool ok;
183  // remove "downloadlink", get the rest as number
184 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
185  const int num = QStringView(key).right(key.size() - tag.size()).toInt(&ok);
186 #else
187  const int num = key.rightRef(key.size() - tag.size()).toInt(&ok);
188 #endif
189  if (ok) {
190  // check if the download actually has a name
191  if (!iter.value().isEmpty()) {
192  descriptions.append(downloadUrlDescription(num));
193  }
194  }
195  }
196  ++iter;
197  }
198  return descriptions;
199 }
200 
201 Attica::DownloadDescription Attica::Content::downloadUrlDescription(int number) const
202 {
203  QString num(QString::number(number));
204  DownloadDescription desc;
205 
206  Attica::DownloadDescription::Type downloadType = Attica::DownloadDescription::LinkDownload;
207  if (attribute(QLatin1String("downloadway") + num) == QLatin1Char('0')) {
208  downloadType = Attica::DownloadDescription::FileDownload;
209  } else if (attribute(QLatin1String("downloadway") + num) == QLatin1Char('1')) {
210  downloadType = Attica::DownloadDescription::LinkDownload;
211  } else if (attribute(QLatin1String("downloadway") + num) == QLatin1Char('2')) {
212  downloadType = Attica::DownloadDescription::PackageDownload;
213  }
214  desc.setType(downloadType);
215  desc.setId(number);
216  desc.setName(attribute(QLatin1String("downloadname") + num));
217  desc.setDistributionType(attribute(QLatin1String("downloadtype") + num));
218  desc.setHasPrice(attribute(QLatin1String("downloadbuy") + num) == QLatin1Char('1'));
219  desc.setLink(attribute(QLatin1String("downloadlink") + num));
220  desc.setPriceReason(attribute(QLatin1String("downloadreason") + num));
221  desc.setPriceAmount(attribute(QLatin1String("downloadprice") + num));
222  desc.setSize(attribute(QLatin1String("downloadsize") + num).toUInt());
223  desc.setGpgFingerprint(attribute(QLatin1String("downloadgpgfingerprint") + num));
224  desc.setGpgSignature(attribute(QLatin1String("downloadgpgsignature") + num));
225  desc.setPackageName(attribute(QLatin1String("downloadpackagename") + num));
226  desc.setRepository(attribute(QLatin1String("downloadrepository") + num));
227  desc.setTags(attribute(QLatin1String("downloadtags") + num).split(QLatin1Char(',')));
228  return desc;
229 }
230 
232 {
234  QMap<QString, QString>::const_iterator iter = d->m_extendedAttributes.constBegin();
235  while (iter != d->m_extendedAttributes.constEnd()) {
236  QString key = iter.key();
237  if (key.startsWith(QLatin1String("homepagetype"))) {
238  bool ok;
239  // remove "homepage", get the rest as number
240 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
241  const int num = QStringView(key).right(key.size() - 12).toInt(&ok);
242 #else
243  const int num = key.rightRef(key.size() - 12).toInt(&ok);
244 #endif
245  if (ok) {
246  // check if the homepage actually has a valid type
247  if (!iter.value().isEmpty()) {
248  homepages.append(homePageEntry(num));
249  }
250  }
251  }
252  ++iter;
253  }
254 
255  return homepages;
256 }
257 
259 {
260  QString num(QString::number(number));
261  HomePageEntry homepage;
262 
263  if (number == 1 && attribute(QStringLiteral("homepage1")).isEmpty()) {
264  num.clear();
265  }
266  homepage.setType(attribute(QLatin1String("homepagetype") + num));
267  homepage.setUrl(QUrl(attribute(QLatin1String("homepage") + num)));
268  return homepage;
269 }
270 
271 QString Attica::Content::version() const
272 {
273  return attribute(QStringLiteral("version"));
274 }
275 
276 QString Attica::Content::author() const
277 {
278  return attribute(QStringLiteral("personid"));
279 }
280 
281 QString Attica::Content::license() const
282 {
283  return attribute(QStringLiteral("licensetype"));
284 }
285 
286 QString Attica::Content::licenseName() const
287 {
288  return attribute(QStringLiteral("license"));
289 }
290 
291 QString Attica::Content::previewPicture(const QString &number) const
292 {
293  return attribute(QLatin1String("previewpic") + number);
294 }
295 
296 QString Attica::Content::smallPreviewPicture(const QString &number) const
297 {
298  return attribute(QLatin1String("smallpreviewpic") + number);
299 }
300 
302 {
303  return d->m_icons;
304 }
305 
307 {
308  return d->m_icons;
309 }
310 
312 {
313  d->m_icons = std::move(icons); // TODO KF6 Make QList const & and remove the std::move
314 }
315 
317 {
318  return d->m_videos;
319 }
320 
322 {
323  d->m_videos = std::move(videos);
324 }
325 
327 {
328  return d->m_tags;
329 }
330 
332 {
333  d->m_tags = tags;
334 }
Represents a single content.
Definition: content.h:30
void setUpdated(const QDateTime &updated)
Sets the time the Content has been last updated.
Definition: content.cpp:119
void setNumberOfComments(int numComments)
Sets the number of comments for the Content.
Definition: content.cpp:99
Content()
Creates an empty Content.
Definition: content.cpp:39
~Content()
Destructor.
Definition: content.cpp:55
void setVideos(QList< QUrl > videos)
Set list of videos.
Definition: content.cpp:321
QString summary() const
A summary description of this content.
Definition: content.cpp:149
int size() const const
QString attribute(const QString &key) const
Get an attribute that is not included in the basis set of attributes exposed by the Content class...
Definition: content.cpp:134
The HomePageEntry class contains information about one home page entry.
Definition: homepageentry.h:24
void setId(const QString &id)
Sets the id of the Content.
Definition: content.cpp:59
void clear()
int downloads() const
Gets the number of downloads for the Content (how often this has been downloaded from the server)...
Definition: content.cpp:94
QString number(int n, int base)
void append(const T &value)
QList< QUrl > videos()
Get all videos for this content.
Definition: content.cpp:316
int numberOfComments() const
Gets the number of comments for the Content.
Definition: content.cpp:104
QStringList tags() const
Get all the tags for this content.
Definition: content.cpp:326
Content & operator=(const Content &other)
Assignment operator.
Definition: content.cpp:49
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
void addAttribute(const QString &key, const QString &value)
Add an attribute that is not included in the basis set of attributes exposed by the Content class...
Definition: content.cpp:129
bool isValid() const
Checks whether this Content has an id.
Definition: content.cpp:144
QStringRef rightRef(int n) const const
QStringView right(qsizetype length) const const
QString id() const
Gets the id of the Content.
Definition: content.cpp:64
QDateTime updated() const
Gets the date and time the Content has been last updated.
Definition: content.cpp:124
QDateTime created() const
Gets the date and time the Content has been created.
Definition: content.cpp:114
void setCreated(const QDateTime &created)
Sets the date and time the Content has been created.
Definition: content.cpp:109
QString description() const
A description of this content.
Definition: content.cpp:154
HomePageEntry homePageEntry(int number) const
Get the details about a home page (a content can have multiple home pages, blog, bugs, ...).
Definition: content.cpp:258
const Key key(const T &value, const Key &defaultKey) const const
QList< Icon > icons()
Get all icons for this content.
Definition: content.cpp:301
QString name() const
Gets the name of the Content.
Definition: content.cpp:74
void setIcons(QList< Icon > icons)
Set list of icons.
Definition: content.cpp:311
int size() const const
QUrl detailpage() const
A webpage with the detailed description of this content.
Definition: content.cpp:159
void setName(const QString &name)
Sets the name of the Content.
Definition: content.cpp:69
void setTags(const QStringList &tags)
Set the list of tags.
Definition: content.cpp:331
QMap< QString, QString > attributes() const
Get all attributes that are not included in the basis set of attributes exposed by the Content class...
Definition: content.cpp:139
void setDownloads(int downloads)
Sets the number of downloads for the Content.
Definition: content.cpp:89
void setRating(int rating)
Sets the rating of the Content.
Definition: content.cpp:79
int rating() const
Gets the rating of the Content.
Definition: content.cpp:84
QList< DownloadDescription > downloadUrlDescriptions() const
Get all possible downloads.
Definition: content.cpp:174
QList< HomePageEntry > homePageEntries()
Get all home pages for this content.
Definition: content.cpp:231
DownloadDescription downloadUrlDescription(int number) const
Get the details about a download (a content can have multiple links, eg for different distros)...
Definition: content.cpp:201
const T value(const Key &key, const T &defaultValue) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Tue Jan 25 2022 22:53:54 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.