Messagelib

webengineparthtmlwriter.cpp
1 /*
2  SPDX-FileCopyrightText: 2016-2021 Laurent Montel <[email protected]>
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 
18 using namespace MessageViewer;
19 
20 WebEnginePartHtmlWriter::WebEnginePartHtmlWriter(MailWebEngineView *view, QObject *parent)
21  : QObject(parent)
22  , mHtmlView(view)
23 {
24  assert(view);
25 }
26 
27 WebEnginePartHtmlWriter::~WebEnginePartHtmlWriter()
28 {
29  delete mTempFile;
30 }
31 
32 void 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 
42  BufferedHtmlWriter::begin();
43  MessageViewer::WebEngineEmbedPart::self()->clear();
44  mState = Begun;
45 }
46 
47 void WebEnginePartHtmlWriter::end()
48 {
49  BufferedHtmlWriter::end();
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() + QLatin1String("/messageviewer_XXXXXX") + QLatin1String(".html"));
64  mTempFile->open();
65  QTextStream stream(mTempFile);
66 
67  const QByteArray codecValue = Util::htmlCodec(data(), codec());
68  stream.setCodec(codecValue.constData());
69  stream << data();
70  // Bug 387061
71  mHtmlView->load(QUrl::fromLocalFile(mTempFile->fileName()));
72  // qDebug() << " tempFile.fileName()" << mTempFile->fileName();
73  mHtmlView->show();
74  mTempFile->close();
75  } else {
76  mHtmlView->setContent(data(), QStringLiteral("text/html;charset=UTF-8"), QUrl(QStringLiteral("file:///")));
77  mHtmlView->show();
78  }
79  clear();
80 
81  mHtmlView->setUpdatesEnabled(true);
82  mHtmlView->update();
83  mState = Ended;
84  Q_EMIT finished();
85 }
86 
87 void WebEnginePartHtmlWriter::reset()
88 {
89  BufferedHtmlWriter::reset();
90  if (mState != Ended) {
91  mState = Begun; // don't run into end()'s warning
92  end();
93  mState = Ended;
94  }
95 }
96 
97 void WebEnginePartHtmlWriter::embedPart(const QByteArray &contentId, const QString &contentURL)
98 {
99  MessageViewer::WebEngineEmbedPart::self()->addEmbedPart(contentId, contentURL);
100 }
101 
102 void WebEnginePartHtmlWriter::insertExtraHead()
103 {
104  const auto headTag(QByteArrayLiteral("<head>"));
105  const int index = m_data.indexOf(headTag);
106  if (index != -1) {
107  m_data.insert(index + headTag.length(), mExtraHead.toUtf8());
108  }
109 }
110 
111 void WebEnginePartHtmlWriter::insertBodyStyle()
112 {
113  const auto bodyTag(QByteArrayLiteral("<body>"));
114  const int index = m_data.indexOf(bodyTag);
115  if (index != -1) {
116  m_data.insert(index + bodyTag.length() - 1, mStyleBody.toUtf8());
117  }
118 }
119 
120 void WebEnginePartHtmlWriter::setStyleBody(const QString &styleBody)
121 {
122  mStyleBody = styleBody;
123 }
124 
125 void WebEnginePartHtmlWriter::setExtraHead(const QString &str)
126 {
127  mExtraHead = str;
128 }
QString tempPath()
const char * constData() const const
const QList< QKeySequence > & end()
The MailWebEngineView class.
KGuiItem reset()
QUrl fromLocalFile(const QString &localFile)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Nov 30 2021 23:05:48 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.