| 1 | // SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 | // SPDX-FileCopyrightText: 2009-2010 Frederik Gladhorn <gladhorn@kde.org> |
| 3 | // SPDX-FileCopyrightText: 2024 Harald Sitter <sitter@kde.org> |
| 4 | |
| 5 | #include "atticarequester_p.h" |
| 6 | |
| 7 | #include "commentsmodel.h" |
| 8 | #include "entry_p.h" |
| 9 | #include "question.h" |
| 10 | #include "tagsfilterchecker.h" |
| 11 | |
| 12 | #include <KFormat> |
| 13 | #include <KLocalizedString> |
| 14 | #include <QCollator> |
| 15 | #include <QDomDocument> |
| 16 | #include <knewstuffcore_debug.h> |
| 17 | |
| 18 | #include <attica/accountbalance.h> |
| 19 | #include <attica/config.h> |
| 20 | #include <attica/content.h> |
| 21 | #include <attica/downloaditem.h> |
| 22 | #include <attica/listjob.h> |
| 23 | #include <attica/person.h> |
| 24 | #include <attica/provider.h> |
| 25 | #include <attica/providermanager.h> |
| 26 | |
| 27 | #include "atticaprovider_p.h" |
| 28 | #include "searchrequest_p.h" |
| 29 | |
| 30 | using namespace Attica; |
| 31 | |
| 32 | namespace |
| 33 | { |
| 34 | Attica::Provider::SortMode atticaSortMode(KNSCore::SortMode sortMode) |
| 35 | { |
| 36 | switch (sortMode) { |
| 37 | case KNSCore::SortMode::Newest: |
| 38 | return Attica::Provider::Newest; |
| 39 | case KNSCore::SortMode::Alphabetical: |
| 40 | return Attica::Provider::Alphabetical; |
| 41 | case KNSCore::SortMode::Downloads: |
| 42 | return Attica::Provider::Downloads; |
| 43 | case KNSCore::SortMode::Rating: |
| 44 | return Attica::Provider::Rating; |
| 45 | } |
| 46 | qCWarning(KNEWSTUFFCORE) << "Unmapped sortMode" << sortMode; |
| 47 | return Attica::Provider::Rating; |
| 48 | } |
| 49 | } // namespace |
| 50 | |
| 51 | namespace KNSCore |
| 52 | { |
| 53 | |
| 54 | AtticaRequester::AtticaRequester(const KNSCore::SearchRequest &request, AtticaProvider *provider, QObject *parent) |
| 55 | : QObject(parent) |
| 56 | , m_request(request) |
| 57 | , m_provider(provider) |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | void AtticaRequester::detailsLoaded(BaseJob *job) |
| 62 | { |
| 63 | if (m_provider->jobSuccess(job)) { |
| 64 | auto *contentJob = dynamic_cast<ItemJob<Content> *>(job); |
| 65 | Content content = contentJob->result(); |
| 66 | auto entry = entryFromAtticaContent(content); |
| 67 | entry.setEntryRequestedId(job->property(name: "providedEntryId" ).toString()); // The ResultsStream should still known that this entry was for its query |
| 68 | Q_EMIT entryDetailsLoaded(entry); |
| 69 | qCDebug(KNEWSTUFFCORE) << "check update finished: " << entry.name(); |
| 70 | } |
| 71 | |
| 72 | if (m_updateJobs.remove(value: job) && m_updateJobs.isEmpty()) { |
| 73 | qCDebug(KNEWSTUFFCORE) << "check update finished." ; |
| 74 | QList<Entry> updatable; |
| 75 | for (const Entry &entry : std::as_const(t&: m_provider->mCachedEntries)) { |
| 76 | if (entry.status() == KNSCore::Entry::Updateable) { |
| 77 | updatable.append(t: entry); |
| 78 | } |
| 79 | } |
| 80 | qCDebug(KNEWSTUFFCORE) << "UPDATABLE" << updatable; |
| 81 | Q_EMIT entriesLoaded(list: updatable); |
| 82 | Q_EMIT loadingDone(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void AtticaRequester::checkForUpdates() |
| 87 | { |
| 88 | if (m_provider->mCachedEntries.isEmpty()) { |
| 89 | Q_EMIT loadingDone(); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | for (const Entry &entry : std::as_const(t&: m_provider->mCachedEntries)) { |
| 94 | ItemJob<Content> *job = m_provider->m_provider.requestContent(contentId: entry.uniqueId()); |
| 95 | connect(sender: job, signal: &BaseJob::finished, context: this, slot: &AtticaRequester::detailsLoaded); |
| 96 | m_updateJobs.insert(value: job); |
| 97 | job->start(); |
| 98 | qCDebug(KNEWSTUFFCORE) << "Checking for update: " << entry.name(); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | Entry::List AtticaRequester::installedEntries() const |
| 103 | { |
| 104 | Entry::List entries; |
| 105 | for (const Entry &entry : std::as_const(t&: m_provider->mCachedEntries)) { |
| 106 | if (entry.status() == KNSCore::Entry::Installed || entry.status() == KNSCore::Entry::Updateable) { |
| 107 | entries.append(t: entry); |
| 108 | } |
| 109 | } |
| 110 | return entries; |
| 111 | } |
| 112 | |
| 113 | void AtticaRequester::start() |
| 114 | { |
| 115 | QMetaObject::invokeMethod(object: this, function: &AtticaRequester::startInternal, type: Qt::QueuedConnection); |
| 116 | } |
| 117 | |
| 118 | void AtticaRequester::categoryContentsLoaded(BaseJob *job) |
| 119 | { |
| 120 | if (!m_provider->jobSuccess(job)) { |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | auto *listJob = dynamic_cast<ListJob<Content> *>(job); |
| 125 | const Content::List contents = listJob->itemList(); |
| 126 | |
| 127 | Entry::List entries; |
| 128 | TagsFilterChecker checker(m_provider->tagFilter()); |
| 129 | TagsFilterChecker downloadschecker(m_provider->downloadTagFilter()); |
| 130 | for (const Content &content : contents) { |
| 131 | if (!content.isValid()) { |
| 132 | qCDebug(KNEWSTUFFCORE) |
| 133 | << "Filtered out an invalid entry. This suggests something is not right on the originating server. Please contact the administrators of" |
| 134 | << m_provider->name() << "and inform them there is an issue with content in the category or categories" << m_request.d->categories; |
| 135 | continue; |
| 136 | } |
| 137 | if (checker.filterAccepts(tags: content.tags())) { |
| 138 | bool filterAcceptsDownloads = true; |
| 139 | if (content.downloads() > 0) { |
| 140 | filterAcceptsDownloads = false; |
| 141 | const QList<Attica::DownloadDescription> descs = content.downloadUrlDescriptions(); |
| 142 | for (const Attica::DownloadDescription &dli : descs) { |
| 143 | if (downloadschecker.filterAccepts(tags: dli.tags())) { |
| 144 | filterAcceptsDownloads = true; |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | if (filterAcceptsDownloads) { |
| 150 | m_provider->mCachedContent.insert(key: content.id(), value: content); |
| 151 | entries.append(t: entryFromAtticaContent(content)); |
| 152 | } else { |
| 153 | qCDebug(KNEWSTUFFCORE) << "Filter has excluded" << content.name() << "on download filter" << m_provider->downloadTagFilter(); |
| 154 | } |
| 155 | } else { |
| 156 | qCDebug(KNEWSTUFFCORE) << "Filter has excluded" << content.name() << "on entry filter" << m_provider->tagFilter(); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | qCDebug(KNEWSTUFFCORE) << "loaded: " << m_request.d->hashForRequest() << " count: " << entries.size(); |
| 161 | Q_EMIT entriesLoaded(list: entries); |
| 162 | Q_EMIT loadingDone(); |
| 163 | } |
| 164 | |
| 165 | void AtticaRequester::startInternal() |
| 166 | { |
| 167 | switch (m_request.d->filter) { |
| 168 | case KNSCore::Filter::None: |
| 169 | break; |
| 170 | case KNSCore::Filter::ExactEntryId: { |
| 171 | ItemJob<Content> *job = m_provider->m_provider.requestContent(contentId: m_request.d->searchTerm); |
| 172 | job->setProperty(name: "providedEntryId" , value: m_request.d->searchTerm); |
| 173 | connect(sender: job, signal: &BaseJob::finished, context: this, slot: &AtticaRequester::detailsLoaded); |
| 174 | job->start(); |
| 175 | return; |
| 176 | } |
| 177 | case KNSCore::Filter::Installed: |
| 178 | if (m_request.d->page == 0) { |
| 179 | Q_EMIT entriesLoaded(list: installedEntries()); |
| 180 | Q_EMIT loadingDone(); |
| 181 | } else { // We always load everything on the first page. The caller may fetchMore and try to read further pages though. |
| 182 | Q_EMIT loadingDone(); |
| 183 | } |
| 184 | return; |
| 185 | case KNSCore::Filter::Updates: |
| 186 | checkForUpdates(); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | Attica::Provider::SortMode sorting = atticaSortMode(sortMode: m_request.d->sortMode); |
| 191 | Attica::Category::List categoriesToSearch; |
| 192 | |
| 193 | if (m_request.d->categories.isEmpty()) { |
| 194 | // search in all categories |
| 195 | categoriesToSearch = m_provider->mCategoryMap.values(); |
| 196 | } else { |
| 197 | categoriesToSearch.reserve(asize: m_request.d->categories.size()); |
| 198 | for (const QString &categoryName : std::as_const(t&: m_request.d->categories)) { |
| 199 | categoriesToSearch.append(other: m_provider->mCategoryMap.values(key: categoryName)); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | ListJob<Content> *job = |
| 204 | m_provider->m_provider.searchContents(categories: categoriesToSearch, search: m_request.d->searchTerm, mode: sorting, page: m_request.d->page, pageSize: m_request.d->pageSize); |
| 205 | job->setProperty(name: "searchRequest" , value: QVariant::fromValue(value: m_request)); |
| 206 | connect(sender: job, signal: &BaseJob::finished, context: this, slot: &AtticaRequester::categoryContentsLoaded); |
| 207 | job->start(); |
| 208 | } |
| 209 | |
| 210 | Entry AtticaRequester::entryFromAtticaContent(const Attica::Content &content) |
| 211 | { |
| 212 | Entry entry; |
| 213 | |
| 214 | entry.setProviderId(m_provider->id()); |
| 215 | entry.setUniqueId(content.id()); |
| 216 | entry.setStatus(KNSCore::Entry::Downloadable); |
| 217 | entry.setVersion(content.version()); |
| 218 | entry.setReleaseDate(content.updated().date()); |
| 219 | entry.setCategory(content.attribute(QStringLiteral("typeid" ))); |
| 220 | |
| 221 | qCDebug(KNEWSTUFFCORE) << "looking for cache entry" ; |
| 222 | auto index = m_provider->mCachedEntries.indexOf(t: entry); |
| 223 | qCDebug(KNEWSTUFFCORE) << "looking for cache entry" << index; |
| 224 | if (index >= 0) { |
| 225 | Entry &cacheEntry = m_provider->mCachedEntries[index]; |
| 226 | qCDebug(KNEWSTUFFCORE) << "cache entry" << cacheEntry << cacheEntry.version() << entry.version(); |
| 227 | // check if updateable |
| 228 | if (((cacheEntry.status() == KNSCore::Entry::Installed) || (cacheEntry.status() == KNSCore::Entry::Updateable)) |
| 229 | && ((cacheEntry.version() != entry.version()) || (cacheEntry.releaseDate() != entry.releaseDate()))) { |
| 230 | cacheEntry.setStatus(KNSCore::Entry::Updateable); |
| 231 | cacheEntry.setUpdateVersion(entry.version()); |
| 232 | cacheEntry.setUpdateReleaseDate(entry.releaseDate()); |
| 233 | } |
| 234 | entry = cacheEntry; |
| 235 | } else { |
| 236 | m_provider->mCachedEntries.append(t: entry); |
| 237 | } |
| 238 | |
| 239 | entry.setName(content.name()); |
| 240 | entry.setHomepage(content.detailpage()); |
| 241 | entry.setRating(content.rating()); |
| 242 | entry.setNumberOfComments(content.numberOfComments()); |
| 243 | entry.setDownloadCount(content.downloads()); |
| 244 | entry.setNumberFans(content.attribute(QStringLiteral("fans" )).toInt()); |
| 245 | entry.setDonationLink(content.attribute(QStringLiteral("donationpage" ))); |
| 246 | entry.setKnowledgebaseLink(content.attribute(QStringLiteral("knowledgebasepage" ))); |
| 247 | entry.setNumberKnowledgebaseEntries(content.attribute(QStringLiteral("knowledgebaseentries" )).toInt()); |
| 248 | entry.setHomepage(content.detailpage()); |
| 249 | |
| 250 | entry.setPreviewUrl(url: content.smallPreviewPicture(QStringLiteral("1" )), type: Entry::PreviewSmall1); |
| 251 | entry.setPreviewUrl(url: content.smallPreviewPicture(QStringLiteral("2" )), type: Entry::PreviewSmall2); |
| 252 | entry.setPreviewUrl(url: content.smallPreviewPicture(QStringLiteral("3" )), type: Entry::PreviewSmall3); |
| 253 | |
| 254 | entry.setPreviewUrl(url: content.previewPicture(QStringLiteral("1" )), type: Entry::PreviewBig1); |
| 255 | entry.setPreviewUrl(url: content.previewPicture(QStringLiteral("2" )), type: Entry::PreviewBig2); |
| 256 | entry.setPreviewUrl(url: content.previewPicture(QStringLiteral("3" )), type: Entry::PreviewBig3); |
| 257 | |
| 258 | entry.setLicense(content.license()); |
| 259 | Author author; |
| 260 | author.setId(content.author()); |
| 261 | author.setName(content.author()); |
| 262 | author.setHomepage(content.attribute(QStringLiteral("profilepage" ))); |
| 263 | entry.setAuthor(author); |
| 264 | |
| 265 | entry.setSource(Entry::Online); |
| 266 | entry.setSummary(content.description()); |
| 267 | entry.setShortSummary(content.summary()); |
| 268 | entry.setChangelog(content.changelog()); |
| 269 | entry.setTags(content.tags()); |
| 270 | |
| 271 | const QList<Attica::DownloadDescription> descs = content.downloadUrlDescriptions(); |
| 272 | entry.d->mDownloadLinkInformationList.clear(); |
| 273 | entry.d->mDownloadLinkInformationList.reserve(asize: descs.size()); |
| 274 | for (const Attica::DownloadDescription &desc : descs) { |
| 275 | entry.d->mDownloadLinkInformationList.append(t: {.name = desc.name(), |
| 276 | .priceAmount = desc.priceAmount(), |
| 277 | .distributionType = desc.distributionType(), |
| 278 | .descriptionLink = desc.link(), |
| 279 | .id = desc.id(), |
| 280 | .isDownloadtypeLink = desc.type() == Attica::DownloadDescription::LinkDownload, |
| 281 | .size = desc.size(), |
| 282 | .tags = desc.tags(), |
| 283 | .version = desc.version()}); |
| 284 | } |
| 285 | |
| 286 | return entry; |
| 287 | } |
| 288 | |
| 289 | [[nodiscard]] KNSCore::SearchRequest AtticaRequester::request() const |
| 290 | { |
| 291 | return m_request; |
| 292 | } |
| 293 | |
| 294 | } // namespace KNSCore |
| 295 | |
| 296 | #include "moc_atticarequester_p.cpp" |
| 297 | |