Changeset View
Changeset View
Standalone View
Standalone View
plugins/messageviewer/bodypartformatter/pkpass/pkpass_plugin.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 "pkpassfile.h" | ||||
21 | #include "pkpassboardingpass.h" | ||||
22 | | ||||
23 | #include <MessageViewer/MessagePartRendererBase> | ||||
24 | #include <MessageViewer/MessagePartRendererManager> | ||||
25 | #include <MessageViewer/MessagePartRenderPlugin> | ||||
26 | | ||||
27 | #include <MimeTreeParser/HtmlWriter> | ||||
28 | #include <MimeTreeParser/MessagePart> | ||||
29 | | ||||
30 | #include <grantlee/metatype.h> | ||||
31 | #include <grantlee/template.h> | ||||
32 | | ||||
33 | #include <QDebug> | ||||
34 | #include <QImage> | ||||
35 | #include <QMetaProperty> | ||||
36 | #include <QUrl> | ||||
37 | | ||||
38 | // Grantlee has no Q_GADGET support yet | ||||
39 | GRANTLEE_BEGIN_LOOKUP(PkPassField) | ||||
40 | const auto idx = PkPassField::staticMetaObject.indexOfProperty(property.toUtf8().constData()); | ||||
41 | if (idx < 0) | ||||
42 | return {}; | ||||
43 | const auto mp = PkPassField::staticMetaObject.property(idx); | ||||
44 | return mp.readOnGadget(&object); | ||||
45 | GRANTLEE_END_LOOKUP | ||||
46 | | ||||
47 | namespace { | ||||
48 | class Formatter : public MessageViewer::MessagePartRendererBase | ||||
49 | { | ||||
50 | public: | ||||
51 | bool render(const MimeTreeParser::MessagePartPtr &msgPart, MimeTreeParser::HtmlWriter *htmlWriter, MessageViewer::RenderContext *context) const override | ||||
52 | { | ||||
53 | Q_UNUSED(context); | ||||
54 | auto mp = msgPart.dynamicCast<MimeTreeParser::AttachmentMessagePart>(); | ||||
55 | if (!mp || mp->isHidden()) { | ||||
56 | return false; | ||||
57 | } | ||||
58 | | ||||
59 | std::unique_ptr<PkPassFile> pass(PkPassFile::fromData(msgPart->content()->decodedContent())); | ||||
60 | if (!qobject_cast<PkPassBoardingPass*>(pass.get())) | ||||
61 | return false; // only boarding passes implemented so far | ||||
62 | | ||||
63 | const auto dir = mp->nodeHelper()->createTempDir(QStringLiteral("pkpass")); | ||||
64 | const auto logo = pass->logo(); | ||||
65 | if (!logo.isNull()) { | ||||
66 | const auto fileName = dir + QStringLiteral("/logo.png"); | ||||
67 | logo.save(fileName); | ||||
68 | pass->setProperty("logoUrl", QUrl::fromLocalFile(fileName)); | ||||
69 | mp->nodeHelper()->addTempFile(fileName); | ||||
70 | } | ||||
71 | | ||||
72 | const auto barcode = pass->barcode(); | ||||
73 | if (!barcode.isNull()) { | ||||
74 | const auto fileName = dir + QStringLiteral("/barcode.png"); | ||||
75 | barcode.save(fileName); | ||||
76 | pass->setProperty("barcodeUrl", QUrl::fromLocalFile(fileName)); | ||||
77 | mp->nodeHelper()->addTempFile(fileName); | ||||
78 | } | ||||
79 | | ||||
80 | auto c = MessageViewer::MessagePartRendererManager::self()->createContext(); | ||||
81 | c.insert(QStringLiteral("block"), mp.data()); | ||||
82 | c.insert(QStringLiteral("pass"), pass.get()); | ||||
83 | auto t = MessageViewer::MessagePartRendererManager::self()->loadByName(QStringLiteral(":/org.kde.messageviewer/pkpass/boardingpass.html")); | ||||
84 | Grantlee::OutputStream s(htmlWriter->stream()); | ||||
85 | t->render(&s, &c); | ||||
86 | return true; | ||||
87 | } | ||||
88 | }; | ||||
89 | | ||||
90 | class Plugin : public QObject, public MessageViewer::MessagePartRenderPlugin | ||||
91 | { | ||||
92 | Q_OBJECT | ||||
93 | Q_INTERFACES(MessageViewer::MessagePartRenderPlugin) | ||||
94 | Q_PLUGIN_METADATA(IID "com.kde.messageviewer.bodypartformatter" FILE "pkpass_plugin.json") | ||||
95 | public: | ||||
96 | explicit Plugin(QObject *parent = nullptr) | ||||
97 | : QObject(parent) | ||||
98 | { | ||||
99 | Grantlee::registerMetaType<PkPassField>(); | ||||
100 | } | ||||
101 | | ||||
102 | MessageViewer::MessagePartRendererBase* renderer(int index) override | ||||
103 | { | ||||
104 | return index == 0 ? new Formatter() : nullptr; | ||||
105 | } | ||||
106 | }; | ||||
107 | | ||||
108 | } | ||||
109 | | ||||
110 | #include "pkpass_plugin.moc" |