5 #include "grantleeview_p.h"
7 #include "application.h"
13 #include <grantlee/qtlocalizer.h>
14 #include <grantlee/metatype.h>
17 #include <QDirIterator>
18 #include <QtCore/QLoggingCategory>
19 #include <QTranslator>
21 Q_LOGGING_CATEGORY(CUTELYST_GRANTLEE,
"cutelyst.grantlee", QtWarningMsg)
26 return object.value(property);
30 return object->property(property.toLatin1().constData());
37 Grantlee::registerMetaType<ParamsMultiMap>();
38 Grantlee::registerMetaType<Cutelyst::Request*>();
42 d->engine =
new Grantlee::Engine(
this);
43 d->engine->addTemplateLoader(d->loader);
51 d->engine->addDefaultLibrary(QStringLiteral(
"grantlee_cutelyst"));
53 auto app = qobject_cast<Application *>(
parent);
60 d->cutelystVar = app->config(QStringLiteral(
"CUTELYST_VAR"), QStringLiteral(
"c")).toString();
62 app->loadTranslations(QStringLiteral(
"plugin_view_grantlee"));
72 return d->includePaths;
78 d->loader->setTemplateDirs(paths);
79 d->includePaths = paths;
83 QString GrantleeView::templateExtension()
const
92 d->extension = extension;
113 if (enable != d->cache.isNull()) {
118 d->engine =
new Grantlee::Engine(
this);
122 d->engine->addTemplateLoader(d->cache);
125 d->engine->addTemplateLoader(d->loader);
144 const auto includePaths = d->includePaths;
145 for (
const QString &includePath : includePaths) {
151 while (it.hasNext()) {
158 if (d->cache->canLoadTemplate(path)) {
159 d->cache->loadByName(path, d->engine);
168 return !d->cache.isNull();
177 const QVariantHash stash = c->
stash();
178 auto it = stash.constFind(QStringLiteral(
"template"));
180 if (it != stash.constEnd()) {
181 templateFile = it.value().toString();
184 templateFile = c->action()->
reverse() + d->extension;
186 templateFile.
remove(0, 1);
191 c->
error(QStringLiteral(
"Cannot render template, template name or template stash key not defined"));
196 qCDebug(CUTELYST_GRANTLEE) <<
"Rendering template" << templateFile;
198 Grantlee::Context gc(stash);
202 auto transIt = d->translators.constFind(c->
locale());
203 if (transIt != d->translators.constEnd()) {
204 localizer.data()->installTranslator(transIt.value(), transIt.key().name());
207 auto catalogIt = d->translationCatalogs.constBegin();
208 while (catalogIt != d->translationCatalogs.constEnd()) {
209 localizer.data()->loadCatalog(catalogIt.value(), catalogIt.key());
213 gc.setLocalizer(localizer);
215 Grantlee::Template tmpl = d->engine->loadByName(templateFile);
216 if (tmpl->error() != Grantlee::NoError) {
222 QString content = tmpl->render(&gc);
223 if (tmpl->error() != Grantlee::NoError) {
229 if (!d->wrapper.isEmpty()) {
230 Grantlee::Template wrapper = d->engine->loadByName(d->wrapper);
231 if (tmpl->error() != Grantlee::NoError) {
237 Grantlee::SafeString safeContent(content,
true);
238 gc.insert(QStringLiteral(
"content"), safeContent);
239 content = wrapper->render(&gc);
241 if (wrapper->error() != Grantlee::NoError) {
255 Q_ASSERT_X(translator,
"add translator to GrantleeView",
"invalid QTranslator object");
256 d->translators.insert(locale, translator);
267 Q_ASSERT_X(!path.
isEmpty(),
"add translation catalog to GrantleeView",
"empty path");
268 Q_ASSERT_X(!catalog.
isEmpty(),
"add translation catalog to GrantleeView",
"empty catalog name");
269 d->translationCatalogs.insert(catalog, path);
275 Q_ASSERT_X(!catalogs.
empty(),
"add translation catalogs to GranteleeView",
"empty QHash");
276 d->translationCatalogs.unite(catalogs);
284 const QDir i18nDir(directory);
285 if (Q_LIKELY(i18nDir.
exists())) {
286 const QString _prefix = prefix.
isEmpty() ? QStringLiteral(
".") : prefix;
287 const QString _suffix = suffix.
isEmpty() ? QStringLiteral(
".qm") : suffix;
290 if (Q_LIKELY(!tsFiles.empty())) {
291 locales.
reserve(tsFiles.size());
293 const QString fn = ts.fileName();
294 const int prefIdx = fn.
indexOf(_prefix);
299 if (Q_LIKELY(trans->load(loc, filename, _prefix, directory))) {
302 qCDebug(CUTELYST_GRANTLEE) <<
"Loaded translations for locale" << loc <<
"from" << ts.absoluteFilePath();
305 qCWarning(CUTELYST_GRANTLEE) <<
"Can not load translations for locale" << loc;
308 qCWarning(CUTELYST_GRANTLEE) <<
"Can not load translations for invalid locale string" << locString;
313 qCWarning(CUTELYST_GRANTLEE) <<
"Can not find translation files for" << filename <<
"in directory" << directory;
316 qCWarning(CUTELYST_GRANTLEE) <<
"Can not load translations from not existing directory:" << directory;
319 qCWarning(CUTELYST_GRANTLEE) <<
"Can not load translations for empty file name or empty path.";
325 #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)