From ca38e3cd92888b186eff1fc7a3c8ca67b3240882 Mon Sep 17 00:00:00 2001 From: Slava Monich Date: Tue, 30 Jan 2018 00:27:40 +0200 Subject: [PATCH] [app] Added hash attribute to BooksBook --- app/src/BooksBook.cpp | 66 +++++++++++++++++++++++++++++++++++-------- app/src/BooksBook.h | 18 ++++++++++-- 2 files changed, 70 insertions(+), 14 deletions(-) diff --git a/app/src/BooksBook.cpp b/app/src/BooksBook.cpp index cbb126e..da47c38 100644 --- a/app/src/BooksBook.cpp +++ b/app/src/BooksBook.cpp @@ -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= 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; } diff --git a/app/src/BooksBook.h b/app/src/BooksBook.h index 0ee2c99..d7cdbf9 100644 --- a/app/src/BooksBook.h +++ b/app/src/BooksBook.h @@ -1,6 +1,6 @@ /* - * Copyright (C) 2015-2017 Jolla Ltd. - * Contact: Slava Monich + * Copyright (C) 2015-2018 Jolla Ltd. + * Copyright (C) 2015-2018 Slava Monich * * 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 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 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