Messagelib

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

KDE's Doxygen guidelines are available online.