Large refactoring of DanbooruService
The idea is to use a long-lived DanbooruService instance, rather than recreating it, so move most of the parameters via setters and getters. Still needs some adjustments for resetting.
This commit is contained in:
parent
e1045e1fa6
commit
f4566d3aca
2 changed files with 119 additions and 49 deletions
|
@ -66,6 +66,9 @@ 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_maxPosts(10),
|
||||||
|
m_currentPage(1),
|
||||||
|
m_tags(QStringList()),
|
||||||
m_postsToFetch(0),
|
m_postsToFetch(0),
|
||||||
m_cache(cache)
|
m_cache(cache)
|
||||||
{
|
{
|
||||||
|
@ -73,26 +76,23 @@ 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()
|
||||||
{
|
{
|
||||||
|
|
||||||
// We can't fetch more than 100 items, API limitation
|
// We can't fetch more than 100 items, API limitation
|
||||||
|
|
||||||
qDebug() << "Fetching with URL" << m_url;
|
qDebug() << "Fetching with URL" << m_url;
|
||||||
|
|
||||||
limit = limit > 100 ? 100 : limit;
|
|
||||||
|
|
||||||
QMap<QString, QString> parameters;
|
QMap<QString, QString> parameters;
|
||||||
|
|
||||||
parameters.insert("limit", QString::number(limit));
|
parameters.insert("limit", QString::number(m_maxPosts));
|
||||||
parameters.insert("page", QString::number(page));
|
parameters.insert("page", QString::number(m_currentPage));
|
||||||
|
|
||||||
QUrl danbooruUrl = requestUrl(m_url, POST_URL, m_username,
|
QUrl danbooruUrl = requestUrl(m_url, POST_URL, m_username,
|
||||||
m_password, parameters, tags);
|
m_password, parameters, m_tags);
|
||||||
|
|
||||||
qDebug() << "Final constructed post URL" << danbooruUrl;
|
qDebug() << "Final constructed post URL" << danbooruUrl;
|
||||||
|
|
||||||
|
@ -182,16 +182,16 @@ void DanbooruService::getPool(int poolId, int page)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DanbooruService::getPoolList(int page)
|
void DanbooruService::getPoolList()
|
||||||
{
|
{
|
||||||
|
|
||||||
QUrl danbooruUrl;
|
QUrl danbooruUrl;
|
||||||
|
|
||||||
if (page == 0) {
|
if (m_currentPage == 1) {
|
||||||
danbooruUrl = requestUrl(m_url, POOL_URL, m_username, m_password);
|
danbooruUrl = requestUrl(m_url, POOL_URL, m_username, m_password);
|
||||||
} else {
|
} else {
|
||||||
QMap<QString, QString> map;
|
QMap<QString, QString> map;
|
||||||
map.insert("page", QString::number(page));
|
map.insert("page", QString::number(m_currentPage));
|
||||||
|
|
||||||
danbooruUrl = requestUrl(m_url, POOL_URL, m_username,
|
danbooruUrl = requestUrl(m_url, POOL_URL, m_username,
|
||||||
m_password, map);
|
m_password, map);
|
||||||
|
@ -323,16 +323,6 @@ void DanbooruService::getRelatedTags(const QStringList &tags,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Getters / setters
|
|
||||||
|
|
||||||
void DanbooruService::setBlacklist(const QSet< QString > &blacklist)
|
|
||||||
{
|
|
||||||
|
|
||||||
if (!blacklist.isEmpty()) {
|
|
||||||
m_blacklist = blacklist;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
const QSet< QString > DanbooruService::blacklist() const
|
const QSet< QString > DanbooruService::blacklist() const
|
||||||
{
|
{
|
||||||
|
@ -359,26 +349,11 @@ const QStringList DanbooruService::allowedRatings() const
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DanbooruService::setMaximumAllowedRating(DanbooruPost::Rating rating)
|
int DanbooruService::currentPage() const {
|
||||||
{
|
return m_currentPage;
|
||||||
DanbooruPost::Ratings flags;
|
|
||||||
|
|
||||||
switch (rating) {
|
|
||||||
case DanbooruPost::Safe:
|
|
||||||
flags = DanbooruPost::Safe;
|
|
||||||
break;
|
|
||||||
case DanbooruPost::Questionable:
|
|
||||||
flags = DanbooruPost::Safe | DanbooruPost::Questionable;
|
|
||||||
break;
|
|
||||||
case DanbooruPost::Explicit:
|
|
||||||
flags = DanbooruPost::Safe | DanbooruPost::Questionable | DanbooruPost::Explicit;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
m_maxRating = flags;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const DanbooruPost::Ratings DanbooruService::maximumAllowedRating() const
|
const DanbooruPost::Ratings DanbooruService::maximumAllowedRating() const
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -386,6 +361,23 @@ const DanbooruPost::Ratings DanbooruService::maximumAllowedRating() const
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int DanbooruService::maxPosts() const
|
||||||
|
{
|
||||||
|
return m_maxPosts;
|
||||||
|
}
|
||||||
|
|
||||||
|
void DanbooruService::nextPostPage()
|
||||||
|
{
|
||||||
|
m_currentPage++;
|
||||||
|
getPostList();
|
||||||
|
}
|
||||||
|
|
||||||
|
QStringList DanbooruService::postTags() const
|
||||||
|
{
|
||||||
|
return m_tags;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Slots
|
// Slots
|
||||||
|
|
||||||
void DanbooruService::processPostList(KJob *job)
|
void DanbooruService::processPostList(KJob *job)
|
||||||
|
@ -393,10 +385,6 @@ 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()));
|
||||||
}
|
}
|
||||||
|
@ -523,6 +511,16 @@ void DanbooruService::processPostList(KJob *job)
|
||||||
void DanbooruService::downloadAllTags(KJob *job)
|
void DanbooruService::downloadAllTags(KJob *job)
|
||||||
{
|
{
|
||||||
Q_UNUSED(job)
|
Q_UNUSED(job)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void DanbooruService::setBlacklist(const QSet< QString > &blacklist)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (!blacklist.isEmpty()) {
|
||||||
|
m_blacklist = blacklist;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DanbooruService::setBoardUrl(const QUrl& url)
|
void DanbooruService::setBoardUrl(const QUrl& url)
|
||||||
|
@ -530,6 +528,26 @@ void DanbooruService::setBoardUrl(const QUrl& url)
|
||||||
m_url = url;
|
m_url = url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DanbooruService::setMaximumAllowedRating(DanbooruPost::Rating rating)
|
||||||
|
{
|
||||||
|
DanbooruPost::Ratings flags;
|
||||||
|
|
||||||
|
switch (rating) {
|
||||||
|
case DanbooruPost::Safe:
|
||||||
|
flags = DanbooruPost::Safe;
|
||||||
|
break;
|
||||||
|
case DanbooruPost::Questionable:
|
||||||
|
flags = DanbooruPost::Safe | DanbooruPost::Questionable;
|
||||||
|
break;
|
||||||
|
case DanbooruPost::Explicit:
|
||||||
|
flags = DanbooruPost::Safe | DanbooruPost::Questionable | DanbooruPost::Explicit;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_maxRating = flags;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
void DanbooruService::setUserName(const QString& username)
|
void DanbooruService::setUserName(const QString& username)
|
||||||
{
|
{
|
||||||
m_username = username;
|
m_username = username;
|
||||||
|
@ -540,9 +558,26 @@ void DanbooruService::setPassword(const QString& password)
|
||||||
m_password = password;
|
m_password = password;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DanbooruService::setPostTags(const QStringList& tags)
|
||||||
|
{
|
||||||
|
if (!tags.isEmpty()) {
|
||||||
|
m_tags = tags;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void DanbooruService::setImageCache(KImageCache* cache)
|
void DanbooruService::setImageCache(KImageCache* cache)
|
||||||
{
|
{
|
||||||
m_cache = cache;
|
m_cache = cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DanbooruService::setMaxPosts(int number) {
|
||||||
|
|
||||||
|
m_maxPosts = number ? number < 100: 100;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void DanbooruService::setCurrentPage(int page) {
|
||||||
|
m_currentPage = page;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace Danbooru
|
} // namespace Danbooru
|
||||||
|
|
|
@ -95,7 +95,9 @@ 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;
|
int m_maxPosts;
|
||||||
|
int m_currentPage;
|
||||||
|
QStringList m_tags;
|
||||||
|
|
||||||
unsigned int m_postsToFetch; // To tell when to quit
|
unsigned int m_postsToFetch; // To tell when to quit
|
||||||
|
|
||||||
|
@ -124,6 +126,9 @@ public:
|
||||||
**/
|
**/
|
||||||
~DanbooruService();
|
~DanbooruService();
|
||||||
|
|
||||||
|
int currentPage() const;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get posts from a the board.
|
* @brief Get posts from a the board.
|
||||||
*
|
*
|
||||||
|
@ -132,16 +137,13 @@ public:
|
||||||
* @param limit The number of posts to fetch (maximum 100)
|
* @param limit The number of posts to fetch (maximum 100)
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
Q_INVOKABLE void getPostList(int page = 1, QStringList tags = QStringList(),
|
Q_INVOKABLE void getPostList();
|
||||||
int limit = 100);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get a list of pools from the board.
|
* @brief Get a list of pools from the board.
|
||||||
*
|
*
|
||||||
* @param page The page to get pools from (default: 1)
|
|
||||||
*
|
|
||||||
**/
|
**/
|
||||||
void getPoolList(int page = 1);
|
void getPoolList();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get the posts associated with a specific pool ID.
|
* @brief Get the posts associated with a specific pool ID.
|
||||||
|
@ -191,6 +193,23 @@ public:
|
||||||
**/
|
**/
|
||||||
const QSet<QString> blacklist() const;
|
const QSet<QString> blacklist() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The number of posts downloaded for each page (max 100)
|
||||||
|
**/
|
||||||
|
int maxPosts() const;
|
||||||
|
|
||||||
|
void nextPostPage();
|
||||||
|
|
||||||
|
QStringList postTags() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Set blacklisted tags.
|
||||||
|
*
|
||||||
|
* Posts with blacklisted tags are not downloaded.
|
||||||
|
*
|
||||||
|
* @param blacklist A QSet<QString> including unwanted tags.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
void setBlacklist(const QSet<QString> &blacklist);
|
void setBlacklist(const QSet<QString> &blacklist);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -236,8 +255,24 @@ public:
|
||||||
*/
|
*/
|
||||||
void setUserName(const QString& username);
|
void setUserName(const QString& username);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Set the password used for login.
|
||||||
|
*
|
||||||
|
* It should not be the password itself, but a SHA1 hash
|
||||||
|
* with a specific salt (which is board-dependent; check their
|
||||||
|
* API access rules).
|
||||||
|
*
|
||||||
|
* @param username The salted password to use.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
void setPassword(const QString& password);
|
void setPassword(const QString& password);
|
||||||
|
|
||||||
|
void setMaxPosts(int number);
|
||||||
|
|
||||||
|
void setCurrentPage(int page);
|
||||||
|
|
||||||
|
void setPostTags(const QStringList& tags);
|
||||||
|
|
||||||
|
|
||||||
private Q_SLOTS:
|
private Q_SLOTS:
|
||||||
void processPostList(KJob *job);
|
void processPostList(KJob *job);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue