Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve attachment handling by using a file watcher after you change … #11650

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 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 @@ -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;
}

Expand All @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions src/gui/entry/NewEntryAttachmentsDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 2 additions & 4 deletions src/gui/styles/dark/darkstyle.qss
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down
6 changes: 2 additions & 4 deletions src/gui/styles/light/lightstyle.qss
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down