Support an image cache like the Python version does
This commit is contained in:
		
					parent
					
						
							
								601975a6fd
							
						
					
				
			
			
				commit
				
					
						e9fde425df
					
				
			
		
					 2 changed files with 61 additions and 14 deletions
				
			
		| 
						 | 
					@ -29,6 +29,7 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <KIO/Job>
 | 
					#include <KIO/Job>
 | 
				
			||||||
#include <KIO/Scheduler>
 | 
					#include <KIO/Scheduler>
 | 
				
			||||||
 | 
					#include <KImageCache>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Own
 | 
					// Own
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -50,14 +51,15 @@ namespace Danbooru {
 | 
				
			||||||
    const QString DanbooruService::RELATED_TAG_URL = "tag/related.json";
 | 
					    const QString DanbooruService::RELATED_TAG_URL = "tag/related.json";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    DanbooruService::DanbooruService(KUrl& boardUrl, QString username,
 | 
					    DanbooruService::DanbooruService(KUrl& boardUrl, QString username,
 | 
				
			||||||
                                     QString password,
 | 
					                                     QString password, KImageCache* cache,
 | 
				
			||||||
                                     QObject* parent):
 | 
					                                     QObject* parent):
 | 
				
			||||||
                                     QObject(parent),
 | 
					                                     QObject(parent),
 | 
				
			||||||
                                     m_url(boardUrl),
 | 
					                                     m_url(boardUrl),
 | 
				
			||||||
                                     m_username(username),
 | 
					                                     m_username(username),
 | 
				
			||||||
                                     m_password(password),
 | 
					                                     m_password(password),
 | 
				
			||||||
                                     m_maxRating(Safe),
 | 
					                                     m_maxRating(Safe),
 | 
				
			||||||
                                     m_currentPosts(0)
 | 
					                                     m_currentPosts(0),
 | 
				
			||||||
 | 
					                                     m_cache(cache)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -357,23 +359,59 @@ namespace Danbooru {
 | 
				
			||||||
                continue;
 | 
					                continue;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            StoredTransferJob* pixmapJob = KIO::storedGet(post->thumbnailUrl(),
 | 
					            QPixmap* pix = new QPixmap();
 | 
				
			||||||
                KIO::NoReload, KIO::HideProgressInfo);
 | 
					            bool result;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // We don't want to overload the servers, so set some rational
 | 
					            if (m_cache) {
 | 
				
			||||||
            // priority
 | 
					                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);
 | 
					                if (m_currentPosts == 0) {
 | 
				
			||||||
            connect(pixmapJob, SIGNAL(result(KJob*)), this,
 | 
					                    Q_EMIT(postDownloadFinished());
 | 
				
			||||||
                    SLOT(downloadThumbnail(KJob*)));
 | 
					                    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);
 | 
					        post->setPixmap(pix);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (m_cache) {
 | 
				
			||||||
 | 
					            qDebug() << "Inserting item";
 | 
				
			||||||
 | 
					            m_cache->insertPixmap(post->thumbnailUrl().url(), *pix);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        m_currentPosts--; // One less post to do
 | 
					        m_currentPosts--; // One less post to do
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        qDebug() << "Current posts remaining" << m_currentPosts;
 | 
					        qDebug() << "Current posts remaining" << m_currentPosts;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -50,6 +50,7 @@
 | 
				
			||||||
class QPixmap;
 | 
					class QPixmap;
 | 
				
			||||||
class KUrl;
 | 
					class KUrl;
 | 
				
			||||||
class KJob;
 | 
					class KJob;
 | 
				
			||||||
 | 
					class KImageCache;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace Danbooru {
 | 
					namespace Danbooru {
 | 
				
			||||||
| 
						 | 
					@ -96,6 +97,8 @@ namespace Danbooru {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        unsigned int m_currentPosts; // To tell when to quit
 | 
					        unsigned int m_currentPosts; // To tell when to quit
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        KImageCache* m_cache; // Pixmap cache
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public:
 | 
					    public:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /**
 | 
					        /**
 | 
				
			||||||
| 
						 | 
					@ -108,7 +111,8 @@ namespace Danbooru {
 | 
				
			||||||
         *
 | 
					         *
 | 
				
			||||||
         **/
 | 
					         **/
 | 
				
			||||||
        DanbooruService(KUrl& boardUrl, QString username = QString(),
 | 
					        DanbooruService(KUrl& boardUrl, QString username = QString(),
 | 
				
			||||||
                        QString password = QString(), QObject* parent = 0);
 | 
					                        QString password = QString(), KImageCache* cache = 0,
 | 
				
			||||||
 | 
					                        QObject* parent = 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /**
 | 
					        /**
 | 
				
			||||||
         * Default destructor.
 | 
					         * Default destructor.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue