Clean up DanbooruService

Remove multi_get for now (broken in kio_http)
Add a default constructor (needed for QML)
Explicitly delete posts
This commit is contained in:
Luca Beltrame 2015-02-05 23:47:29 +01:00
parent 719ef7ecaa
commit f8b59e78b5
2 changed files with 45 additions and 4 deletions

View file

@ -20,6 +20,10 @@
* *
*/ */
// STL
#include <algorithm>
// Qt // Qt
#include <QDebug> #include <QDebug>
@ -31,6 +35,7 @@
#include <KIO/Job> #include <KIO/Job>
#include <KIO/Scheduler> #include <KIO/Scheduler>
#include <KImageCache> #include <KImageCache>
#include <KIO/MultiGetJob>
// Own // Own
@ -44,6 +49,7 @@ namespace Danbooru
{ {
using KIO::StoredTransferJob; using KIO::StoredTransferJob;
using KIO::MultiGetJob;
const QString DanbooruService::POST_URL = "post/index.json" ; const QString DanbooruService::POST_URL = "post/index.json" ;
const QString DanbooruService::TAG_URL = "tag/index.xml"; const QString DanbooruService::TAG_URL = "tag/index.xml";
@ -52,6 +58,16 @@ const QString DanbooruService::ARTIST_URL = "artist/index.json";
const QString DanbooruService::POOL_DATA_URL = "pool/show.xml"; const QString DanbooruService::POOL_DATA_URL = "pool/show.xml";
const QString DanbooruService::RELATED_TAG_URL = "tag/related.json"; const QString DanbooruService::RELATED_TAG_URL = "tag/related.json";
DanbooruService::DanbooruService(QObject *parent):
QObject(parent),
m_url(QUrl()),
m_username(QString()),
m_password(QString()),
m_maxRating(DanbooruPost::Safe),
m_postsToFetch(0),
m_cache(0){
}
DanbooruService::DanbooruService(QUrl &boardUrl, QString username, DanbooruService::DanbooruService(QUrl &boardUrl, QString username,
QString password, KImageCache *cache, QString password, KImageCache *cache,
QObject *parent): QObject *parent):
@ -67,7 +83,8 @@ DanbooruService::DanbooruService(QUrl &boardUrl, QString username,
DanbooruService::~DanbooruService() DanbooruService::~DanbooruService()
{ {
qDeleteAll(m_posts);
m_posts.clear();
} }
void DanbooruService::getPostList(int page, QStringList tags, int limit) void DanbooruService::getPostList(int page, QStringList tags, int limit)
@ -384,6 +401,10 @@ void DanbooruService::processPostList(KJob *job)
// qDebug() << "Got post data OK"; // qDebug() << "Got post data OK";
if (!m_posts.isEmpty()) {
m_posts.clear();
}
if (job->error()) { if (job->error()) {
Q_EMIT(downloadError(job->errorString())); Q_EMIT(downloadError(job->errorString()));
} }
@ -419,6 +440,21 @@ void DanbooruService::processPostList(KJob *job)
m_postsToFetch = postList.length(); m_postsToFetch = postList.length();
int postId = 0;
for (auto element: postList) {
QVariantMap map = element.toMap();
DanbooruPost *post = new DanbooruPost(map);
if (isPostBlacklisted(post, m_blacklist, m_maxRating)) {
m_postsToFetch--;
delete post;
continue;
}
m_posts.insert(postId, post);
postId++;
}
for (auto element : postList) { for (auto element : postList) {
QVariantMap map = element.toMap(); QVariantMap map = element.toMap();
@ -434,9 +470,10 @@ void DanbooruService::processPostList(KJob *job)
QPixmap pix; QPixmap pix;
// Usare QHash<id, url> per KIO::multi_get!
if (m_cache->findPixmap(post->thumbnailUrl().url(), &pix)) { if (m_cache->findPixmap(post->thumbnailUrl().url(), &pix)) {
qDebug() << "in cache";
post->setPixmap(pix); post->setPixmap(pix);
Q_EMIT(postDownloaded(post)); Q_EMIT(postDownloaded(post));
m_postsToFetch--; m_postsToFetch--;

View file

@ -95,6 +95,7 @@ private:
QString m_password; QString m_password;
QSet<QString> m_blacklist; QSet<QString> m_blacklist;
DanbooruPost::Ratings m_maxRating; DanbooruPost::Ratings m_maxRating;
QHash<int, DanbooruPost*> m_posts;
unsigned int m_postsToFetch; // To tell when to quit unsigned int m_postsToFetch; // To tell when to quit
@ -102,6 +103,9 @@ private:
public: public:
DanbooruService(QObject *parent = 0);
/** /**
* @brief Construct a default instance of the service. * @brief Construct a default instance of the service.
* *
@ -130,8 +134,8 @@ public:
* @param limit The number of posts to fetch (maximum 100) * @param limit The number of posts to fetch (maximum 100)
* *
**/ **/
void getPostList(int page = 1, QStringList tags = QStringList(), Q_INVOKABLE void getPostList(int page = 1, QStringList tags = QStringList(),
int limit = 100); int limit = 100);
/** /**
* @brief Get a list of pools from the board. * @brief Get a list of pools from the board.