-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add preview functionality for entry attachments
Supported types: - text - image Fixes #11506
- Loading branch information
w15dev
committed
Jan 8, 2025
1 parent
df95ad6
commit 47d1bb0
Showing
9 changed files
with
223 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright (C) 2021 KeePassXC Team <[email protected]> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 2 or (at your option) | ||
* version 3 of the License. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "PreviewEntryAttachmentsDialog.h" | ||
#include "core/EntryAttachments.h" | ||
#include "ui_EntryAttachmentsDialog.h" | ||
|
||
#include <QDialogButtonBox> | ||
#include <QMimeDatabase> | ||
#include <QTextCursor> | ||
#include <QtGlobal> | ||
|
||
PreviewEntryAttachmentsDialog::PreviewEntryAttachmentsDialog(QPointer<EntryAttachments> attachments, QWidget* parent) | ||
: QDialog(parent) | ||
, m_attachments(std::move(attachments)) | ||
, m_ui(new Ui::EntryAttachmentsDialog) | ||
{ | ||
Q_ASSERT(m_attachments); | ||
|
||
m_ui->setupUi(this); | ||
|
||
setWindowTitle(tr("Preview entry attachment")); | ||
|
||
m_ui->titleEdit->setReadOnly(true); | ||
m_ui->attachmentTextEdit->setReadOnly(true); | ||
|
||
m_ui->dialogButtons->clear(); | ||
m_ui->dialogButtons->addButton(QDialogButtonBox::Close); | ||
|
||
connect(m_ui->dialogButtons, SIGNAL(rejected()), this, SLOT(reject())); | ||
} | ||
|
||
PreviewEntryAttachmentsDialog::~PreviewEntryAttachmentsDialog() = default; | ||
|
||
void PreviewEntryAttachmentsDialog::setAttachment(const QString& name) | ||
{ | ||
m_name = name; | ||
m_type = attachmentType(m_name); | ||
|
||
m_ui->titleEdit->setText(m_name); | ||
|
||
update(); | ||
} | ||
|
||
void PreviewEntryAttachmentsDialog::update() | ||
{ | ||
if (m_type == AttachmentType::Unknown) { | ||
updateTextAttachment(tr("No preview available").toUtf8()); | ||
} else if (const auto data = m_attachments->value(m_name); m_type == AttachmentType::Image) { | ||
updateImageAttachment(data); | ||
} else if (m_type == AttachmentType::PlantText) { | ||
updateTextAttachment(data); | ||
} | ||
} | ||
|
||
void PreviewEntryAttachmentsDialog::updateTextAttachment(const QByteArray& data) | ||
{ | ||
m_ui->attachmentTextEdit->setPlainText(QString::fromUtf8(data)); | ||
} | ||
|
||
void PreviewEntryAttachmentsDialog::updateImageAttachment(const QByteArray& data) | ||
{ | ||
QImage image{}; | ||
image.loadFromData(data); | ||
|
||
m_ui->attachmentTextEdit->clear(); | ||
auto cursor = m_ui->attachmentTextEdit->textCursor(); | ||
|
||
cursor.insertImage(image.scaled(m_ui->attachmentTextEdit->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation)); | ||
} | ||
|
||
PreviewEntryAttachmentsDialog::AttachmentType PreviewEntryAttachmentsDialog::attachmentType(const QString& name) const | ||
{ | ||
const auto data = m_attachments->value(name); | ||
|
||
QMimeDatabase mimeDb{}; | ||
const auto mime = mimeDb.mimeTypeForData(data); | ||
|
||
if (auto mimeName = mime.name(); mimeName.startsWith("image/")) { | ||
return AttachmentType::Image; | ||
} else if (mimeName.startsWith("text/")) { | ||
return AttachmentType::PlantText; | ||
} | ||
|
||
return AttachmentType::Unknown; | ||
} | ||
|
||
void PreviewEntryAttachmentsDialog::resizeEvent(QResizeEvent* event) | ||
{ | ||
QDialog::resizeEvent(event); | ||
|
||
if (m_type == AttachmentType::Image) { | ||
update(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright (C) 2021 KeePassXC Team <[email protected]> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 2 or (at your option) | ||
* version 3 of the License. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef PREVIEWENTRYATTACHMENTSWIDGET_H | ||
#define PREVIEWENTRYATTACHMENTSWIDGET_H | ||
|
||
#include <QDialog> | ||
#include <QPointer> | ||
|
||
namespace Ui | ||
{ | ||
class EntryAttachmentsDialog; | ||
} | ||
|
||
class QByteArray; | ||
class EntryAttachments; | ||
|
||
class PreviewEntryAttachmentsDialog : public QDialog | ||
{ | ||
Q_OBJECT | ||
public: | ||
explicit PreviewEntryAttachmentsDialog(QPointer<EntryAttachments> attachments, QWidget* parent = nullptr); | ||
~PreviewEntryAttachmentsDialog() override; | ||
|
||
void setAttachment(const QString& name); | ||
|
||
private: | ||
enum class AttachmentType | ||
{ | ||
Image, | ||
PlantText, | ||
Unknown | ||
}; | ||
|
||
void resizeEvent(QResizeEvent* event) override; | ||
|
||
AttachmentType attachmentType(const QString& name) const; | ||
|
||
void update(); | ||
void updateTextAttachment(const QByteArray& data); | ||
void updateImageAttachment(const QByteArray& data); | ||
private: | ||
QPointer<EntryAttachments> m_attachments; | ||
|
||
QScopedPointer<Ui::EntryAttachmentsDialog> m_ui; | ||
|
||
QString m_name; | ||
AttachmentType m_type{AttachmentType::Unknown}; | ||
}; | ||
|
||
#endif // PREVIEWENTRYATTACHMENTSWIDGET_H |