Commit 203890d8 authored by Volker Krause's avatar Volker Krause

Add renderer plugin loader

Summary:
With this it's now possible to implement the diff highlighter using the
new API. However, it's far from convenient still as we can't easily
re-use Grantlee template fragments for the attachment containers yet.

Reviewers: knauss

Reviewed By: knauss

Subscribers: #kde_pim

Tags: #kde_pim

Differential Revision: https://phabricator.kde.org/D8114
parent 846c3722
......@@ -165,6 +165,7 @@ set(libmessageviewer_messagepartthemes_default_SRCS
messagepartthemes/default/plugins/textmessagepartrenderer.cpp
messagepartthemes/default/plugins/quotehtml.cpp
messagepartthemes/default/messagepartrenderbase.cpp
messagepartthemes/default/messagepartrenderplugin.cpp
messagepartthemes/default/messagepartrendererfactory.cpp
)
......@@ -366,6 +367,16 @@ ecm_generate_headers(MessageViewer_Camelcaseviewerplugin_HEADERS
RELATIVE viewerplugins
)
ecm_generate_headers(MessageViewer_Camelcaserenderer_HEADERS
HEADER_NAMES
HtmlBlock
MessagePartRendererBase
MessagePartRenderPlugin
REQUIRED_HEADERS MessageViewer_renderer_HEADERS
PREFIX MessageViewer
RELATIVE messagepartthemes/default
)
ecm_generate_pri_file(BASE_NAME MessageViewer
LIB_NAME KF5MessageViewer
DEPS "PimCommon MessageCore AkonadiCore AkonadiMime Contacts Libkleo MimeTreeParser" FILENAME_VAR PRI_FILENAME INCLUDE_INSTALL_DIR ${KDE_INSTALL_INCLUDEDIR_KF5}/MessageViewer
......@@ -384,6 +395,7 @@ install(FILES
${MessageViewer_Camelcaseantispam_HEADERS}
${MessageViewer_Camelfindbar_HEADERS}
${MessageViewer_Camelcasescam_HEADERS}
${MessageViewer_Camelcaserenderer_HEADERS}
DESTINATION ${KDE_INSTALL_INCLUDEDIR_KF5}/MessageViewer
COMPONENT Devel
)
......@@ -401,6 +413,7 @@ install(FILES
${MessageViewer_widgets_HEADERS}
${MessageViewer_antispam_HEADERS}
${MessageViewer_findbar_HEADERS}
${MessageViewer_renderer_HEADERS}
${CMAKE_CURRENT_BINARY_DIR}/messageviewer_export.h
${CMAKE_CURRENT_BINARY_DIR}/globalsettings_messageviewer.h
${CMAKE_CURRENT_BINARY_DIR}/messageviewer_debug.h
......
......@@ -861,12 +861,11 @@ bool DefaultRendererPrivate::renderWithFactory(const QString &className, const M
{
if (!mRendererFactory)
return false;
const auto registry = mRendererFactory->typeRegistry(className);
if (registry.empty())
return false;
const auto plugin = registry.at(0);
return plugin->render(msgPart, htmlWriter, this);
for (auto r : mRendererFactory->typeRegistry(className)) {
if (r->render(msgPart, htmlWriter, this))
return true;
}
return false;
}
QString DefaultRendererPrivate::renderFactory(const MessagePart::Ptr &msgPart, HtmlWriter *_htmlWriter)
......
......@@ -20,6 +20,8 @@
#ifndef __MIMETREEPARSER_HTMLBLOCK_H__
#define __MIMETREEPARSER_HTMLBLOCK_H__
#include "messageviewer_export.h"
#include <QString>
#include <QSharedPointer>
......@@ -30,7 +32,7 @@ class Content;
namespace MimeTreeParser {
class HtmlWriter;
class HTMLBlock
class MESSAGEVIEWER_EXPORT HTMLBlock
{
public:
typedef QSharedPointer<HTMLBlock> Ptr;
......@@ -53,7 +55,7 @@ private:
// The attachment mark is a div that is placed around the attchment. It is used for drawing
// a yellow border around the attachment when scrolling to it. When scrolling to it, the border
// color of the div is changed, see KMReaderWin::scrollToAttachment().
class AttachmentMarkBlock : public HTMLBlock
class MESSAGEVIEWER_EXPORT AttachmentMarkBlock : public HTMLBlock
{
public:
AttachmentMarkBlock(MimeTreeParser::HtmlWriter *writer, KMime::Content *node);
......
......@@ -47,7 +47,7 @@ namespace MessageViewer {
class CSSHelperBase;
class RenderContext
class MESSAGEVIEWER_EXPORT RenderContext
{
public:
virtual ~RenderContext();
......@@ -56,7 +56,7 @@ public:
virtual bool renderWithFactory(const QString &className, const MimeTreeParser::MessagePartPtr &msgPart, MimeTreeParser::HtmlWriter *writer) = 0;
};
class MessagePartRendererBase
class MESSAGEVIEWER_EXPORT MessagePartRendererBase
{
public:
MessagePartRendererBase();
......
......@@ -30,6 +30,7 @@
#include "messagepartrendererfactory.h"
#include "messagepartrendererfactory_p.h"
#include "messagepartrenderplugin.h"
#include "messagepartrendererbase.h"
#include "messageviewer_debug.h"
......@@ -38,6 +39,11 @@
#include "plugins/messagepartrenderer.h"
#include "plugins/textmessagepartrenderer.h"
#include <KPluginLoader>
#include <QJsonArray>
#include <QJsonObject>
using namespace MessageViewer;
void MessagePartRendererFactoryPrivate::setup()
......@@ -50,7 +56,33 @@ void MessagePartRendererFactoryPrivate::setup()
void MessagePartRendererFactoryPrivate::loadPlugins()
{
qCDebug(MESSAGEVIEWER_LOG) << "plugin loading is not enabled in libmimetreeparser";
KPluginLoader::forEachPlugin(QStringLiteral("messageviewer/bodypartformatter"), [this](const QString &path) {
QPluginLoader loader(path);
const auto pluginData = loader.metaData().value(QLatin1String("MetaData")).toObject().value(QLatin1String("renderer")).toArray();
if (pluginData.isEmpty()) {
qCWarning(MESSAGEVIEWER_LOG) << "Plugin" << path << "has no meta data.";
return;
}
auto plugin = qobject_cast<MessagePartRenderPlugin*>(loader.instance());
if (!plugin) {
qCWarning(MESSAGEVIEWER_LOG) << path << "is not a MessagePartRendererPlugin";
return;
}
MessagePartRendererBase *renderer = nullptr;
for (int i = 0; (renderer = plugin->renderer(i)) && i < pluginData.size(); ++i) {
const auto metaData = pluginData.at(i).toObject();
const auto type = metaData.value(QLatin1String("type")).toString();
if (type.isEmpty()) {
qCWarning(MESSAGEVIEWER_LOG) << path << "returned empty type specification for index" << i;
break;
}
// TODO add plugin priority like we have for BPFs
qCDebug(MESSAGEVIEWER_LOG) << "renderer plugin for " << type;
insert(type, renderer/*, priority*/);
}
});
}
void MessagePartRendererFactoryPrivate::initalize_builtin_renderers()
......
/*
Copyright (c) 2017 Volker Krause <vkrause@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 "messagepartrenderplugin.h"
using namespace MessageViewer;
MessagePartRenderPlugin::~MessagePartRenderPlugin() = default;
/*
Copyright (c) 2017 Volker Krause <vkrause@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_MESSAGEPARTRENDERPLUGIN_H
#define MESSAGEVIEWER_MESSAGEPARTRENDERPLUGIN_H
#include "messageviewer_export.h"
#include <QtPlugin>
namespace MessageViewer {
class MessagePartRendererBase;
/**
* Plugin interface for MessagePartRendererBase instances.
*/
class MESSAGEVIEWER_EXPORT MessagePartRenderPlugin
{
public:
virtual ~MessagePartRenderPlugin();
virtual MessagePartRendererBase* renderer(int index) = 0;
};
}
Q_DECLARE_INTERFACE(MessageViewer::MessagePartRenderPlugin, "org.kde.messageviewer.messagepartrenderer/1.0")
#endif // MESSAGEVIEWER_MESSAGEPARTRENDERPLUGIN_H
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