Messagelib

webengineparthtmlwriter.cpp
1/*
2 SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6#include "webengineparthtmlwriter.h"
7#include "webengineembedpart.h"
8
9#include "messageviewer/messageviewerutil.h"
10#include "messageviewer_debug.h"
11#include "viewer/webengine/mailwebengineview.h"
12#include <QUrl>
13
14#include <QDir>
15#include <QTemporaryFile>
16#include <cassert>
17
18using namespace MessageViewer;
19
20WebEnginePartHtmlWriter::WebEnginePartHtmlWriter(MailWebEngineView *view, QObject *parent)
21 : QObject(parent)
22 , mHtmlView(view)
23{
24 assert(view);
25}
26
27WebEnginePartHtmlWriter::~WebEnginePartHtmlWriter()
28{
29 delete mTempFile;
30}
31
32void WebEnginePartHtmlWriter::begin()
33{
34 if (mState != Ended) {
35 qCWarning(MESSAGEVIEWER_LOG) << "begin() called on non-ended session!";
36 reset();
37 }
38
39 delete mTempFile;
40 mTempFile = nullptr;
41
43 MessageViewer::WebEngineEmbedPart::self()->clear();
44 mState = Begun;
45}
46
47void WebEnginePartHtmlWriter::end()
48{
50 if (mState != Begun) {
51 qCWarning(MESSAGEVIEWER_LOG) << "Called on non-begun or queued session!";
52 }
53 if (!mExtraHead.isEmpty()) {
54 insertExtraHead();
55 mExtraHead.clear();
56 }
57 if (!mStyleBody.isEmpty()) {
58 insertBodyStyle();
59 mStyleBody.clear();
60 }
61 if (data().size() > 2000000) {
62 // qDebug() << " load big message ";
63 mTempFile = new QTemporaryFile(QDir::tempPath() + QLatin1StringView("/messageviewer_XXXXXX") + QLatin1StringView(".html"));
64 mTempFile->open();
65 QTextStream stream(mTempFile);
66 stream.setEncoding(Util::htmlEncoding(data(), codec()));
67 stream << data();
68 // Bug 387061
69 mHtmlView->load(QUrl::fromLocalFile(mTempFile->fileName()));
70 // qDebug() << " tempFile.fileName()" << mTempFile->fileName();
71 mHtmlView->show();
72 mTempFile->close();
73 } else {
74 mHtmlView->setContent(data(), QStringLiteral("text/html;charset=UTF-8"), QUrl(QStringLiteral("file:///")));
75 mHtmlView->show();
76 }
77 clear();
78
79 mHtmlView->setUpdatesEnabled(true);
80 mHtmlView->update();
81 mState = Ended;
82 Q_EMIT finished();
83}
84
85void WebEnginePartHtmlWriter::reset()
86{
88 if (mState != Ended) {
89 mState = Begun; // don't run into end()'s warning
90 end();
91 mState = Ended;
92 }
93}
94
95void WebEnginePartHtmlWriter::embedPart(const QByteArray &contentId, const QString &contentURL)
96{
97 MessageViewer::WebEngineEmbedPart::self()->addEmbedPart(contentId, contentURL);
98}
99
100void WebEnginePartHtmlWriter::insertExtraHead()
101{
102 const auto headTag(QByteArrayLiteral("<head>"));
103 const int index = m_data.indexOf(headTag);
104 if (index != -1) {
105 m_data.insert(index + headTag.length(), mExtraHead.toUtf8());
106 }
107}
108
109void WebEnginePartHtmlWriter::insertBodyStyle()
110{
111 const auto bodyTag(QByteArrayLiteral("<body>"));
112 const int index = m_data.indexOf(bodyTag);
113 if (index != -1) {
114 m_data.insert(index + bodyTag.length() - 1, mStyleBody.toUtf8());
115 }
116}
117
118void WebEnginePartHtmlWriter::setStyleBody(const QString &styleBody)
119{
120 mStyleBody = styleBody;
121}
122
123void WebEnginePartHtmlWriter::setExtraHead(const QString &str)
124{
125 mExtraHead = str;
126}
127
128#include "moc_webengineparthtmlwriter.cpp"
void reset() override
Stop all possibly pending processing in order to be able to call begin() again.
void begin() override
Signal the begin of stuff to write.
void end() override
Signal the end of stuff to write.
QTextStream * stream() const
Returns a QTextStream on device().
The MailWebEngineView class.
MESSAGEVIEWER_EXPORT QStringConverter::Encoding htmlEncoding(const QByteArray &data, const QByteArray &codec)
Return a QTextCodec for the specified charset.
int indexOf(char ch, int from) const const
QByteArray & insert(int i, char ch)
QString tempPath()
virtual void close() override
Q_EMITQ_EMIT
T qobject_cast(QObject *object)
void clear()
bool isEmpty() const const
QByteArray toUtf8() const const
virtual QString fileName() const const override
QUrl fromLocalFile(const QString &localFile)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:37:31 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.