[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 // BooksBook::CoverContext
// ========================================================================== // ==========================================================================
class BooksBook::CoverPaintContext : public BooksPaintContext { class BooksBook::CoverPaintContext : public BooksPaintContext {
public: public:
CoverPaintContext(); 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 // BooksBook
// ========================================================================== // ==========================================================================
@ -307,14 +334,14 @@ BooksBook::BooksBook(const BooksStorage& aStorage, QString aRelativePath,
iRef(1), iRef(1),
iStorage(aStorage), iStorage(aStorage),
iBook(aBook), 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(); 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()); AuthorList authors(iBook->authors());
const int n = authors.size(); const int n = authors.size();
for (int i=0; i<n; i++) { for (int i=0; i<n; i++) {
@ -324,8 +351,9 @@ BooksBook::BooksBook(const BooksStorage& aStorage, QString aRelativePath,
if (iStorage.isValid()) { if (iStorage.isValid()) {
iStateDir = QDir::cleanPath(iStorage.configDir().path() + iStateDir = QDir::cleanPath(iStorage.configDir().path() +
QDir::separator() + aRelativePath); QDir::separator() + aRelativePath);
iStateFilePath = QDir::cleanPath(iStateDir + iStateFileBase = QDir::cleanPath(iStateDir +
QDir::separator() + iFileName + BOOKS_STATE_FILE_SUFFIX); QDir::separator() + iFileName);
iStateFilePath = storageFile(BOOKS_STATE_FILE_SUFFIX);
// Load the state // Load the state
QVariantMap state; QVariantMap state;
if (HarbourJson::load(iStateFilePath, state)) { if (HarbourJson::load(iStateFilePath, state)) {
@ -356,6 +384,12 @@ BooksBook::BooksBook(const BooksStorage& aStorage, QString aRelativePath,
} else if (iPageStackPos >= iPageStack.count()) { } else if (iPageStackPos >= iPageStack.count()) {
iPageStackPos = iPageStack.count() - 1; 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 // Refcounted BooksBook objects are managed by C++ code
QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership); QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
} }
@ -365,9 +399,11 @@ void BooksBook::init()
iFontSizeAdjust = 0; iFontSizeAdjust = 0;
iPageStackPos = 0; iPageStackPos = 0;
iCoverTask = NULL; iCoverTask = NULL;
iHashTask = NULL;
iCoverTasksDone = false; iCoverTasksDone = false;
iCopyingOut = false; iCopyingOut = false;
iSaveTimer = NULL; iSaveTimer = NULL;
moveToThread(qApp->thread());
} }
BooksBook::~BooksBook() BooksBook::~BooksBook()
@ -375,6 +411,7 @@ BooksBook::~BooksBook()
HDEBUG(qPrintable(iPath)); HDEBUG(qPrintable(iPath));
HASSERT(!iRef.load()); HASSERT(!iRef.load());
if (iCoverTask) iCoverTask->release(this); if (iCoverTask) iCoverTask->release(this);
if (iHashTask) iHashTask->release(this);
} }
BooksItem* BooksBook::retain() BooksItem* BooksBook::retain()
@ -559,6 +596,15 @@ void BooksBook::onGuessCoverTaskDone()
Q_EMIT loadingCoverChanged(); 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 QString BooksBook::cachedImagePath() const
{ {
if (!iStateDir.isEmpty() && !iBook.isNull()) { if (!iStateDir.isEmpty() && !iBook.isNull()) {
@ -614,9 +660,7 @@ BooksBook* BooksBook::newBook(const BooksStorage& aStorage, QString aRelPath,
QFileInfo(QDir(aStorage.fullPath(aRelPath)), aFileName). QFileInfo(QDir(aStorage.fullPath(aRelPath)), aFileName).
absoluteFilePath()); absoluteFilePath());
if (!ref.isNull()) { if (!ref.isNull()) {
BooksBook* book = new BooksBook(aStorage, aRelPath, ref); return new BooksBook(aStorage, aRelPath, ref);
book->moveToThread(qApp->thread());
return book;
} else { } else {
return NULL; return NULL;
} }

View file

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