Use the base class implementation in the Moebooru API

This commit is contained in:
Luca Beltrame 2018-08-25 18:53:36 +02:00
parent 98c3faa5be
commit 15446c74ac
Signed by: einar
GPG key ID: 8DF631FD021DB0C5
2 changed files with 3 additions and 150 deletions

View file

@ -91,10 +91,7 @@ void MoebooruService::getPostList() {
KIO::StoredTransferJob *job = KIO::storedGet(danbooruUrl, KIO::NoReload,
KIO::HideProgressInfo);
// This job can use JSON data
job->setProperty("needsXML", false);
connect(job, &KIO::StoredTransferJob::result, this, &MoebooruService::processPostList);
connect(job, &KIO::StoredTransferJob::result, this, &DanbooruServiceBase::processPostList);
}
@ -176,7 +173,7 @@ void MoebooruService::getPool(int poolId, int page)
job->setProperty("is_pool", true);
connect(job, &KIO::StoredTransferJob::result, this, &MoebooruService::processPostList);
connect(job, &KIO::StoredTransferJob::result, this, &DanbooruServiceBase::processPostList);
}
@ -236,7 +233,7 @@ void MoebooruService::getRelatedTags(const QStringList &tags,
KIO::HideProgressInfo
);
connect(job, &StoredTransferJob::result, [this](KJob * job) {
connect(job, &StoredTransferJob::result, this, [this](KJob * job) {
if (job->error()) {
Q_EMIT(downloadError(job->errorString()));
@ -322,149 +319,6 @@ void MoebooruService::processTagList(KJob *job)
}
}
void MoebooruService::processPostList(KJob *job)
{
qCDebug(LIBDANBOORU) << "Got post data OK";
if (job->error()) {
Q_EMIT(downloadError(job->errorString()));
}
StoredTransferJob *jobResult = qobject_cast<StoredTransferJob *>(job);
if (jobResult == 0) {
Q_EMIT(downloadError(QString("Internal error")));
return;
}
QByteArray data = jobResult->data();
bool ok;
bool is_pool = job->property("is_pool").toBool();
QList<QVariantMap> postList;
if (is_pool) {
// Special cases for pools
QVariantMap postMap = parseResult(data, Danbooru::MoeBooru, Danbooru::Pool, &ok).at(0);
auto postData = postMap.value("raw_post_data").toList();
for (const auto post: postData) {
postList.append(extractPostData(post, apiType()));
}
} else {
postList = parseResult(data, Danbooru::MoeBooru, Danbooru::Post, &ok);
}
if (!ok) {
Q_EMIT(downloadError(QString("Unable to decode data")));
return;
}
// How many posts do we have to fetch?
if (postList.isEmpty()) {
qCDebug(LIBDANBOORU) << "No posts found";
Q_EMIT(postDownloadFinished());
return;
}
m_postsToFetch = postList.length();
qCDebug(LIBDANBOORU) << "Found " << m_postsToFetch << "posts to fetch" << "with limit" << m_maxPosts;
// This is mostly needed for pools
if (postList.length() > m_maxPosts) {
m_postsToFetch = m_maxPosts;
postList = postList.mid(0, m_maxPosts);
}
for (const QVariantMap element : qAsConst(postList)) {
DanbooruPost *post = new DanbooruPost(element);
// Remove unwanted posts
if (isPostBlacklisted(post, m_blacklist, m_maxRating)) {
m_postsToFetch--;
delete post;
continue;
}
QPixmap pix;
qCDebug(LIBDANBOORU) << "About to download images";
if (m_cache && m_cache->findPixmap(post->thumbnailUrl().url(), &pix)) {
qDebug() << "reimplement";
post->setPixmap(pix);
Q_EMIT(postDownloaded(post));
m_postsToFetch--;
if (m_postsToFetch == 0) {
qCDebug(LIBDANBOORU) << "Post download finished";
Q_EMIT(postDownloadFinished());
}
} else {
qCDebug(LIBDANBOORU) << "Downloading image" << post->thumbnailUrl();
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 *>(job), 1);
QVariant variant;
variant.setValue(post);
connect(pixmapJob, &StoredTransferJob::result, [post, this, pix](KJob * job) mutable {
if (job->error())
{
Q_EMIT(downloadError(job->errorString()));
return;
}
StoredTransferJob *jobResult = qobject_cast<StoredTransferJob *>(job);
if (!pix.loadFromData(jobResult->data()))
{
Q_EMIT(downloadError(QString("Pixmap data could not be loaded")));
return;
}
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
qCDebug(LIBDANBOORU) << "Current posts remaining: " << m_postsToFetch;
Q_EMIT(postDownloaded(post));
if (m_postsToFetch == 0)
{
qCDebug(LIBDANBOORU) << "Post download finished";
Q_EMIT(postDownloadFinished());
}
});
}
}
}
void MoebooruService::downloadAllTags(KJob* job) {
Q_UNUSED(job);
}

View file

@ -53,7 +53,6 @@ public:
DanbooruTag::TagType tagType = DanbooruTag::General) override;
private Q_SLOTS:
void processPostList(KJob *job);
void processTagList(KJob *job);
void downloadAllTags(KJob *job);