From 1974c19eadd497e355ac985a00d0571f3e6c7712 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20Knau=C3=9F?= Date: Tue, 11 Oct 2016 16:18:50 +0200 Subject: create model for new mailviewer --- framework/domain/messageparser_old.cpp | 140 +++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 framework/domain/messageparser_old.cpp (limited to 'framework/domain/messageparser_old.cpp') diff --git a/framework/domain/messageparser_old.cpp b/framework/domain/messageparser_old.cpp new file mode 100644 index 00000000..a364c8ab --- /dev/null +++ b/framework/domain/messageparser_old.cpp @@ -0,0 +1,140 @@ +/* + 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 "messageparser.h" +#include "mimetreeparser/interface.h" + +PartModel::PartModel(QSharedPointer partTree, std::shared_ptr parser) + : mPartTree(partTree) + , mParser(parser) +{ +} + +QHash PartModel::roleNames() const +{ + QHash roles; + roles[Text] = "text"; + roles[IsHtml] = "isHtml"; + roles[IsHidden] = "isHidden"; + roles[IsEncrypted] = "isEncrypted"; + roles[IsAttachment] = "isAttachment"; + roles[HasContent] = "hasContent"; + roles[Type] = "type"; + roles[IsHidden] = "isHidden"; + return roles; +} + +QModelIndex PartModel::index(int row, int column, const QModelIndex &parent) const +{ + // qDebug() << "index " << parent << row << column << mPartTree->subParts().size(); + if (!parent.isValid()) { + if (row < mPartTree->subParts().size()) { + auto part = mPartTree->subParts().at(row); + return createIndex(row, column, part.data()); + } + } else { + auto part = static_cast(parent.internalPointer()); + auto subPart = part->subParts().at(row); + return createIndex(row, column, subPart.data()); + } + return QModelIndex(); +} + +QVariant PartModel::data(const QModelIndex &index, int role) const +{ + // qDebug() << "Getting data for index"; + if (index.isValid()) { + auto part = static_cast(index.internalPointer()); + switch (role) { + case Text: { + // qDebug() << "Getting text: " << part->property("text").toString(); + // FIXME: we should have a list per part, and not one for all parts. + auto text = part->property("htmlContent").toString(); + auto rx = QRegExp("src=(\"|')cid:([^\1]*)\1"); + int pos = 0; + while ((pos = rx.indexIn(text, pos)) != -1) { + auto repl = mParser->getPart(rx.cap(2).toUtf8()); + if (repl.isValid()) { + text.replace(rx.cap(0), QString("src=\"%1\"").arg(repl.toString())); + } + pos += rx.matchedLength(); + } + return text; + } + case IsAttachment: + return part->property("attachment").toBool(); + case IsEncrypted: + return part->property("isEncrypted").toBool(); + case IsHtml: + return part->property("isHtml").toBool(); + case HasContent: + return !part->property("htmlContent").toString().isEmpty(); + case Type: + return part->metaObject()->className(); + case IsHidden: + return false; + //return part->property("isHidden").toBool(); + + } + } + return QVariant(); +} + +QModelIndex PartModel::parent(const QModelIndex &index) const +{ + // qDebug() << "parent " << index; + if (index.isValid()) { + auto part = static_cast(index.internalPointer()); + auto parentPart = static_cast(part->parentPart()); + auto row = 0;//get the parents parent to find the index + if (!parentPart) { + parentPart = mPartTree.data(); + } + int i = 0; + for (const auto &p : parentPart->subParts()) { + if (p.data() == part) { + row = i; + break; + } + i++; + } + return createIndex(row, index.column(), parentPart); + } + return QModelIndex(); +} + +int PartModel::rowCount(const QModelIndex &parent) const +{ + // qDebug() << "Row count " << parent; + if (!parent.isValid()) { + // qDebug() << "Row count " << mPartTree->subParts().size(); + return mPartTree->subParts().size(); + } else { + auto part = static_cast(parent.internalPointer()); + if (part) { + return part->subParts().size(); + } + } + return 0; +} + +int PartModel::columnCount(const QModelIndex &parent) const +{ + // qDebug() << "Column count " << parent; + return 1; +} + -- cgit v1.2.3 From 1fe0ab05ae2bfe6505736f598bd535a71fb86a6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20Knau=C3=9F?= Date: Mon, 17 Oct 2016 15:35:49 +0200 Subject: replace cid links with actuall content of the images --- framework/domain/messageparser_old.cpp | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'framework/domain/messageparser_old.cpp') diff --git a/framework/domain/messageparser_old.cpp b/framework/domain/messageparser_old.cpp index a364c8ab..a4247d8c 100644 --- a/framework/domain/messageparser_old.cpp +++ b/framework/domain/messageparser_old.cpp @@ -64,14 +64,25 @@ QVariant PartModel::data(const QModelIndex &index, int role) const // qDebug() << "Getting text: " << part->property("text").toString(); // FIXME: we should have a list per part, and not one for all parts. auto text = part->property("htmlContent").toString(); - auto rx = QRegExp("src=(\"|')cid:([^\1]*)\1"); - int pos = 0; + const auto rx = QRegExp("(src)\\s*=\\s*(\"|')(cid:[^\"']+)\\2"); + int pos = 0; while ((pos = rx.indexIn(text, pos)) != -1) { - auto repl = mParser->getPart(rx.cap(2).toUtf8()); - if (repl.isValid()) { - text.replace(rx.cap(0), QString("src=\"%1\"").arg(repl.toString())); - } + const auto link = QUrl(rx.cap(3).toUtf8()); pos += rx.matchedLength(); + const auto repl = mParser->getPart(link); + if (!repl) { + continue; + } + const auto content = repl->content(); + if(content.size() < 1) { + continue; + } + const auto mailMime = content.first()->mailMime(); + const auto mimetype = mailMime->mimetype().name(); + if (mimetype.startsWith("image/")) { + const auto data = content.first()->content(); + text.replace(rx.cap(0), QString("src=\"data:%1;base64,%2\"").arg(mimetype, QString::fromLatin1(data.toBase64()))); + } } return text; } -- cgit v1.2.3