Messagelib

templateparserextracthtmlinfo.cpp
1 /*
2  SPDX-FileCopyrightText: 2017-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "templateparserextracthtmlinfo.h"
8 #include "templateextracthtmlelementfrommail.h"
9 #include "templateextracttextfrommail.h"
10 #include "templateparser_debug.h"
11 
12 using namespace TemplateParser;
13 
14 TemplateParserExtractHtmlInfo::TemplateParserExtractHtmlInfo(QObject *parent)
15  : QObject(parent)
16 {
17 }
18 
19 TemplateParserExtractHtmlInfo::~TemplateParserExtractHtmlInfo()
20 {
21  if (mTemplateWebEngineView) {
22  mTemplateWebEngineView->deleteLater();
23  }
24  if (mExtractHtmlElementWebEngineView) {
25  mExtractHtmlElementWebEngineView->deleteLater();
26  }
27 }
28 
29 void TemplateParserExtractHtmlInfo::setHtmlForExtractingTextPlain(const QString &html)
30 {
31  mHtmlForExtractingTextPlain = html;
32 }
33 
34 void TemplateParserExtractHtmlInfo::setHtmlForExtractionHeaderAndBody(const QString &html)
35 {
36  mHtmlForExtractionHeaderAndBody = html;
37 }
38 
39 void TemplateParserExtractHtmlInfo::setTemplate(const QString &str)
40 {
41  mTemplateStr = str;
42 }
43 
44 void TemplateParserExtractHtmlInfo::start()
45 {
46  mResult.clear();
47  mResult.mTemplate = mTemplateStr;
48  if (!mHtmlForExtractingTextPlain.isEmpty()) {
49  mTemplateWebEngineView = new TemplateExtractTextFromMail;
50  connect(mTemplateWebEngineView, &TemplateExtractTextFromMail::loadContentDone, this, &TemplateParserExtractHtmlInfo::slotExtractToPlainTextFinished);
51  mTemplateWebEngineView->setHtmlContent(mHtmlForExtractingTextPlain);
52  } else {
53  qCDebug(TEMPLATEPARSER_LOG) << "html string is empty for extracting to plainText";
54  slotExtractToPlainTextFinished(false);
55  }
56 }
57 
58 void TemplateParserExtractHtmlInfo::slotExtractToPlainTextFinished(bool success)
59 {
60  if (success) {
61  mResult.mPlainText = mTemplateWebEngineView->plainText();
62  } else {
63  qCDebug(TEMPLATEPARSER_LOG) << "Impossible to extract plaintext";
64  }
65  if (!mHtmlForExtractionHeaderAndBody.isEmpty()) {
66  mExtractHtmlElementWebEngineView = new TemplateExtractHtmlElementFromMail;
67  connect(mExtractHtmlElementWebEngineView,
68  &TemplateExtractHtmlElementFromMail::loadContentDone,
69  this,
70  &TemplateParserExtractHtmlInfo::slotExtractHtmlElementFinished);
71  mExtractHtmlElementWebEngineView->setHtmlContent(mHtmlForExtractionHeaderAndBody);
72  } else {
73  qCDebug(TEMPLATEPARSER_LOG) << "html string is empty for extracting to header and body";
74  slotExtractHtmlElementFinished(false);
75  }
76 }
77 
78 void TemplateParserExtractHtmlInfo::slotExtractHtmlElementFinished(bool success)
79 {
80  if (success) {
81  mResult.mBodyElement = mExtractHtmlElementWebEngineView->bodyElement();
82  mResult.mHeaderElement = mExtractHtmlElementWebEngineView->headerElement();
83  mResult.mHtmlElement = mExtractHtmlElementWebEngineView->htmlElement();
84  } else {
85  qCDebug(TEMPLATEPARSER_LOG) << "Impossible to extract html element";
86  }
87  Q_EMIT finished(mResult);
88  deleteLater();
89 }
void clear()
The TemplateExtractTextFromMail class.
void deleteLater()
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Nov 30 2021 23:05:48 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.