Changed NewestPacks to new cache and networkAccessManager

This commit is contained in:
Digital Artifex
2026-06-07 02:13:35 -04:00
parent 423afc0b50
commit 957d3554b3
3 changed files with 117 additions and 30 deletions
+44 -19
View File
@@ -1,6 +1,7 @@
#include "newestpacksmodel.h" #include "newestpacksmodel.h"
#include "common/coreservices.h" #include "common/coreservices.h"
#include "common/logging.h" #include "common/logging.h"
#include "common/wallpapercache.h"
NewestPacksModel::NewestPacksModel(QObject *parent) : QAbstractListModel{parent} NewestPacksModel::NewestPacksModel(QObject *parent) : QAbstractListModel{parent}
{ {
@@ -19,49 +20,44 @@ auto NewestPacksModel::data(const QModelIndex &index, int role) const -> QVarian
return {}; return {};
} }
WallpaperCache *dataPoint = m_wallpaperCache.at(index.row()); WallpaperCache dataPoint = m_wallpaperCache.at(index.row());
if(dataPoint == nullptr)
{
return {};
}
QVariant data; QVariant data;
switch(static_cast<DataRole>(role)) switch(static_cast<DataRole>(role))
{ {
case UriRole: case UriRole:
data = dataPoint->uri; data = dataPoint.uri;
break; break;
case AuthorRole: case AuthorRole:
data = dataPoint->author; data = dataPoint.author;
break; break;
case DescriptionRole: case DescriptionRole:
data = dataPoint->description; data = dataPoint.description;
break; break;
case NameRole: case NameRole:
data = dataPoint->name; data = dataPoint.name;
break; break;
case ThumbnailRole: case ThumbnailRole:
data = dataPoint->thumbnail; data = dataPoint.thumbnail;
break; break;
case CreatedDateRole: case CreatedDateRole:
data = dataPoint->createdDate; data = dataPoint.createdDate;
break; break;
case AuthorIdRole: case AuthorIdRole:
data = dataPoint->authorId; data = dataPoint.authorId;
break; break;
case PriceRole: case PriceRole:
data = dataPoint->price; data = dataPoint.price;
break; break;
case CurrencyRole: case CurrencyRole:
data = dataPoint->currency; data = dataPoint.currency;
break; break;
case DownloadCountRole: case DownloadCountRole:
data = dataPoint->downloadCount; data = dataPoint.downloadCount;
break; break;
case TypeRole: case TypeRole:
data = dataPoint->type; data = dataPoint.type;
break; break;
} }
@@ -72,7 +68,8 @@ auto NewestPacksModel::index(int row, int column, const QModelIndex &parent) con
{ {
Q_UNUSED(parent) 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 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 auto NewestPacksModel::setData(const QModelIndex &, const QVariant &, int) -> bool
{ {
return true; return false;
} }
auto NewestPacksModel::state() const -> NewestPacksModel::State auto NewestPacksModel::state() const -> NewestPacksModel::State
@@ -131,3 +128,31 @@ auto NewestPacksModel::setErrorString(const QString &errorString) -> void
m_errorString = errorString; m_errorString = errorString;
emit errorStringChanged(); 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();
}
+29 -3
View File
@@ -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 <https://www.gnu.org/licenses/>
*/
#ifndef NEWESTPACKSMODEL_H #ifndef NEWESTPACKSMODEL_H
#define NEWESTPACKSMODEL_H #define NEWESTPACKSMODEL_H
#include <QObject> #include <QObject>
@@ -19,7 +37,6 @@
#include <QEventLoop> #include <QEventLoop>
#include "paginators/newestpackspaginator.h" #include "paginators/newestpackspaginator.h"
#include "common/wallpapercache.h"
class KOMPLEX_EXPORT NewestPacksModel : public QAbstractListModel class KOMPLEX_EXPORT NewestPacksModel : public QAbstractListModel
{ {
@@ -81,12 +98,19 @@ public:
auto errorString() const -> QString; auto errorString() const -> QString;
auto setErrorString(const QString &errorString) -> void; auto setErrorString(const QString &errorString) -> void;
auto resultsPerPage() const -> qint64;
auto setResultsPerPage(qint64 resultsPerPage) -> void;
qint64 page() const;
void setPage(qint64 page);
signals: signals:
auto installedWallpapersChanged() -> void; auto installedWallpapersChanged() -> void;
auto stateChanged() -> void; auto stateChanged() -> void;
auto errorStringChanged() -> void; auto errorStringChanged() -> void;
auto resultsPerPageChanged() -> void;
void pageChanged();
private: private:
static inline const QHash<int, QByteArray> m_dataRoles = static inline const QHash<int, QByteArray> m_dataRoles =
@@ -147,6 +171,8 @@ private:
Q_PROPERTY(State state READ state WRITE setState RESET resetState NOTIFY stateChanged FINAL) 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(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) Q_DECLARE_METATYPE(NewestPacksModel)
@@ -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 <https://www.gnu.org/licenses/>
*/
#ifndef NEWESTPACKSPAGINATOR_H #ifndef NEWESTPACKSPAGINATOR_H
#define NEWESTPACKSPAGINATOR_H #define NEWESTPACKSPAGINATOR_H
@@ -20,7 +38,13 @@ class KOMPLEX_EXPORT NewestPacksPaginator : public Paginator<WallpaperCache>
{ {
public: public:
explicit NewestPacksPaginator(); explicit NewestPacksPaginator() : Paginator<WallpaperCache>()
{
SlidingCacheController<WallpaperCache> *controller = this->controller();
controller->setFetch(
std::bind(&NewestPacksPaginator::fetch, this, std::placeholders::_1, std::placeholders::_2)
);
}
protected: protected:
/** /**
@@ -38,7 +62,7 @@ protected:
) const -> FetchResult<WallpaperCache> ) const -> FetchResult<WallpaperCache>
{ {
QUrl url( QUrl url(
"https://www.digitalartifex.dev/v2/packs/newest/" QString::fromUtf8("%1/v2/packs/newest/").arg(KOMPLEX_API_HOST)
); );
QNetworkRequest request(url); QNetworkRequest request(url);
@@ -73,7 +97,7 @@ protected:
QJsonObject rootObject = document.object(); QJsonObject rootObject = document.object();
const QJsonArray resultsArray = rootObject.value(QString::fromUtf8("results")).toArray(); const QJsonArray resultsArray = rootObject.value(QString::fromUtf8("results")).toArray();
QList<WallpaperCache*> fetchedResults; QList<WallpaperCache> fetchedResults;
for(const auto result : resultsArray) for(const auto result : resultsArray)
{ {
@@ -85,7 +109,6 @@ protected:
QJsonObject resultObject = result.toObject(); QJsonObject resultObject = result.toObject();
fetchedResults.append( fetchedResults.append(
new WallpaperCache
{ {
resultObject.value(QString::fromUtf8("uri")).toString(), resultObject.value(QString::fromUtf8("uri")).toString(),
resultObject.value(QString::fromUtf8("name")).toString(), resultObject.value(QString::fromUtf8("name")).toString(),
@@ -95,11 +118,15 @@ protected:
resultObject.value(QString::fromUtf8("thumbnail")).toString(), resultObject.value(QString::fromUtf8("thumbnail")).toString(),
QDateTime::fromString( QDateTime::fromString(
resultObject.value(QString::fromUtf8("createdDate")).toString() resultObject.value(QString::fromUtf8("createdDate")).toString()
), ),
resultObject.value(QString::fromUtf8("price")).toDouble(), resultObject.value(QString::fromUtf8("price")).toDouble(),
resultObject.value(QString::fromUtf8("currency")).toString(), resultObject.value(QString::fromUtf8("currency")).toString(),
static_cast<quint64>(resultObject.value(QString::fromUtf8("downloadCount")).toInteger()), static_cast<quint64>(
static_cast<qint8>(resultObject.value(QString::fromUtf8("description")).toInt()), resultObject.value(QString::fromUtf8("downloadCount")
).toInteger()),
static_cast<qint8>(
resultObject.value(QString::fromUtf8("description")
).toInt()),
} }
); );
} }
@@ -121,7 +148,16 @@ protected:
auto getNetworkReply(const QNetworkRequest &request) const -> QNetworkReply* auto getNetworkReply(const QNetworkRequest &request) const -> QNetworkReply*
{ {
QEventLoop loop; QEventLoop loop;
QNetworkAccessManager *manager = CoreServices::networkAccessManager(); QWeakPointer<QNetworkAccessManager> managerReference = CoreServices::networkAccessManager();
QSharedPointer<QNetworkAccessManager> manager = managerReference.toStrongRef();
LOG_ERROR_X(!manager,
"NewestPacksPaginator::getNetworkReply",
"Network manager reference has already been deleted",
nullptr
);
QNetworkReply *reply = manager->post(request, nullptr); QNetworkReply *reply = manager->post(request, nullptr);
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit); QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
QObject::connect(reply, &QNetworkReply::errorOccurred, &loop, QObject::connect(reply, &QNetworkReply::errorOccurred, &loop,