Changeset View
Changeset View
Standalone View
Standalone View
plugins/messageviewer/bodypartformatter/semantic/semanticrenderer.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright (c) 2017 Volker Krause <vkrause@kde.org> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or modify it | ||||
5 | under the terms of the GNU Library General Public License as published by | ||||
6 | the Free Software Foundation; either version 2 of the License, or (at your | ||||
7 | option) any later version. | ||||
8 | | ||||
9 | This library is distributed in the hope that it will be useful, but WITHOUT | ||||
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||||
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||||
12 | License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU Library General Public License | ||||
15 | along with this library; see the file COPYING.LIB. If not, write to the | ||||
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||||
17 | 02110-1301, USA. | ||||
18 | */ | ||||
19 | | ||||
20 | #include "semanticrenderer.h" | ||||
21 | #include "datatypes.h" | ||||
22 | #include "jsonlddocument.h" | ||||
23 | #include "semanticmemento.h" | ||||
24 | #include "semantic_debug.h" | ||||
25 | | ||||
26 | #include <MessageViewer/MessagePartRendererManager> | ||||
27 | #include <MimeTreeParser/HtmlWriter> | ||||
28 | | ||||
29 | #include <grantlee/metatype.h> | ||||
30 | #include <grantlee/template.h> | ||||
31 | | ||||
32 | #include <QDebug> | ||||
33 | #include <QGuiApplication> | ||||
34 | #include <QJsonDocument> | ||||
35 | #include <QMetaProperty> | ||||
36 | #include <QPalette> | ||||
37 | | ||||
38 | // Grantlee has no Q_GADGET support yet | ||||
39 | #define GRANTLEE_MAKE_GADGET(Class) \ | ||||
40 | GRANTLEE_BEGIN_LOOKUP(Class) \ | ||||
41 | const auto idx = Class::staticMetaObject.indexOfProperty(property.toUtf8().constData()); \ | ||||
42 | if (idx < 0) \ | ||||
43 | return {}; \ | ||||
44 | const auto mp = Class::staticMetaObject.property(idx); \ | ||||
45 | return mp.readOnGadget(&object); \ | ||||
46 | GRANTLEE_END_LOOKUP | ||||
47 | | ||||
48 | GRANTLEE_MAKE_GADGET(Airport) | ||||
49 | GRANTLEE_MAKE_GADGET(Airline) | ||||
50 | GRANTLEE_MAKE_GADGET(Flight) | ||||
51 | GRANTLEE_MAKE_GADGET(FlightReservation) | ||||
52 | GRANTLEE_MAKE_GADGET(LodgingBusiness) | ||||
53 | GRANTLEE_MAKE_GADGET(LodgingReservation) | ||||
54 | GRANTLEE_MAKE_GADGET(PostalAddress) | ||||
55 | | ||||
56 | SemanticRenderer::SemanticRenderer() | ||||
57 | { | ||||
58 | Grantlee::registerMetaType<Airport>(); | ||||
59 | Grantlee::registerMetaType<Airline>(); | ||||
60 | Grantlee::registerMetaType<Flight>(); | ||||
61 | Grantlee::registerMetaType<FlightReservation>(); | ||||
62 | Grantlee::registerMetaType<LodgingBusiness>(); | ||||
63 | Grantlee::registerMetaType<LodgingReservation>(); | ||||
64 | Grantlee::registerMetaType<PostalAddress>(); | ||||
65 | } | ||||
66 | | ||||
67 | bool SemanticRenderer::render(const MimeTreeParser::MessagePartPtr &msgPart, MimeTreeParser::HtmlWriter *htmlWriter, MessageViewer::RenderContext *context) const | ||||
68 | { | ||||
69 | Q_UNUSED(context); | ||||
70 | const auto mpList = msgPart.dynamicCast<MimeTreeParser::MessagePartList>(); | ||||
71 | if (!msgPart->isRoot() || !mpList->hasSubParts()) | ||||
72 | return false; | ||||
73 | | ||||
74 | qCDebug(SEMANTIC_LOG) << "========================================= Semantic Rendering"; | ||||
75 | const auto node = mpList->subParts().at(0)->content(); | ||||
76 | const auto nodeHelper = msgPart->nodeHelper(); | ||||
77 | if (!nodeHelper || !node) | ||||
78 | return false; | ||||
79 | | ||||
80 | auto memento = dynamic_cast<SemanticMemento*>(nodeHelper->bodyPartMemento(node->topLevel(), "org.kde.messageviewer.semanticData")); | ||||
81 | if (!memento || memento->isEmpty()) | ||||
82 | return false; | ||||
83 | | ||||
84 | const auto decodedData = JsonLdDocument::fromJson(memento->data()); | ||||
85 | if (decodedData.isEmpty()) { | ||||
86 | qCDebug(SEMANTIC_LOG) << "Unhandled content:" << QJsonDocument(memento->data()).toJson(); | ||||
87 | return false; | ||||
88 | } | ||||
89 | | ||||
90 | auto c = MessageViewer::MessagePartRendererManager::self()->createContext(); | ||||
91 | c.insert(QStringLiteral("data"), decodedData); | ||||
92 | | ||||
93 | const auto pal = qGuiApp->palette(); | ||||
94 | QVariantMap style; | ||||
95 | style.insert(QStringLiteral("frameColor"), pal.link().color().name()); | ||||
96 | c.insert(QStringLiteral("style"), style); | ||||
97 | | ||||
98 | auto t = MessageViewer::MessagePartRendererManager::self()->loadByName(QStringLiteral(":/org.kde.messageviewer/semantic/semantic.html")); | ||||
99 | Grantlee::OutputStream s(htmlWriter->stream()); | ||||
100 | t->render(&s, &c); | ||||
101 | return false; // yes, false, we want the rest of the email rendered normally after this | ||||
102 | } |