[app] Added hash attribute to BooksBook

This commit is contained in:
Slava Monich 2018-01-30 00:27:40 +02:00
parent ed219c1af1
commit ca38e3cd92
2 changed files with 70 additions and 14 deletions

View file

@ -66,6 +66,7 @@
// ==========================================================================
// BooksBook::CoverContext
// ==========================================================================
class BooksBook::CoverPaintContext : public BooksPaintContext {
public:
CoverPaintContext();
@ -289,6 +290,32 @@ void BooksBook::GuessCoverTask::performTask()
}
}
// ==========================================================================
// BooksBook::HashTask
// ==========================================================================
class BooksBook::HashTask : public BooksTask
{
public:
HashTask(QString aPath, QThread* aThread);
virtual void performTask();
public:
QString iPath;
QByteArray iHash;
};
BooksBook::HashTask::HashTask(QString aPath, QThread* aThread) :
BooksTask(aThread), iPath(aPath)
{
}
void BooksBook::HashTask::performTask()
{
iHash = BooksUtil::computeFileHashAndSetAttr(iPath, this);
}
// ==========================================================================
// BooksBook
// ==========================================================================
@ -307,14 +334,14 @@ BooksBook::BooksBook(const BooksStorage& aStorage, QString aRelativePath,
iRef(1),
iStorage(aStorage),
iBook(aBook),
iTaskQueue(BooksTaskQueue::defaultQueue())
iFormatPlugin(PluginCollection::Instance().plugin(*iBook)),
iTaskQueue(BooksTaskQueue::defaultQueue()),
iTitle(QString::fromStdString(iBook->title())),
iPath(QString::fromStdString(iBook->file().physicalFilePath())),
iFileName(QFileInfo(iPath).fileName()),
iHash(BooksUtil::fileHashAttr(iPath))
{
init();
HASSERT(!iBook.isNull());
iTitle = QString::fromStdString(iBook->title());
iPath = QString::fromStdString(iBook->file().physicalFilePath());
iFileName = QFileInfo(iPath).fileName();
iFormatPlugin = PluginCollection::Instance().plugin(*iBook);
AuthorList authors(iBook->authors());
const int n = authors.size();
for (int i=0; i<n; i++) {
@ -324,8 +351,9 @@ BooksBook::BooksBook(const BooksStorage& aStorage, QString aRelativePath,
if (iStorage.isValid()) {
iStateDir = QDir::cleanPath(iStorage.configDir().path() +
QDir::separator() + aRelativePath);
iStateFilePath = QDir::cleanPath(iStateDir +
QDir::separator() + iFileName + BOOKS_STATE_FILE_SUFFIX);
iStateFileBase = QDir::cleanPath(iStateDir +
QDir::separator() + iFileName);
iStateFilePath = storageFile(BOOKS_STATE_FILE_SUFFIX);
// Load the state
QVariantMap state;
if (HarbourJson::load(iStateFilePath, state)) {
@ -356,6 +384,12 @@ BooksBook::BooksBook(const BooksStorage& aStorage, QString aRelativePath,
} else if (iPageStackPos >= iPageStack.count()) {
iPageStackPos = iPageStack.count() - 1;
}
if (iHash.isEmpty()) {
HDEBUG("need to calculate hash for" << qPrintable(iPath));
iHashTask = new HashTask(iPath, thread());
connect(iHashTask, SIGNAL(done()), SLOT(onHashTaskDone()));
iTaskQueue->submit(iHashTask);
}
// Refcounted BooksBook objects are managed by C++ code
QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
}
@ -365,9 +399,11 @@ void BooksBook::init()
iFontSizeAdjust = 0;
iPageStackPos = 0;
iCoverTask = NULL;
iHashTask = NULL;
iCoverTasksDone = false;
iCopyingOut = false;
iSaveTimer = NULL;
moveToThread(qApp->thread());
}
BooksBook::~BooksBook()
@ -375,6 +411,7 @@ BooksBook::~BooksBook()
HDEBUG(qPrintable(iPath));
HASSERT(!iRef.load());
if (iCoverTask) iCoverTask->release(this);
if (iHashTask) iHashTask->release(this);
}
BooksItem* BooksBook::retain()
@ -559,6 +596,15 @@ void BooksBook::onGuessCoverTaskDone()
Q_EMIT loadingCoverChanged();
}
void BooksBook::onHashTaskDone()
{
iHash = iHashTask->iHash;
HDEBUG(QString(iHash.toHex()));
iHashTask->release(this);
iHashTask = NULL;
Q_EMIT hashChanged();
}
QString BooksBook::cachedImagePath() const
{
if (!iStateDir.isEmpty() && !iBook.isNull()) {
@ -614,9 +660,7 @@ BooksBook* BooksBook::newBook(const BooksStorage& aStorage, QString aRelPath,
QFileInfo(QDir(aStorage.fullPath(aRelPath)), aFileName).
absoluteFilePath());
if (!ref.isNull()) {
BooksBook* book = new BooksBook(aStorage, aRelPath, ref);
book->moveToThread(qApp->thread());
return book;
return new BooksBook(aStorage, aRelPath, ref);
} else {
return NULL;
}

View file

@ -1,6 +1,6 @@
/*
* Copyright (C) 2015-2017 Jolla Ltd.
* Contact: Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2015-2018 Jolla Ltd.
* Copyright (C) 2015-2018 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of the BSD license as follows:
*
@ -49,6 +49,7 @@
class BooksBook : public QObject, public BooksItem
{
class HashTask;
class CoverTask;
class LoadCoverTask;
class GuessCoverTask;
@ -76,12 +77,14 @@ public:
QString title() const;
QString authors() const;
QByteArray hash() const;
int fontSizeAdjust() const;
bool setFontSizeAdjust(int aFontSizeAdjust);
int pageStackPos() const;
BooksPos::List pageStack() const;
void setPageStack(BooksPos::List aStack, int aStackPos);
shared_ptr<Book> bookRef() const;
QString storageFile(QString aSuffix) const;
bool copyingOut() const;
bool loadingCover() const;
@ -112,11 +115,13 @@ Q_SIGNALS:
void accessibleChanged();
void copyingOutChanged();
void fontSizeAdjustChanged();
void hashChanged();
void movedAway();
private Q_SLOTS:
void onLoadCoverTaskDone();
void onGuessCoverTaskDone();
void onHashTaskDone();
void saveState();
private:
@ -139,14 +144,17 @@ private:
shared_ptr<BooksTaskQueue> iTaskQueue;
BooksSaveTimer* iSaveTimer;
CoverTask* iCoverTask;
HashTask* iHashTask;
bool iCoverTasksDone;
bool iCopyingOut;
QString iTitle;
QString iAuthors;
QString iFileName;
QString iPath;
QString iFileName;
QString iStateDir;
QString iStateFilePath;
QString iStateFileBase;
QByteArray iHash;
};
QML_DECLARE_TYPE(BooksBook)
@ -155,6 +163,8 @@ inline QString BooksBook::title() const
{ return iTitle; }
inline QString BooksBook::authors() const
{ return iAuthors; }
inline QByteArray BooksBook::hash() const
{ return iHash; }
inline int BooksBook::fontSizeAdjust() const
{ return iFontSizeAdjust; }
inline int BooksBook::pageStackPos() const
@ -171,5 +181,7 @@ inline bool BooksBook::isCanceled(CopyOperation* aObserver)
{ return aObserver && aObserver->isCanceled(); }
inline QImage BooksBook::coverImage() const
{ return iCoverImage; }
inline QString BooksBook::storageFile(QString aSuffix) const
{ return iStateFileBase + aSuffix; }
#endif // BOOKS_BOOK_H