summaryrefslogtreecommitdiffstats
path: root/framework/domain/messageparser.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-04-07 12:09:39 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-04-07 12:15:33 +0200
commit5880ba07757f85c41474d3eb6473facdcb468482 (patch)
tree6d162874246c29d4234f628efd3a80efd8510b6a /framework/domain/messageparser.cpp
parent5f2a3aa64cedc7f5517a1047dd699a286faf1898 (diff)
downloadkube-5880ba07757f85c41474d3eb6473facdcb468482.tar.gz
kube-5880ba07757f85c41474d3eb6473facdcb468482.zip
Replace the HTML view by a tree of QML views.
This allows us to do all the visual hints for the mail parts in qml, instead of HTML.
Diffstat (limited to 'framework/domain/messageparser.cpp')
-rw-r--r--framework/domain/messageparser.cpp127
1 files changed, 122 insertions, 5 deletions
diff --git a/framework/domain/messageparser.cpp b/framework/domain/messageparser.cpp
index 7089e2d8..cdaf9db4 100644
--- a/framework/domain/messageparser.cpp
+++ b/framework/domain/messageparser.cpp
@@ -26,9 +26,117 @@
26#include <QImage> 26#include <QImage>
27#include <QDebug> 27#include <QDebug>
28#include <QTime> 28#include <QTime>
29#include <QUrl>
29#include <MimeTreeParser/ObjectTreeParser> 30#include <MimeTreeParser/ObjectTreeParser>
30#include <MimeTreeParser/MessagePart> 31#include <MimeTreeParser/MessagePart>
31 32
33PartModel::PartModel(QSharedPointer<MimeTreeParser::MessagePart> partTree, QMap<QByteArray, QUrl> embeddedPartMap) : mPartTree(partTree), mEmbeddedPartMap(embeddedPartMap)
34{
35}
36
37QHash<int, QByteArray> PartModel::roleNames() const
38{
39 QHash<int, QByteArray> roles;
40 roles[Text] = "text";
41 roles[IsHtml] = "isHtml";
42 roles[IsEncrypted] = "isEncrypted";
43 roles[IsAttachment] = "isAttachment";
44 roles[HasContent] = "hasContent";
45 roles[Type] = "type";
46 return roles;
47}
48
49QModelIndex PartModel::index(int row, int column, const QModelIndex &parent) const
50{
51 // qDebug() << "index " << parent << row << column << mPartTree->messageParts().size();
52 if (!parent.isValid()) {
53 if (row < mPartTree->messageParts().size()) {
54 auto part = mPartTree->messageParts().at(row);
55 return createIndex(row, column, part.data());
56 }
57 } else {
58 auto part = static_cast<MimeTreeParser::MessagePart*>(parent.internalPointer());
59 auto subPart = part->messageParts().at(row);
60 return createIndex(row, column, subPart.data());
61 }
62 return QModelIndex();
63}
64
65QVariant PartModel::data(const QModelIndex &index, int role) const
66{
67 // qDebug() << "Getting data for index";
68 if (index.isValid()) {
69 auto part = static_cast<MimeTreeParser::MessagePart*>(index.internalPointer());
70 switch (role) {
71 case Text: {
72 // qDebug() << "Getting text: " << part->property("text").toString();
73 // FIXME: we should have a list per part, and not one for all parts.
74 auto text = part->property("htmlContent").toString();
75 for (const auto &cid : mEmbeddedPartMap.keys()) {
76 text.replace(QString("src=\"cid:%1\"").arg(QString(cid)), QString("src=\"%1\"").arg(mEmbeddedPartMap.value(cid).toString()));
77 }
78 return text;
79 }
80 case IsAttachment:
81 return part->property("attachment").toBool();
82 case IsEncrypted:
83 return part->property("isEncrypted").toBool();
84 case IsHtml:
85 return part->property("isHtml").toBool();
86 case HasContent:
87 return !part->property("htmlContent").toString().isEmpty();
88 case Type:
89 return part->metaObject()->className();
90 }
91 }
92 return QVariant();
93}
94
95QModelIndex PartModel::parent(const QModelIndex &index) const
96{
97 // qDebug() << "parent " << index;
98 if (index.isValid()) {
99 auto part = static_cast<MimeTreeParser::MessagePart*>(index.internalPointer());
100 auto parentPart = static_cast<MimeTreeParser::MessagePart*>(part->parentPart());
101 auto row = 0;//get the parents parent to find the index
102 if (!parentPart) {
103 parentPart = mPartTree.data();
104 }
105 int i = 0;
106 for (const auto &p : parentPart->messageParts()) {
107 if (p.data() == part) {
108 row = i;
109 break;
110 }
111 i++;
112 }
113 return createIndex(row, index.column(), parentPart);
114 }
115 return QModelIndex();
116}
117
118int PartModel::rowCount(const QModelIndex &parent) const
119{
120 // qDebug() << "Row count " << parent;
121 if (!parent.isValid()) {
122 // qDebug() << "Row count " << mPartTree->messageParts().size();
123 return mPartTree->messageParts().size();
124 } else {
125 auto part = static_cast<MimeTreeParser::MessagePart*>(parent.internalPointer());
126 if (part) {
127 return part->messageParts().size();
128 }
129 }
130 return 0;
131}
132
133int PartModel::columnCount(const QModelIndex &parent) const
134{
135 // qDebug() << "Column count " << parent;
136 return 1;
137}
138
139
32MessageParser::MessageParser(QObject *parent) 140MessageParser::MessageParser(QObject *parent)
33 : QObject(parent) 141 : QObject(parent)
34{ 142{
@@ -64,20 +172,29 @@ void MessageParser::setMessage(const QVariant &message)
64 ObjectTreeSource source(&htmlWriter, &cssHelper); 172 ObjectTreeSource source(&htmlWriter, &cssHelper);
65 MimeTreeParser::ObjectTreeParser otp(&source, mNodeHelper.get()); 173 MimeTreeParser::ObjectTreeParser otp(&source, mNodeHelper.get());
66 174
67 const auto partTree = otp.parseToTree(msg.data()).dynamicCast<MimeTreeParser::MessagePartList>(); 175 mPartTree = otp.parseToTree(msg.data()).dynamicCast<MimeTreeParser::MessagePart>();
68 176
69 htmlWriter.begin(QString()); 177 htmlWriter.begin(QString());
70 htmlWriter.queue(cssHelper.htmlHead(false)); 178 htmlWriter.queue(cssHelper.htmlHead(false));
71 179
72 if (partTree) { 180 if (mPartTree) {
73 partTree->fix(); 181 mPartTree->fix();
74 partTree->copyContentFrom(); 182 mPartTree->copyContentFrom();
75 partTree->html(false); 183 mPartTree->html(false);
76 } 184 }
77 185
78 htmlWriter.queue(QStringLiteral("</body></html>")); 186 htmlWriter.queue(QStringLiteral("</body></html>"));
79 htmlWriter.end(); 187 htmlWriter.end();
80 188
189 mEmbeddedPartMap = htmlWriter.embeddedParts();
81 mHtml = htmlWriter.html(); 190 mHtml = htmlWriter.html();
82 emit htmlChanged(); 191 emit htmlChanged();
83} 192}
193
194QAbstractItemModel *MessageParser::partTree() const
195{
196 qDebug() << "Getting partTree";
197 qDebug() << "Row count " << mPartTree->messageParts().size();
198 return new PartModel(mPartTree, mEmbeddedPartMap);
199}
200