Commit 023c3ad1 authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Try to add specific body style.

parent cc0c830a
......@@ -66,6 +66,11 @@ void BufferedHtmlWriter::embedPart(const QByteArray &contentId, const QString &u
Q_UNUSED(url);
}
void BufferedHtmlWriter::setStyleBody(const QString &styleBody)
{
Q_UNUSED(styleBody)
}
QByteArray BufferedHtmlWriter::data() const
{
return m_data;
......
......@@ -46,6 +46,7 @@ public:
void setExtraHead(const QString &str) override;
void embedPart(const QByteArray &contentId, const QString &url) override;
void setStyleBody(const QString &styleBody) override;
protected:
QByteArray m_data;
......
......@@ -94,3 +94,8 @@ void FileHtmlWriter::embedPart(const QByteArray &contentId, const QString &url)
void FileHtmlWriter::setExtraHead(const QString &)
{
}
void FileHtmlWriter::setStyleBody(const QString &)
{
}
......@@ -54,6 +54,7 @@ public:
Q_REQUIRED_RESULT QIODevice *device() const override;
void embedPart(const QByteArray &contentId, const QString &url) override;
void setExtraHead(const QString &str) override;
void setStyleBody(const QString &styleBody) override;
private:
QFile mFile;
......
......@@ -66,6 +66,10 @@ void WebEnginePartHtmlWriter::end()
insertExtraHead();
mExtraHead.clear();
}
if (!mStyleBody.isEmpty()) {
//insertBodyStyle();
mStyleBody.clear();
}
// see QWebEnginePage::setHtml()
//mHtmlView->setContent(data(), QStringLiteral("text/html;charset=UTF-8"), QUrl(QStringLiteral("file:///")));
......@@ -108,12 +112,26 @@ void WebEnginePartHtmlWriter::embedPart(const QByteArray &contentId, const QStri
void WebEnginePartHtmlWriter::insertExtraHead()
{
const auto headTag(QByteArrayLiteral("<head>"));
const int index = m_data.indexOf(headTag);
const int index = m_data.indexOf(headTag);
if (index != -1) {
m_data.insert(index + headTag.length(), mExtraHead.toUtf8());
}
}
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;
}
void WebEnginePartHtmlWriter::setExtraHead(const QString &str)
{
mExtraHead = str;
......
......@@ -42,15 +42,19 @@ public:
void embedPart(const QByteArray &contentId, const QString &url) override;
void setExtraHead(const QString &str) override;
void setStyleBody(const QString &styleBody) override;
Q_SIGNALS:
void finished();
private:
void insertExtraHead();
void insertBodyStyle();
private:
MailWebEngineView *mHtmlView = nullptr;
QString mExtraHead;
QString mStyleBody;
enum State {
Begun,
Queued,
......
......@@ -95,6 +95,8 @@ public:
virtual void setExtraHead(const QString &str) = 0;
virtual void setStyleBody(const QString &styleBody) = 0;
void setCodec(const QByteArray &codec);
Q_REQUIRED_RESULT QByteArray codec() const;
private:
......
......@@ -156,6 +156,21 @@ void MessageViewerUtilsTest::shouldExtractHtml_data()
}
}
void MessageViewerUtilsTest::shouldExtractBodyStyle()
{
QFETCH(QString, input);
QFETCH(QString, output);
QCOMPARE(MessageViewer::Util::parseBodyStyle(input), output);
}
void MessageViewerUtilsTest::shouldExtractBodyStyle_data()
{
QTest::addColumn<QString>("input");
QTest::addColumn<QString>("output");
QTest::newRow("empty") << QString() << QString();
QTest::newRow("test1") << QStringLiteral("<body style=\"overflow-wrap:break-word; word-break: break-word;white-space:pre-wrap;\">")
<< QStringLiteral("style=\"white-space:pre-wrap;\"");
}
void MessageViewerUtilsTest::shouldExtractHtmlBenchmark()
{
......
......@@ -39,6 +39,10 @@ private Q_SLOTS:
void shouldExtractHtml();
void shouldExtractHtml_data();
void shouldExtractBodyStyle();
void shouldExtractBodyStyle_data();
void shouldUseCorrectCodec();
void shouldUseCorrectCodec_data();
};
......
......@@ -28,6 +28,7 @@
#include <Akonadi/KMime/MessageFlags>
#include <QTimer>
using namespace MessageViewer;
Q_GLOBAL_STATIC(Akonadi::Item::List, sListItem)
......@@ -79,20 +80,22 @@ MarkMessageReadHandler::~MarkMessageReadHandler()
void MarkMessageReadHandler::setItem(const Akonadi::Item &item)
{
if (sListItem->contains(item) || d->mItemQueue == item
|| item.hasFlag(Akonadi::MessageFlags::Queued)) {
return;
}
d->mTimer.stop();
if (MessageViewer::MessageViewerSettings::self()->delayedMarkAsRead()) {
if (sListItem->contains(item) || d->mItemQueue == item
|| item.hasFlag(Akonadi::MessageFlags::Queued)) {
return;
}
if (d->mTimer.isActive()) {
d->mTimer.stop();
}
if (item.hasFlag(Akonadi::MessageFlags::Seen)) {
return;
}
sListItem->removeAll(d->mItemQueue);
d->mItemQueue = item;
sListItem->append(item);
if (item.hasFlag(Akonadi::MessageFlags::Seen)) {
return;
}
sListItem->removeAll(d->mItemQueue);
d->mItemQueue = item;
sListItem->append(item);
if (MessageViewer::MessageViewerSettings::self()->delayedMarkAsRead()) {
const int delayedMarkTime = MessageViewer::MessageViewerSettings::self()->delayedMarkTime();
if (delayedMarkTime != 0) {
d->mTimer.start(delayedMarkTime * 1000);
......
......@@ -688,6 +688,26 @@ void Util::readGravatarConfig()
}
}
QString Util::parseBodyStyle(const QString &style)
{
const int indexStyle = style.indexOf(QLatin1String("style=\""));
if (indexStyle != -1) {
const int indexEnd = style.indexOf(QLatin1Char('"'), indexStyle + 7);
if (indexEnd != -1) {
const QStringRef styleStr = style.midRef(indexStyle + 7, indexEnd - (indexStyle + 7));
const auto lstStyle = styleStr.split(QLatin1Char(';'), Qt::SkipEmptyParts);
QStringList lst;
for (const auto &style : lstStyle) {
if (style.trimmed().contains(QLatin1String("white-space"))) {
lst.append(style.toString().trimmed());
}
}
return QStringLiteral(" style=\"%1").arg(lst.join(QLatin1Char(';'))) + QStringLiteral(";\"");
}
}
return QString();
}
// FIXME this used to go through the full webkit parser to extract the body and head blocks
// until we have that back, at least attempt to fix some of the damage
// yes, "parsing" HTML with regexps is very very wrong, but it's still better than not filtering
......
......@@ -87,6 +87,7 @@ struct HtmlMessageInfo {
Q_REQUIRED_RESULT MESSAGEVIEWER_EXPORT HtmlMessageInfo processHtml(const QString &htmlSource);
Q_REQUIRED_RESULT MESSAGEVIEWER_EXPORT QByteArray htmlCodec(const QByteArray &data, const QByteArray &currentCodec);
Q_REQUIRED_RESULT MESSAGEVIEWER_EXPORT QString generateFileNameForExtension(const Akonadi::Item &msgBase, const QString &extension);
Q_REQUIRED_RESULT MESSAGEVIEWER_EXPORT QString parseBodyStyle(const QString &style);
}
}
Q_DECLARE_TYPEINFO(MessageViewer::Util::HtmlMessageInfo, Q_MOVABLE_TYPE);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment