summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--framework/domain/messageparser.h25
-rw-r--r--framework/domain/messageparser_new.cpp153
-rw-r--r--framework/domain/modeltest.cpp2
3 files changed, 44 insertions, 136 deletions
diff --git a/framework/domain/messageparser.h b/framework/domain/messageparser.h
index 55c884f9..559fa6f9 100644
--- a/framework/domain/messageparser.h
+++ b/framework/domain/messageparser.h
@@ -98,31 +98,6 @@ private:
98}; 98};
99 99
100 100
101class NewContentModel : public QAbstractItemModel {
102 Q_OBJECT
103public:
104 NewContentModel (const PartPtr &part, const std::shared_ptr<Parser> &parser);
105
106public:
107 enum Roles {
108 TypeRole = Qt::UserRole + 1,
109 ContentRole,
110 IsEmbededRole,
111 SecurityLevelRole
112 };
113
114 QHash<int, QByteArray> roleNames() const Q_DECL_OVERRIDE;
115 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
116 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
117 QModelIndex parent(const QModelIndex &index) const Q_DECL_OVERRIDE;
118 int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
119 int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
120
121private:
122 const PartPtr &mPart;
123 const std::shared_ptr<Parser> &mParser;
124};
125
126class NewModel : public QAbstractItemModel { 101class NewModel : public QAbstractItemModel {
127 Q_OBJECT 102 Q_OBJECT
128public: 103public:
diff --git a/framework/domain/messageparser_new.cpp b/framework/domain/messageparser_new.cpp
index da5ab2e7..57f8172f 100644
--- a/framework/domain/messageparser_new.cpp
+++ b/framework/domain/messageparser_new.cpp
@@ -29,6 +29,11 @@ Q_DECLARE_METATYPE(Encryption *)
29class Entry 29class Entry
30{ 30{
31public: 31public:
32 Entry()
33 : mParent(nullptr)
34 {
35 }
36
32 ~Entry() 37 ~Entry()
33 { 38 {
34 foreach(auto child, mChildren) { 39 foreach(auto child, mChildren) {
@@ -36,7 +41,7 @@ public:
36 } 41 }
37 mChildren.clear(); 42 mChildren.clear();
38 } 43 }
39 44
40 void addChild(Entry *entry) 45 void addChild(Entry *entry)
41 { 46 {
42 mChildren.append(entry); 47 mChildren.append(entry);
@@ -68,6 +73,7 @@ class NewModelPrivate
68{ 73{
69public: 74public:
70 NewModelPrivate(NewModel *q_ptr, const std::shared_ptr<Parser> &parser); 75 NewModelPrivate(NewModel *q_ptr, const std::shared_ptr<Parser> &parser);
76 ~NewModelPrivate();
71 77
72 void createTree(); 78 void createTree();
73 Entry *addSignatures(Entry *parent, QVector<Signature::Ptr> signatures); 79 Entry *addSignatures(Entry *parent, QVector<Signature::Ptr> signatures);
@@ -88,10 +94,9 @@ public:
88 94
89 NewModel *q; 95 NewModel *q;
90 QVector<Part::Ptr> mParts; 96 QVector<Part::Ptr> mParts;
91 Entry *mRoot; 97 std::unique_ptr<Entry> mRoot;
92 98
93 std::shared_ptr<Parser> mParser; 99 std::shared_ptr<Parser> mParser;
94 QMap<Part *, std::shared_ptr<NewContentModel>> mContentMap;
95private: 100private:
96 QMap<std::shared_ptr<Signature>, QSharedPointer<QVariant>> mSignatureMap; 101 QMap<std::shared_ptr<Signature>, QSharedPointer<QVariant>> mSignatureMap;
97 QMap<std::shared_ptr<Encryption>, QSharedPointer<QVariant>> mEncryptionMap; 102 QMap<std::shared_ptr<Encryption>, QSharedPointer<QVariant>> mEncryptionMap;
@@ -101,16 +106,18 @@ private:
101 106
102NewModelPrivate::NewModelPrivate(NewModel *q_ptr, const std::shared_ptr<Parser> &parser) 107NewModelPrivate::NewModelPrivate(NewModel *q_ptr, const std::shared_ptr<Parser> &parser)
103 : q(q_ptr) 108 : q(q_ptr)
109 , mRoot(std::unique_ptr<Entry>(new Entry))
104 , mParser(parser) 110 , mParser(parser)
105{ 111{
106 mParts = mParser->collectContentParts(); 112 mParts = mParser->collectContentParts();
107 foreach(const auto &part, mParts) {
108 mContentMap.insert(part.get(), std::shared_ptr<NewContentModel>(new NewContentModel(part, mParser)));
109 }
110 createTree(); 113 createTree();
111} 114}
112 115
113Entry * NewModelPrivate::addSignatures(Entry *parent, QVector<Signature::Ptr> signatures) 116NewModelPrivate::~NewModelPrivate()
117{
118}
119
120Entry *NewModelPrivate::addSignatures(Entry *parent, QVector<Signature::Ptr> signatures)
114{ 121{
115 auto ret = parent; 122 auto ret = parent;
116 foreach(const auto &sig, signatures) { 123 foreach(const auto &sig, signatures) {
@@ -160,15 +167,15 @@ Entry * NewModelPrivate::addPart(Entry *parent, Part *part)
160 167
161void NewModelPrivate::createTree() 168void NewModelPrivate::createTree()
162{ 169{
163 mRoot = new Entry(); 170 auto root = mRoot.get();
164 auto parent = mRoot; 171 auto parent = root;
165 Part *pPart = nullptr; 172 Part *pPart = nullptr;
166 QVector<Signature::Ptr> signatures; 173 QVector<Signature::Ptr> signatures;
167 QVector<Encryption::Ptr> encryptions; 174 QVector<Encryption::Ptr> encryptions;
168 foreach(const auto part, mParts) { 175 foreach(const auto part, mParts) {
169 auto _parent = parent; 176 auto _parent = parent;
170 if (pPart != part->parent()) { 177 if (pPart != part->parent()) {
171 auto _parent = mRoot; 178 auto _parent = root;
172 _parent = addEncryptions(_parent, part->parent()->encryptions()); 179 _parent = addEncryptions(_parent, part->parent()->encryptions());
173 _parent = addSignatures(_parent, part->parent()->signatures()); 180 _parent = addSignatures(_parent, part->parent()->signatures());
174 signatures = part->parent()->signatures(); 181 signatures = part->parent()->signatures();
@@ -202,7 +209,6 @@ QSharedPointer<QVariant> NewModelPrivate::getVar(const std::shared_ptr<Encryptio
202 return mEncryptionMap.value(enc); 209 return mEncryptionMap.value(enc);
203} 210}
204 211
205
206QSharedPointer<QVariant> NewModelPrivate::getVar(const std::shared_ptr<Part> &part) 212QSharedPointer<QVariant> NewModelPrivate::getVar(const std::shared_ptr<Part> &part)
207{ 213{
208 return getVar(part.get()); 214 return getVar(part.get());
@@ -259,7 +265,6 @@ int NewModelPrivate::getPos(Encryption *encryption)
259 return i; 265 return i;
260} 266}
261 267
262
263int NewModelPrivate::getPos(Part *part) 268int NewModelPrivate::getPos(Part *part)
264{ 269{
265 int i = 0; 270 int i = 0;
@@ -297,20 +302,18 @@ QHash<int, QByteArray> NewModel::roleNames() const
297{ 302{
298 QHash<int, QByteArray> roles; 303 QHash<int, QByteArray> roles;
299 roles[TypeRole] = "type"; 304 roles[TypeRole] = "type";
300 roles[ContentsRole] = "contents";
301 roles[ContentRole] = "content"; 305 roles[ContentRole] = "content";
302 roles[IsEmbededRole] = "embeded"; 306 roles[IsEmbededRole] = "embeded";
303 roles[SecurityLevelRole] = "securityLevel"; 307 roles[SecurityLevelRole] = "securityLevel";
304 return roles; 308 return roles;
305} 309}
306 310
307
308QModelIndex NewModel::index(int row, int column, const QModelIndex &parent) const 311QModelIndex NewModel::index(int row, int column, const QModelIndex &parent) const
309{ 312{
310 if (row < 0 || column != 0) { 313 if (row < 0 || column != 0) {
311 return QModelIndex(); 314 return QModelIndex();
312 } 315 }
313 Entry *entry = d->mRoot; 316 Entry *entry = d->mRoot.get();
314 if (parent.isValid()) { 317 if (parent.isValid()) {
315 entry = static_cast<Entry *>(parent.internalPointer()); 318 entry = static_cast<Entry *>(parent.internalPointer());
316 } 319 }
@@ -324,14 +327,27 @@ QModelIndex NewModel::index(int row, int column, const QModelIndex &parent) cons
324QVariant NewModel::data(const QModelIndex &index, int role) const 327QVariant NewModel::data(const QModelIndex &index, int role) const
325{ 328{
326 if (!index.isValid()) { 329 if (!index.isValid()) {
327 if (role == Qt::DisplayRole) { 330 switch (role) {
331 case Qt::DisplayRole:
328 return QString("root"); 332 return QString("root");
333 case IsEmbededRole:
334 return false;
329 } 335 }
330 return QVariant(); 336 return QVariant();
331 } 337 }
338
332 if (index.internalPointer()) { 339 if (index.internalPointer()) {
333 const auto entry = static_cast<Entry *>(index.internalPointer()); 340 const auto entry = static_cast<Entry *>(index.internalPointer());
334 const auto _data = entry->mData; 341 const auto _data = entry->mData;
342 if (entry == d->mRoot.get()|| !_data) {
343 switch (role) {
344 case Qt::DisplayRole:
345 return QString("root");
346 case IsEmbededRole:
347 return false;
348 }
349 return QVariant();
350 }
335 if (_data->userType() == qMetaTypeId<Signature *>()) { 351 if (_data->userType() == qMetaTypeId<Signature *>()) {
336 const auto signature = _data->value<Signature *>(); 352 const auto signature = _data->value<Signature *>();
337 int i = d->getPos(signature); 353 int i = d->getPos(signature);
@@ -342,6 +358,8 @@ QVariant NewModel::data(const QModelIndex &index, int role) const
342 return QStringLiteral("Signature"); 358 return QStringLiteral("Signature");
343 case SecurityLevelRole: 359 case SecurityLevelRole:
344 return QStringLiteral("RED"); 360 return QStringLiteral("RED");
361 case IsEmbededRole:
362 return data(index.parent(), IsEmbededRole);
345 } 363 }
346 } else if (_data->userType() == qMetaTypeId<Encryption *>()) { 364 } else if (_data->userType() == qMetaTypeId<Encryption *>()) {
347 const auto encryption = _data->value<Encryption *>(); 365 const auto encryption = _data->value<Encryption *>();
@@ -353,17 +371,17 @@ QVariant NewModel::data(const QModelIndex &index, int role) const
353 return QStringLiteral("Encryption"); 371 return QStringLiteral("Encryption");
354 case SecurityLevelRole: 372 case SecurityLevelRole:
355 return QStringLiteral("GREEN"); 373 return QStringLiteral("GREEN");
374 case IsEmbededRole:
375 return data(index.parent(), IsEmbededRole);
356 } 376 }
357 } else if (_data->userType() == qMetaTypeId<Part *>()) { 377 } else if (_data->userType() == qMetaTypeId<Part *>()) {
358 const auto part = _data->value<Part *>(); 378 const auto part = _data->value<Part *>();
359 switch (role) { 379 switch (role) {
360 case Qt::DisplayRole: 380 case Qt::DisplayRole:
361 case TypeRole: 381 case TypeRole:
362 return QString::fromLatin1(part->type()); 382 return QString::fromLatin1(part->type());
363 case IsEmbededRole: 383 case IsEmbededRole:
364 return index.parent().isValid(); 384 return data(index.parent(), IsEmbededRole);
365 case ContentsRole:
366 return QVariant::fromValue<QAbstractItemModel *>(d->mContentMap.value(part).get());
367 } 385 }
368 } else if (_data->userType() == qMetaTypeId<Content *>()) { 386 } else if (_data->userType() == qMetaTypeId<Content *>()) {
369 const auto content = _data->value<Content *>(); 387 const auto content = _data->value<Content *>();
@@ -374,7 +392,7 @@ QVariant NewModel::data(const QModelIndex &index, int role) const
374 case TypeRole: 392 case TypeRole:
375 return QString::fromLatin1(content->type()); 393 return QString::fromLatin1(content->type());
376 case IsEmbededRole: 394 case IsEmbededRole:
377 return false; 395 return data(index.parent(), IsEmbededRole);
378 case ContentRole: { 396 case ContentRole: {
379 auto text = content->encodedContent(); 397 auto text = content->encodedContent();
380 if (data(index, TypeRole).toString() == "HtmlContent") { 398 if (data(index, TypeRole).toString() == "HtmlContent") {
@@ -413,7 +431,7 @@ QModelIndex NewModel::parent(const QModelIndex &index) const
413 return QModelIndex(); 431 return QModelIndex();
414 } 432 }
415 const auto entry = static_cast<Entry *>(index.internalPointer()); 433 const auto entry = static_cast<Entry *>(index.internalPointer());
416 if (entry->mParent) { 434 if (entry->mParent && entry->mParent != d->mRoot.get()) {
417 return createIndex(entry->pos(), 0, entry->mParent); 435 return createIndex(entry->pos(), 0, entry->mParent);
418 } 436 }
419 return QModelIndex(); 437 return QModelIndex();
@@ -437,88 +455,3 @@ int NewModel::columnCount(const QModelIndex &parent) const
437{ 455{
438 return 1; 456 return 1;
439} 457}
440
441NewContentModel::NewContentModel(const Part::Ptr &part, const std::shared_ptr<Parser> &parser)
442 : mPart(part)
443 , mParser(parser)
444{
445}
446
447QHash<int, QByteArray> NewContentModel::roleNames() const
448{
449 QHash<int, QByteArray> roles;
450 roles[TypeRole] = "type";
451 roles[ContentRole] = "content";
452 roles[IsEmbededRole] = "embeded";
453 roles[SecurityLevelRole] = "securityLevel";
454 return roles;
455}
456
457QModelIndex NewContentModel::index(int row, int column, const QModelIndex &parent) const
458{
459 if (!parent.isValid()) {
460 if (row < mPart->content().size()) {
461 auto part = mPart->content().at(row);
462 return createIndex(row, column, part.get());
463 }
464 }
465 return QModelIndex();
466}
467
468QVariant NewContentModel::data(const QModelIndex &index, int role) const
469{
470 auto content = static_cast<Content *>(index.internalPointer());
471 switch (role) {
472 case TypeRole:
473 return QString::fromLatin1(content->type());
474 case IsEmbededRole:
475 return false;
476 case ContentRole: {
477 auto text = content->encodedContent();
478 if (data(index, TypeRole).toString() == "HtmlContent") {
479 const auto rx = QRegExp("(src)\\s*=\\s*(\"|')(cid:[^\"']+)\\2");
480 int pos = 0;
481 while ((pos = rx.indexIn(text, pos)) != -1) {
482 const auto link = QUrl(rx.cap(3).toUtf8());
483 pos += rx.matchedLength();
484 const auto repl = mParser->getPart(link);
485 if (!repl) {
486 continue;
487 }
488 const auto content = repl->content();
489 if(content.size() < 1) {
490 continue;
491 }
492 const auto mailMime = content.first()->mailMime();
493 const auto mimetype = mailMime->mimetype().name();
494 if (mimetype.startsWith("image/")) {
495 const auto data = content.first()->content();
496 text.replace(rx.cap(0), QString("src=\"data:%1;base64,%2\"").arg(mimetype, QString::fromLatin1(data.toBase64())));
497 }
498 }
499 }
500 return text;
501 }
502 case SecurityLevelRole:
503 return content->encryptions().size() > mPart->encryptions().size() ? "red": "black"; //test for gpg inline
504 }
505 return QVariant();
506}
507
508QModelIndex NewContentModel::parent(const QModelIndex &index) const
509{
510 return QModelIndex();
511}
512
513int NewContentModel::rowCount(const QModelIndex &parent) const
514{
515 if (!parent.isValid()) {
516 return mPart->content().size();
517 }
518 return 0;
519}
520
521int NewContentModel::columnCount(const QModelIndex &parent) const
522{
523 return 1;
524}
diff --git a/framework/domain/modeltest.cpp b/framework/domain/modeltest.cpp
index 295ff6d0..9e1d92fa 100644
--- a/framework/domain/modeltest.cpp
+++ b/framework/domain/modeltest.cpp
@@ -339,7 +339,7 @@ void ModelTest::checkChildren ( const QModelIndex &parent, int currentDepth )
339 // rowCount() and columnCount() said that it existed... 339 // rowCount() and columnCount() said that it existed...
340 QVERIFY( index.isValid() ); 340 QVERIFY( index.isValid() );
341 341
342 qWarning() << "\tchild("<< r <<", " << c << ", " << index.column() << "):" << model->data(index).toString(); 342 qWarning() << "\tchild("<< r <<", " << c << ", " << index.column() << "):" << model->data(index).toString() << index.internalPointer();
343 343
344 // index() should always return the same index when called twice in a row 344 // index() should always return the same index when called twice in a row
345 QModelIndex modifiedIndex = model->index ( r, c, parent ); 345 QModelIndex modifiedIndex = model->index ( r, c, parent );