Messagelib

templateextracthtmlelementfrommail.cpp
1/*
2 SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org>
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
12template<typename Arg, typename R, typename C>
13struct InvokeWrapperFunction {
14 R *receiver;
15 void (C::*memberFun)(Arg);
16 void operator()(Arg result)
17 {
18 (receiver->*memberFun)(result);
19 }
20};
21
22template<typename Arg, typename R, typename C>
23InvokeWrapperFunction<Arg, R, C> invokeFunction(R *receiver, void (C::*memberFun)(Arg))
24{
25 InvokeWrapperFunction<Arg, R, C> wrapper = {receiver, memberFun};
26 return wrapper;
27}
28
29using namespace TemplateParser;
30
31TemplateExtractHtmlElementFromMail::TemplateExtractHtmlElementFromMail(QObject *parent)
32 : QObject(parent)
33 , mPage(new TemplateWebEnginePage(this))
34{
35 connect(mPage, &TemplateWebEnginePage::loadFinished, this, &TemplateExtractHtmlElementFromMail::slotLoadFinished);
36}
37
38TemplateExtractHtmlElementFromMail::~TemplateExtractHtmlElementFromMail() = default;
39
40void TemplateExtractHtmlElementFromMail::clear()
41{
42 mBodyElement.clear();
43 mHeaderElement.clear();
44 mHtmlElement.clear();
45}
46
47void TemplateExtractHtmlElementFromMail::setHtmlContent(const QString &html)
48{
49 clear();
50 mHtmlElement = html;
51 mPage->setHtml(html);
52}
53
54QString 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
67void 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
78void 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
91QString TemplateExtractHtmlElementFromMail::htmlElement() const
92{
93 return mHtmlElement;
94}
95
96QString TemplateExtractHtmlElementFromMail::headerElement() const
97{
98 return mHeaderElement;
99}
100
101QString TemplateExtractHtmlElementFromMail::bodyElement() const
102{
103 return mBodyElement;
104}
105
106#include "moc_templateextracthtmlelementfrommail.cpp"
Q_EMITQ_EMIT
void clear()
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
bool isValid() const const
QMap< QString, QVariant > toMap() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:07:25 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.