Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit a5ff3a0c authored by Volker Krause's avatar Volker Krause

Cleanup HtmlWriter API

Summary:
- merge Interface::HtmlWriter and HtmlWriter
- remove queue() and flush(), which with the single synchronous buffer
  send to WebEngine have no meanings anymore
- remove the unused CSS argument to begin()
- remove TeeHtmlWriter, its debugging use has been superseded by the much
  more convenient direct access to the HTML source code in the mail source
  view

This prepares the next step of making HtmlWriter conceptually a QIODevice,
so we can use it directly with Grantlee's streaming API.

Reviewers: knauss

Reviewed By: knauss

Subscribers: #kde_pim

Tags: #kde_pim

Differential Revision: https://phabricator.kde.org/D8083
parent c5be8cab
......@@ -36,7 +36,7 @@ public:
{
}
void begin(const QString &) override
void begin() override
{
}
......@@ -48,18 +48,6 @@ public:
{
}
void reset() override
{
}
void queue(const QString &) override
{
}
void flush() override
{
}
void embedPart(const QByteArray &, const QString &) override
{
}
......
......@@ -138,10 +138,6 @@ set(libmessageviewer_htmlwriter_webengine_SRCS
htmlwriter/webengineembedpart.cpp
)
set(libmessageviewer_htmlwriter_SRCS
htmlwriter/teehtmlwriter.cpp
)
set(libmessageviewer_antispam_SRCS
antispam/spamheaderanalyzer.cpp
antispam/antispamconfig.cpp
......@@ -185,7 +181,6 @@ set(libmessageviewer_SRCS
${libmessageviewer_scamdetection_SRCS}
${libmessageviewer_findbar_SRCS}
${libmessageviewer_utils_SRCS}
${libmessageviewer_htmlwriter_SRCS}
${libmessageviewer_antispam_SRCS}
${libmessageviewer_job_SRCS}
${libmessageviewer_viewerplugins_SRCS}
......
/* -*- c++ -*-
teehtmlwriter.cpp
This file is part of KMail, the KDE mail client.
Copyright (c) 2003 Marc Mutz <mutz@kde.org>
KMail is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
KMail is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
In addition, as a special exception, the copyright holders give
permission to link the code of this program with any edition of
the Qt library by Trolltech AS, Norway (or with modified versions
of Qt that use the same license as Qt), and distribute linked
combinations including the two. You must obey the GNU General
Public License in all respects for all of the code used other than
Qt. If you modify this file, you may extend this exception to
your version of the file, but you are not obligated to do so. If
you do not wish to do so, delete this exception statement from
your version.
*/
#include "teehtmlwriter.h"
namespace MessageViewer {
TeeHtmlWriter::TeeHtmlWriter(HtmlWriter *writer1, MimeTreeParser::HtmlWriter *writer2)
: MimeTreeParser::HtmlWriter()
{
if (writer1) {
mWriters.append(writer1);
}
if (writer2) {
mWriters.append(writer2);
}
}
TeeHtmlWriter::~TeeHtmlWriter()
{
for (QList<HtmlWriter *>::Iterator it = mWriters.begin(); it != mWriters.end(); ++it) {
delete(*it);
}
}
void TeeHtmlWriter::addHtmlWriter(HtmlWriter *writer)
{
if (writer) {
mWriters.append(writer);
}
}
void TeeHtmlWriter::begin(const QString &css)
{
QList<HtmlWriter *>::const_iterator end(mWriters.constEnd());
for (QList<HtmlWriter *>::const_iterator it = mWriters.constBegin(); it != end; ++it) {
(*it)->begin(css);
}
}
void TeeHtmlWriter::end()
{
QList<HtmlWriter *>::const_iterator end(mWriters.constEnd());
for (QList<HtmlWriter *>::const_iterator it = mWriters.constBegin(); it != end; ++it) {
(*it)->end();
}
}
void TeeHtmlWriter::reset()
{
QList<HtmlWriter *>::const_iterator end(mWriters.constEnd());
for (QList<HtmlWriter *>::const_iterator it = mWriters.constBegin(); it != end; ++it) {
(*it)->reset();
}
}
void TeeHtmlWriter::write(const QString &str)
{
QList<HtmlWriter *>::const_iterator end(mWriters.constEnd());
for (QList<HtmlWriter *>::const_iterator it = mWriters.constBegin(); it != end; ++it) {
(*it)->write(str);
}
}
void TeeHtmlWriter::queue(const QString &str)
{
QList<HtmlWriter *>::const_iterator end(mWriters.constEnd());
for (QList<HtmlWriter *>::const_iterator it = mWriters.constBegin(); it != end; ++it) {
(*it)->queue(str);
}
}
void TeeHtmlWriter::flush()
{
QList<HtmlWriter *>::const_iterator end(mWriters.constEnd());
for (QList<HtmlWriter *>::const_iterator it = mWriters.constBegin(); it != end; ++it) {
(*it)->flush();
}
}
void TeeHtmlWriter::embedPart(const QByteArray &contentId, const QString &url)
{
QList<HtmlWriter *>::const_iterator end(mWriters.constEnd());
for (QList<HtmlWriter *>::const_iterator it = mWriters.constBegin(); it != end; ++it) {
(*it)->embedPart(contentId, url);
}
}
void TeeHtmlWriter::extraHead(const QString &)
{
}
} //
/* -*- c++ -*-
teehtmlwriter.h
This file is part of KMail, the KDE mail client.
Copyright (c) 2003 Marc Mutz <mutz@kde.org>
KMail is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
KMail is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
In addition, as a special exception, the copyright holders give
permission to link the code of this program with any edition of
the Qt library by Trolltech AS, Norway (or with modified versions
of Qt that use the same license as Qt), and distribute linked
combinations including the two. You must obey the GNU General
Public License in all respects for all of the code used other than
Qt. If you modify this file, you may extend this exception to
your version of the file, but you are not obligated to do so. If
you do not wish to do so, delete this exception statement from
your version.
*/
#ifndef __MESSAGEVIEWER_TEEHTMLWRITER_H__
#define __MESSAGEVIEWER_TEEHTMLWRITER_H__
#include <MimeTreeParser/HtmlWriter>
#include <QList>
class QString;
namespace MessageViewer {
/** @short A HtmlWriter that dispatches all calls to a list of other HtmlWriters
@author Marc Mutz <mutz@kde.org>
**/
class TeeHtmlWriter : public MimeTreeParser::HtmlWriter
{
public:
explicit TeeHtmlWriter(HtmlWriter *writer1 = nullptr, MimeTreeParser::HtmlWriter *writer2 = nullptr);
virtual ~TeeHtmlWriter();
void addHtmlWriter(HtmlWriter *writer);
//
// HtmlWriter Interface
//
void begin(const QString &cssDefs) override;
void end() override;
void reset() override;
void write(const QString &str) override;
void queue(const QString &str) override;
void flush() override;
void embedPart(const QByteArray &contentId, const QString &url) override;
void extraHead(const QString &str) override;
private:
/** We own the HtmlWriters added to us! */
QList<HtmlWriter *> mWriters;
};
} // namespace MessageViewer
#endif // __MESSAGEVIEWER_TEEHTMLWRITER_H__
......@@ -40,10 +40,8 @@ WebEnginePartHtmlWriter::~WebEnginePartHtmlWriter()
{
}
void WebEnginePartHtmlWriter::begin(const QString &css)
void WebEnginePartHtmlWriter::begin()
{
// The stylesheet is now included CSSHelper::htmlHead()
Q_UNUSED(css);
if (mState != Ended) {
qCWarning(MESSAGEVIEWER_LOG) << "begin() called on non-ended session!";
reset();
......@@ -90,17 +88,6 @@ void WebEnginePartHtmlWriter::write(const QString &str)
mHtml.append(str);
}
void WebEnginePartHtmlWriter::queue(const QString &str)
{
write(str);
}
void WebEnginePartHtmlWriter::flush()
{
mState = Begun; // don't run into end()'s warning
end();
}
void WebEnginePartHtmlWriter::embedPart(const QByteArray &contentId, const QString &contentURL)
{
MessageViewer::WebEngineEmbedPart::self()->addEmbedPart(contentId, contentURL);
......
......@@ -35,12 +35,10 @@ public:
explicit WebEnginePartHtmlWriter(MailWebEngineView *view, QObject *parent = nullptr);
~WebEnginePartHtmlWriter();
void begin(const QString &cssDefs) override;
void begin() override;
void end() override;
void reset() override;
void write(const QString &str) override;
void queue(const QString &str) override;
void flush() override;
void embedPart(const QByteArray &contentId, const QString &url) override;
void extraHead(const QString &str) override;
......
......@@ -333,13 +333,12 @@ void RenderTest::testRender()
connect(&nodeHelper, &MimeTreeParser::NodeHelper::update, &loop, &QEventLoop::quit);
otp.setAllowAsync(bAsync);
fileWriter.begin(QString());
fileWriter.queue(cssHelper.htmlHead(false));
fileWriter.begin();
fileWriter.write(cssHelper.htmlHead(false));
otp.parseObjectTree(msg.data());
fileWriter.queue(QStringLiteral("</body></html>"));
fileWriter.flush();
fileWriter.write(QStringLiteral("</body></html>"));
fileWriter.end();
if (!bAsync) {
......@@ -351,13 +350,12 @@ void RenderTest::testRender()
MimeTreeParser::ObjectTreeParser otp(&testSource, &nodeHelper);
otp.setAllowAsync(bAsync);
fileWriter.begin(QString());
fileWriter.queue(cssHelper.htmlHead(false));
fileWriter.begin();
fileWriter.write(cssHelper.htmlHead(false));
otp.parseObjectTree(msg.data());
fileWriter.queue(QStringLiteral("</body></html>"));
fileWriter.flush();
fileWriter.write(QStringLiteral("</body></html>"));
fileWriter.end();
testRenderTree(otp.parsedPart());
......
......@@ -38,31 +38,19 @@ public:
{
}
void begin(const QString &) override
void begin() override
{
}
void write(const QString &) override
void write(const QString &str) override
{
html += str;
}
void end() override
{
}
void reset() override
{
}
void queue(const QString &str) override
{
html.append(str);
}
void flush() override
{
}
void embedPart(const QByteArray &, const QString &) override
{
}
......
......@@ -320,9 +320,9 @@ public:
{
}
void begin(const QString &text) override
void begin() override
{
mBaseWriter->begin(text);
mBaseWriter->begin();
}
void write(const QString &str) override
......@@ -340,16 +340,6 @@ public:
mBaseWriter->reset();
}
void queue(const QString &str) override
{
html.append(str);
}
void flush() override
{
mBaseWriter->flush();
}
void embedPart(const QByteArray &contentId, const QString &url) override
{
mBaseWriter->embedPart(contentId, url);
......@@ -395,7 +385,7 @@ Interface::ObjectTreeSource *DefaultRendererPrivate::source() const
void DefaultRendererPrivate::renderSubParts(const MessagePart::Ptr &msgPart, const QSharedPointer<CacheHtmlWriter> &htmlWriter)
{
foreach (const auto &m, msgPart->subParts())
htmlWriter->queue(renderFactory(m, htmlWriter));
htmlWriter->write(renderFactory(m, htmlWriter));
}
QString DefaultRendererPrivate::render(const MessagePartList::Ptr &mp)
......@@ -471,7 +461,7 @@ QString DefaultRendererPrivate::render(const EncapsulatedRfc822MessagePart::Ptr
= HTMLBlock::Ptr(new AttachmentMarkBlock(htmlWriter.data(), mp->attachmentContent()));
}
const auto html = t->render(&c);
htmlWriter->queue(html);
htmlWriter->write(html);
}
return htmlWriter->html;
}
......@@ -523,7 +513,7 @@ QString DefaultRendererPrivate::render(const HtmlMessagePart::Ptr &mp)
= HTMLBlock::Ptr(new AttachmentMarkBlock(htmlWriter.data(), mp->attachmentContent()));
}
const auto html = t->render(&c);
htmlWriter->queue(html);
htmlWriter->write(html);
}
return htmlWriter->html;
}
......@@ -842,7 +832,7 @@ QString DefaultRendererPrivate::render(const SignedMessagePart::Ptr &mp)
= HTMLBlock::Ptr(new AttachmentMarkBlock(htmlWriter.data(),
mp->attachmentContent()));
}
htmlWriter->queue(renderSigned(mp));
htmlWriter->write(renderSigned(mp));
}
return htmlWriter->html;
}
......@@ -857,7 +847,7 @@ QString DefaultRendererPrivate::render(const SignedMessagePart::Ptr &mp)
} else if (!metaData.inProgress) {
auto part = renderWithFactory(QStringLiteral("MimeTreeParser::MessagePart"), mp);
if (part) {
htmlWriter->queue(part->html());
htmlWriter->write(part->html());
}
}
}
......@@ -877,7 +867,7 @@ QString DefaultRendererPrivate::render(const EncryptedMessagePart::Ptr &mp)
= HTMLBlock::Ptr(new AttachmentMarkBlock(htmlWriter.data(),
mp->attachmentContent()));
}
htmlWriter->queue(renderEncrypted(mp));
htmlWriter->write(renderEncrypted(mp));
}
return htmlWriter->html;
}
......@@ -894,7 +884,7 @@ QString DefaultRendererPrivate::render(const EncryptedMessagePart::Ptr &mp)
} else if (!metaData.inProgress) {
auto part = renderWithFactory(QStringLiteral("MimeTreeParser::MessagePart"), mp);
if (part) {
htmlWriter->queue(part->html());
htmlWriter->write(part->html());
}
}
}
......@@ -925,7 +915,7 @@ QString DefaultRendererPrivate::render(const AlternativeMessagePart::Ptr &mp)
part = mp->mChildParts[mode];
}
htmlWriter->queue(render(part));
htmlWriter->write(render(part));
}
return htmlWriter->html;
}
......@@ -965,7 +955,7 @@ QString DefaultRendererPrivate::render(const CertMessagePart::Ptr &mp)
= HTMLBlock::Ptr(new AttachmentMarkBlock(htmlWriter.data(), mp->attachmentContent()));
}
const auto html = t->render(&c);
htmlWriter->queue(html);
htmlWriter->write(html);
}
return htmlWriter->html;
}
......
......@@ -75,14 +75,14 @@ AttachmentMarkBlock::~AttachmentMarkBlock()
void AttachmentMarkBlock::internalEnter()
{
if (mWriter) {
mWriter->queue(enter());
mWriter->write(enter());
}
}
void AttachmentMarkBlock::internalExit()
{
if (mWriter) {
mWriter->queue(exit());
mWriter->write(exit());
}
}
......@@ -113,14 +113,14 @@ RootBlock::~RootBlock()
void RootBlock::internalEnter()
{
if (mWriter) {
mWriter->queue(enter());
mWriter->write(enter());
}
}
void RootBlock::internalExit()
{
if (mWriter) {
mWriter->queue(exit());
mWriter->write(exit());
}
}
......
......@@ -48,7 +48,7 @@ public:
{
}
void begin(const QString &text) override
void begin() override
{
}
......@@ -61,19 +61,6 @@ public:
{
}
void reset() override
{
}
void queue(const QString &str) override
{
html.append(str);
}
void flush() override
{
}
void embedPart(const QByteArray &contentId, const QString &url) override
{
embedParts.insert(contentId, url);
......@@ -109,7 +96,7 @@ QVector<QSharedPointer<PartRendered> > PartRendered::renderSubParts(
foreach (const auto &_m, mp->subParts()) {
CacheHtmlWriter cacheWriter;
DefaultRenderer::Ptr renderer = mp->source()->messagePartTheme(_m);
cacheWriter.queue(renderer->html());
cacheWriter.write(renderer->html());
ret.append(QSharedPointer<WrapperPartRendered>(new WrapperPartRendered(&cacheWriter)));
}
return ret;
......
......@@ -19,7 +19,6 @@
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
//#define MESSAGEVIEWER_READER_HTML_DEBUG 1
#include "viewer_p.h"
#include "viewer.h"
......@@ -45,10 +44,6 @@
#include "viewerplugins/viewerplugintoolmanager.h"
#include <KContacts/VCardConverter>
#include "htmlwriter/webengineembedpart.h"
#ifdef MESSAGEVIEWER_READER_HTML_DEBUG
#include <MimeMessagePart/FileHtmlWriter>
#include "htmlwriter/teehtmlwriter.h"
#endif
#include <PimCommon/NetworkUtil>
#include <unistd.h> // link()
//KDE includes
......@@ -905,8 +900,8 @@ void ViewerPrivate::displayMessage()
setDisplayFormatMessageOverwrite(attr->messageFormat());
}
htmlWriter()->begin(QString());
htmlWriter()->queue(mCSSHelper->htmlHead(mUseFixedFont));
htmlWriter()->begin();
htmlWriter()->write(mCSSHelper->htmlHead(mUseFixedFont));
if (!mMainWindow) {
q->setWindowTitle(mMessage->subject()->asUnicodeString());
......@@ -926,7 +921,7 @@ void ViewerPrivate::displayMessage()
mBackgroundError = scheme.background(KColorScheme::NegativeBackground).color();
}
htmlWriter()->queue(QStringLiteral(
htmlWriter()->write(QStringLiteral(
"<div style=\"background:%1;color:%2;border:1px solid %3\">%4</div>").arg(
mBackgroundError.
name(),
......@@ -936,7 +931,7 @@ void ViewerPrivate::displayMessage()
mForegroundError
.
name(), attr->message().toHtmlEscaped()));
htmlWriter()->queue(QStringLiteral("<p></p>"));
htmlWriter()->write(QStringLiteral("<p></p>"));
}
parseContent(mMessage.data());
......@@ -945,7 +940,7 @@ void ViewerPrivate::displayMessage()
#endif
mColorBar->update();
htmlWriter()->queue(QStringLiteral("</body></html>"));
htmlWriter()->write(QStringLiteral("</body></html>"));
connect(mViewer, &MailWebEngineView::loadFinished, this,
&ViewerPrivate::executeCustomScriptsAfterLoading, Qt::UniqueConnection);
connect(
......@@ -958,7 +953,7 @@ void ViewerPrivate::displayMessage()
"attachmentInjectionPoint"));
mViewer->addScript(js, QStringLiteral("attachment_injection"), QWebEngineScript::DocumentReady);
htmlWriter()->flush();
htmlWriter()->end();
}
void ViewerPrivate::collectionFetchedForStoringDecryptedMessage(KJob *job)
......@@ -1070,7 +1065,7 @@ void ViewerPrivate::parseContent(KMime::Content *content)
KMime::Message *message = dynamic_cast<KMime::Message *>(content);
if (message) {
htmlWriter()->queue(writeMsgHeader(message, hasVCard ? vCardContent : nullptr, true));
htmlWriter()->write(writeMsgHeader(message, hasVCard ? vCardContent : nullptr, true));
}
// Pass control to the OTP now, which does the real work
......@@ -1150,12 +1145,7 @@ void ViewerPrivate::initHtmlWidget()
{
if (!htmlWriter()) {
mPartHtmlWriter = new WebEnginePartHtmlWriter(mViewer, nullptr);
#ifdef MESSAGEVIEWER_READER_HTML_DEBUG
mHtmlWriter = new TeeHtmlWriter(new FileHtmlWriter(QString()),
mPartHtmlWriter);
#else
mHtmlWriter = mPartHtmlWriter;
#endif
}
connect(mViewer->page(), &QWebEnginePage::linkHovered,
this, &ViewerPrivate::slotUrlOn);
......@@ -1458,13 +1448,13 @@ void ViewerPrivate::setMessagePart(KMime::Content *node)
}
}
htmlWriter()->begin(QString());
htmlWriter()->queue(mCSSHelper->htmlHead(mUseFixedFont));
htmlWriter()->begin();
htmlWriter()->write(mCSSHelper->htmlHead(mUseFixedFont));
parseContent(node);
htmlWriter()->queue(QStringLiteral("</body></html>"));
htmlWriter()->flush();
htmlWriter()->write(QStringLiteral("</body></html>"));
htmlWriter()->end();
}
}
......@@ -2354,7 +2344,7 @@ void ViewerPrivate::updateReaderWin()
#ifndef QT_NO_TREEVIEW
mMimePartTree->hide();
#endif
htmlWriter()->begin(QString());
htmlWriter()->begin();
htmlWriter()->write(mCSSHelper->htmlHead(mUseFixedFont) + QLatin1String("</body></html>"));
htmlWriter()->end();
}
......
......@@ -31,31 +31,19 @@ public:
{
}
void begin(const QString &) override
void begin() override
{
}
void write(const QString &) override
void write(const QString &str) override
{
html += str;
}
void end() override
{
}
void reset() override
{
}
void queue(const QString &str) override
{
html.append(str);