From 5ee5e4998a5a4c2f77913182833967b81ac3bb0b Mon Sep 17 00:00:00 2001 From: Kuznetsov Oleg Date: Sat, 1 Feb 2025 15:59:02 +0300 Subject: [PATCH] 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 --- src/core/EntryAttachments.cpp | 31 ++++++++++++++++++--- src/gui/entry/NewEntryAttachmentsDialog.cpp | 6 ++-- src/gui/styles/dark/darkstyle.qss | 6 ++-- src/gui/styles/light/lightstyle.qss | 6 ++-- 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/core/EntryAttachments.cpp b/src/core/EntryAttachments.cpp index e5c94b732..110d1e52b 100644 --- a/src/core/EntryAttachments.cpp +++ b/src/core/EntryAttachments.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -236,8 +237,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; } @@ -250,15 +253,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) diff --git a/src/gui/entry/NewEntryAttachmentsDialog.cpp b/src/gui/entry/NewEntryAttachmentsDialog.cpp index b8da3b791..7adbff332 100644 --- a/src/gui/entry/NewEntryAttachmentsDialog.cpp +++ b/src/gui/entry/NewEntryAttachmentsDialog.cpp @@ -33,9 +33,7 @@ NewEntryAttachmentsDialog::NewEntryAttachmentsDialog(QPointer 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; } diff --git a/src/gui/styles/dark/darkstyle.qss b/src/gui/styles/dark/darkstyle.qss index 2b0cfebc0..8da2d1a40 100644 --- a/src/gui/styles/dark/darkstyle.qss +++ b/src/gui/styles/dark/darkstyle.qss @@ -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; } diff --git a/src/gui/styles/light/lightstyle.qss b/src/gui/styles/light/lightstyle.qss index a2b6d7769..0eaf7ca88 100644 --- a/src/gui/styles/light/lightstyle.qss +++ b/src/gui/styles/light/lightstyle.qss @@ -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; }