Skip to content

Commit

Permalink
Improve attachment handling by using a file watcher after you change …
Browse files Browse the repository at this point in the history
…a file and undo that change in the KeePassXC app.

This change avoids a situation where the open file has changed or an entry in the application has changed (possibly to be implemented in the future) and when you open that entry the editor shows you outdated data.
  • Loading branch information
w15dev committed Jan 12, 2025
1 parent 40ee047 commit eae8287
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/core/EntryAttachments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <QDesktopServices>
#include <QDir>
#include <QProcessEnvironment>
#include <QScopeGuard>
#include <QSet>
#include <QTemporaryFile>
#include <QUrl>
Expand Down Expand Up @@ -250,6 +251,25 @@ bool EntryAttachments::openAttachment(const QString& key, QString* errorMessage)
watcher->start(tmpFile.fileName(), 5);
connect(watcher.data(), &FileWatcher::fileChanged, this, &EntryAttachments::attachmentFileModified);
m_attachmentFileWatchers.insert(tmpFile.fileName(), watcher);
} else if (auto path = m_openedAttachments.value(key); m_attachmentFileWatchers.contains(path)) {
auto watcher = m_attachmentFileWatchers.value(path);
watcher->stop();

QFile file(path);
auto finally = qScopeGuard([&file, &watcher, &path] {
file.close();
watcher->start(path, 5);
});

const auto attachmentData = value(key);

const bool saveOk = file.open(QIODevice::WriteOnly) && file.setPermissions(QFile::ReadOwner | QFile::WriteOwner)
&& file.write(attachmentData) == attachmentData.size() && file.flush();

if (!saveOk) {
*errorMessage = QString("%1 - %2").arg(key, file.errorString());
return false;
}
}

const bool openOk = QDesktopServices::openUrl(QUrl::fromLocalFile(m_openedAttachments.value(key)));
Expand Down

0 comments on commit eae8287

Please sign in to comment.