Messagelib

templateextracthtmlelementfrommail.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 "templateextracthtmlelementfrommail.h"
8 #include "templateparser_debug.h"
9 #include "templatewebenginepage.h"
10 #include <QWebEngineScript>
11 
12 template<typename Arg, typename R, typename C> struct InvokeWrapperFunction {
13  R *receiver;
14  void (C::*memberFun)(Arg);
15  void operator()(Arg result)
16  {
17  (receiver->*memberFun)(result);
18  }
19 };
20 
21 template<typename Arg, typename R, typename C> InvokeWrapperFunction<Arg, R, C> invokeFunction(R *receiver, void (C::*memberFun)(Arg))
22 {
23  InvokeWrapperFunction<Arg, R, C> wrapper = {receiver, memberFun};
24  return wrapper;
25 }
26 
27 using namespace TemplateParser;
28 
29 TemplateExtractHtmlElementFromMail::TemplateExtractHtmlElementFromMail(QObject *parent)
30  : QObject(parent)
31  , mPage(new TemplateWebEnginePage(this))
32 {
33  connect(mPage, &TemplateWebEnginePage::loadFinished, this, &TemplateExtractHtmlElementFromMail::slotLoadFinished);
34 }
35 
36 TemplateExtractHtmlElementFromMail::~TemplateExtractHtmlElementFromMail() = default;
37 
38 void TemplateExtractHtmlElementFromMail::clear()
39 {
40  mBodyElement.clear();
41  mHeaderElement.clear();
42  mHtmlElement.clear();
43 }
44 
45 void TemplateExtractHtmlElementFromMail::setHtmlContent(const QString &html)
46 {
47  clear();
48  mHtmlElement = html;
49  mPage->setHtml(html);
50 }
51 
52 QString extractHeaderBodyScript()
53 {
54  const QString source = QStringLiteral(
55  "(function() {"
56  "var res = {"
57  " body: document.getElementsByTagName('body')[0].innerHTML,"
58  " header: document.getElementsByTagName('head')[0].innerHTML"
59  "};"
60  "return res;"
61  "})()");
62  return source;
63 }
64 
65 void TemplateExtractHtmlElementFromMail::slotLoadFinished(bool success)
66 {
67  if (success) {
68  mPage->runJavaScript(extractHeaderBodyScript(),
69  (QWebEngineScript::UserWorld + 2),
70  invokeFunction(this, &TemplateExtractHtmlElementFromMail::handleHtmlInfo));
71  } else {
72  Q_EMIT loadContentDone(false);
73  }
74 }
75 
76 void TemplateExtractHtmlElementFromMail::handleHtmlInfo(const QVariant &result)
77 {
78  if (result.isValid()) {
79  const QVariantMap map = result.toMap();
80  mBodyElement = map.value(QStringLiteral("body")).toString();
81  mHeaderElement = map.value(QStringLiteral("header")).toString();
82  Q_EMIT loadContentDone(true);
83  } else {
84  qCWarning(TEMPLATEPARSER_LOG) << "Impossible to get value";
85  Q_EMIT loadContentDone(false);
86  }
87 }
88 
89 QString TemplateExtractHtmlElementFromMail::htmlElement() const
90 {
91  return mHtmlElement;
92 }
93 
94 QString TemplateExtractHtmlElementFromMail::headerElement() const
95 {
96  return mHeaderElement;
97 }
98 
99 QString TemplateExtractHtmlElementFromMail::bodyElement() const
100 {
101  return mBodyElement;
102 }
QMap< QString, QVariant > toMap() const const
bool isValid() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QFuture< void > map(Sequence &sequence, MapFunctor function)
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Dec 6 2021 23:04:58 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.