6#include "application.h"
9#include "grantleeview_p.h"
12#include <grantlee/metatype.h>
13#include <grantlee/qtlocalizer.h>
15#include <QDirIterator>
18#include <QtCore/QLoggingCategory>
20Q_LOGGING_CATEGORY(CUTELYST_GRANTLEE,
"cutelyst.grantlee", QtWarningMsg)
25return object.value(property);
29return object->property(property.toLatin1().constData());
37 Grantlee::registerMetaType<ParamsMultiMap>();
38 Grantlee::registerMetaType<Cutelyst::Request *>();
43 d->engine =
new Grantlee::Engine(
this);
44 d->engine->addTemplateLoader(d->loader);
48 qgetenv(
"CUTELYST_PLUGINS_DIR").split(
';');
53 d->engine->addDefaultLibrary(QStringLiteral(
"grantlee_cutelyst"));
63 app->config(QStringLiteral(
"CUTELYST_VAR"), QStringLiteral(
"c")).toString();
65 app->loadTranslations(QStringLiteral(
"plugin_view_grantlee"));
75 return d->includePaths;
81 d->loader->setTemplateDirs(paths);
82 d->includePaths = paths;
95 d->extension = extension;
116 if (enable != d->cache.isNull()) {
121 d->engine =
new Grantlee::Engine(
this);
125 new Grantlee::CachingLoaderDecorator(d->loader));
126 d->engine->addTemplateLoader(d->cache);
129 d->engine->addTemplateLoader(d->loader);
148 const auto includePaths = d->includePaths;
149 for (
const QString &includePath : includePaths) {
154 while (it.hasNext()) {
161 if (d->cache->canLoadTemplate(path)) {
162 d->cache->loadByName(path, d->engine);
171 return !d->cache.isNull();
180 const QVariantHash stash = c->
stash();
181 auto it = stash.constFind(QStringLiteral(
"template"));
183 if (it != stash.constEnd()) {
184 templateFile = it.value().toString();
187 templateFile = c->action()->
reverse() + d->extension;
189 templateFile.
remove(0, 1);
194 c->
error(QStringLiteral(
195 "Cannot render template, template name or template stash key not defined"));
200 qCDebug(CUTELYST_GRANTLEE) <<
"Rendering template" << templateFile;
202 Grantlee::Context gc(stash);
206 auto transIt = d->translators.constFind(c->
locale());
207 if (transIt != d->translators.constEnd()) {
208 localizer.data()->installTranslator(transIt.value(), transIt.key().name());
211 auto catalogIt = d->translationCatalogs.constBegin();
212 while (catalogIt != d->translationCatalogs.constEnd()) {
213 localizer.data()->loadCatalog(catalogIt.value(), catalogIt.key());
217 gc.setLocalizer(localizer);
219 Grantlee::Template tmpl = d->engine->loadByName(templateFile);
220 if (tmpl->error() != Grantlee::NoError) {
226 QString content = tmpl->render(&gc);
227 if (tmpl->error() != Grantlee::NoError) {
233 if (!d->wrapper.isEmpty()) {
234 Grantlee::Template wrapper = d->engine->loadByName(d->wrapper);
235 if (tmpl->error() != Grantlee::NoError) {
241 Grantlee::SafeString safeContent(content,
true);
242 gc.insert(QStringLiteral(
"content"), safeContent);
243 content = wrapper->render(&gc);
245 if (wrapper->error() != Grantlee::NoError) {
259 Q_ASSERT_X(translator,
"add translator to GrantleeView",
"invalid QTranslator object");
260 d->translators.insert(locale, translator);
271 Q_ASSERT_X(!path.
isEmpty(),
"add translation catalog to GrantleeView",
"empty path");
272 Q_ASSERT_X(!catalog.
isEmpty(),
"add translation catalog to GrantleeView",
"empty catalog name");
273 d->translationCatalogs.insert(catalog, path);
279 Q_ASSERT_X(!catalogs.
empty(),
"add translation catalogs to GranteleeView",
"empty QHash");
280 d->translationCatalogs.unite(catalogs);
291 const QDir i18nDir(directory);
292 if (Q_LIKELY(i18nDir.
exists())) {
293 const QString _prefix = prefix.
isEmpty() ? QStringLiteral(
".") : prefix;
294 const QString _suffix = suffix.
isEmpty() ? QStringLiteral(
".qm") : suffix;
298 if (Q_LIKELY(!tsFiles.empty())) {
299 locales.
reserve(tsFiles.size());
301 const QString fn = ts.fileName();
302 const int prefIdx = fn.
indexOf(_prefix);
309 if (Q_LIKELY(trans->load(loc, filename, _prefix, directory))) {
312 qCDebug(CUTELYST_GRANTLEE) <<
"Loaded translations for locale" << loc
313 <<
"from" << ts.absoluteFilePath();
316 qCWarning(CUTELYST_GRANTLEE)
317 <<
"Can not load translations for locale" << loc;
320 qCWarning(CUTELYST_GRANTLEE)
321 <<
"Can not load translations for invalid locale string" << locString;
326 qCWarning(CUTELYST_GRANTLEE) <<
"Can not find translation files for" << filename
327 <<
"in directory" << directory;
330 qCWarning(CUTELYST_GRANTLEE)
331 <<
"Can not load translations from not existing directory:" << directory;
334 qCWarning(CUTELYST_GRANTLEE)
335 <<
"Can not load translations for empty file name or empty path.";
341#include "moc_grantleeview.cpp"
void stash(const QVariantHash &unite)
QLocale locale() const noexcept
Response * res() const noexcept
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
void setStash(const QString &key, const QVariant &value)
bool error() const noexcept
Returns true if an error was set.
QStringList includePaths() const
Returns the list of include paths.
QVector< QLocale > loadTranslationsFromDir(const QString &filename, const QString &directory, const QString &prefix=QStringLiteral("."), const QString &suffix=QStringLiteral(".qm"))
Grantlee::Engine * engine() const
QByteArray render(Context *c) const final
void setWrapper(const QString &name)
Sets the template wrapper name, the template will be rendered into content variable in which the wrap...
void addTranslator(const QLocale &locale, QTranslator *translator)
bool isCaching() const
Returns true if caching is enabled.
void setTemplateExtension(const QString &extension)
Sets the template extension, defaults to ".html".
GrantleeView(QObject *parent=nullptr, const QString &name=QString())
Constructs a GrantleeView object with the given parent and name.
QString wrapper() const
Returns the template wrapper.
void setIncludePaths(const QStringList &paths)
Sets the list of include paths which will be looked for when resolving templates files.
void setCache(bool enable)
Sets if template caching should be done, this increases performance at the cost of higher memory usag...
void addTranslationCatalog(const QString &path, const QString &catalog)
void addTranslationCatalogs(const QHash< QString, QString > &catalogs)
QString templateExtension() const
Returns the template extension.
void setBody(QIODevice *body)
View(QObject *parent, const QString &name)
The Cutelyst namespace holds all public Cutelyst API.
QMultiMap< QString, QString > ParamsMultiMap
QFileInfoList entryInfoList(Filters filters, SortFlags sort) const const
bool exists() const const
Language language() const const
QObject * parent() const const
T qobject_cast(QObject *object)
QSharedPointer< T > create(Args &&... args)
QString fromLocal8Bit(const char *str, int size)
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QString mid(int position, int n) const const
QString & remove(int position, int n)
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
QByteArray toUtf8() const const
QVariant fromValue(const T &value)
void append(const T &value)