18 #include "cuteleeview_p.h"
19 #include "cutelystcutelee.h"
21 #include "application.h"
27 #include <cutelee/qtlocalizer.h>
28 #include <cutelee/metatype.h>
31 #include <QDirIterator>
32 #include <QtCore/QLoggingCategory>
33 #include <QTranslator>
35 Q_LOGGING_CATEGORY(CUTELYST_CUTELEE,
"cutelyst.cutelee", QtWarningMsg)
40 return object.value(property);
44 return object->property(property.toLatin1().constData());
51 Cutelee::registerMetaType<ParamsMultiMap>();
52 Cutelee::registerMetaType<Cutelyst::Request*>();
54 d->loader = std::make_shared<Cutelee::FileSystemTemplateLoader>();
56 d->engine =
new Cutelee::Engine(
this);
57 d->engine->addTemplateLoader(d->loader);
65 d->engine->insertDefaultLibrary(QStringLiteral(
"cutelee_cutelyst"),
new CutelystCutelee(d->engine));
67 auto app = qobject_cast<Application *>(
parent);
74 d->cutelystVar = app->config(QStringLiteral(
"CUTELYST_VAR"), QStringLiteral(
"c")).toString();
76 app->loadTranslations(QStringLiteral(
"plugin_view_cutelee"));
86 return d->includePaths;
92 d->loader->setTemplateDirs(paths);
93 d->includePaths = paths;
97 QString CuteleeView::templateExtension()
const
106 d->extension = extension;
127 if (enable && d->cache) {
132 d->engine =
new Cutelee::Engine(
this);
135 d->cache = std::make_shared<Cutelee::CachingLoaderDecorator>(d->loader);
136 d->engine->addTemplateLoader(d->cache);
139 d->engine->addTemplateLoader(d->loader);
158 const auto includePaths = d->includePaths;
159 for (
const QString &includePath : includePaths) {
165 while (it.hasNext()) {
172 if (d->cache->canLoadTemplate(path)) {
173 d->cache->loadByName(path, d->engine);
191 const QVariantHash stash = c->
stash();
192 auto it = stash.constFind(QStringLiteral(
"template"));
194 if (it != stash.constEnd()) {
195 templateFile = it.value().toString();
198 templateFile = c->action()->
reverse() + d->extension;
200 templateFile.
remove(0, 1);
205 c->
error(QStringLiteral(
"Cannot render template, template name or template stash key not defined"));
210 qCDebug(CUTELYST_CUTELEE) <<
"Rendering template" << templateFile;
212 Cutelee::Context gc(stash);
214 auto localizer = std::make_shared<Cutelee::QtLocalizer>(c->
locale());
216 auto transIt = d->translators.constFind(c->
locale());
217 if (transIt != d->translators.constEnd()) {
218 localizer.get()->installTranslator(transIt.value(), transIt.key().name());
221 auto catalogIt = d->translationCatalogs.constBegin();
222 while (catalogIt != d->translationCatalogs.constEnd()) {
223 localizer.get()->loadCatalog(catalogIt.value(), catalogIt.key());
227 gc.setLocalizer(localizer);
229 Cutelee::Template tmpl = d->engine->loadByName(templateFile);
230 if (tmpl->error() != Cutelee::NoError) {
236 QString content = tmpl->render(&gc);
237 if (tmpl->error() != Cutelee::NoError) {
243 if (!d->wrapper.isEmpty()) {
244 Cutelee::Template wrapper = d->engine->loadByName(d->wrapper);
245 if (tmpl->error() != Cutelee::NoError) {
251 Cutelee::SafeString safeContent(content,
true);
252 gc.insert(QStringLiteral(
"content"), safeContent);
253 content = wrapper->render(&gc);
255 if (wrapper->error() != Cutelee::NoError) {
269 Q_ASSERT_X(translator,
"add translator to CuteleeView",
"invalid QTranslator object");
270 d->translators.insert(locale, translator);
281 Q_ASSERT_X(!path.
isEmpty(),
"add translation catalog to CuteleeView",
"empty path");
282 Q_ASSERT_X(!catalog.
isEmpty(),
"add translation catalog to CuteleeView",
"empty catalog name");
283 d->translationCatalogs.insert(catalog, path);
289 Q_ASSERT_X(!catalogs.empty(),
"add translation catalogs to GranteleeView",
"empty QHash");
290 d->translationCatalogs.unite(catalogs);
298 const QDir i18nDir(directory);
299 if (Q_LIKELY(i18nDir.
exists())) {
300 const QString _prefix = prefix.
isEmpty() ? QStringLiteral(
".") : prefix;
301 const QString _suffix = suffix.
isEmpty() ? QStringLiteral(
".qm") : suffix;
304 if (Q_LIKELY(!tsFiles.empty())) {
305 locales.
reserve(tsFiles.size());
307 const QString fn = ts.fileName();
308 const int prefIdx = fn.
indexOf(_prefix);
313 if (Q_LIKELY(trans->load(loc, filename, _prefix, directory))) {
316 qCDebug(CUTELYST_CUTELEE) <<
"Loaded translations for locale" << loc <<
"from" << ts.absoluteFilePath();
319 qCWarning(CUTELYST_CUTELEE) <<
"Can not load translations for locale" << loc;
322 qCWarning(CUTELYST_CUTELEE) <<
"Can not load translations for invalid locale string" << locString;
327 qCWarning(CUTELYST_CUTELEE) <<
"Can not find translation files for" << filename <<
"in directory" << directory;
330 qCWarning(CUTELYST_CUTELEE) <<
"Can not load translations from not existing directory:" << directory;
333 qCWarning(CUTELYST_CUTELEE) <<
"Can not load translations for empty file name or empty path.";
339 #include "moc_cuteleeview.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.
void setWrapper(const QString &name)
Sets the template wrapper name, the template will be rendered into content variable in which the wrap...
CuteleeView(QObject *parent=nullptr, const QString &name=QString())
Constructs a CuteleeView object with the given parent and name.
void addTranslator(const QLocale &locale, QTranslator *translator)
QByteArray render(Context *c) const final
void setTemplateExtension(const QString &extension)
Sets the template extension, defaults to ".html".
void addTranslationCatalogs(const QMultiHash< QString, QString > &catalogs)
QVector< QLocale > loadTranslationsFromDir(const QString &filename, const QString &directory, const QString &prefix=QStringLiteral("."), const QString &suffix=QStringLiteral(".qm"))
bool isCaching() const
Returns true if caching is enabled.
Cutelee::Engine * engine() const
void addTranslationCatalog(const QString &path, const QString &catalog)
void setCache(bool enable)
Sets if template caching should be done, this increases performance at the cost of higher memory usag...
void setIncludePaths(const QStringList &paths)
Sets the list of include paths which will be looked for when resolving templates files.
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
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)