1 | /* |
2 | This file is part of KDE. |
3 | |
4 | SPDX-FileCopyrightText: 2008 Cornelius Schumacher <schumacher@kde.org> |
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 Q_DECL_HIDDEN Content::Private : public QSharedData |
16 | { |
17 | public: |
18 | QString m_id; |
19 | QString m_name; |
20 | int m_downloads; |
21 | int ; |
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 | |
39 | Content::Content() |
40 | : d(new Private) |
41 | { |
42 | } |
43 | |
44 | Content::Content(const Attica::Content &other) |
45 | : d(other.d) |
46 | { |
47 | } |
48 | |
49 | Content &Content::operator=(const Attica::Content &other) |
50 | { |
51 | d = other.d; |
52 | return *this; |
53 | } |
54 | |
55 | Content::~Content() |
56 | { |
57 | } |
58 | |
59 | void Content::setId(const QString &u) |
60 | { |
61 | d->m_id = u; |
62 | } |
63 | |
64 | QString Content::id() const |
65 | { |
66 | return d->m_id; |
67 | } |
68 | |
69 | void Content::setName(const QString &name) |
70 | { |
71 | d->m_name = name; |
72 | } |
73 | |
74 | QString Content::name() const |
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 | |
89 | void Content::setDownloads(int v) |
90 | { |
91 | d->m_downloads = v; |
92 | } |
93 | |
94 | int Content::downloads() const |
95 | { |
96 | return d->m_downloads; |
97 | } |
98 | |
99 | void Content::(int v) |
100 | { |
101 | d->m_numberOfComments = v; |
102 | } |
103 | |
104 | int Content::() const |
105 | { |
106 | return d->m_numberOfComments; |
107 | } |
108 | |
109 | void Content::setCreated(const QDateTime &date) |
110 | { |
111 | d->m_created = date; |
112 | } |
113 | |
114 | QDateTime Content::created() const |
115 | { |
116 | return d->m_created; |
117 | } |
118 | |
119 | void Content::setUpdated(const QDateTime &date) |
120 | { |
121 | d->m_updated = date; |
122 | } |
123 | |
124 | QDateTime Content::updated() const |
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 | |
134 | QString Content::attribute(const QString &key) const |
135 | { |
136 | return d->m_extendedAttributes.value(key); |
137 | } |
138 | |
139 | QMap<QString, QString> Content::attributes() const |
140 | { |
141 | return d->m_extendedAttributes; |
142 | } |
143 | |
144 | bool Content::isValid() const |
145 | { |
146 | return !(d->m_id.isEmpty()); |
147 | } |
148 | |
149 | QString Content::summary() const |
150 | { |
151 | return attribute(QStringLiteral("summary" )); |
152 | } |
153 | |
154 | QString Content::description() const |
155 | { |
156 | return attribute(QStringLiteral("description" )); |
157 | } |
158 | |
159 | QUrl Content::detailpage() const |
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 | |
174 | QList<Attica::DownloadDescription> Attica::Content::downloadUrlDescriptions() const |
175 | { |
176 | QList<Attica::DownloadDescription> descriptions; |
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(s: tag)) { |
182 | bool ok; |
183 | // remove "downloadlink", get the rest as number |
184 | const int num = QStringView(key).right(n: key.size() - tag.size()).toInt(ok: &ok); |
185 | if (ok) { |
186 | // check if the download actually has a name |
187 | if (!iter.value().isEmpty()) { |
188 | descriptions.append(t: downloadUrlDescription(number: num)); |
189 | } |
190 | } |
191 | } |
192 | ++iter; |
193 | } |
194 | return descriptions; |
195 | } |
196 | |
197 | Attica::DownloadDescription Attica::Content::downloadUrlDescription(int number) const |
198 | { |
199 | QString num(QString::number(number)); |
200 | DownloadDescription desc; |
201 | |
202 | Attica::DownloadDescription::Type downloadType = Attica::DownloadDescription::LinkDownload; |
203 | if (attribute(key: QLatin1String("downloadway" ) + num) == QLatin1Char('0')) { |
204 | downloadType = Attica::DownloadDescription::FileDownload; |
205 | } else if (attribute(key: QLatin1String("downloadway" ) + num) == QLatin1Char('1')) { |
206 | downloadType = Attica::DownloadDescription::LinkDownload; |
207 | } else if (attribute(key: QLatin1String("downloadway" ) + num) == QLatin1Char('2')) { |
208 | downloadType = Attica::DownloadDescription::PackageDownload; |
209 | } |
210 | desc.setType(downloadType); |
211 | desc.setId(number); |
212 | desc.setName(attribute(key: QLatin1String("downloadname" ) + num)); |
213 | desc.setDistributionType(attribute(key: QLatin1String("downloadtype" ) + num)); |
214 | desc.setHasPrice(attribute(key: QLatin1String("downloadbuy" ) + num) == QLatin1Char('1')); |
215 | desc.setLink(attribute(key: QLatin1String("downloadlink" ) + num)); |
216 | desc.setPriceReason(attribute(key: QLatin1String("downloadreason" ) + num)); |
217 | desc.setPriceAmount(attribute(key: QLatin1String("downloadprice" ) + num)); |
218 | desc.setSize(attribute(key: QLatin1String("downloadsize" ) + num).toUInt()); |
219 | desc.setGpgFingerprint(attribute(key: QLatin1String("downloadgpgfingerprint" ) + num)); |
220 | desc.setGpgSignature(attribute(key: QLatin1String("downloadgpgsignature" ) + num)); |
221 | desc.setPackageName(attribute(key: QLatin1String("downloadpackagename" ) + num)); |
222 | desc.setRepository(attribute(key: QLatin1String("downloadrepository" ) + num)); |
223 | desc.setTags(attribute(key: QLatin1String("downloadtags" ) + num).split(sep: QLatin1Char(','))); |
224 | return desc; |
225 | } |
226 | |
227 | QList<HomePageEntry> Attica::Content::homePageEntries() |
228 | { |
229 | QList<Attica::HomePageEntry> homepages; |
230 | QMap<QString, QString>::const_iterator iter = d->m_extendedAttributes.constBegin(); |
231 | while (iter != d->m_extendedAttributes.constEnd()) { |
232 | QString key = iter.key(); |
233 | if (key.startsWith(s: QLatin1String("homepagetype" ))) { |
234 | bool ok; |
235 | // remove "homepage", get the rest as number |
236 | const int num = QStringView(key).right(n: key.size() - 12).toInt(ok: &ok); |
237 | if (ok) { |
238 | // check if the homepage actually has a valid type |
239 | if (!iter.value().isEmpty()) { |
240 | homepages.append(t: homePageEntry(number: num)); |
241 | } |
242 | } |
243 | } |
244 | ++iter; |
245 | } |
246 | |
247 | return homepages; |
248 | } |
249 | |
250 | Attica::HomePageEntry Attica::Content::homePageEntry(int number) const |
251 | { |
252 | QString num(QString::number(number)); |
253 | HomePageEntry homepage; |
254 | |
255 | if (number == 1 && attribute(QStringLiteral("homepage1" )).isEmpty()) { |
256 | num.clear(); |
257 | } |
258 | homepage.setType(attribute(key: QLatin1String("homepagetype" ) + num)); |
259 | homepage.setUrl(QUrl(attribute(key: QLatin1String("homepage" ) + num))); |
260 | return homepage; |
261 | } |
262 | |
263 | QString Attica::Content::version() const |
264 | { |
265 | return attribute(QStringLiteral("version" )); |
266 | } |
267 | |
268 | QString Attica::Content::author() const |
269 | { |
270 | return attribute(QStringLiteral("personid" )); |
271 | } |
272 | |
273 | QString Attica::Content::license() const |
274 | { |
275 | return attribute(QStringLiteral("licensetype" )); |
276 | } |
277 | |
278 | QString Attica::Content::licenseName() const |
279 | { |
280 | return attribute(QStringLiteral("license" )); |
281 | } |
282 | |
283 | QString Attica::Content::previewPicture(const QString &number) const |
284 | { |
285 | return attribute(key: QLatin1String("previewpic" ) + number); |
286 | } |
287 | |
288 | QString Attica::Content::smallPreviewPicture(const QString &number) const |
289 | { |
290 | return attribute(key: QLatin1String("smallpreviewpic" ) + number); |
291 | } |
292 | |
293 | QList<Icon> Attica::Content::icons() |
294 | { |
295 | return d->m_icons; |
296 | } |
297 | |
298 | QList<Icon> Attica::Content::icons() const |
299 | { |
300 | return d->m_icons; |
301 | } |
302 | |
303 | void Attica::Content::setIcons(QList<Icon> icons) |
304 | { |
305 | d->m_icons = std::move(icons); // TODO KF6 Make QList const & and remove the std::move |
306 | } |
307 | |
308 | QList<QUrl> Attica::Content::videos() |
309 | { |
310 | return d->m_videos; |
311 | } |
312 | |
313 | void Attica::Content::setVideos(QList<QUrl> videos) |
314 | { |
315 | d->m_videos = std::move(videos); |
316 | } |
317 | |
318 | QStringList Attica::Content::tags() const |
319 | { |
320 | return d->m_tags; |
321 | } |
322 | |
323 | void Attica::Content::setTags(const QStringList &tags) |
324 | { |
325 | d->m_tags = tags; |
326 | } |
327 | |