Implement "Overwrite attachment" confirmation dialog.

This commit is contained in:
Patrick Sean Klein 2021-10-24 17:20:04 +02:00 committed by Jonathan White
parent 55f2bd41aa
commit 20db504c3a
3 changed files with 42 additions and 17 deletions

View File

@ -3533,6 +3533,15 @@ Do you want to save the changes to your database?</source>
Error: %1</source> Error: %1</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>Confirm Overwrite Attachment</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Attachment &quot;%1&quot; already exists.
Would you like to overwrite the existing attachment?</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>EntryAttributesModel</name> <name>EntryAttributesModel</name>

View File

@ -152,7 +152,7 @@ void EntryAttachmentsWidget::insertAttachments()
if (filenames.isEmpty()) { if (filenames.isEmpty()) {
return; return;
} }
const auto confirmedFileNames = confirmLargeAttachments(filenames); const auto confirmedFileNames = confirmAttachmentSelection(filenames);
if (confirmedFileNames.isEmpty()) { if (confirmedFileNames.isEmpty()) {
return; return;
} }
@ -342,28 +342,44 @@ bool EntryAttachmentsWidget::insertAttachments(const QStringList& filenames, QSt
return errors.isEmpty(); return errors.isEmpty();
} }
QStringList EntryAttachmentsWidget::confirmLargeAttachments(const QStringList& filenames) QStringList EntryAttachmentsWidget::confirmAttachmentSelection(const QStringList& filenames)
{ {
const QString confirmation(tr("%1 is a big file (%2 MB).\nYour database may get very large and reduce "
"performance.\n\nAre you sure to add this file?"));
QStringList confirmedFileNames; QStringList confirmedFileNames;
for (const auto& file : filenames) { for (const auto& file : filenames) {
QFileInfo fileInfo(file); const QFileInfo fileInfo(file);
double size = fileInfo.size() / (1024.0 * 1024.0); auto fileName = fileInfo.fileName();
// Ask for confirmation before adding files over 5 MB in size
if (size > 5.0) { // Ask for confirmation if overwriting an existing attachment
auto fileName = fileInfo.fileName(); if (m_entryAttachments->hasKey(fileName)) {
auto result = MessageBox::question(this, auto result = MessageBox::question(this,
tr("Confirm Attachment"), tr("Confirm Overwrite Attachment"),
confirmation.arg(fileName, QString::number(size, 'f', 1)), tr("Attachment \"%1\" already exists. \n"
MessageBox::Yes | MessageBox::No, "Would you like to overwrite the existing attachment?")
.arg(fileName),
MessageBox::Overwrite | MessageBox::No,
MessageBox::No); MessageBox::No);
if (result == MessageBox::Yes) { if (result == MessageBox::No) {
confirmedFileNames << file; continue;
} }
} else {
confirmedFileNames << file;
} }
// Ask for confirmation before adding files over 5 MB in size
double size = fileInfo.size() / (1024.0 * 1024.0);
if (size > 5.0) {
auto result =
MessageBox::question(this,
tr("Confirm Attachment"),
tr("%1 is a big file (%2 MB).\nYour database may get very large and reduce "
"performance.\n\nAre you sure to add this file?")
.arg(fileName, QString::number(size, 'f', 1)),
MessageBox::Yes | MessageBox::No,
MessageBox::No);
if (result == MessageBox::No) {
continue;
}
}
confirmedFileNames << file;
} }
return confirmedFileNames; return confirmedFileNames;

View File

@ -69,7 +69,7 @@ private slots:
private: private:
bool insertAttachments(const QStringList& fileNames, QString& errorMessage); bool insertAttachments(const QStringList& fileNames, QString& errorMessage);
QStringList confirmLargeAttachments(const QStringList& filenames); QStringList confirmAttachmentSelection(const QStringList& filenames);
bool eventFilter(QObject* watched, QEvent* event) override; bool eventFilter(QObject* watched, QEvent* event) override;