Messagelib

utils.cpp
1 /*
2  SPDX-FileCopyrightText: 2016 Sandro KnauƟ <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "utils.h"
8 
9 using namespace MimeTreeParser;
10 
11 MimeMessagePart::Ptr MimeTreeParser::createAndParseTempNode(Interface::BodyPart &part, KMime::Content *parentNode, const char *content, const char *cntDesc)
12 {
13  auto newNode = new KMime::Content();
14  newNode->setContent(KMime::CRLFtoLF(content));
15  newNode->parse();
16 
17  if (!newNode->head().isEmpty()) {
18  newNode->contentDescription()->from7BitString(cntDesc);
19  }
20  part.nodeHelper()->attachExtraContent(parentNode, newNode);
21 
22  return MimeMessagePart::Ptr(new MimeMessagePart(part.objectTreeParser(), newNode, false));
23 }
24 
25 KMime::Content *MimeTreeParser::findTypeInDirectChilds(KMime::Content *content, const QByteArray &mimeType)
26 {
27  if (mimeType.isEmpty()) {
28  return content;
29  }
30 
31  const auto contents = content->contents();
32  for (const auto child : contents) {
33  if ((!child->contentType()->isEmpty()) && (mimeType == child->contentType(false)->mimeType())) {
34  return child;
35  }
36  }
37  return nullptr;
38 }
39 
40 MessagePart::Ptr MimeTreeParser::toplevelTextNode(MessagePart::Ptr messageTree)
41 {
42  const auto subParts = messageTree->subParts();
43  for (const auto &mp : subParts) {
44  auto text = mp.dynamicCast<TextMessagePart>();
45  auto attach = mp.dynamicCast<AttachmentMessagePart>();
46  if (text && !attach) {
47  return text;
48  } else if (const auto alternative = mp.dynamicCast<AlternativeMessagePart>()) {
49  return alternative;
50  } else {
51  auto ret = toplevelTextNode(mp);
52  if (ret) {
53  return ret;
54  }
55  }
56  }
57  return {};
58 }
virtual MimeTreeParser::ObjectTreeParser * objectTreeParser() const =0
For making it easier to refactor, add objectTreeParser.
virtual MimeTreeParser::NodeHelper * nodeHelper() const =0
Ok, this is ugly, exposing the node helper here, but there is too much useful stuff in there for real...
KCALUTILS_EXPORT QString mimeType()
bool isEmpty() const const
interface of message body parts.
Definition: bodypart.h:44
void attachExtraContent(KMime::Content *topLevelNode, KMime::Content *content)
Attach an extra node to an existing node.
QVector< Content * > contents() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat May 21 2022 03:53:37 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.