Improve attachment handling when changes are discarded

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.

* Fixes bug from previous attachments preview commit
* Fix logic error when creating new attachment
This commit is contained in:
Kuznetsov Oleg 2025-02-01 15:59:02 +03:00 committed by Jonathan White
parent 4e40a7fd79
commit c3214f6776
No known key found for this signature in database
GPG Key ID: 440FC65F2E0C6E01
4 changed files with 33 additions and 16 deletions

View File

@ -24,6 +24,7 @@
#include <QDesktopServices>
#include <QDir>
#include <QProcessEnvironment>
#include <QScopeGuard>
#include <QSet>
#include <QTemporaryFile>
#include <QUrl>
@ -235,8 +236,10 @@ bool EntryAttachments::openAttachment(const QString& key, QString* errorMessage)
const bool saveOk = tmpFile.open() && tmpFile.setPermissions(QFile::ReadOwner | QFile::WriteOwner)
&& tmpFile.write(attachmentData) == attachmentData.size() && tmpFile.flush();
if (!saveOk && errorMessage) {
*errorMessage = QString("%1 - %2").arg(key, tmpFile.errorString());
if (!saveOk) {
if (errorMessage) {
*errorMessage = QString("%1 - %2").arg(key, tmpFile.errorString());
}
return false;
}
@ -249,15 +252,35 @@ 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)) {
// If we are already watching an open attachment file, overwrite it with the information from the entry
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) {
if (errorMessage) {
*errorMessage = QString("%1 - %2").arg(key, file.errorString());
}
return false;
}
}
const bool openOk = QDesktopServices::openUrl(QUrl::fromLocalFile(m_openedAttachments.value(key)));
if (!openOk && errorMessage) {
*errorMessage = tr("Cannot open file \"%1\"").arg(key);
return false;
}
return true;
return openOk;
}
void EntryAttachments::attachmentFileModified(const QString& path)

View File

@ -33,9 +33,7 @@ NewEntryAttachmentsDialog::NewEntryAttachmentsDialog(QPointer<EntryAttachments>
setWindowTitle(tr("New entry attachment"));
m_ui->dialogButtons->clear();
m_ui->dialogButtons->addButton(QDialogButtonBox::Ok);
m_ui->dialogButtons->addButton(QDialogButtonBox::Cancel);
m_ui->dialogButtons->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(m_ui->dialogButtons, SIGNAL(accepted()), this, SLOT(saveAttachment()));
connect(m_ui->dialogButtons, SIGNAL(rejected()), this, SLOT(reject()));
@ -67,7 +65,7 @@ void NewEntryAttachmentsDialog::saveAttachment()
auto text = m_ui->attachmentTextEdit->toPlainText().toUtf8();
QString error;
if (validateFileName(fileName, error)) {
if (!validateFileName(fileName, error)) {
QMessageBox::warning(this, tr("Save attachment"), error);
return;
}

View File

@ -1,14 +1,12 @@
DatabaseWidget:!active,
DatabaseWidget #groupView:!active, DatabaseWidget #tagView:!active,
EntryPreviewWidget QLineEdit:!active, EntryPreviewWidget QTextEdit:!active,
EntryPreviewWidget TagsEdit:!active, QStatusBar:!active {
EntryPreviewWidget *:!active[blendIn="true"], QStatusBar:!active {
background-color: #404042;
}
DatabaseWidget:disabled,
DatabaseWidget #groupView:disabled, DatabaseWidget #tagView:disabled,
EntryPreviewWidget QLineEdit:disabled, EntryPreviewWidget QTextEdit:disabled,
EntryPreviewWidget TagsEdit:disabled, QStatusBar:disabled {
EntryPreviewWidget *:disabled[blendIn="true"], QStatusBar:disabled {
background-color: #424242;
}

View File

@ -1,14 +1,12 @@
DatabaseWidget:!active,
DatabaseWidget #groupView:!active, DatabaseWidget #tagView:!active,
EntryPreviewWidget QLineEdit:!active, EntryPreviewWidget QTextEdit:!active,
EntryPreviewWidget TagsEdit:!active, QStatusBar:!active {
EntryPreviewWidget *:!active[blendIn="true"], QStatusBar:!active {
background-color: #FCFCFC;
}
DatabaseWidget:disabled,
DatabaseWidget #groupView:disabled, DatabaseWidget #tagView:disabled,
EntryPreviewWidget QLineEdit:disabled, EntryPreviewWidget QTextEdit:disabled,
EntryPreviewWidget TagsEdit:disabled, QStatusBar:disabled {
EntryPreviewWidget *:disabled[blendIn="true"], QStatusBar:disabled {
background-color: #EDEDED;
}