webengineparthtmlwriter.cpp 3.29 KB
Newer Older
1
/*
2
  SPDX-FileCopyrightText: 2016-2020 Laurent Montel <montel@kde.org>
3

4
  SPDX-License-Identifier: GPL-2.0-or-later
5 6
*/
#include "webengineparthtmlwriter.h"
Laurent Montel's avatar
Laurent Montel committed
7
#include "webengineembedpart.h"
8 9

#include "messageviewer_debug.h"
Laurent Montel's avatar
Laurent Montel committed
10
#include "viewer/webengine/mailwebengineview.h"
11
#include "messageviewer/messageviewerutil.h"
12 13 14 15
#include <QUrl>

#include <cassert>
#include <QByteArray>
16 17
#include <QTemporaryFile>
#include <QDir>
18 19 20

using namespace MessageViewer;

Laurent Montel's avatar
Laurent Montel committed
21
WebEnginePartHtmlWriter::WebEnginePartHtmlWriter(MailWebEngineView *view, QObject *parent)
Laurent Montel's avatar
Laurent Montel committed
22 23
    : QObject(parent)
    , mHtmlView(view)
24 25 26 27 28 29
{
    assert(view);
}

WebEnginePartHtmlWriter::~WebEnginePartHtmlWriter()
{
30
    delete mTempFile;
31 32
}

Volker Krause's avatar
Volker Krause committed
33
void WebEnginePartHtmlWriter::begin()
34 35 36 37 38 39
{
    if (mState != Ended) {
        qCWarning(MESSAGEVIEWER_LOG) << "begin() called on non-ended session!";
        reset();
    }

40 41
    delete mTempFile;

42
    BufferedHtmlWriter::begin();
Laurent Montel's avatar
Laurent Montel committed
43
    MessageViewer::WebEngineEmbedPart::self()->clear();
44 45
    mState = Begun;
}
Laurent Montel's avatar
Laurent Montel committed
46

47 48
void WebEnginePartHtmlWriter::end()
{
49
    BufferedHtmlWriter::end();
50 51 52 53 54 55 56
    if (mState != Begun) {
        qCWarning(MESSAGEVIEWER_LOG) << "Called on non-begun or queued session!";
    }
    if (!mExtraHead.isEmpty()) {
        insertExtraHead();
        mExtraHead.clear();
    }
57
    if (!mStyleBody.isEmpty()) {
Laurent Montel's avatar
Laurent Montel committed
58
        insertBodyStyle();
59 60
        mStyleBody.clear();
    }
61
    if (data().size() > 2000000) {
Laurent Montel's avatar
Laurent Montel committed
62 63 64 65
        qDebug() << " load big message ";
        mTempFile = new QTemporaryFile(QDir::tempPath() + QLatin1String("/messageviewer_XXXXXX")+ QLatin1String(".html"));
        mTempFile->open();
        QTextStream stream(mTempFile);
66

Laurent Montel's avatar
Laurent Montel committed
67 68 69 70 71 72 73 74 75 76 77 78
        const QByteArray codecValue = Util::htmlCodec(data(), codec());
        stream.setCodec(codecValue.constData());
        stream << data();
        //Bug 387061
        mHtmlView->load(QUrl::fromLocalFile(mTempFile->fileName()));
        //qDebug() << " tempFile.fileName()" << mTempFile->fileName();
        mHtmlView->show();
        mTempFile->close();
    } else {
        mHtmlView->setContent(data(), QStringLiteral("text/html;charset=UTF-8"), QUrl(QStringLiteral("file:///")));
        mHtmlView->show();
    }
79
    clear();
80 81 82 83 84 85 86 87 88

    mHtmlView->setUpdatesEnabled(true);
    mHtmlView->update();
    mState = Ended;
    Q_EMIT finished();
}

void WebEnginePartHtmlWriter::reset()
{
89
    BufferedHtmlWriter::reset();
90 91 92 93 94 95 96
    if (mState != Ended) {
        mState = Begun; // don't run into end()'s warning
        end();
        mState = Ended;
    }
}

Laurent Montel's avatar
Laurent Montel committed
97
void WebEnginePartHtmlWriter::embedPart(const QByteArray &contentId, const QString &contentURL)
98
{
Laurent Montel's avatar
Laurent Montel committed
99
    MessageViewer::WebEngineEmbedPart::self()->addEmbedPart(contentId, contentURL);
100 101 102 103
}

void WebEnginePartHtmlWriter::insertExtraHead()
{
104
    const auto headTag(QByteArrayLiteral("<head>"));
Laurent Montel's avatar
Laurent Montel committed
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 112 113 114 115 116 117 118 119 120 121 122 123 124
void WebEnginePartHtmlWriter::insertBodyStyle()
{
    const auto bodyTag(QByteArrayLiteral("<body>"));
    const int index = m_data.indexOf(bodyTag);
    if (index != -1) {
        m_data.insert(index + bodyTag.length() - 1, mStyleBody.toUtf8());
    }
}

void WebEnginePartHtmlWriter::setStyleBody(const QString &styleBody)
{
    mStyleBody = styleBody;
}

Laurent Montel's avatar
Laurent Montel committed
125
void WebEnginePartHtmlWriter::setExtraHead(const QString &str)
126 127 128
{
    mExtraHead = str;
}