Issue warning before allowing large attachments

Fixes #3782
This commit is contained in:
Prabesh Paudel 2020-04-30 23:16:02 -04:00 committed by Jonathan White
parent a83345d136
commit 443614a375
2 changed files with 35 additions and 4 deletions

View File

@ -141,15 +141,17 @@ void EntryAttachmentsWidget::insertAttachments()
defaultDirPath = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation).first(); defaultDirPath = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation).first();
} }
const QStringList filenames = fileDialog()->getOpenFileNames(this, tr("Select files"), defaultDirPath); const auto filenames = fileDialog()->getOpenFileNames(this, tr("Select files"), defaultDirPath);
if (filenames.isEmpty()) { if (filenames.isEmpty()) {
return; return;
} }
const auto confirmedFileNames = confirmLargeAttachments(filenames);
if (confirmedFileNames.isEmpty()) {
return;
}
config()->set(Config::LastAttachmentDir, QFileInfo(filenames.first()).absolutePath()); config()->set(Config::LastAttachmentDir, QFileInfo(filenames.first()).absolutePath());
QString errorMessage; QString errorMessage;
if (!insertAttachments(filenames, errorMessage)) { if (!insertAttachments(confirmedFileNames, errorMessage)) {
errorOccurred(errorMessage); errorOccurred(errorMessage);
} }
emit widgetUpdated(); emit widgetUpdated();
@ -353,6 +355,33 @@ bool EntryAttachmentsWidget::openAttachment(const QModelIndex& index, QString& e
return true; return true;
} }
QStringList EntryAttachmentsWidget::confirmLargeAttachments(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();
auto result = MessageBox::question(this,
tr("Confirm Attachment"),
confirmation.arg(fileName, QString::number(size, 'f', 1)),
MessageBox::Yes | MessageBox::No,
MessageBox::No);
if (result == MessageBox::Yes) {
confirmedFileNames << file;
}
} else {
confirmedFileNames << file;
}
}
return confirmedFileNames;
}
bool EntryAttachmentsWidget::eventFilter(QObject* watched, QEvent* e) bool EntryAttachmentsWidget::eventFilter(QObject* watched, QEvent* e)
{ {
if (watched == m_ui->attachmentsView->viewport() && !isReadOnly()) { if (watched == m_ui->attachmentsView->viewport() && !isReadOnly()) {

View File

@ -54,6 +54,8 @@ private:
bool insertAttachments(const QStringList& fileNames, QString& errorMessage); bool insertAttachments(const QStringList& fileNames, QString& errorMessage);
bool openAttachment(const QModelIndex& index, QString& errorMessage); bool openAttachment(const QModelIndex& index, QString& errorMessage);
QStringList confirmLargeAttachments(const QStringList& filenames);
bool eventFilter(QObject* watched, QEvent* event) override; bool eventFilter(QObject* watched, QEvent* event) override;
QScopedPointer<Ui::EntryAttachmentsWidget> m_ui; QScopedPointer<Ui::EntryAttachmentsWidget> m_ui;