Cutelyst  2.3.0
viewemailtemplate.cpp
1 /*
2  * Copyright (C) 2015-2018 Daniel Nicoletti <dantti12@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 #include "viewemailtemplate_p.h"
19 
20 #include <Cutelyst/Context>
21 
22 #include <SimpleMail/mimemessage.h>
23 #include <SimpleMail/emailaddress.h>
24 #include <SimpleMail/mimetext.h>
25 
26 #include <QtCore/QLoggingCategory>
27 
28 Q_LOGGING_CATEGORY(CUTELYST_VIEW_EMAILTEMPLATE, "cutelyst.view.emailtemplate")
29 
30 using namespace Cutelyst;
31 
32 ViewEmailTemplate::ViewEmailTemplate(QObject *parent, const QString &name) : ViewEmail(new ViewEmailTemplatePrivate, parent, name)
33 {
34  Q_D(ViewEmailTemplate);
35 
36  d->defaultContentType = QByteArrayLiteral("text/html");
37 }
38 
39 QString ViewEmailTemplate::templatePrefix() const
40 {
41  Q_D(const ViewEmailTemplate);
42  return d->templatePrefix;
43 }
44 
45 void ViewEmailTemplate::setTemplatePrefix(const QString &prefix)
46 {
47  Q_D(ViewEmailTemplate);
48  d->templatePrefix = prefix;
49  Q_EMIT changedProp();
50 }
51 
52 QString ViewEmailTemplate::defaultView() const
53 {
54  Q_D(const ViewEmailTemplate);
55  return d->defaultView;
56 }
57 
58 void ViewEmailTemplate::setDefaultView(const QString &view)
59 {
60  Q_D(ViewEmailTemplate);
61  d->defaultView = view;
62  Q_EMIT changedProp();
63 }
64 
65 MimePart *generatePart(Context *c, const ViewEmailTemplatePrivate *d, const QVariantHash &partHash)
66 {
67  const QString defaultView = d->defaultView;
68 
69  View *view = nullptr;
70  auto viewIt = partHash.constFind(QStringLiteral("view"));
71  if (viewIt != partHash.constEnd() && !viewIt.value().toString().isEmpty()) {
72  // use the view specified for the email part
73  const QString viewString = viewIt.value().toString();
74  qCDebug(CUTELYST_VIEW_EMAILTEMPLATE) << "Using specified view" << viewString << "for rendering.";
75  view = c->view(viewString);
76  } else if (!defaultView.isEmpty()) {
77  // if none specified use the configured default view
78  qCDebug(CUTELYST_VIEW_EMAILTEMPLATE) << "Using default view" << defaultView << "for rendering.";
79  view = c->view(defaultView);
80  } else {
81  // else fallback to Cutelysts default view
82  qCDebug(CUTELYST_VIEW_EMAILTEMPLATE) << "Using Cutelysts default view for rendering.";
83  view = c->view(QString());
84  }
85 
86  // validate the per template view
87  if (!view) {
88  c->error(QStringLiteral("Could not find a view to render"));
89  return nullptr;
90  }
91 
92  QString templateString = partHash.value(QStringLiteral("template")).toString();;
93  // prefix with template_prefix if configured
94  if (!d->templatePrefix.isEmpty()) {
95  templateString = d->templatePrefix + QLatin1Char('/') + templateString;
96  }
97 
98  // render the email part
99  const QVariantHash currentStash = c->stash();
100  c->stash(partHash);
101  c->setStash(QStringLiteral("template"), templateString);
102  QByteArray output = view->render(c);
103  if (c->error()) {
104  qCDebug(CUTELYST_VIEW_EMAILTEMPLATE) << "Errors" << c->errors();
105  }
106  c->stash() = currentStash;
107 
108  MimePart *part = new MimePart();
109  part->setContent(output);
110 
111  d->setupAttributes(part, partHash);
112 
113  return part;
114 }
115 
116 QByteArray ViewEmailTemplate::render(Context *c) const
117 {
118  Q_D(const ViewEmailTemplate);
119 
120  QByteArray ret;
121  QVariantHash email = c->stash(d->stashKey).toHash();
122  const QString templateName = email.value(QStringLiteral("template")).toString();
123  const QVariantList templateList = email.value(QStringLiteral("templates")).toList();
124  if (templateName.isEmpty() && templateList.isEmpty()) {
125  ret = ViewEmail::render(c);
126  return ret;
127  }
128 
129  QVariantList parts = email.value(QStringLiteral("parts")).toList();
130  if (!templateList.isEmpty() && templateList.first().type() == QVariant::Hash) {
131  // multipart API
132  for (const QVariant &part : templateList) {
133  const QVariantHash partHash = part.toHash();
134  MimePart *partObj = generatePart(c, d, partHash);
135  parts.append(QVariant::fromValue(partObj));
136  }
137 
138  } else if (!templateName.isEmpty()) {
139  // single part API
140  QVariantHash partArgs({
141  {QStringLiteral("template"), templateName},
142 
143  });
144  auto contentTypeIt = email.constFind(QStringLiteral("content_type"));
145  if (contentTypeIt != email.constEnd() && !contentTypeIt.value().toString().isEmpty()) {
146  partArgs.insert(QStringLiteral("content_type"), contentTypeIt.value().toString());
147  }
148  MimePart *partObj = generatePart(c, d, partArgs);
149  parts.append(QVariant::fromValue(partObj));
150  }
151  email.insert(QStringLiteral("parts"), parts);
152  c->setStash(d->stashKey, email);
153 
154  ret = ViewEmail::render(c);
155  return ret;
156 }
157 
158 #include "moc_viewemailtemplate.cpp"
QByteArray render(Context *c) const override
void setStash(const QString &key, const QVariant &value)
Definition: context.cpp:207
void setDefaultView(const QString &view)
The Cutelyst Context.
Definition: context.h:50
QByteArray render(Context *c) const override
Definition: viewemail.cpp:170
QString defaultView() const
void setTemplatePrefix(const QString &prefix)
QStringList errors() const
Returns a list of errors that were defined.
Definition: context.cpp:80
View * view(const QString &name=QString()) const
Definition: context.cpp:182
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
virtual QByteArray render(Context *c) const =0
Cutelyst View abstract view component
Definition: view.h:33
bool error() const
Returns true if an error was set.
Definition: context.cpp:63
void stash(const QVariantHash &unite)
Definition: context.h:515