Commit b1696f34 authored by Sandro Knauß's avatar Sandro Knauß

Start with interface for plugins for DefaultRenderer

parent ae63d01b
......@@ -159,6 +159,12 @@ set(libmessageviewer_messagepartthemes_default_SRCS
messagepartthemes/default/defaultrenderer.cpp
messagepartthemes/default/htmlblock.cpp
messagepartthemes/default/messagepartrenderermanager.cpp
messagepartthemes/default/partrendered.cpp
messagepartthemes/default/rendererpluginfactorysingleton.cpp
messagepartthemes/default/plugins/messagepartrenderer.cpp
messagepartthemes/default/plugins/plugins.cpp
messagepartthemes/default/messagepartrenderbase.cpp
messagepartthemes/default/messagepartrendererfactorybase.cpp
)
set(libmessageviewer_SRCS
......
......@@ -23,8 +23,13 @@
#include "messageviewer_debug.h"
#include "rendererpluginfactorysingleton.h"
#include "converthtmltoplaintext.h"
#include "messagepartrendererbase.h"
#include "messagepartrendererfactorybase.h"
#include "htmlblock.h"
#include "partrendered.h"
#include "utils/iconnamecache.h"
#include "utils/mimetype.h"
#include "viewer/csshelperbase.h"
......@@ -46,9 +51,6 @@
#include <KTextToHTML>
#include <QApplication>
#include <QFile>
#include <QStandardPaths>
#include <QTextCodec>
#include <QUrl>
#include <grantlee/context.h>
......@@ -388,10 +390,11 @@ public:
MimeTreeParser::HtmlWriter *mBaseWriter;
};
DefaultRendererPrivate::DefaultRendererPrivate(const Interface::MessagePart::Ptr &msgPart, CSSHelperBase *cssHelper)
DefaultRendererPrivate::DefaultRendererPrivate(const Interface::MessagePart::Ptr &msgPart, CSSHelperBase *cssHelper, const MessagePartRendererFactoryBase *rendererFactory)
: mMsgPart(msgPart)
, mOldWriter(msgPart->htmlWriter())
, mCSSHelper(cssHelper)
, mRendererFactory(rendererFactory)
{
mHtml = renderFactory(mMsgPart, QSharedPointer<CacheHtmlWriter>());
}
......@@ -1280,10 +1283,39 @@ public:
return htmlWriter->html;
}
QSharedPointer<PartRendered> DefaultRendererPrivate::renderWithFactory(QString className, const Interface::MessagePart::Ptr &msgPart)
{
if (mRendererFactory) {
const auto registry = mRendererFactory->typeRegistry(className);
if (registry.size() > 0) {
const auto plugin = registry.at(0);
return plugin->render(this, msgPart);
}
}
return QSharedPointer<PartRendered>();
}
QString DefaultRendererPrivate::renderFactory(const Interface::MessagePart::Ptr &msgPart, const QSharedPointer<CacheHtmlWriter> &htmlWriter)
{
const QString className = QString::fromUtf8(msgPart->metaObject()->className());
const auto rendered = renderWithFactory(className, msgPart);
if (rendered) {
const auto parts = rendered->embededParts();
foreach(auto key, parts.keys()) {
htmlWriter->embedPart(key, parts.value(key));
}
foreach(auto entry, rendered->extraHeader()) {
htmlWriter->extraHead(entry);
}
return rendered->html();
}
if (className == QStringLiteral("MimeTreeParser::MessagePartList")) {
auto mp = msgPart.dynamicCast<MessagePartList>();
if (mp) {
......@@ -1358,7 +1390,7 @@ public:
}
DefaultRenderer::DefaultRenderer(const MimeTreeParser::Interface::MessagePart::Ptr &msgPart, CSSHelperBase *cssHelper)
: d(new MimeTreeParser::DefaultRendererPrivate(msgPart, cssHelper))
: d(new MimeTreeParser::DefaultRendererPrivate(msgPart, cssHelper, rendererPluginFactoryInstance()))
{
}
......
......@@ -31,6 +31,7 @@ class PartRendered;
namespace MessageViewer
{
class MessagePartRendererFactoryBase;
class CSSHelperBase;
}
......@@ -40,7 +41,7 @@ namespace MimeTreeParser
class DefaultRendererPrivate
{
public:
DefaultRendererPrivate(const Interface::MessagePart::Ptr &msgPart, CSSHelperBase *cssHelper);
DefaultRendererPrivate(const Interface::MessagePart::Ptr &msgPart, CSSHelperBase *cssHelper, const MessagePartRendererFactoryBase *rendererFactory);
~DefaultRendererPrivate();
inline QString alignText();
......
/*
Copyright (c) 2017 Sandro Knauß <sknauss@kde.org>
KMail is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
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 "messagepartrendererbase.h"
MessagePartRendererBase::MessagePartRendererBase()
{
}
MessagePartRendererBase::~MessagePartRendererBase()
{
}
/*
Copyright (c) 2017 Sandro Knauß <sknauss@kde.org>
KMail is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
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_MESSAGEPARTRENDERERBASE_H__
#define __MESSAGEVIEWER_MESSAGEPARTRENDERERBASE_H__
#include "messageviewer_export.h"
#include <memory>
#include <map>
#include <QMap>
#include <QString>
#include <QSharedPointer>
namespace MimeTreeParser
{
class DefaultRendererPrivate;
namespace Interface
{
class MessagePart;
typedef QSharedPointer<MessagePart> MessagePartPtr;
}
}
class PartRendered;
class MessagePartRendererBase {
public:
MessagePartRendererBase();
virtual ~MessagePartRendererBase();
virtual QSharedPointer<PartRendered> render(MimeTreeParser::DefaultRendererPrivate *, const MimeTreeParser::Interface::MessagePartPtr &) const = 0;
};
#endif
/*
This file is part of KMail, the KDE mail client.
Copyright (c) 2017 Sandro Knauß <sknauss@kde.org>
KMail is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
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 "messagepartrendererfactorybase.h"
#include "messagepartrendererfactorybase_p.h"
#include "messagepartrendererbase.h"
#include "messageviewer_debug.h"
using namespace MessageViewer;
MessagePartRendererFactoryBasePrivate::MessagePartRendererFactoryBasePrivate(MessagePartRendererFactoryBase *factory)
: q(factory)
, mAll(std::unique_ptr<TypeRegistry>(new TypeRegistry()))
{
}
MessagePartRendererFactoryBasePrivate::~MessagePartRendererFactoryBasePrivate()
{
}
void MessagePartRendererFactoryBasePrivate::setup()
{
if (mAll->isEmpty()) {
initalize_builtin_renderers();
q->loadPlugins();
}
}
void MessagePartRendererFactoryBasePrivate::insert(const QString &type, MessagePartRendererBase *formatter)
{
if (type.isEmpty() || !formatter) {
return;
}
(*mAll)[type].insert((*mAll)[type].begin(), formatter);
}
MessagePartRendererFactoryBase::MessagePartRendererFactoryBase()
: d(std::unique_ptr<MessagePartRendererFactoryBasePrivate>(new MessagePartRendererFactoryBasePrivate(this)))
{
}
MessagePartRendererFactoryBase::~MessagePartRendererFactoryBase()
{
}
void MessagePartRendererFactoryBase::insert(const QString &type, MessagePartRendererBase *formatter)
{
d->insert(type, formatter);
}
std::vector<MessagePartRendererBase *> MessagePartRendererFactoryBase::typeRegistry(const QString &type) const
{
d->setup();
Q_ASSERT(!d->mAll->isEmpty());
return d->mAll->value(type);
}
void MessagePartRendererFactoryBase::loadPlugins()
{
qCDebug(MESSAGEVIEWER_LOG) << "plugin loading is not enabled in libmimetreeparser";
}
/*
This file is part of KMail, the KDE mail client.
Copyright (c) 2017 Sandro Knauß <sknauss@kde.org>
KMail is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
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 __MIMETREEPARSER_MESSAGEPARTRENDERERBASEFACTORY_H__
#define __MIMETREEPARSER_MESSAGEPARTRENDERERBASEFACTORY_H__
#include "messageviewer_export.h"
#include <memory>
#include <map>
#include <QMap>
#include <QString>
#include <QSharedPointer>
class MessagePartRendererBase;
namespace MessageViewer
{
class MessagePartRendererFactoryBasePrivate;
class MESSAGEVIEWER_EXPORT MessagePartRendererFactoryBase
{
public:
MessagePartRendererFactoryBase();
virtual ~MessagePartRendererFactoryBase();
std::vector<MessagePartRendererBase *> typeRegistry(const QString &type) const;
protected:
void insert(const QString &type, MessagePartRendererBase *formatter);
virtual void loadPlugins();
private:
static MessagePartRendererFactoryBase *mSelf;
std::unique_ptr<MessagePartRendererFactoryBasePrivate> d;
friend class MessagePartRendererFactoryBasePrivate;
private:
// disabled
const MessagePartRendererFactoryBase &operator=(const MessagePartRendererFactoryBase &);
MessagePartRendererFactoryBase(const MessagePartRendererFactoryBase &);
};
}
#endif
/*
Copyright (c) 2017 Sandro Knauß <sknauss@kde.org>
KMail is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
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_MESSAGEPARTRENDERERBASEFACTORY_P_H__
#define __MESSAGEVIEWER_MESSAGEPARTRENDERERBASEFACTORY_P_H__
#include <map>
#include <QMap>
class MessagePartRendererBase;
namespace MessageViewer
{
class MessagePartRendererFactoryBase;
typedef QMap<QString, std::vector<MessagePartRendererBase*>> TypeRegistry;
class MessagePartRendererFactoryBasePrivate
{
public:
MessagePartRendererFactoryBasePrivate(MessagePartRendererFactoryBase *factory);
~MessagePartRendererFactoryBasePrivate();
void setup();
void initalize_builtin_renderers(); //defined in pugins/plugins.cpp
void insert(const QString &type, MessagePartRendererBase *formatter);
MessagePartRendererFactoryBase *q;
std::unique_ptr<TypeRegistry> mAll;
};
}
#endif
/*
Copyright (c) 2017 Sandro Knauß <sknauss@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library 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 Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "partrendered.h"
#include "defaultrenderer.h"
#include "defaultrenderer_p.h"
#include "htmlblock.h"
#include "autotests/util.h"
#include "autotests/setupenv.h"
#include "messagepartrenderermanager.h"
#include <MimeTreeParser/HtmlWriter>
#include <MimeTreeParser/MessagePart>
#include <MessageCore/StringUtil>
#include <grantlee/context.h>
#include <grantlee/template.h>
#include <QApplication>
#include <QDebug>
using namespace MessageViewer;
class CacheHtmlWriter : public MimeTreeParser::HtmlWriter
{
public:
explicit CacheHtmlWriter() {}
virtual ~CacheHtmlWriter() {}
void begin(const QString &text) Q_DECL_OVERRIDE {}
void write(const QString &str) Q_DECL_OVERRIDE {html.append(str);}
void end() Q_DECL_OVERRIDE {}
void reset() Q_DECL_OVERRIDE {}
void queue(const QString &str) Q_DECL_OVERRIDE {html.append(str);}
void flush() Q_DECL_OVERRIDE {}
void embedPart(const QByteArray &contentId, const QString &url) Q_DECL_OVERRIDE {embedParts.insert(contentId, url);}
void extraHead(const QString &extra) Q_DECL_OVERRIDE {head.append(extra);}
QString html;
QString head;
QMap<QByteArray, QString> embedParts;
};
PartRendered::PartRendered()
{
}
PartRendered::~PartRendered()
{
}
inline QString PartRendered::alignText()
{
return QApplication::isRightToLeft() ? QStringLiteral("rtl") : QStringLiteral("ltr");
}
QVector<QSharedPointer<PartRendered>> PartRendered::renderSubParts(MimeTreeParser::MessagePart::Ptr mp)
{
Test::CSSHelper testCSSHelper;
QVector<QSharedPointer<PartRendered>> ret;
foreach (const auto &_m, mp->subParts()) {
CacheHtmlWriter cacheWriter;
DefaultRenderer::Ptr renderer = mp->source()->messagePartTheme(_m);
cacheWriter.queue(renderer->html());
ret.append(QSharedPointer<WrapperPartRendered>(new WrapperPartRendered(&cacheWriter)));
}
return ret;
}
WrapperPartRendered::WrapperPartRendered(CacheHtmlWriter *htmlWriter)
: PartRendered()
{
mHtml = htmlWriter->html;
mHead = htmlWriter->head;
mEmbeded = htmlWriter->embedParts;
}
WrapperPartRendered::~WrapperPartRendered()
{
}
QString WrapperPartRendered::html()
{
return mHtml;
}
QString WrapperPartRendered::extraHeader()
{
return mHead;
}
QMap<QByteArray, QString> WrapperPartRendered::embededParts()
{
return mEmbeded;
}
/*
Copyright (c) 2017 Sandro Knauß <sknauss@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library 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 Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#ifndef __MESSAGEVIEWER_PARTRENDERED_H__
#define __MESSAGEVIEWER_PARTRENDERED_H__
#include <QMap>
#include <QString>
#include <QSharedPointer>
#include <QVector>
namespace MimeTreeParser
{
class MessagePart;
typedef QSharedPointer<MessagePart> MessagePartPtr;
class DefaultRendererPrivate;
}
namespace KMime
{
class Content;
}
class CacheHtmlWriter;
class PartRendered
{
public:
PartRendered();
virtual ~PartRendered();
inline QString alignText();
virtual QString html() = 0;
virtual QMap<QByteArray, QString> embededParts() = 0;
virtual QString extraHeader() = 0;