From e452707fdfbd61be1e5633b516b653b7337e7865 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 29 May 2017 16:17:04 +0200 Subject: Reduced the messagetreeparser to aproximately what we actually require While in a much more managable state it's still not pretty. However, further refactoring can now gradually happen as we need to do further work on it. Things that should happen eventually: * Simplify the logic that creates the messageparts (we don't need the whole formatter plugin complexity) * Get rid of the nodehelper (let the parts hold the necessary data) * Get rid of partmetadata (let the part handleit) --- framework/src/domain/mime/partmodel.cpp | 179 ++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 framework/src/domain/mime/partmodel.cpp (limited to 'framework/src/domain/mime/partmodel.cpp') diff --git a/framework/src/domain/mime/partmodel.cpp b/framework/src/domain/mime/partmodel.cpp new file mode 100644 index 00000000..b86251ef --- /dev/null +++ b/framework/src/domain/mime/partmodel.cpp @@ -0,0 +1,179 @@ +/* + Copyright (c) 2016 Sandro Knauß + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library 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 Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#include "partmodel.h" + +#include +#include "htmlutils.h" + +#include +#include +#include + +class PartModelPrivate +{ +public: + PartModelPrivate(PartModel *q_ptr, const std::shared_ptr &parser); + ~PartModelPrivate(); + + void createTree(); + PartModel *q; + QVector mParts; + std::shared_ptr mParser; +}; + +PartModelPrivate::PartModelPrivate(PartModel *q_ptr, const std::shared_ptr &parser) + : q(q_ptr) + , mParser(parser) +{ + mParts = mParser->collectContentParts(); + qWarning() << "Collected content parts: " << mParts.size(); +} + +PartModelPrivate::~PartModelPrivate() +{ +} + +PartModel::PartModel(std::shared_ptr parser) + : d(std::unique_ptr(new PartModelPrivate(this, parser))) +{ +} + +PartModel::~PartModel() +{ +} + +QHash PartModel::roleNames() const +{ + QHash roles; + roles[TypeRole] = "type"; + roles[ContentRole] = "content"; + roles[IsEmbededRole] = "embeded"; + roles[IsEncryptedRole] = "encrypted"; + roles[IsSignedRole] = "signed"; + roles[SecurityLevelRole] = "securityLevel"; + roles[EncryptionErrorType] = "errorType"; + roles[EncryptionErrorString] = "errorString"; + roles[IsErrorRole] = "error"; + return roles; +} + +QModelIndex PartModel::index(int row, int column, const QModelIndex &parent) const +{ + if (row < 0 || column != 0) { + return QModelIndex(); + } + if (row < d->mParts.size()) { + return createIndex(row, column, d->mParts.at(row).data()); + } + return QModelIndex(); +} + +QVariant PartModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) { + switch (role) { + case Qt::DisplayRole: + return QString("root"); + case IsEmbededRole: + return false; + } + return QVariant(); + } + + if (index.internalPointer()) { + const auto messagePart = static_cast(index.internalPointer()); + qWarning() << "Found message part " << messagePart->metaObject()->className() << messagePart->partMetaData()->status << messagePart->error(); + Q_ASSERT(messagePart); + switch(role) { + case Qt::DisplayRole: + return QStringLiteral("Content%1"); + case TypeRole: { + if (messagePart->error()) { + return "error"; + } + //For simple html we don't need a browser + auto complexHtml = [&] { + if (messagePart->isHtml()) { + const auto text = messagePart->text(); + if (text.contains("error(); + case ContentRole: { + auto errorText = messagePart->partMetaData()->errorText; + if (!errorText.isEmpty()) { + return errorText; + } + const auto text = messagePart->isHtml() ? messagePart->htmlContent() : messagePart->text(); + if (messagePart->isHtml()) { + return d->mParser->resolveCidLinks(text); + } else { //We assume plain + //We alwas do richtext (so we get highlighted links and stuff). + return HtmlUtils::linkify(Qt::convertFromPlainText(text)); + } + return text; + } + case IsEncryptedRole: + return messagePart->encryptions().size() > 0; + case IsSignedRole: + return messagePart->signatures().size() > 0; + case EncryptionErrorType: + return messagePart->error(); + case EncryptionErrorString: + return messagePart->errorString(); + } + } + return QVariant(); +} + +QModelIndex PartModel::parent(const QModelIndex &index) const +{ + return QModelIndex(); +} + +int PartModel::rowCount(const QModelIndex &parent) const +{ + return d->mParts.count(); +} + +int PartModel::columnCount(const QModelIndex &parent) const +{ + return 1; +} -- cgit v1.2.3