Skip to content

Commit

Permalink
Avoid reading data files when calculating hash
Browse files Browse the repository at this point in the history
  • Loading branch information
hluk committed Apr 21, 2024
1 parent fdcea22 commit be01502
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
8 changes: 8 additions & 0 deletions plugins/itemsync/filewatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) );
Expand Down Expand Up @@ -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>("SyncDataFile");
#else
Expand Down
5 changes: 0 additions & 5 deletions src/common/contenttype.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ enum {
*/
removeFormats,

/**
* Item hash
*/
hash,

hasText,
hasHtml,
text,
Expand Down
7 changes: 6 additions & 1 deletion src/common/textdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions src/item/clipboarditem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions src/item/serialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class DataFile {
return QFileInfo(m_path).size();
}

QString toString() const { return m_path; }

QByteArray readAll() const
{
QFile f(m_path);
Expand Down Expand Up @@ -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>("DataFile");
#else
Expand Down

0 comments on commit be01502

Please sign in to comment.