Add missing bits to the API and get rid of the cache

Cache added a lot of unecessarity complexity, in particular. It made
no sense to add it to a class which handles network responses.
This commit is contained in:
Luca Beltrame 2020-08-19 16:28:38 +02:00
parent 02bb6ffcaa
commit 32fc63118d
Signed by: einar
GPG key ID: 4707F46E9EC72DEC
2 changed files with 72 additions and 58 deletions

View file

@ -37,6 +37,8 @@ DanbooruServiceBase::DanbooruServiceBase(QUrl boardUrl, QObject* parent):
m_maxRating(Danbooru::Safe), m_maxRating(Danbooru::Safe),
m_maxPosts(10), m_maxPosts(10),
m_currentPage(1), m_currentPage(1),
m_minimumWidth(-1),
m_minimumHeight(-1),
m_tags(QStringList()), m_tags(QStringList()),
m_postsToFetch(0) m_postsToFetch(0)
{ {
@ -86,6 +88,16 @@ int DanbooruServiceBase::maxPosts() const
return m_maxPosts; return m_maxPosts;
} }
int DanbooruServiceBase::minimumHeight() const
{
return m_minimumHeight;
}
int DanbooruServiceBase::minimumWidth() const
{
return m_minimumWidth;
}
void DanbooruServiceBase::nextPostPage() void DanbooruServiceBase::nextPostPage()
{ {
m_currentPage++; m_currentPage++;
@ -106,6 +118,9 @@ QStringList DanbooruServiceBase::postTags() const
void DanbooruServiceBase::reset() void DanbooruServiceBase::reset()
{ {
m_currentPage = 1; m_currentPage = 1;
m_minimumHeight = -1;
m_minimumWidth = -1;
m_maxRating = Danbooru::Safe;
m_tags = QStringList(); m_tags = QStringList();
} }
@ -132,7 +147,7 @@ void DanbooruServiceBase::setBlacklist(const QStringList &blacklist)
m_blacklist.clear(); m_blacklist.clear();
for (auto element : blacklist) { for (const auto &element : blacklist) {
m_blacklist.insert(element); m_blacklist.insert(element);
} }
@ -148,10 +163,7 @@ void DanbooruServiceBase::setCurrentPage(int page)
m_currentPage = page; m_currentPage = page;
} }
void DanbooruServiceBase::setImageCache(KImageCache *cache)
{
m_cache = cache;
}
void DanbooruServiceBase::setMaximumAllowedRating(Danbooru::Rating rating) void DanbooruServiceBase::setMaximumAllowedRating(Danbooru::Rating rating)
{ {
@ -203,7 +215,7 @@ void DanbooruServiceBase::setUserName(const QString &username)
} }
void DanbooruServiceBase::processPixmap(KJob* job) { void DanbooruServiceBase::processPixmap(KJob* job) {
if (job->error()) if (job->error())
{ {
Q_EMIT(downloadError(job->errorString())); Q_EMIT(downloadError(job->errorString()));
@ -221,7 +233,7 @@ void DanbooruServiceBase::processPixmap(KJob* job) {
QByteArray data = jobResult->data(); QByteArray data = jobResult->data();
Danbooru::DanbooruPost* post = job->property("post").value<DanbooruPost*>(); Danbooru::DanbooruPost* post = job->property("post").value<DanbooruPost*>();
auto pix = job->property("pixmap").value<QPixmap>(); auto pix = job->property("pixmap").value<QPixmap>();
if (!pix.loadFromData(jobResult->data())) if (!pix.loadFromData(jobResult->data()))
{ {
Q_EMIT(downloadError(QString("Pixmap data could not be loaded"))); Q_EMIT(downloadError(QString("Pixmap data could not be loaded")));
@ -230,11 +242,6 @@ void DanbooruServiceBase::processPixmap(KJob* job) {
post->setPixmap(pix); post->setPixmap(pix);
if (m_cache)
{
//qCDebug(LIBDANBOORU) << "Inserting item in cache";
m_cache->insertPixmap(post->thumbnailUrl().url(), pix);
}
m_postsToFetch--; // One less post to do m_postsToFetch--; // One less post to do
@ -246,7 +253,7 @@ void DanbooruServiceBase::processPixmap(KJob* job) {
qCDebug(LIBDANBOORU) << "Post download finished"; qCDebug(LIBDANBOORU) << "Post download finished";
Q_EMIT(postDownloadFinished()); Q_EMIT(postDownloadFinished());
} }
} }
void DanbooruServiceBase::processPostList(KJob *job) void DanbooruServiceBase::processPostList(KJob *job)
@ -267,7 +274,7 @@ void DanbooruServiceBase::processPostList(KJob *job)
} }
QByteArray data = jobResult->data(); QByteArray data = jobResult->data();
bool ok; bool ok;
bool is_pool = job->property("is_pool").toBool(); bool is_pool = job->property("is_pool").toBool();
@ -277,10 +284,10 @@ void DanbooruServiceBase::processPostList(KJob *job)
// Special cases for pools // Special cases for pools
QVariantMap postMap = parseResult(data, apiType(), Danbooru::Pool, &ok).at(0); QVariantMap postMap = parseResult(data, apiType(), Danbooru::Pool, &ok).at(0);
auto postData = postMap.value("raw_post_data").toList(); auto postData = postMap.value("raw_post_data").toList();
for (const auto post: postData) { for (const auto &post: postData) {
postList.append(extractPostData(post, apiType())); postList.append(extractPostData(post, apiType()));
} }
} else { } else {
postList = parseResult(data, apiType(), Danbooru::Post, &ok); postList = parseResult(data, apiType(), Danbooru::Post, &ok);
} }
@ -307,10 +314,10 @@ void DanbooruServiceBase::processPostList(KJob *job)
postList = postList.mid(0, m_maxPosts); postList = postList.mid(0, m_maxPosts);
} }
for (const QVariantMap element : qAsConst(postList)) { for (const QVariantMap &element : qAsConst(postList)) {
DanbooruPost *post = new DanbooruPost(element); DanbooruPost *post = new DanbooruPost(element);
// Remove unwanted posts // Remove unwanted posts
if (isPostBlacklisted(post, m_blacklist, m_maxRating)) { if (isPostBlacklisted(post, m_blacklist, m_maxRating)) {
@ -322,38 +329,38 @@ void DanbooruServiceBase::processPostList(KJob *job)
QPixmap pix; QPixmap pix;
qCDebug(LIBDANBOORU) << "About to download images"; qCDebug(LIBDANBOORU) << "About to download images";
qCDebug(LIBDANBOORU) << "Downloading image" << post->thumbnailUrl();
StoredTransferJob *pixmapJob = KIO::storedGet(post->thumbnailUrl(),
KIO::NoReload, KIO::HideProgressInfo);
if (m_cache && m_cache->findPixmap(post->thumbnailUrl().url(), &pix)) { // We don't want to overload the servers, so set some rational
// priority
post->setPixmap(pix);
Q_EMIT(postDownloaded(post));
m_postsToFetch--;
if (m_postsToFetch == 0) { KIO::Scheduler::setJobPriority(static_cast<KIO::SimpleJob *>(pixmapJob), 1);
qCDebug(LIBDANBOORU) << "Post download finished";
Q_EMIT(postDownloadFinished());
}
} else { QVariant variant;
variant.setValue(post);
pixmapJob->setProperty("post", variant);
pixmapJob->setProperty("pix", pix);
qCDebug(LIBDANBOORU) << "Downloading image" << post->thumbnailUrl(); connect(pixmapJob, &StoredTransferJob::result, this, &DanbooruServiceBase::processPixmap);
StoredTransferJob *pixmapJob = KIO::storedGet(post->thumbnailUrl(),
KIO::NoReload, KIO::HideProgressInfo);
// We don't want to overload the servers, so set some rational }
// priority
KIO::Scheduler::setJobPriority(static_cast<KIO::SimpleJob *>(pixmapJob), 1); }
QVariant variant; void DanbooruServiceBase::setMinimumHeight(int height) {
variant.setValue(post);
pixmapJob->setProperty("post", variant);
pixmapJob->setProperty("pix", pix);
connect(pixmapJob, &StoredTransferJob::result, this, &DanbooruServiceBase::processPixmap); if (height > 0) {
m_minimumHeight = height;
}
} }
void DanbooruServiceBase::setMinimumWidth(int width) {
if (width > 0) {
m_minimumWidth = width;
} }
} }

View file

@ -53,12 +53,12 @@ private:
// URI functions // URI functions
virtual const QLatin1String postUri() const = 0; virtual const QUrl postUri() const = 0;
virtual const QLatin1String poolUri() const = 0; virtual const QUrl poolUri() const = 0;
virtual const QLatin1String artistUri() const = 0; virtual const QUrl artistUri() const = 0;
virtual const QLatin1String tagUri() const = 0; virtual const QUrl tagUri() const = 0;
virtual const QLatin1String poolDataUri() const = 0; virtual const QUrl poolDataUri() const = 0;
virtual const QLatin1String relatedTagUri() const = 0; virtual const QUrl relatedTagUri() const = 0;
protected: protected:
@ -71,12 +71,12 @@ protected:
Danbooru::Ratings m_maxRating; Danbooru::Ratings m_maxRating;
int m_maxPosts; int m_maxPosts;
int m_currentPage; int m_currentPage;
int m_minimumWidth;
int m_minimumHeight;
QStringList m_tags; QStringList m_tags;
unsigned int m_postsToFetch; // To tell when to quit unsigned int m_postsToFetch; // To tell when to quit
KImageCache *m_cache; // Pixmap cache
public: public:
explicit DanbooruServiceBase(QUrl boardUrl = QUrl(), QObject* parent=nullptr); explicit DanbooruServiceBase(QUrl boardUrl = QUrl(), QObject* parent=nullptr);
@ -97,7 +97,7 @@ public:
const QStringList allowedRatings() const; const QStringList allowedRatings() const;
/** /**
* @brief Return the api type of the searvice. * @brief Return the api type of the service.
**/ **/
virtual Danbooru::ApiType apiType() const; virtual Danbooru::ApiType apiType() const;
@ -132,6 +132,18 @@ public:
**/ **/
int maxPosts() const; int maxPosts() const;
/**
* @return The minimum required height for a post to be considered
*
**/
int minimumHeight() const;
/**
* @return The minimum required width for a post to be considered
*
**/
int minimumWidth() const;
/** /**
* @brief Move to the next post page and retrieve new posts. * @brief Move to the next post page and retrieve new posts.
* *
@ -188,14 +200,6 @@ public:
**/ **/
void setCurrentPage(int page); void setCurrentPage(int page);
/**
* @brief Set an image cache to use.
*
* @param cache A pointer to an instance of KImageCache.
*/
void setImageCache(KImageCache *cache);
/** /**
* @brief Set the maximum allowed rating for the board. * @brief Set the maximum allowed rating for the board.
* *
@ -210,6 +214,9 @@ public:
**/ **/
void setMaxPosts(int number); void setMaxPosts(int number);
void setMinimumHeight(int height);
void setMinimumWidth(int width);
/** /**
* @brief Set the password used for login. * @brief Set the password used for login.
* *
@ -353,7 +360,7 @@ Q_SIGNALS:
* downloaded. * downloaded.
**/ **/
void tagDownloaded(Danbooru::DanbooruTag *tag); void tagDownloaded(Danbooru::DanbooruTag *tag);
public Q_SLOTS: public Q_SLOTS:
void processPixmap(KJob* job); void processPixmap(KJob* job);
void processPostList(KJob* job); void processPostList(KJob* job);