Support an image cache like the Python version does

This commit is contained in:
Luca Beltrame 2013-04-01 11:31:52 +02:00
parent 601975a6fd
commit e9fde425df
2 changed files with 61 additions and 14 deletions

View file

@ -29,6 +29,7 @@
#include <KIO/Job>
#include <KIO/Scheduler>
#include <KImageCache>
// Own
@ -50,14 +51,15 @@ namespace Danbooru {
const QString DanbooruService::RELATED_TAG_URL = "tag/related.json";
DanbooruService::DanbooruService(KUrl& boardUrl, QString username,
QString password,
QString password, KImageCache* cache,
QObject* parent):
QObject(parent),
m_url(boardUrl),
m_username(username),
m_password(password),
m_maxRating(Safe),
m_currentPosts(0)
m_currentPosts(0),
m_cache(cache)
{
}
@ -357,23 +359,59 @@ namespace Danbooru {
continue;
}
StoredTransferJob* pixmapJob = KIO::storedGet(post->thumbnailUrl(),
KIO::NoReload, KIO::HideProgressInfo);
QPixmap* pix = new QPixmap();
bool result;
// We don't want to overload the servers, so set some rational
// priority
if (m_cache) {
qDebug() << "Checking cache";
result = m_cache->findPixmap(post->thumbnailUrl().url(),
pix);
} else {
result = false;
}
KIO::Scheduler::setJobPriority(static_cast<KIO::SimpleJob*>(job),
1);
QVariant variant;
if (result) {
qDebug() << "Cache hit";
post->setPixmap(pix);
Q_EMIT(postDownloaded(post));
m_currentPosts--;
variant.setValue(post);
// Shortcut in case we have all posts in the cache or the
// last post is in the cache
pixmapJob->setProperty("danbooruPost", variant);
connect(pixmapJob, SIGNAL(result(KJob*)), this,
SLOT(downloadThumbnail(KJob*)));
if (m_currentPosts == 0) {
Q_EMIT(postDownloadFinished());
return;
}
} else {
delete pix;
StoredTransferJob* pixmapJob = KIO::storedGet(
post->thumbnailUrl(),
KIO::NoReload, KIO::HideProgressInfo
);
KIO::Scheduler::setJobPriority(
static_cast<KIO::SimpleJob*>(job),
1
);
QVariant variant;
variant.setValue(post);
// We don't want to overload the servers, so set some rational
// priority
pixmapJob->setProperty("danbooruPost", variant);
connect(pixmapJob, SIGNAL(result(KJob*)), this,
SLOT(downloadThumbnail(KJob*)));
}
}
}
@ -541,6 +579,11 @@ namespace Danbooru {
post->setPixmap(pix);
if (m_cache) {
qDebug() << "Inserting item";
m_cache->insertPixmap(post->thumbnailUrl().url(), *pix);
}
m_currentPosts--; // One less post to do
qDebug() << "Current posts remaining" << m_currentPosts;

View file

@ -50,6 +50,7 @@
class QPixmap;
class KUrl;
class KJob;
class KImageCache;
namespace Danbooru {
@ -96,6 +97,8 @@ namespace Danbooru {
unsigned int m_currentPosts; // To tell when to quit
KImageCache* m_cache; // Pixmap cache
public:
/**
@ -108,7 +111,8 @@ namespace Danbooru {
*
**/
DanbooruService(KUrl& boardUrl, QString username = QString(),
QString password = QString(), QObject* parent = 0);
QString password = QString(), KImageCache* cache = 0,
QObject* parent = 0);
/**
* Default destructor.