Page Menu
Home
GnuPG
Search
Configure Global Search
Log In
Files
F22947987
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Size
24 KB
Subscribers
None
View Options
diff --git a/src/core/messageparser.cpp b/src/core/messageparser.cpp
index 85291b7..198152e 100644
--- a/src/core/messageparser.cpp
+++ b/src/core/messageparser.cpp
@@ -1,227 +1,247 @@
// SPDX-FileCopyrightText: 2016 Christian Mollekopf <mollekopf@kolabsys.com>
// SPDX-License-Identifier: LGPL-2.0-or-later
#include "messageparser.h"
#include "attachmentmodel.h"
#include "mimetreeparser_core_debug.h"
#include "objecttreeparser.h"
#include "partmodel.h"
#include <KLocalizedString>
#include <QElapsedTimer>
namespace
{
template<typename T>
const T *findHeader(KMime::Content *content, KMime::Content *protectedHeaderNode)
{
if (protectedHeaderNode) {
auto header = protectedHeaderNode->header<T>();
if (header) {
return header;
}
}
auto header = content->header<T>();
if (header || !content->parent()) {
return header;
}
return findHeader<T>(content->parent(), nullptr);
}
const KMime::Headers::Base *findHeader(KMime::Content *content, const char *headerType)
{
const auto header = content->headerByType(headerType);
if (header || !content->parent()) {
return header;
}
return findHeader(content->parent(), headerType);
}
+
+QString mailboxesToHtml(const KMime::Types::Mailbox::List &mailboxes)
+{
+ QStringList html;
+ for (const auto &mailbox : mailboxes) {
+ if (mailbox.hasName() && mailbox.hasAddress()) {
+ html << QStringLiteral("%1 <<a href=\"mailto:%2\">%2</a>>").arg(mailbox.name(), QString::fromUtf8(mailbox.address()));
+ } else if (!mailbox.hasName() && mailbox.hasAddress()) {
+ html << QStringLiteral("<a href=\"mailto:%2\">%2</a>").arg(QString::fromUtf8(mailbox.address()));
+ } else if (mailbox.hasName() && !mailbox.hasAddress()) {
+ html << mailbox.name();
+ } else {
+ html << i18nc("Missing cc or to field in an email", "Unknow");
+ }
+ }
+
+ return html.join(i18nc("list separator", ", "));
+}
}
class MessagePartPrivate
{
public:
std::shared_ptr<MimeTreeParser::ObjectTreeParser> mParser;
KMime::Message::Ptr mMessage;
KMime::Content *protectedHeaderNode = nullptr;
std::unique_ptr<KMime::Content> ownedContent;
};
MessageParser::MessageParser(QObject *parent)
: QObject(parent)
, d(std::unique_ptr<MessagePartPrivate>(new MessagePartPrivate))
{
}
MessageParser::~MessageParser()
{
}
KMime::Message::Ptr MessageParser::message() const
{
return d->mMessage;
}
void MessageParser::setMessage(const KMime::Message::Ptr message)
{
if (message == d->mMessage) {
return;
}
if (!message) {
qCWarning(MIMETREEPARSER_CORE_LOG) << Q_FUNC_INFO << "Empty message given";
return;
}
d->mMessage = message;
QElapsedTimer time;
time.start();
auto parser = std::make_shared<MimeTreeParser::ObjectTreeParser>();
parser->parseObjectTree(message.data());
qCDebug(MIMETREEPARSER_CORE_LOG) << "Message parsing took: " << time.elapsed();
parser->decryptParts();
qCDebug(MIMETREEPARSER_CORE_LOG) << "Message parsing and decryption/verification: " << time.elapsed();
d->mParser = parser;
const auto contentParts = parser->collectContentParts();
for (const auto &part : contentParts) {
if (!part->node()) {
continue;
}
const auto contentType = part->node()->contentType();
if (contentType && contentType->hasParameter(QStringLiteral("protected-headers"))) {
const auto contentDisposition = part->node()->contentDisposition();
// Check for legacy format for protected-headers
if (contentDisposition && contentDisposition->disposition() == KMime::Headers::CDinline) {
d->ownedContent = std::make_unique<KMime::Content>();
// we put the decoded content as encoded content of the new node
// as the header are inline in part->node()
d->ownedContent->setContent(part->node()->decodedContent());
d->ownedContent->parse();
d->protectedHeaderNode = d->ownedContent.get();
break;
}
d->protectedHeaderNode = part->node();
break;
}
}
Q_EMIT htmlChanged();
}
bool MessageParser::loaded() const
{
return bool{d->mParser};
}
QString MessageParser::structureAsString() const
{
if (!d->mParser) {
return {};
}
return d->mParser->structureAsString();
}
PartModel *MessageParser::parts() const
{
if (!d->mParser) {
return nullptr;
}
const auto model = new PartModel(d->mParser);
return model;
}
AttachmentModel *MessageParser::attachments() const
{
if (!d->mParser) {
return nullptr;
}
auto attachmentModel = new AttachmentModel(d->mParser);
attachmentModel->setParent(const_cast<MessageParser *>(this));
return attachmentModel;
}
QString MessageParser::subject() const
{
if (d->mMessage) {
const auto header = findHeader<KMime::Headers::Subject>(d->mMessage.get(), d->protectedHeaderNode);
if (header) {
return header->asUnicodeString();
}
}
return QString();
}
QString MessageParser::from() const
{
if (d->mMessage) {
const auto header = findHeader<KMime::Headers::From>(d->mMessage.get(), d->protectedHeaderNode);
- if (header) {
- return header->displayString();
+ if (!header) {
+ return {};
}
+ return mailboxesToHtml(header->mailboxes());
}
return QString();
}
QString MessageParser::sender() const
{
if (d->mMessage) {
const auto header = findHeader<KMime::Headers::Sender>(d->mMessage.get(), d->protectedHeaderNode);
- if (header) {
- return header->displayString();
+ if (!header) {
+ return {};
}
+ return mailboxesToHtml(header->mailboxes());
}
return QString();
}
QString MessageParser::to() const
{
if (d->mMessage) {
const auto header = findHeader<KMime::Headers::To>(d->mMessage.get(), d->protectedHeaderNode);
if (!header) {
return {};
}
- return header->displayString();
+ return mailboxesToHtml(header->mailboxes());
}
return QString();
}
QString MessageParser::cc() const
{
if (d->mMessage) {
const auto header = findHeader<KMime::Headers::Cc>(d->mMessage.get(), d->protectedHeaderNode);
if (!header) {
return {};
}
- return header->displayString();
+ return mailboxesToHtml(header->mailboxes());
}
return QString();
}
QString MessageParser::bcc() const
{
if (d->mMessage) {
const auto header = findHeader<KMime::Headers::Bcc>(d->mMessage.get(), d->protectedHeaderNode);
if (!header) {
return {};
}
- return header->displayString();
+ return mailboxesToHtml(header->mailboxes());
}
return QString();
}
QDateTime MessageParser::date() const
{
if (d->mMessage) {
const auto header = findHeader<KMime::Headers::Date>(d->mMessage.get(), d->protectedHeaderNode);
if (header) {
return header->dateTime();
}
}
return QDateTime();
}
diff --git a/src/widgets/messageviewer.cpp b/src/widgets/messageviewer.cpp
index 26c440c..aa684c5 100644
--- a/src/widgets/messageviewer.cpp
+++ b/src/widgets/messageviewer.cpp
@@ -1,408 +1,421 @@
// SPDX-FileCopyrightText: 2023 Carl Schwan <carl.schwan@gnupg.com>
// SPDX-License-Identifier: LGPL-2.0-or-later
#include "messageviewer.h"
#include "attachmentview_p.h"
#include "messagecontainerwidget_p.h"
#include "mimetreeparser_widgets_debug.h"
#include "urlhandler_p.h"
#include <KCalendarCore/Event>
#include <KCalendarCore/ICalFormat>
#include <KCalendarCore/Incidence>
#include <KMessageWidget>
#include <KLocalizedString>
#include <MimeTreeParserCore/AttachmentModel>
#include <MimeTreeParserCore/MessageParser>
#include <MimeTreeParserCore/ObjectTreeParser>
#include <MimeTreeParserCore/PartModel>
#include <QAction>
+#include <QDesktopServices>
#include <QFileDialog>
#include <QFormLayout>
#include <QGroupBox>
#include <QLabel>
#include <QMenu>
#include <QScrollArea>
#include <QSplitter>
#include <QStandardPaths>
#include <QVBoxLayout>
using namespace MimeTreeParser::Widgets;
class MessageViewer::Private
{
public:
Private(MessageViewer *q_ptr)
: q{q_ptr}
, messageWidget(new KMessageWidget(q_ptr))
{
createActions();
messageWidget->setCloseButtonVisible(true);
messageWidget->hide();
}
MessageViewer *q;
QVBoxLayout *layout = nullptr;
KMime::Message::Ptr message;
MessageParser parser;
QScrollArea *scrollArea = nullptr;
QFormLayout *formLayout = nullptr;
AttachmentView *attachmentView = nullptr;
MimeTreeParser::MessagePart::List selectedParts;
UrlHandler *urlHandler = nullptr;
KMessageWidget *const messageWidget = nullptr;
QAction *saveAttachmentAction = nullptr;
QAction *openAttachmentAction = nullptr;
QAction *importPublicKeyAction = nullptr;
void createActions()
{
saveAttachmentAction = new QAction(QIcon::fromTheme(QStringLiteral("document-save-as")), i18n("&Save Attachment As..."), q);
connect(saveAttachmentAction, &QAction::triggered, q, [this]() {
saveSelectedAttachments();
});
openAttachmentAction = new QAction(i18nc("to open", "Open"), q);
connect(openAttachmentAction, &QAction::triggered, q, [this]() {
openSelectedAttachments();
});
importPublicKeyAction = new QAction(i18nc("@action:inmenu", "Import public key"), q);
connect(importPublicKeyAction, &QAction::triggered, q, [this]() {
importPublicKey();
});
}
void openSelectedAttachments();
void saveSelectedAttachments();
void selectionChanged();
void showContextMenu();
void importPublicKey();
void recursiveBuildViewer(PartModel *parts, QVBoxLayout *layout, const QModelIndex &parent);
};
void MessageViewer::Private::openSelectedAttachments()
{
Q_ASSERT(selectedParts.count() >= 1);
for (const auto &part : std::as_const(selectedParts)) {
parser.attachments()->openAttachment(part);
}
}
void MessageViewer::Private::saveSelectedAttachments()
{
Q_ASSERT(selectedParts.count() >= 1);
for (const auto &part : std::as_const(selectedParts)) {
QString pname = part->filename();
if (pname.isEmpty()) {
pname = i18nc("Fallback when file has no name", "unnamed");
}
const QString path = QFileDialog::getSaveFileName(q, i18n("Save Attachment As"), pname);
parser.attachments()->saveAttachmentToPath(part, path);
}
}
void MessageViewer::Private::importPublicKey()
{
Q_ASSERT(selectedParts.count() == 1);
parser.attachments()->importPublicKey(selectedParts[0]);
}
void MessageViewer::Private::showContextMenu()
{
const int numberOfParts(selectedParts.count());
QMenu menu;
if (numberOfParts == 1) {
const QString mimetype = QString::fromLatin1(selectedParts.first()->mimeType());
if (mimetype == QLatin1String("application/pgp-keys")) {
menu.addAction(importPublicKeyAction);
}
}
menu.addAction(openAttachmentAction);
menu.addAction(saveAttachmentAction);
menu.exec(QCursor::pos());
}
void MessageViewer::Private::selectionChanged()
{
const QModelIndexList selectedRows = attachmentView->selectionModel()->selectedRows();
MimeTreeParser::MessagePart::List selectedParts;
selectedParts.reserve(selectedRows.count());
for (const QModelIndex &index : selectedRows) {
auto part = attachmentView->model()->data(index, AttachmentModel::AttachmentPartRole).value<MimeTreeParser::MessagePart::Ptr>();
selectedParts.append(part);
}
this->selectedParts = selectedParts;
}
MessageViewer::MessageViewer(QWidget *parent)
: QSplitter(Qt::Vertical, parent)
, d(std::make_unique<MessageViewer::Private>(this))
{
setObjectName(QStringLiteral("MessageViewerSplitter"));
setChildrenCollapsible(false);
setSizes({0});
addWidget(d->messageWidget);
auto headersArea = new QWidget(this);
headersArea->setSizePolicy(sizePolicy().horizontalPolicy(), QSizePolicy::Expanding);
addWidget(headersArea);
d->urlHandler = new UrlHandler(this);
d->formLayout = new QFormLayout(headersArea);
auto widget = new QWidget(this);
d->layout = new QVBoxLayout(widget);
d->layout->setSizeConstraint(QLayout::SetMinAndMaxSize);
d->layout->setObjectName(QStringLiteral("PartLayout"));
d->scrollArea = new QScrollArea(this);
d->scrollArea->setWidget(widget);
d->scrollArea->setWidgetResizable(true);
d->scrollArea->setBackgroundRole(QPalette::Base);
addWidget(d->scrollArea);
setStretchFactor(2, 2);
d->attachmentView = new AttachmentView(this);
d->attachmentView->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
addWidget(d->attachmentView);
connect(d->attachmentView, &AttachmentView::contextMenuRequested, this, [this] {
d->selectionChanged();
d->showContextMenu();
});
setMinimumSize(600, 600);
}
MessageViewer::~MessageViewer()
{
QLayoutItem *child;
while ((child = d->layout->takeAt(0)) != nullptr) {
delete child->widget();
delete child;
}
}
KMime::Message::Ptr MessageViewer::message() const
{
return d->parser.message();
}
void MessageViewer::Private::recursiveBuildViewer(PartModel *parts, QVBoxLayout *layout, const QModelIndex &parent)
{
for (int i = 0, count = parts->rowCount(parent); i < count; i++) {
const auto type = static_cast<PartModel::Types>(parts->data(parts->index(i, 0, parent), PartModel::TypeRole).toUInt());
const auto content = parts->data(parts->index(i, 0, parent), PartModel::ContentRole).toString();
const auto signatureInfo = parts->data(parts->index(i, 0, parent), PartModel::SignatureDetails).value<SignatureInfo>();
const auto isSigned = parts->data(parts->index(i, 0, parent), PartModel::IsSignedRole).toBool();
const auto signatureSecurityLevel =
static_cast<PartModel::SecurityLevel>(parts->data(parts->index(i, 0, parent), PartModel::SignatureSecurityLevelRole).toInt());
const auto encryptionInfo = parts->data(parts->index(i, 0, parent), PartModel::EncryptionDetails).value<SignatureInfo>();
const auto isEncrypted = parts->data(parts->index(i, 0, parent), PartModel::IsEncryptedRole).toBool();
const auto encryptionSecurityLevel =
static_cast<PartModel::SecurityLevel>(parts->data(parts->index(i, 0, parent), PartModel::EncryptionSecurityLevelRole).toInt());
const auto displayEncryptionInfo =
i == 0 || parts->data(parts->index(i - 1, 0, parent), PartModel::EncryptionDetails).value<SignatureInfo>().keyId != encryptionInfo.keyId;
const auto displaySignatureInfo =
i == 0 || parts->data(parts->index(i - 1, 0, parent), PartModel::SignatureDetails).value<SignatureInfo>().keyId != signatureInfo.keyId;
switch (type) {
case PartModel::Types::Plain: {
auto container = new MessageWidgetContainer(isSigned,
signatureInfo,
signatureSecurityLevel,
displaySignatureInfo,
isEncrypted,
encryptionInfo,
encryptionSecurityLevel,
displayEncryptionInfo,
urlHandler);
auto label = new QLabel(content);
label->setTextInteractionFlags(Qt::TextSelectableByMouse);
label->setWordWrap(true);
container->layout()->addWidget(label);
layout->addWidget(container);
break;
}
case PartModel::Types::Ical: {
auto container = new MessageWidgetContainer(isSigned,
signatureInfo,
signatureSecurityLevel,
displaySignatureInfo,
isEncrypted,
encryptionInfo,
encryptionSecurityLevel,
displayEncryptionInfo,
urlHandler);
KCalendarCore::ICalFormat format;
auto incidence = format.fromString(content);
auto widget = new QGroupBox(container);
widget->setTitle(i18n("Invitation"));
auto incidenceLayout = new QFormLayout(widget);
incidenceLayout->addRow(i18n("&Summary:"), new QLabel(incidence->summary()));
incidenceLayout->addRow(i18n("&Organizer:"), new QLabel(incidence->organizer().fullName()));
if (incidence->location().length() > 0) {
incidenceLayout->addRow(i18n("&Location:"), new QLabel(incidence->location()));
}
incidenceLayout->addRow(i18n("&Start date:"), new QLabel(incidence->dtStart().toLocalTime().toString()));
if (const auto event = incidence.dynamicCast<KCalendarCore::Event>()) {
incidenceLayout->addRow(i18n("&End date:"), new QLabel(event->dtEnd().toLocalTime().toString()));
}
if (incidence->description().length() > 0) {
incidenceLayout->addRow(i18n("&Details:"), new QLabel(incidence->description()));
}
container->layout()->addWidget(widget);
layout->addWidget(container);
break;
}
case PartModel::Types::Encapsulated: {
auto container = new MessageWidgetContainer(isSigned,
signatureInfo,
signatureSecurityLevel,
displaySignatureInfo,
isEncrypted,
encryptionInfo,
encryptionSecurityLevel,
displayEncryptionInfo,
urlHandler);
auto groupBox = new QGroupBox(container);
groupBox->setSizePolicy(QSizePolicy::MinimumExpanding, q->sizePolicy().verticalPolicy());
groupBox->setTitle(i18n("Encapsulated email"));
auto encapsulatedLayout = new QVBoxLayout(groupBox);
auto header = new QWidget(groupBox);
auto headerLayout = new QFormLayout(header);
const auto from = parts->data(parts->index(i, 0, parent), PartModel::SenderRole).toString();
const auto date = parts->data(parts->index(i, 0, parent), PartModel::DateRole).toDateTime();
headerLayout->addRow(i18n("From:"), new QLabel(from));
headerLayout->addRow(i18n("Date:"), new QLabel(date.toLocalTime().toString()));
encapsulatedLayout->addWidget(header);
recursiveBuildViewer(parts, encapsulatedLayout, parts->index(i, 0, parent));
container->layout()->addWidget(groupBox);
layout->addWidget(container);
break;
}
case PartModel::Types::Error: {
const auto errorString = parts->data(parts->index(i, 0, parent), PartModel::ErrorString).toString();
auto errorWidget = new KMessageWidget(errorString);
errorWidget->setCloseButtonVisible(false);
errorWidget->setMessageType(KMessageWidget::MessageType::Error);
QObject::connect(errorWidget, &KMessageWidget::linkActivated, errorWidget, [this, errorWidget](const QString &link) {
QUrl url(link);
if (url.path() == QStringLiteral("showCertificate")) {
urlHandler->handleClick(QUrl(link), errorWidget->window()->windowHandle());
}
});
errorWidget->setWordWrap(true);
layout->addWidget(errorWidget);
break;
}
default:
qCWarning(MIMETREEPARSER_WIDGET_LOG) << parts->data(parts->index(i, 0, parent), PartModel::ContentRole) << type;
}
}
}
+static QLabel *createLabel(const QString &content)
+{
+ const auto label = new QLabel(content);
+ label->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
+ label->setOpenExternalLinks(true);
+ return label;
+}
+
void MessageViewer::setMessage(const KMime::Message::Ptr message)
{
setUpdatesEnabled(false);
d->parser.setMessage(message);
connect(d->parser.attachments(), &AttachmentModel::info, this, [this](const QString &message) {
d->messageWidget->setMessageType(KMessageWidget::Information);
d->messageWidget->setText(message);
d->messageWidget->animatedShow();
});
connect(d->parser.attachments(), &AttachmentModel::errorOccurred, this, [this](const QString &message) {
d->messageWidget->setMessageType(KMessageWidget::Error);
d->messageWidget->setText(message);
d->messageWidget->animatedShow();
});
for (int i = d->formLayout->rowCount() - 1; i >= 0; i--) {
d->formLayout->removeRow(i);
}
if (!d->parser.subject().isEmpty()) {
- d->formLayout->addRow(i18n("&Subject:"), new QLabel(d->parser.subject()));
+ const auto label = new QLabel(d->parser.subject());
+ label->setTextFormat(Qt::PlainText);
+ d->formLayout->addRow(i18n("&Subject:"), label);
}
if (!d->parser.from().isEmpty()) {
- d->formLayout->addRow(i18n("&From:"), new QLabel(d->parser.from()));
+ d->formLayout->addRow(i18n("&From:"), createLabel(d->parser.from()));
}
if (!d->parser.sender().isEmpty() && d->parser.from() != d->parser.sender()) {
- d->formLayout->addRow(i18n("&Sender:"), new QLabel(d->parser.sender()));
+ d->formLayout->addRow(i18n("&Sender:"), createLabel(d->parser.sender()));
}
if (!d->parser.to().isEmpty()) {
- d->formLayout->addRow(i18n("&To:"), new QLabel(d->parser.to()));
+ d->formLayout->addRow(i18n("&To:"), createLabel(d->parser.to()));
}
if (!d->parser.cc().isEmpty()) {
- d->formLayout->addRow(i18n("&CC:"), new QLabel(d->parser.cc()));
+ const auto label = new QLabel(d->parser.cc());
+ label->setOpenExternalLinks(true);
+ d->formLayout->addRow(i18n("&CC:"), createLabel(d->parser.cc()));
}
if (!d->parser.bcc().isEmpty()) {
- d->formLayout->addRow(i18n("&BCC:"), new QLabel(d->parser.bcc()));
+ d->formLayout->addRow(i18n("&BCC:"), createLabel(d->parser.bcc()));
}
const auto parts = d->parser.parts();
QLayoutItem *child;
while ((child = d->layout->takeAt(0)) != nullptr) {
delete child->widget();
delete child;
}
d->recursiveBuildViewer(parts, d->layout, {});
d->layout->addStretch();
d->attachmentView->setModel(d->parser.attachments());
d->attachmentView->setVisible(d->parser.attachments()->rowCount() > 0);
connect(d->attachmentView->selectionModel(), &QItemSelectionModel::selectionChanged, this, [this] {
d->selectionChanged();
});
connect(d->attachmentView, &QAbstractItemView::doubleClicked, this, [this](const QModelIndex &) {
// Since this is only emitted if a valid index is double clicked we can assume
// that the first click of the double click set the selection accordingly.
d->openSelectedAttachments();
});
setUpdatesEnabled(true);
}
void MessageViewer::print(QPainter *painter, int width)
{
const auto oldSize = size();
resize(width - 30, oldSize.height());
d->scrollArea->setFrameShape(QFrame::NoFrame);
render(painter);
d->scrollArea->setFrameShape(QFrame::StyledPanel);
resize(oldSize);
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sat, May 10, 8:48 AM (1 d, 20 h)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
02/23/6d0291582d328d29b6908b53fddf
Attached To
rMTP MIME Tree Parser
Event Timeline
Log In to Comment