[libdanbooru/danbooruservice] Move thumbnail downloading to a lambda
slot This greatly reduces complexity and prevents the ugly passing of properties along the jobs.
This commit is contained in:
parent
66cd4f46d1
commit
3f760a7690
2 changed files with 48 additions and 51 deletions
|
@ -61,7 +61,7 @@ DanbooruService::DanbooruService(QUrl& boardUrl, QString username,
|
||||||
m_username(username),
|
m_username(username),
|
||||||
m_password(password),
|
m_password(password),
|
||||||
m_maxRating(Danbooru::DanbooruPost::Safe),
|
m_maxRating(Danbooru::DanbooruPost::Safe),
|
||||||
m_currentPosts(0),
|
m_postsToFetch(0),
|
||||||
m_cache(cache)
|
m_cache(cache)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -318,80 +318,43 @@ void DanbooruService::processPostList(KJob* job)
|
||||||
|
|
||||||
// How many posts do we have to fetch?
|
// How many posts do we have to fetch?
|
||||||
|
|
||||||
m_currentPosts = postList.length();
|
m_postsToFetch = postList.length();
|
||||||
|
|
||||||
for (auto element : postList) {
|
for (auto element : postList) {
|
||||||
|
|
||||||
QVariantMap map = element.toMap();
|
QVariantMap map = element.toMap();
|
||||||
|
|
||||||
DanbooruPost* post = new DanbooruPost(map);
|
DanbooruPost* post = new DanbooruPost(map);
|
||||||
|
|
||||||
// First check, for rating
|
// First check, for rating
|
||||||
|
|
||||||
if (post->rating() > m_maxRating) {
|
if (isPostBlacklisted(post, m_blacklist, m_maxRating)) {
|
||||||
m_currentPosts--;
|
m_postsToFetch--;
|
||||||
delete post;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// second check, blacklist
|
|
||||||
|
|
||||||
|
|
||||||
bool inBlacklist = false;
|
|
||||||
for (auto tag: post->tags()) {
|
|
||||||
|
|
||||||
if (m_blacklist.contains(tag)) {
|
|
||||||
inBlacklist = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inBlacklist) {
|
|
||||||
m_currentPosts--;
|
|
||||||
delete post;
|
delete post;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
QPixmap pix;
|
QPixmap pix;
|
||||||
// QPixmap* pix = new QPixmap();
|
|
||||||
bool result;
|
|
||||||
|
|
||||||
if (m_cache) {
|
if (m_cache->findPixmap(post->thumbnailUrl().url(), &pix)) {
|
||||||
result = m_cache->findPixmap(post->thumbnailUrl().url(),
|
|
||||||
&pix);
|
|
||||||
} else {
|
|
||||||
result = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (result) {
|
|
||||||
post->setPixmap(pix);
|
post->setPixmap(pix);
|
||||||
Q_EMIT(postDownloaded(post));
|
Q_EMIT(postDownloaded(post));
|
||||||
m_currentPosts--;
|
m_postsToFetch--;
|
||||||
|
|
||||||
// Shortcut in case we have all posts in the cache or the
|
if (m_postsToFetch == 0) {
|
||||||
// last post is in the cache
|
qDebug() << "Post download finished";
|
||||||
|
|
||||||
if (m_currentPosts == 0) {
|
|
||||||
Q_EMIT(postDownloadFinished());
|
Q_EMIT(postDownloadFinished());
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
|
StoredTransferJob* pixmapJob = KIO::storedGet(post->thumbnailUrl(),
|
||||||
StoredTransferJob* pixmapJob = KIO::storedGet(
|
|
||||||
post->thumbnailUrl(),
|
|
||||||
KIO::NoReload, KIO::HideProgressInfo
|
KIO::NoReload, KIO::HideProgressInfo
|
||||||
);
|
);
|
||||||
|
|
||||||
KIO::Scheduler::setJobPriority(
|
KIO::Scheduler::setJobPriority(static_cast<KIO::SimpleJob*>(job), 1);
|
||||||
static_cast<KIO::SimpleJob*>(job),
|
|
||||||
1
|
|
||||||
);
|
|
||||||
|
|
||||||
QVariant variant;
|
QVariant variant;
|
||||||
|
|
||||||
variant.setValue(post);
|
variant.setValue(post);
|
||||||
|
|
||||||
// We don't want to overload the servers, so set some rational
|
// We don't want to overload the servers, so set some rational
|
||||||
|
@ -399,12 +362,46 @@ void DanbooruService::processPostList(KJob* job)
|
||||||
|
|
||||||
pixmapJob->setProperty("danbooruPost", variant);
|
pixmapJob->setProperty("danbooruPost", variant);
|
||||||
|
|
||||||
connect(pixmapJob, &StoredTransferJob::result, this, &DanbooruService::downloadThumbnail);
|
connect(pixmapJob, &StoredTransferJob::result, [post, this] (KJob* job) {
|
||||||
|
|
||||||
|
if (job->error()) {
|
||||||
|
Q_EMIT(downloadError(job->errorString()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QPixmap pix;
|
||||||
|
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) {
|
||||||
|
//qDebug() << "Inserting item in cache";
|
||||||
|
m_cache->insertPixmap(post->thumbnailUrl().url(), pix);
|
||||||
|
}
|
||||||
|
|
||||||
|
m_postsToFetch--; // One less post to do
|
||||||
|
|
||||||
|
//qDebug() << "Current posts remaining" << m_currentPosts;
|
||||||
|
Q_EMIT(postDownloaded(post));
|
||||||
|
|
||||||
|
if (m_postsToFetch == 0) {
|
||||||
|
qDebug() << "Post download finished";
|
||||||
|
Q_EMIT(postDownloadFinished());
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DanbooruService::processTagList(KJob* job)
|
void DanbooruService::processTagList(KJob* job)
|
||||||
|
@ -572,12 +569,12 @@ void DanbooruService::downloadThumbnail(KJob* job)
|
||||||
m_cache->insertPixmap(post->thumbnailUrl().url(), pix);
|
m_cache->insertPixmap(post->thumbnailUrl().url(), pix);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_currentPosts--; // One less post to do
|
m_postsToFetch--; // One less post to do
|
||||||
|
|
||||||
//qDebug() << "Current posts remaining" << m_currentPosts;
|
//qDebug() << "Current posts remaining" << m_currentPosts;
|
||||||
Q_EMIT(postDownloaded(post));
|
Q_EMIT(postDownloaded(post));
|
||||||
|
|
||||||
if (m_currentPosts == 0) {
|
if (m_postsToFetch == 0) {
|
||||||
//qDebug() << "Post download finished";
|
//qDebug() << "Post download finished";
|
||||||
Q_EMIT(postDownloadFinished());
|
Q_EMIT(postDownloadFinished());
|
||||||
}
|
}
|
||||||
|
|
|
@ -96,7 +96,7 @@ namespace Danbooru {
|
||||||
QSet<QString> m_blacklist;
|
QSet<QString> m_blacklist;
|
||||||
DanbooruPost::Ratings m_maxRating;
|
DanbooruPost::Ratings m_maxRating;
|
||||||
|
|
||||||
unsigned int m_currentPosts; // To tell when to quit
|
unsigned int m_postsToFetch; // To tell when to quit
|
||||||
|
|
||||||
KImageCache* m_cache; // Pixmap cache
|
KImageCache* m_cache; // Pixmap cache
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue