5 #include "viewemailtemplate_p.h"
7 #include <Cutelyst/Context>
8 #include <SimpleMail/emailaddress.h>
9 #include <SimpleMail/mimemessage.h>
10 #include <SimpleMail/mimetext.h>
12 #include <QtCore/QLoggingCategory>
14 Q_LOGGING_CATEGORY(CUTELYST_VIEW_EMAILTEMPLATE,
"cutelyst.view.emailtemplate", QtWarningMsg)
19 :
ViewEmail(new ViewEmailTemplatePrivate, parent, name)
23 d->defaultContentType = QByteArrayLiteral(
"text/html");
29 return d->templatePrefix;
35 d->templatePrefix = prefix;
42 return d->defaultView;
48 d->defaultView = view;
52 std::shared_ptr<MimePart>
53 generatePart(
Context *c,
const ViewEmailTemplatePrivate *d,
const QVariantHash &partHash)
55 const QString defaultView = d->defaultView;
58 auto viewIt = partHash.constFind(QStringLiteral(
"view"));
59 if (viewIt != partHash.constEnd() && !viewIt.value().toString().isEmpty()) {
61 const QString viewString = viewIt.value().toString();
62 qCDebug(CUTELYST_VIEW_EMAILTEMPLATE)
63 <<
"Using specified view" << viewString <<
"for rendering.";
64 view = c->
view(viewString);
65 }
else if (!defaultView.
isEmpty()) {
67 qCDebug(CUTELYST_VIEW_EMAILTEMPLATE)
68 <<
"Using default view" << defaultView <<
"for rendering.";
69 view = c->
view(defaultView);
72 qCDebug(CUTELYST_VIEW_EMAILTEMPLATE) <<
"Using Cutelysts default view for rendering.";
78 c->
appendError(QStringLiteral(
"Could not find a view to render"));
82 QString templateString = partHash.value(QStringLiteral(
"template")).toString();
85 if (!d->templatePrefix.isEmpty()) {
86 templateString = d->templatePrefix +
QLatin1Char(
'/') + templateString;
90 const QVariantHash currentStash = c->
stash();
92 c->
setStash(QStringLiteral(
"template"), templateString);
95 qCDebug(CUTELYST_VIEW_EMAILTEMPLATE) <<
"Errors" << c->
errors();
97 c->
stash() = currentStash;
99 auto part = std::make_shared<MimePart>();
100 part->setContent(output);
102 d->setupAttributes(part, partHash);
112 QVariantHash email = c->
stash(d->stashKey).toHash();
113 const QString templateName = email.value(QStringLiteral(
"template")).toString();
114 const QVariantList templateList = email.value(QStringLiteral(
"templates")).toList();
115 if (templateName.
isEmpty() && templateList.isEmpty()) {
120 QVariantList parts = email.value(QStringLiteral(
"parts")).toList();
123 for (
const QVariant &part : templateList) {
124 const QVariantHash partHash = part.toHash();
125 auto partObj = generatePart(c, d, partHash);
129 }
else if (!templateName.
isEmpty()) {
131 QVariantHash partArgs({
132 {QStringLiteral(
"template"), templateName},
135 auto contentTypeIt = email.constFind(QStringLiteral(
"content_type"));
136 if (contentTypeIt != email.constEnd() && !contentTypeIt.value().toString().isEmpty()) {
137 partArgs.insert(QStringLiteral(
"content_type"), contentTypeIt.value().toString());
139 auto partObj = generatePart(c, d, partArgs);
142 email.insert(QStringLiteral(
"parts"), parts);
149 #include "moc_viewemailtemplate.cpp"
QStringList errors() const noexcept
void stash(const QVariantHash &unite)
void setStash(const QString &key, const QVariant &value)
View * view(QStringView name={}) const
void appendError(const QString &error)
bool error() const noexcept
A view that renders stash data using another view and sends it via e-mail.
void setDefaultView(const QString &view)
QByteArray render(Context *c) const override
ViewEmailTemplate(QObject *parent, const QString &name=QString())
void setTemplatePrefix(const QString &prefix)
A view that sends stash data via e-mail.
QByteArray render(Context *c) const override
Abstract View component for Cutelyst.
virtual QByteArray render(Context *c) const =0
The Cutelyst namespace holds all public Cutelyst API.
bool isEmpty() const const
QVariant fromValue(T &&value)