Changed NewestPacks to new cache and networkAccessManager
This commit is contained in:
@@ -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<DataRole>(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();
|
||||
}
|
||||
|
||||
@@ -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
|
||||
#define NEWESTPACKSMODEL_H
|
||||
#include <QObject>
|
||||
@@ -19,7 +37,6 @@
|
||||
#include <QEventLoop>
|
||||
|
||||
#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<int, QByteArray> 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)
|
||||
|
||||
|
||||
@@ -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
|
||||
#define NEWESTPACKSPAGINATOR_H
|
||||
|
||||
@@ -20,7 +38,13 @@ class KOMPLEX_EXPORT NewestPacksPaginator : public Paginator<WallpaperCache>
|
||||
{
|
||||
|
||||
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:
|
||||
/**
|
||||
@@ -38,7 +62,7 @@ protected:
|
||||
) const -> FetchResult<WallpaperCache>
|
||||
{
|
||||
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<WallpaperCache*> fetchedResults;
|
||||
QList<WallpaperCache> 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(),
|
||||
@@ -98,8 +121,12 @@ protected:
|
||||
),
|
||||
resultObject.value(QString::fromUtf8("price")).toDouble(),
|
||||
resultObject.value(QString::fromUtf8("currency")).toString(),
|
||||
static_cast<quint64>(resultObject.value(QString::fromUtf8("downloadCount")).toInteger()),
|
||||
static_cast<qint8>(resultObject.value(QString::fromUtf8("description")).toInt()),
|
||||
static_cast<quint64>(
|
||||
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*
|
||||
{
|
||||
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);
|
||||
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
|
||||
QObject::connect(reply, &QNetworkReply::errorOccurred, &loop,
|
||||
|
||||
Reference in New Issue
Block a user