From 5eb160a7929f6b3030658f73a941f21dcfa4b9ea Mon Sep 17 00:00:00 2001 From: Digital Artifex <7929434+DigitalArtifex@users.noreply.github.com> Date: Sun, 7 Jun 2026 02:13:35 -0400 Subject: [PATCH] Changed NewestPacks to new cache and networkAccessManager --- KomplexHubPlugin/newestpacksmodel.cpp | 63 +++++++++++++------ KomplexHubPlugin/newestpacksmodel.h | 32 +++++++++- .../paginators/newestpackspaginator.h | 52 ++++++++++++--- 3 files changed, 117 insertions(+), 30 deletions(-) diff --git a/KomplexHubPlugin/newestpacksmodel.cpp b/KomplexHubPlugin/newestpacksmodel.cpp index e7a2815..9500044 100644 --- a/KomplexHubPlugin/newestpacksmodel.cpp +++ b/KomplexHubPlugin/newestpacksmodel.cpp @@ -1,6 +1,7 @@ #include "newestpacksmodel.h" #include "common/coreservices.h" #include "common/logging.h" +#include "common/wallpapercache.h" NewestPacksModel::NewestPacksModel(QObject *parent) : QAbstractListModel{parent} { @@ -19,49 +20,44 @@ auto NewestPacksModel::data(const QModelIndex &index, int role) const -> QVarian return {}; } - WallpaperCache *dataPoint = m_wallpaperCache.at(index.row()); - - if(dataPoint == nullptr) - { - return {}; - } + WallpaperCache dataPoint = m_wallpaperCache.at(index.row()); QVariant data; switch(static_cast(role)) { case UriRole: - data = dataPoint->uri; + data = dataPoint.uri; break; case AuthorRole: - data = dataPoint->author; + data = dataPoint.author; break; case DescriptionRole: - data = dataPoint->description; + data = dataPoint.description; break; case NameRole: - data = dataPoint->name; + data = dataPoint.name; break; case ThumbnailRole: - data = dataPoint->thumbnail; + data = dataPoint.thumbnail; break; case CreatedDateRole: - data = dataPoint->createdDate; + data = dataPoint.createdDate; break; case AuthorIdRole: - data = dataPoint->authorId; + data = dataPoint.authorId; break; case PriceRole: - data = dataPoint->price; + data = dataPoint.price; break; case CurrencyRole: - data = dataPoint->currency; + data = dataPoint.currency; break; case DownloadCountRole: - data = dataPoint->downloadCount; + data = dataPoint.downloadCount; break; case TypeRole: - data = dataPoint->type; + data = dataPoint.type; break; } @@ -72,7 +68,8 @@ auto NewestPacksModel::index(int row, int column, const QModelIndex &parent) con { Q_UNUSED(parent) - return createIndex(row, column, m_wallpaperCache.at(row)); + WallpaperCache data = m_wallpaperCache.at(row); + return createIndex(row, column, &data); } auto NewestPacksModel::columnCount(const QModelIndex &) const -> int @@ -87,7 +84,7 @@ auto NewestPacksModel::parent(const QModelIndex &) const -> QModelIndex auto NewestPacksModel::setData(const QModelIndex &, const QVariant &, int) -> bool { - return true; + return false; } auto NewestPacksModel::state() const -> NewestPacksModel::State @@ -131,3 +128,31 @@ auto NewestPacksModel::setErrorString(const QString &errorString) -> void m_errorString = errorString; emit errorStringChanged(); } + +auto NewestPacksModel::resultsPerPage() const -> qint64 +{ + return m_resultsPerPage; +} + +auto NewestPacksModel::setResultsPerPage(qint64 resultsPerPage) -> void +{ + if (m_resultsPerPage == resultsPerPage) + return; + + m_resultsPerPage = resultsPerPage; + emit resultsPerPageChanged(); +} + +qint64 NewestPacksModel::page() const +{ + return m_page; +} + +void NewestPacksModel::setPage(qint64 page) +{ + if (m_page == page) + return; + + m_page = page; + emit pageChanged(); +} diff --git a/KomplexHubPlugin/newestpacksmodel.h b/KomplexHubPlugin/newestpacksmodel.h index 20201a8..04942be 100644 --- a/KomplexHubPlugin/newestpacksmodel.h +++ b/KomplexHubPlugin/newestpacksmodel.h @@ -1,3 +1,21 @@ +/* + * Komplex Wallpaper Engine + * Copyright (C) 2026 @DigitalArtifex + * https://digitalartifex.dev - https://github.com/DigitalArtifex + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + */ #ifndef NEWESTPACKSMODEL_H #define NEWESTPACKSMODEL_H #include @@ -19,7 +37,6 @@ #include #include "paginators/newestpackspaginator.h" -#include "common/wallpapercache.h" class KOMPLEX_EXPORT NewestPacksModel : public QAbstractListModel { @@ -81,12 +98,19 @@ public: auto errorString() const -> QString; auto setErrorString(const QString &errorString) -> void; + auto resultsPerPage() const -> qint64; + auto setResultsPerPage(qint64 resultsPerPage) -> void; + + qint64 page() const; + void setPage(qint64 page); + signals: auto installedWallpapersChanged() -> void; - auto stateChanged() -> void; - auto errorStringChanged() -> void; + auto resultsPerPageChanged() -> void; + + void pageChanged(); private: static inline const QHash m_dataRoles = @@ -147,6 +171,8 @@ private: Q_PROPERTY(State state READ state WRITE setState RESET resetState NOTIFY stateChanged FINAL) Q_PROPERTY(QString errorString READ errorString WRITE setErrorString NOTIFY errorStringChanged FINAL) + Q_PROPERTY(qint64 resultsPerPage READ resultsPerPage WRITE setResultsPerPage NOTIFY resultsPerPageChanged FINAL) + Q_PROPERTY(qint64 page READ page WRITE setPage NOTIFY pageChanged FINAL) }; Q_DECLARE_METATYPE(NewestPacksModel) diff --git a/KomplexHubPlugin/paginators/newestpackspaginator.h b/KomplexHubPlugin/paginators/newestpackspaginator.h index b89b084..25685ac 100644 --- a/KomplexHubPlugin/paginators/newestpackspaginator.h +++ b/KomplexHubPlugin/paginators/newestpackspaginator.h @@ -1,3 +1,21 @@ +/* + * Komplex Wallpaper Engine + * Copyright (C) 2026 @DigitalArtifex + * https://digitalartifex.dev - https://github.com/DigitalArtifex + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + */ #ifndef NEWESTPACKSPAGINATOR_H #define NEWESTPACKSPAGINATOR_H @@ -20,7 +38,13 @@ class KOMPLEX_EXPORT NewestPacksPaginator : public Paginator { public: - explicit NewestPacksPaginator(); + explicit NewestPacksPaginator() : Paginator() + { + SlidingCacheController *controller = this->controller(); + controller->setFetch( + std::bind(&NewestPacksPaginator::fetch, this, std::placeholders::_1, std::placeholders::_2) + ); + } protected: /** @@ -38,7 +62,7 @@ protected: ) const -> FetchResult { QUrl url( - "https://www.digitalartifex.dev/v2/packs/newest/" + QString::fromUtf8("%1/v2/packs/newest/").arg(KOMPLEX_API_HOST) ); QNetworkRequest request(url); @@ -73,7 +97,7 @@ protected: QJsonObject rootObject = document.object(); const QJsonArray resultsArray = rootObject.value(QString::fromUtf8("results")).toArray(); - QList fetchedResults; + QList fetchedResults; for(const auto result : resultsArray) { @@ -85,7 +109,6 @@ protected: QJsonObject resultObject = result.toObject(); fetchedResults.append( - new WallpaperCache { resultObject.value(QString::fromUtf8("uri")).toString(), resultObject.value(QString::fromUtf8("name")).toString(), @@ -95,11 +118,15 @@ protected: resultObject.value(QString::fromUtf8("thumbnail")).toString(), QDateTime::fromString( resultObject.value(QString::fromUtf8("createdDate")).toString() - ), + ), resultObject.value(QString::fromUtf8("price")).toDouble(), resultObject.value(QString::fromUtf8("currency")).toString(), - static_cast(resultObject.value(QString::fromUtf8("downloadCount")).toInteger()), - static_cast(resultObject.value(QString::fromUtf8("description")).toInt()), + static_cast( + resultObject.value(QString::fromUtf8("downloadCount") + ).toInteger()), + static_cast( + resultObject.value(QString::fromUtf8("description") + ).toInt()), } ); } @@ -121,7 +148,16 @@ protected: auto getNetworkReply(const QNetworkRequest &request) const -> QNetworkReply* { QEventLoop loop; - QNetworkAccessManager *manager = CoreServices::networkAccessManager(); + QWeakPointer managerReference = CoreServices::networkAccessManager(); + + QSharedPointer manager = managerReference.toStrongRef(); + + LOG_ERROR_X(!manager, + "NewestPacksPaginator::getNetworkReply", + "Network manager reference has already been deleted", + nullptr + ); + QNetworkReply *reply = manager->post(request, nullptr); QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit); QObject::connect(reply, &QNetworkReply::errorOccurred, &loop,