Changeset View
Changeset View
Standalone View
Standalone View
templateparser/src/templateparserjob.cpp
Show All 23 Lines | |||||
24 | #include "customtemplates_kfg.h" | 24 | #include "customtemplates_kfg.h" | ||
25 | #include "templatesconfiguration_kfg.h" | 25 | #include "templatesconfiguration_kfg.h" | ||
26 | #include "templatesconfiguration.h" | 26 | #include "templatesconfiguration.h" | ||
27 | 27 | | |||
28 | #include <MessageCore/ImageCollector> | 28 | #include <MessageCore/ImageCollector> | ||
29 | #include <MessageCore/StringUtil> | 29 | #include <MessageCore/StringUtil> | ||
30 | 30 | | |||
31 | #include <MimeTreeParser/ObjectTreeParser> | 31 | #include <MimeTreeParser/ObjectTreeParser> | ||
32 | #include <MimeTreeParser/MessagePart> | ||||
32 | #include <MimeTreeParser/SimpleObjectTreeSource> | 33 | #include <MimeTreeParser/SimpleObjectTreeSource> | ||
33 | 34 | | |||
34 | #include <KIdentityManagement/Identity> | 35 | #include <KIdentityManagement/Identity> | ||
35 | #include <KIdentityManagement/IdentityManager> | 36 | #include <KIdentityManagement/IdentityManager> | ||
36 | 37 | | |||
37 | #include <KCharsets> | 38 | #include <KCharsets> | ||
38 | #include <KLocalizedString> | 39 | #include <KLocalizedString> | ||
39 | #include <KMessageBox> | 40 | #include <KMessageBox> | ||
▲ Show 20 Lines • Show All 251 Lines • ▼ Show 20 Line(s) | |||||
291 | } | 292 | } | ||
292 | 293 | | |||
293 | void TemplateParserJob::processWithIdentity(uint uoid, const KMime::Message::Ptr &aorig_msg, qint64 afolder) | 294 | void TemplateParserJob::processWithIdentity(uint uoid, const KMime::Message::Ptr &aorig_msg, qint64 afolder) | ||
294 | { | 295 | { | ||
295 | d->mIdentity = uoid; | 296 | d->mIdentity = uoid; | ||
296 | process(aorig_msg, afolder); | 297 | process(aorig_msg, afolder); | ||
297 | } | 298 | } | ||
298 | 299 | | |||
300 | MimeTreeParser::MessagePart::Ptr toplevelTextNode(MimeTreeParser::MessagePart::Ptr messageTree) | ||||
301 | { | ||||
302 | foreach (const auto &mp, messageTree->subParts()) { | ||||
303 | auto text = mp.dynamicCast<MimeTreeParser::TextMessagePart>(); | ||||
304 | const auto attach = mp.dynamicCast<MimeTreeParser::AttachmentMessagePart>(); | ||||
305 | if (text && !attach) { | ||||
306 | // TextMessagePart can have several subparts cause of PGP inline, we search for the first part with content | ||||
307 | foreach (const auto &sub, mp->subParts()) { | ||||
308 | if (!sub->text().trimmed().isEmpty()) { | ||||
309 | return sub; | ||||
310 | } | ||||
311 | } | ||||
312 | return text; | ||||
313 | } else if (const auto html = mp.dynamicCast<MimeTreeParser::HtmlMessagePart>()) { | ||||
314 | return html; | ||||
315 | } else if (const auto alternative = mp.dynamicCast<MimeTreeParser::AlternativeMessagePart>()) { | ||||
316 | return alternative; | ||||
317 | } else { | ||||
318 | auto ret = toplevelTextNode(mp); | ||||
319 | if (ret) { | ||||
320 | return ret; | ||||
321 | } | ||||
322 | } | ||||
323 | } | ||||
324 | return MimeTreeParser::MessagePart::Ptr(); | ||||
325 | } | ||||
326 | | ||||
299 | void TemplateParserJob::processWithTemplate(const QString &tmpl) | 327 | void TemplateParserJob::processWithTemplate(const QString &tmpl) | ||
300 | { | 328 | { | ||
301 | d->mOtp->parseObjectTree(d->mOrigMsg.data()); | 329 | d->mOtp->parseObjectTree(d->mOrigMsg.data()); | ||
302 | 330 | | |||
303 | TemplateParserExtractHtmlInfo *job = new TemplateParserExtractHtmlInfo(this); | 331 | const auto mp = toplevelTextNode(d->mOtp->parsedPart()); | ||
304 | connect(job, &TemplateParserExtractHtmlInfo::finished, this, &TemplateParserJob::slotExtractInfoDone); | 332 | | ||
333 | QString plainText = mp->plaintextContent(); | ||||
334 | QString htmlElement; | ||||
305 | 335 | | |||
306 | QString plainText = d->mOtp->plainTextContent(); | 336 | if (mp->isHtml()) { | ||
337 | htmlElement = d->mOtp->htmlContent(); | ||||
307 | if (plainText.isEmpty()) { //HTML-only mails | 338 | if (plainText.isEmpty()) { //HTML-only mails | ||
308 | plainText = d->mOtp->htmlContent(); | 339 | plainText = htmlElement; | ||
309 | } | 340 | } | ||
310 | 341 | } else { //plain mails only | |||
311 | job->setHtmlForExtractingTextPlain(plainText); | 342 | QString htmlReplace = plainText.toHtmlEscaped(); | ||
312 | job->setTemplate(tmpl); | | |||
313 | | ||||
314 | QString htmlElement = d->mOtp->htmlContent(); | | |||
315 | | ||||
316 | if (htmlElement.isEmpty()) { //plain mails only | | |||
317 | QString htmlReplace = d->mOtp->plainTextContent().toHtmlEscaped(); | | |||
318 | htmlReplace = htmlReplace.replace(QLatin1Char('\n'), QStringLiteral("<br />")); | 343 | htmlReplace = htmlReplace.replace(QLatin1Char('\n'), QStringLiteral("<br />")); | ||
319 | htmlElement = QStringLiteral("<html><head></head><body>%1</body></html>\n").arg(htmlReplace); | 344 | htmlElement = QStringLiteral("<html><head></head><body>%1</body></html>\n").arg(htmlReplace); | ||
320 | } | 345 | } | ||
321 | 346 | | |||
347 | TemplateParserExtractHtmlInfo *job = new TemplateParserExtractHtmlInfo(this); | ||||
348 | connect(job, &TemplateParserExtractHtmlInfo::finished, this, &TemplateParserJob::slotExtractInfoDone); | ||||
349 | | ||||
350 | job->setHtmlForExtractingTextPlain(plainText); | ||||
351 | job->setTemplate(tmpl); | ||||
352 | | ||||
322 | job->setHtmlForExtractionHeaderAndBody(htmlElement); | 353 | job->setHtmlForExtractionHeaderAndBody(htmlElement); | ||
323 | job->start(); | 354 | job->start(); | ||
324 | } | 355 | } | ||
325 | 356 | | |||
326 | void TemplateParserJob::slotExtractInfoDone(const TemplateParserExtractHtmlInfoResult &result) | 357 | void TemplateParserJob::slotExtractInfoDone(const TemplateParserExtractHtmlInfoResult &result) | ||
327 | { | 358 | { | ||
328 | d->mExtractHtmlInfoResult = result; | 359 | d->mExtractHtmlInfoResult = result; | ||
329 | const QString tmpl = d->mExtractHtmlInfoResult.mTemplate; | 360 | const QString tmpl = d->mExtractHtmlInfoResult.mTemplate; | ||
▲ Show 20 Lines • Show All 1148 Lines • ▼ Show 20 Line(s) | |||||
1478 | { | 1509 | { | ||
1479 | if (!d->mSelection.isEmpty() && (isSelectionAllowed == SelectionAllowed)) { | 1510 | if (!d->mSelection.isEmpty() && (isSelectionAllowed == SelectionAllowed)) { | ||
1480 | return d->mSelection; | 1511 | return d->mSelection; | ||
1481 | } | 1512 | } | ||
1482 | 1513 | | |||
1483 | if (!d->mOrigMsg) { | 1514 | if (!d->mOrigMsg) { | ||
1484 | return QString(); | 1515 | return QString(); | ||
1485 | } | 1516 | } | ||
1486 | QString result = d->mOtp->plainTextContent(); | 1517 | const auto mp = toplevelTextNode(d->mOtp->parsedPart()); | ||
1518 | QString result = mp->plaintextContent(); | ||||
1487 | if (result.isEmpty()) { | 1519 | if (result.isEmpty()) { | ||
1488 | result = d->mExtractHtmlInfoResult.mPlainText; | 1520 | result = d->mExtractHtmlInfoResult.mPlainText; | ||
1489 | } | 1521 | } | ||
1490 | if (aStripSignature) { | 1522 | if (aStripSignature) { | ||
1491 | result = MessageCore::StringUtil::stripSignature(result); | 1523 | result = MessageCore::StringUtil::stripSignature(result); | ||
1492 | } | 1524 | } | ||
1493 | 1525 | | |||
1494 | return result; | 1526 | return result; | ||
▲ Show 20 Lines • Show All 119 Lines • Show Last 20 Lines |