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

Implement "Overwrite attachment" confirmation dialog. #7083

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
9 changes: 9 additions & 0 deletions share/translations/keepassxc_en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3533,6 +3533,15 @@ Do you want to save the changes to your database?</source>
Error: %1</source>
<translation type="unfinished"></translation>
</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>
<name>EntryAttributesModel</name>
Expand Down
48 changes: 32 additions & 16 deletions src/gui/entry/EntryAttachmentsWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void EntryAttachmentsWidget::insertAttachments()
if (filenames.isEmpty()) {
return;
}
const auto confirmedFileNames = confirmLargeAttachments(filenames);
const auto confirmedFileNames = confirmAttachmentSelection(filenames);
if (confirmedFileNames.isEmpty()) {
return;
}
Expand Down Expand Up @@ -342,28 +342,44 @@ bool EntryAttachmentsWidget::insertAttachments(const QStringList& filenames, QSt
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;
for (const auto& file : filenames) {
QFileInfo fileInfo(file);
double size = fileInfo.size() / (1024.0 * 1024.0);
// Ask for confirmation before adding files over 5 MB in size
if (size > 5.0) {
auto fileName = fileInfo.fileName();
const QFileInfo fileInfo(file);
auto fileName = fileInfo.fileName();

// Ask for confirmation if overwriting an existing attachment
if (m_entryAttachments->hasKey(fileName)) {
auto result = MessageBox::question(this,
tr("Confirm Attachment"),
confirmation.arg(fileName, QString::number(size, 'f', 1)),
MessageBox::Yes | MessageBox::No,
tr("Confirm Overwrite Attachment"),
tr("Attachment \"%1\" already exists. \n"
"Would you like to overwrite the existing attachment?")
.arg(fileName),
MessageBox::Overwrite | MessageBox::No,
MessageBox::No);
if (result == MessageBox::Yes) {
confirmedFileNames << file;
if (result == MessageBox::No) {
continue;
}
}

// 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;
}
} else {
confirmedFileNames << file;
}

confirmedFileNames << file;
}

return confirmedFileNames;
Expand Down
2 changes: 1 addition & 1 deletion src/gui/entry/EntryAttachmentsWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private slots:
private:
bool insertAttachments(const QStringList& fileNames, QString& errorMessage);

QStringList confirmLargeAttachments(const QStringList& filenames);
QStringList confirmAttachmentSelection(const QStringList& filenames);

bool eventFilter(QObject* watched, QEvent* event) override;

Expand Down