Cutelyst  1.11.0
viewemailtemplate.cpp
1 /*
2  * Copyright (C) 2015-2017 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 }
50 
51 QString ViewEmailTemplate::defaultView() const
52 {
53  Q_D(const ViewEmailTemplate);
54  return d->defaultView;
55 }
56 
57 void ViewEmailTemplate::setDefaultView(const QString &view)
58 {
59  Q_D(ViewEmailTemplate);
60  d->defaultView = view;
61 }
62 
63 MimePart *generatePart(Context *c, const ViewEmailTemplatePrivate *d, const QVariantHash &partHash)
64 {
65  const QString defaultView = d->defaultView;
66 
67  View *view = nullptr;
68  auto viewIt = partHash.constFind(QStringLiteral("view"));
69  if (viewIt != partHash.constEnd() && !viewIt.value().toString().isEmpty()) {
70  // use the view specified for the email part
71  const QString viewString = viewIt.value().toString();
72  qCDebug(CUTELYST_VIEW_EMAILTEMPLATE) << "Using specified view" << viewString << "for rendering.";
73  view = c->view(viewString);
74  } else if (!defaultView.isEmpty()) {
75  // if none specified use the configured default view
76  qCDebug(CUTELYST_VIEW_EMAILTEMPLATE) << "Using default view" << defaultView << "for rendering.";
77  view = c->view(defaultView);
78  } else {
79  // else fallback to Cutelysts default view
80  qCDebug(CUTELYST_VIEW_EMAILTEMPLATE) << "Using Cutelysts default view" << defaultView << "for rendering.";
81  view = c->view();
82  }
83 
84  // validate the per template view
85  if (!view) {
86  c->error(QStringLiteral("Could not find a view to render"));
87  return nullptr;
88  }
89 
90  QString templateString = partHash.value(QStringLiteral("template")).toString();;
91  // prefix with template_prefix if configured
92  if (!d->templatePrefix.isEmpty()) {
93  templateString = d->templatePrefix + QLatin1Char('/') + templateString;
94  }
95 
96  // render the email part
97  const QVariantHash currentStash = c->stash();
98  c->stash(partHash);
99  c->setStash(QStringLiteral("template"), templateString);
100  QByteArray output = view->render(c);
101  if (c->error()) {
102  qCDebug(CUTELYST_VIEW_EMAILTEMPLATE) << "Errors" << c->errors();
103  }
104  c->stash() = currentStash;
105 
106  MimePart *part = new MimePart();
107  part->setContent(output);
108 
109  d->setupAttributes(part, partHash);
110 
111  return part;
112 }
113 
114 QByteArray ViewEmailTemplate::render(Context *c) const
115 {
116  Q_D(const ViewEmailTemplate);
117 
118  QByteArray ret;
119  QVariantHash email = c->stash(d->stashKey).toHash();
120  const QString templateName = email.value(QStringLiteral("template")).toString();
121  const QVariantList templateList = email.value(QStringLiteral("templates")).toList();
122  if (templateName.isEmpty() && templateList.isEmpty()) {
123  ret = ViewEmail::render(c);
124  return ret;
125  }
126 
127  QVariantList parts = email.value(QStringLiteral("parts")).toList();
128  if (!templateList.isEmpty() && templateList.first().type() == QVariant::Hash) {
129  // multipart API
130  for (const QVariant &part : templateList) {
131  const QVariantHash partHash = part.toHash();
132  MimePart *partObj = generatePart(c, d, partHash);
133  parts.append(QVariant::fromValue(partObj));
134  }
135 
136  } else if (!templateName.isEmpty()) {
137  // single part API
138  QVariantHash partArgs({
139  {QStringLiteral("template"), templateName},
140 
141  });
142  auto contentTypeIt = email.constFind(QStringLiteral("content_type"));
143  if (contentTypeIt != email.constEnd() && !contentTypeIt.value().toString().isEmpty()) {
144  partArgs.insert(QStringLiteral("content_type"), contentTypeIt.value().toString());
145  }
146  MimePart *partObj = generatePart(c, d, partArgs);
147  parts.append(QVariant::fromValue(partObj));
148  }
149  email.insert(QStringLiteral("parts"), parts);
150  c->setStash(d->stashKey, email);
151 
152  ret = ViewEmail::render(c);
153  return ret;
154 }
155 
156 #include "moc_viewemailtemplate.cpp"
QString defaultView() const
QByteArray render(Context *c) const override
void setStash(const QString &key, const QVariant &value)
Definition: context.cpp:208
View * view() const
Definition: context.cpp:177
bool error() const
Returns true if an error was set.
Definition: context.cpp:64
void setDefaultView(const QString &view)
The Cutelyst Context.
Definition: context.h:50
QByteArray render(Context *c) const override
Definition: viewemail.cpp:166
void setTemplatePrefix(const QString &prefix)
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
virtual QByteArray render(Context *c) const =0
QStringList errors() const
Returns a list of errors that were defined.
Definition: context.cpp:81
Cutelyst View abstract view component
Definition: view.h:33
void stash(const QVariantHash &unite)
Definition: context.h:499