74 if (i < length - 1) result.push_back(' '); |
74 if (i < length - 1) result.push_back(' '); |
75 } |
75 } |
76 return result; |
76 return result; |
77 } |
77 } |
78 |
78 |
79 std::string fetchBodyText(std::shared_ptr<vmime::body> body) { |
79 std::string fetchBodyText(std::shared_ptr<vmime::body> body, size_t& bodyLength) { |
80 std::stringstream result; |
80 std::stringstream result; |
81 vmime::utility::outputStreamAdapter resultAdapter(result); |
81 vmime::utility::outputStreamAdapter resultAdapter(result); |
82 |
82 |
83 const vmime::charset targetEncoding = vmime::charset("utf-8"); |
83 const vmime::charset targetEncoding = vmime::charset("utf-8"); |
84 const vmime::charset sourceEncoding = body->getCharset(); |
84 const vmime::charset sourceEncoding = body->getCharset(); |
86 vmime::shared_ptr <vmime::charsetConverter> charsetConverter = vmime::charsetConverter::create(sourceEncoding, targetEncoding); |
86 vmime::shared_ptr <vmime::charsetConverter> charsetConverter = vmime::charsetConverter::create(sourceEncoding, targetEncoding); |
87 vmime::shared_ptr <vmime::utility::charsetFilteredOutputStream> resultConverter = charsetConverter->getFilteredOutputStream(resultAdapter); |
87 vmime::shared_ptr <vmime::utility::charsetFilteredOutputStream> resultConverter = charsetConverter->getFilteredOutputStream(resultAdapter); |
88 |
88 |
89 body->getContents()->extract(*resultConverter); |
89 body->getContents()->extract(*resultConverter); |
90 resultConverter->flush(); |
90 resultConverter->flush(); |
|
91 bodyLength = result.tellp(); |
91 |
92 |
92 return result.str(); |
93 return result.str(); |
93 } |
94 } |
94 |
95 |
95 std::string fetchBodyBinary(std::shared_ptr<vmime::body> body) { |
96 std::string fetchBodyBinary(std::shared_ptr<vmime::body> body, size_t& bodyLength) { |
96 std::stringstream result; |
97 std::stringstream result; |
97 vmime::utility::outputStreamAdapter resultAdapter(result); |
98 vmime::utility::outputStreamAdapter resultAdapter(result); |
98 body->getContents()->extract(resultAdapter); |
99 body->getContents()->extract(resultAdapter); |
99 resultAdapter.flush(); |
100 resultAdapter.flush(); |
|
101 bodyLength = result.tellp(); |
100 return toHex(result.str()); |
102 return toHex(result.str()); |
101 } |
103 } |
102 |
104 |
103 void appendBody(xmlpp::Element* element, std::shared_ptr<vmime::body> body) { |
105 void appendBody(xmlpp::Element* element, std::shared_ptr<vmime::body> body) { |
104 element->set_attribute("content-type", format(body->getContentType())); |
106 element->set_attribute("content-type", format(body->getContentType())); |
105 // element->set_attribute("content-type-charset", body->getCharset().getName()); |
107 // element->set_attribute("content-type-charset", body->getCharset().getName()); |
106 // element->set_attribute("content-transfer-encoding", body->getEncoding().getName()); |
108 // element->set_attribute("content-transfer-encoding", body->getEncoding().getName()); |
107 // TODO: size of raw data |
109 // TODO: size of raw data |
108 |
110 |
109 if (body->getPartCount() == 0) { |
111 if (body->getPartCount() == 0) { |
110 if (body->getContentType().getType() == "text") element->add_child_cdata(fetchBodyText(body)); |
112 size_t bodyLength = 0; |
111 else element->add_child_text(fetchBodyBinary(body)); |
113 if (body->getContentType().getType() == "text") element->add_child_cdata(fetchBodyText(body, bodyLength)); |
|
114 else element->add_child_text(fetchBodyBinary(body, bodyLength)); |
|
115 element->set_attribute("length", std::to_string(bodyLength)); |
|
116 |
112 // TODO: if content is valid XML, import it in the DOM tree instead of pasting as a nested text/cdata |
117 // TODO: if content is valid XML, import it in the DOM tree instead of pasting as a nested text/cdata |
113 // TODO: optional trim of long data |
118 // TODO: optional trim of long data |
114 } else { |
119 } else { |
115 for (auto part : body->getPartList()) { |
120 for (auto part : body->getPartList()) { |
116 xmlpp::Element* partElement = element->add_child("part"); |
121 xmlpp::Element* partElement = element->add_child("part"); |