18 #include "grantleeview_p.h"
20 #include "application.h"
26 #include <grantlee/qtlocalizer.h>
27 #include <grantlee/metatype.h>
30 #include <QDirIterator>
31 #include <QtCore/QLoggingCategory>
32 #include <QTranslator>
34 Q_LOGGING_CATEGORY(CUTELYST_GRANTLEE,
"cutelyst.grantlee", QtWarningMsg)
39 return object.value(property);
43 return object->property(property.toLatin1().constData());
50 Grantlee::registerMetaType<ParamsMultiMap>();
51 Grantlee::registerMetaType<Cutelyst::Request*>();
55 d->engine =
new Grantlee::Engine(
this);
56 d->engine->addTemplateLoader(d->loader);
64 d->engine->addDefaultLibrary(QStringLiteral(
"grantlee_cutelyst"));
66 auto app = qobject_cast<Application *>(
parent);
73 d->cutelystVar = app->config(QStringLiteral(
"CUTELYST_VAR"), QStringLiteral(
"c")).toString();
75 app->loadTranslations(QStringLiteral(
"plugin_view_grantlee"));
85 return d->includePaths;
91 d->loader->setTemplateDirs(paths);
92 d->includePaths = paths;
96 QString GrantleeView::templateExtension()
const
105 d->extension = extension;
126 if (enable != d->cache.isNull()) {
131 d->engine =
new Grantlee::Engine(
this);
135 d->engine->addTemplateLoader(d->cache);
138 d->engine->addTemplateLoader(d->loader);
157 const auto includePaths = d->includePaths;
158 for (
const QString &includePath : includePaths) {
164 while (it.hasNext()) {
171 if (d->cache->canLoadTemplate(path)) {
172 d->cache->loadByName(path, d->engine);
181 return !d->cache.isNull();
190 const QVariantHash stash = c->
stash();
191 auto it = stash.constFind(QStringLiteral(
"template"));
193 if (it != stash.constEnd()) {
194 templateFile = it.value().toString();
197 templateFile = c->action()->
reverse() + d->extension;
199 templateFile.
remove(0, 1);
204 c->
error(QStringLiteral(
"Cannot render template, template name or template stash key not defined"));
209 qCDebug(CUTELYST_GRANTLEE) <<
"Rendering template" << templateFile;
211 Grantlee::Context gc(stash);
215 auto transIt = d->translators.constFind(c->
locale());
216 if (transIt != d->translators.constEnd()) {
217 localizer.data()->installTranslator(transIt.value(), transIt.key().name());
220 auto catalogIt = d->translationCatalogs.constBegin();
221 while (catalogIt != d->translationCatalogs.constEnd()) {
222 localizer.data()->loadCatalog(catalogIt.value(), catalogIt.key());
226 gc.setLocalizer(localizer);
228 Grantlee::Template tmpl = d->engine->loadByName(templateFile);
229 if (tmpl->error() != Grantlee::NoError) {
235 QString content = tmpl->render(&gc);
236 if (tmpl->error() != Grantlee::NoError) {
242 if (!d->wrapper.isEmpty()) {
243 Grantlee::Template wrapper = d->engine->loadByName(d->wrapper);
244 if (tmpl->error() != Grantlee::NoError) {
250 Grantlee::SafeString safeContent(content,
true);
251 gc.insert(QStringLiteral(
"content"), safeContent);
252 content = wrapper->render(&gc);
254 if (wrapper->error() != Grantlee::NoError) {
268 Q_ASSERT_X(translator,
"add translator to GrantleeView",
"invalid QTranslator object");
269 d->translators.insert(locale, translator);
280 Q_ASSERT_X(!path.
isEmpty(),
"add translation catalog to GrantleeView",
"empty path");
281 Q_ASSERT_X(!catalog.
isEmpty(),
"add translation catalog to GrantleeView",
"empty catalog name");
282 d->translationCatalogs.insert(catalog, path);
288 Q_ASSERT_X(!catalogs.
empty(),
"add translation catalogs to GranteleeView",
"empty QHash");
289 d->translationCatalogs.unite(catalogs);
297 const QDir i18nDir(directory);
298 if (Q_LIKELY(i18nDir.
exists())) {
299 const QString _prefix = prefix.
isEmpty() ? QStringLiteral(
".") : prefix;
300 const QString _suffix = suffix.
isEmpty() ? QStringLiteral(
".qm") : suffix;
303 if (Q_LIKELY(!tsFiles.empty())) {
304 locales.
reserve(tsFiles.size());
306 const QString fn = ts.fileName();
307 const int prefIdx = fn.
indexOf(_prefix);
312 if (Q_LIKELY(trans->load(loc, filename, _prefix, directory))) {
315 qCDebug(CUTELYST_GRANTLEE) <<
"Loaded translations for locale" << loc <<
"from" << ts.absoluteFilePath();
318 qCWarning(CUTELYST_GRANTLEE) <<
"Can not load translations for locale" << loc;
321 qCWarning(CUTELYST_GRANTLEE) <<
"Can not load translations for invalid locale string" << locString;
326 qCWarning(CUTELYST_GRANTLEE) <<
"Can not find translation files for" << filename <<
"in directory" << directory;
329 qCWarning(CUTELYST_GRANTLEE) <<
"Can not load translations from not existing directory:" << directory;
332 qCWarning(CUTELYST_GRANTLEE) <<
"Can not load translations for empty file name or empty path.";
338 #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.
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.
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)
void setBody(QIODevice *body)
Cutelyst View abstract view component
The Cutelyst namespace holds all public Cutelyst API.
QFileInfoList entryInfoList(QDir::Filters filters, QDir::SortFlags sort) const const
bool exists() const const
QLocale::Language language() const const
QObject * parent() const const
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)