Skip to content

Commit

Permalink
Add Open and Save functionality to attachment preview dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
w15dev committed Jan 11, 2025
1 parent deed178 commit 66e0e43
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
4 changes: 4 additions & 0 deletions share/translations/keepassxc_en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7170,6 +7170,10 @@ Do you want to overwrite it?</source>
<source>No preview available</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Open</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>QMessageBox</name>
Expand Down
6 changes: 6 additions & 0 deletions src/gui/entry/EntryAttachmentsWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,13 @@ void EntryAttachmentsWidget::previewAttachments()
PreviewEntryAttachmentsDialog previewDialog{m_entryAttachments};
previewDialog.setAttachment(m_attachmentsModel->keyByIndex(index));

connect(&previewDialog, SIGNAL(openAttachment(QString)), SLOT(openSelectedAttachments()));
connect(&previewDialog, SIGNAL(saveAttachment(QString)), SLOT(saveSelectedAttachments()));

previewDialog.exec();

// Set focus back to the widget to allow keyboard navigation
setFocus();
}

void EntryAttachmentsWidget::removeSelectedAttachments()
Expand Down
38 changes: 33 additions & 5 deletions src/gui/entry/PreviewEntryAttachmentsDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

#include <QDialogButtonBox>
#include <QMimeDatabase>
#include <QPushButton>
#include <QTextCursor>
#include <QtDebug>


PreviewEntryAttachmentsDialog::PreviewEntryAttachmentsDialog(QPointer<EntryAttachments> attachments, QWidget* parent)
: QDialog(parent)
, m_attachments(std::move(attachments))
Expand All @@ -35,18 +35,46 @@ PreviewEntryAttachmentsDialog::PreviewEntryAttachmentsDialog(QPointer<EntryAttac
m_ui->setupUi(this);

setWindowTitle(tr("Preview entry attachment"));
// Disable the help button in the title bar
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);

m_ui->titleEdit->setReadOnly(true);
m_ui->attachmentTextEdit->setReadOnly(true);
m_ui->errorLabel->setVisible(false);
connect(m_attachments, &EntryAttachments::keyModified, [this](const QString& name) {
if (m_name == name) {
update();
}
});

enableReadOnlyMode();
initDialogButtons();
}

PreviewEntryAttachmentsDialog::~PreviewEntryAttachmentsDialog() = default;

void PreviewEntryAttachmentsDialog::initDialogButtons()
{
m_ui->dialogButtons->clear();
m_ui->dialogButtons->addButton(QDialogButtonBox::Close);
m_ui->dialogButtons->addButton(QDialogButtonBox::Save);

auto button = m_ui->dialogButtons->addButton(QDialogButtonBox::Open);
button->setText(tr("Open"));

connect(m_ui->dialogButtons, SIGNAL(rejected()), this, SLOT(reject()));
connect(m_ui->dialogButtons, &QDialogButtonBox::clicked, [this](QAbstractButton* button) {
if (m_ui->dialogButtons->standardButton(button) == QDialogButtonBox::Open) {
emit openAttachment(m_name);
} else if (m_ui->dialogButtons->standardButton(button) == QDialogButtonBox::Save) {
emit saveAttachment(m_name);
}
});
}

PreviewEntryAttachmentsDialog::~PreviewEntryAttachmentsDialog() = default;
void PreviewEntryAttachmentsDialog::enableReadOnlyMode()
{
m_ui->titleEdit->setReadOnly(true);
m_ui->attachmentTextEdit->setReadOnly(true);
m_ui->errorLabel->setVisible(false);
}

void PreviewEntryAttachmentsDialog::setAttachment(const QString& name)
{
Expand Down
7 changes: 7 additions & 0 deletions src/gui/entry/PreviewEntryAttachmentsDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ class PreviewEntryAttachmentsDialog : public QDialog

void setAttachment(const QString& name);

Q_SIGNALS:
void openAttachment(const QString& name);
void saveAttachment(const QString& name);

private:
void initDialogButtons();
void enableReadOnlyMode();

void resizeEvent(QResizeEvent* event) override;

core::MimeType attachmentType(const QString& name) const;
Expand Down
6 changes: 3 additions & 3 deletions tests/TestMimeTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ QTEST_GUILESS_MAIN(TestMimeType)

void TestMimeType::testMimePlantText()
{
const std::array TextMimeTypes = {
const std::array<QString, 14> TextMimeTypes = {
"text/plain", // Plain text
"text/html", // HTML documents
"text/css", // CSS stylesheets
Expand All @@ -32,7 +32,7 @@ void TestMimeType::testMimePlantText()

void TestMimeType::testMimeImage()
{
constexpr static std::array ImageMimeTypes = {
const std::array<QString, 6> ImageMimeTypes = {
"image/jpeg", // JPEG images
"image/png", // PNG images
"image/gif", // GIF images
Expand All @@ -48,7 +48,7 @@ void TestMimeType::testMimeImage()

void TestMimeType::testMimeUnknown()
{
constexpr static std::array UnknownMimeTypes = {
const std::array<QString, 22> UnknownMimeTypes = {
"audio/mpeg", // MPEG audio files
"video/mp4", // MP4 video files
"application/pdf", // PDF documents
Expand Down

0 comments on commit 66e0e43

Please sign in to comment.