From 32fc63118dad4df8d68d79691d32f48eda0ab132 Mon Sep 17 00:00:00 2001 From: Luca Beltrame Date: Wed, 19 Aug 2020 16:28:38 +0200 Subject: [PATCH] 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. --- src/libdanbooru/servicebase.cpp | 87 ++++++++++++++++++--------------- src/libdanbooru/servicebase.h | 43 +++++++++------- 2 files changed, 72 insertions(+), 58 deletions(-) diff --git a/src/libdanbooru/servicebase.cpp b/src/libdanbooru/servicebase.cpp index c98e1d7..111e1b8 100644 --- a/src/libdanbooru/servicebase.cpp +++ b/src/libdanbooru/servicebase.cpp @@ -37,6 +37,8 @@ DanbooruServiceBase::DanbooruServiceBase(QUrl boardUrl, QObject* parent): m_maxRating(Danbooru::Safe), m_maxPosts(10), m_currentPage(1), + m_minimumWidth(-1), + m_minimumHeight(-1), m_tags(QStringList()), m_postsToFetch(0) { @@ -86,6 +88,16 @@ int DanbooruServiceBase::maxPosts() const return m_maxPosts; } +int DanbooruServiceBase::minimumHeight() const +{ + return m_minimumHeight; +} + +int DanbooruServiceBase::minimumWidth() const +{ + return m_minimumWidth; +} + void DanbooruServiceBase::nextPostPage() { m_currentPage++; @@ -106,6 +118,9 @@ QStringList DanbooruServiceBase::postTags() const void DanbooruServiceBase::reset() { m_currentPage = 1; + m_minimumHeight = -1; + m_minimumWidth = -1; + m_maxRating = Danbooru::Safe; m_tags = QStringList(); } @@ -132,7 +147,7 @@ void DanbooruServiceBase::setBlacklist(const QStringList &blacklist) m_blacklist.clear(); - for (auto element : blacklist) { + for (const auto &element : blacklist) { m_blacklist.insert(element); } @@ -148,10 +163,7 @@ void DanbooruServiceBase::setCurrentPage(int page) m_currentPage = page; } -void DanbooruServiceBase::setImageCache(KImageCache *cache) -{ - m_cache = cache; -} + void DanbooruServiceBase::setMaximumAllowedRating(Danbooru::Rating rating) { @@ -203,7 +215,7 @@ void DanbooruServiceBase::setUserName(const QString &username) } void DanbooruServiceBase::processPixmap(KJob* job) { - + if (job->error()) { Q_EMIT(downloadError(job->errorString())); @@ -221,7 +233,7 @@ void DanbooruServiceBase::processPixmap(KJob* job) { QByteArray data = jobResult->data(); Danbooru::DanbooruPost* post = job->property("post").value(); auto pix = job->property("pixmap").value(); - + if (!pix.loadFromData(jobResult->data())) { Q_EMIT(downloadError(QString("Pixmap data could not be loaded"))); @@ -230,11 +242,6 @@ void DanbooruServiceBase::processPixmap(KJob* job) { 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 @@ -246,7 +253,7 @@ void DanbooruServiceBase::processPixmap(KJob* job) { qCDebug(LIBDANBOORU) << "Post download finished"; Q_EMIT(postDownloadFinished()); } - + } void DanbooruServiceBase::processPostList(KJob *job) @@ -267,7 +274,7 @@ void DanbooruServiceBase::processPostList(KJob *job) } QByteArray data = jobResult->data(); - + bool ok; bool is_pool = job->property("is_pool").toBool(); @@ -277,10 +284,10 @@ void DanbooruServiceBase::processPostList(KJob *job) // Special cases for pools QVariantMap postMap = parseResult(data, apiType(), Danbooru::Pool, &ok).at(0); auto postData = postMap.value("raw_post_data").toList(); - for (const auto post: postData) { + for (const auto &post: postData) { postList.append(extractPostData(post, apiType())); } - + } else { postList = parseResult(data, apiType(), Danbooru::Post, &ok); } @@ -307,10 +314,10 @@ void DanbooruServiceBase::processPostList(KJob *job) postList = postList.mid(0, m_maxPosts); } - for (const QVariantMap element : qAsConst(postList)) { + for (const QVariantMap &element : qAsConst(postList)) { DanbooruPost *post = new DanbooruPost(element); - + // Remove unwanted posts if (isPostBlacklisted(post, m_blacklist, m_maxRating)) { @@ -322,38 +329,38 @@ void DanbooruServiceBase::processPostList(KJob *job) QPixmap pix; 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)) { - - post->setPixmap(pix); - Q_EMIT(postDownloaded(post)); - m_postsToFetch--; + // We don't want to overload the servers, so set some rational + // priority - if (m_postsToFetch == 0) { - qCDebug(LIBDANBOORU) << "Post download finished"; - Q_EMIT(postDownloadFinished()); - } + KIO::Scheduler::setJobPriority(static_cast(pixmapJob), 1); - } else { + QVariant variant; + variant.setValue(post); + pixmapJob->setProperty("post", variant); + pixmapJob->setProperty("pix", pix); - qCDebug(LIBDANBOORU) << "Downloading image" << post->thumbnailUrl(); - StoredTransferJob *pixmapJob = KIO::storedGet(post->thumbnailUrl(), - KIO::NoReload, KIO::HideProgressInfo); + connect(pixmapJob, &StoredTransferJob::result, this, &DanbooruServiceBase::processPixmap); - // We don't want to overload the servers, so set some rational - // priority + } - KIO::Scheduler::setJobPriority(static_cast(pixmapJob), 1); +} - QVariant variant; - variant.setValue(post); - pixmapJob->setProperty("post", variant); - pixmapJob->setProperty("pix", pix); +void DanbooruServiceBase::setMinimumHeight(int height) { - connect(pixmapJob, &StoredTransferJob::result, this, &DanbooruServiceBase::processPixmap); + if (height > 0) { + m_minimumHeight = height; + } - } +} +void DanbooruServiceBase::setMinimumWidth(int width) { + + if (width > 0) { + m_minimumWidth = width; } } diff --git a/src/libdanbooru/servicebase.h b/src/libdanbooru/servicebase.h index a70bcb2..60adca5 100644 --- a/src/libdanbooru/servicebase.h +++ b/src/libdanbooru/servicebase.h @@ -53,12 +53,12 @@ private: // URI functions - virtual const QLatin1String postUri() const = 0; - virtual const QLatin1String poolUri() const = 0; - virtual const QLatin1String artistUri() const = 0; - virtual const QLatin1String tagUri() const = 0; - virtual const QLatin1String poolDataUri() const = 0; - virtual const QLatin1String relatedTagUri() const = 0; + virtual const QUrl postUri() const = 0; + virtual const QUrl poolUri() const = 0; + virtual const QUrl artistUri() const = 0; + virtual const QUrl tagUri() const = 0; + virtual const QUrl poolDataUri() const = 0; + virtual const QUrl relatedTagUri() const = 0; protected: @@ -71,12 +71,12 @@ protected: Danbooru::Ratings m_maxRating; int m_maxPosts; int m_currentPage; + int m_minimumWidth; + int m_minimumHeight; QStringList m_tags; unsigned int m_postsToFetch; // To tell when to quit - KImageCache *m_cache; // Pixmap cache - public: explicit DanbooruServiceBase(QUrl boardUrl = QUrl(), QObject* parent=nullptr); @@ -97,7 +97,7 @@ public: 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; @@ -132,6 +132,18 @@ public: **/ 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. * @@ -188,14 +200,6 @@ public: **/ 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. * @@ -210,6 +214,9 @@ public: **/ void setMaxPosts(int number); + void setMinimumHeight(int height); + void setMinimumWidth(int width); + /** * @brief Set the password used for login. * @@ -353,7 +360,7 @@ Q_SIGNALS: * downloaded. **/ void tagDownloaded(Danbooru::DanbooruTag *tag); - + public Q_SLOTS: void processPixmap(KJob* job); void processPostList(KJob* job);