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 mTempFile->write(data());
66 // Bug 387061
67 mHtmlView->load(QUrl::fromLocalFile(mTempFile->fileName()));
68 // qDebug() << " tempFile.fileName()" << mTempFile->fileName();
69 mHtmlView->show();
70 mTempFile->close();
71 } else {
72 mHtmlView->setContent(data(), QStringLiteral("text/html;charset=UTF-8"), QUrl(QStringLiteral("file:///")));
73 mHtmlView->show();
74 }
75 clear();
76
77 mHtmlView->setUpdatesEnabled(true);
78 mHtmlView->update();
79 mState = Ended;
80 Q_EMIT finished();
81}
82
83void WebEnginePartHtmlWriter::reset()
84{
86 if (mState != Ended) {
87 mState = Begun; // don't run into end()'s warning
88 end();
89 mState = Ended;
90 }
91}
92
93void WebEnginePartHtmlWriter::embedPart(const QByteArray &contentId, const QString &contentURL)
94{
95 MessageViewer::WebEngineEmbedPart::self()->addEmbedPart(contentId, contentURL);
96}
97
98void WebEnginePartHtmlWriter::insertExtraHead()
99{
100 const auto headTag(QByteArrayLiteral("<head>"));
101 const int index = m_data.indexOf(headTag);
102 if (index != -1) {
103 m_data.insert(index + headTag.length(), mExtraHead.toUtf8());
104 }
105}
106
107void WebEnginePartHtmlWriter::insertBodyStyle()
108{
109 const auto bodyTag(QByteArrayLiteral("<body>"));
110 const int index = m_data.indexOf(bodyTag);
111 if (index != -1) {
112 m_data.insert(index + bodyTag.length() - 1, mStyleBody.toUtf8());
113 }
114}
115
116void WebEnginePartHtmlWriter::setStyleBody(const QString &styleBody)
117{
118 mStyleBody = styleBody;
119}
120
121void WebEnginePartHtmlWriter::setExtraHead(const QString &str)
122{
123 mExtraHead = str;
124}
125
126#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.
The MailWebEngineView class.
qsizetype indexOf(QByteArrayView bv, qsizetype from) const const
QByteArray & insert(qsizetype i, QByteArrayView data)
QString tempPath()
virtual void close() override
qint64 write(const QByteArray &data)
Q_EMITQ_EMIT
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 Fri Jul 19 2024 11:56:56 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.