From be01502b05ef038c2d55ed30b8e0a60dd9a608dd Mon Sep 17 00:00:00 2001 From: Lukas Holecek Date: Sun, 21 Apr 2024 11:38:15 +0200 Subject: [PATCH] Avoid reading data files when calculating hash --- plugins/itemsync/filewatcher.cpp | 8 ++++++++ src/common/contenttype.h | 5 ----- src/common/textdata.cpp | 7 ++++++- src/item/clipboarditem.cpp | 2 -- src/item/serialize.cpp | 3 +++ 5 files changed, 17 insertions(+), 8 deletions(-) diff --git a/plugins/itemsync/filewatcher.cpp b/plugins/itemsync/filewatcher.cpp index 84525c3e89..3d0dd02838 100644 --- a/plugins/itemsync/filewatcher.cpp +++ b/plugins/itemsync/filewatcher.cpp @@ -49,6 +49,13 @@ class SyncDataFile { return f.size(); } + QString toString() const { + if ( m_format.isEmpty() ) + return m_path; + + return QStringLiteral("%1\n%2").arg(m_path, m_format); + } + QByteArray readAll() const { COPYQ_LOG_VERBOSE( QStringLiteral("ItemSync: Reading file: %1").arg(m_path) ); @@ -95,6 +102,7 @@ QDataStream &operator>>(QDataStream &in, SyncDataFile &value) void registerSyncDataFileConverter() { QMetaType::registerConverter(&SyncDataFile::readAll); + QMetaType::registerConverter(&SyncDataFile::toString); #if QT_VERSION < QT_VERSION_CHECK(6,0,0) qRegisterMetaTypeStreamOperators("SyncDataFile"); #else diff --git a/src/common/contenttype.h b/src/common/contenttype.h index ede145f7dc..1e4f8aa70e 100644 --- a/src/common/contenttype.h +++ b/src/common/contenttype.h @@ -27,11 +27,6 @@ enum { */ removeFormats, - /** - * Item hash - */ - hash, - hasText, hasHtml, text, diff --git a/src/common/textdata.cpp b/src/common/textdata.cpp index aa9302179b..083ce2d43a 100644 --- a/src/common/textdata.cpp +++ b/src/common/textdata.cpp @@ -48,7 +48,12 @@ uint hash(const QVariantMap &data) continue; seed = hash(seed, mime); - seed = hash(seed, data[mime].toByteArray()); + + const auto &value = it.value(); + if ( value.type() == QVariant::ByteArray ) + seed = hash(seed, value.toByteArray()); + else + seed = hash(seed, value.toString()); } return seed; diff --git a/src/item/clipboarditem.cpp b/src/item/clipboarditem.cpp index 306d48a849..8590e43520 100644 --- a/src/item/clipboarditem.cpp +++ b/src/item/clipboarditem.cpp @@ -143,8 +143,6 @@ QVariant ClipboardItem::data(int role) const case contentType::data: return m_data; // copy-on-write, so this should be fast - case contentType::hash: - return dataHash(); case contentType::hasText: return m_data.contains(mimeText) || m_data.contains(mimeTextUtf8) diff --git a/src/item/serialize.cpp b/src/item/serialize.cpp index ca58b6205c..7d9d87bb2e 100644 --- a/src/item/serialize.cpp +++ b/src/item/serialize.cpp @@ -38,6 +38,8 @@ class DataFile { return QFileInfo(m_path).size(); } + QString toString() const { return m_path; } + QByteArray readAll() const { QFile f(m_path); @@ -216,6 +218,7 @@ QString dataFilePath(const QByteArray &bytes, bool create = false) void registerDataFileConverter() { QMetaType::registerConverter(&DataFile::readAll); + QMetaType::registerConverter(&DataFile::toString); #if QT_VERSION < QT_VERSION_CHECK(6,0,0) qRegisterMetaTypeStreamOperators("DataFile"); #else