5 #include "cuteleeview_p.h"
6 #include "cutelystcutelee.h"
8 #include "application.h"
14 #include <cutelee/qtlocalizer.h>
15 #include <cutelee/metatype.h>
18 #include <QDirIterator>
19 #include <QtCore/QLoggingCategory>
20 #include <QTranslator>
22 Q_LOGGING_CATEGORY(CUTELYST_CUTELEE,
"cutelyst.cutelee", QtWarningMsg)
27 return object.value(property);
31 return object->property(property.toLatin1().constData());
38 Cutelee::registerMetaType<ParamsMultiMap>();
39 Cutelee::registerMetaType<Cutelyst::Request*>();
41 d->loader = std::make_shared<Cutelee::FileSystemTemplateLoader>();
43 d->engine =
new Cutelee::Engine(
this);
44 d->engine->addTemplateLoader(d->loader);
48 auto app = qobject_cast<Application *>(parent);
55 d->cutelystVar = app->config(QStringLiteral(
"CUTELYST_VAR"), QStringLiteral(
"c")).toString();
57 app->loadTranslations(QStringLiteral(
"plugin_view_cutelee"));
64 QStringList CuteleeView::includePaths()
const
67 return d->includePaths;
73 d->loader->setTemplateDirs(paths);
74 d->includePaths = paths;
78 QString CuteleeView::templateExtension()
const
87 d->extension = extension;
91 QString CuteleeView::wrapper()
const
108 if (enable && d->cache) {
113 d->engine =
new Cutelee::Engine(
this);
116 d->cache = std::make_shared<Cutelee::CachingLoaderDecorator>(d->loader);
117 d->engine->addTemplateLoader(d->cache);
120 d->engine->addTemplateLoader(d->loader);
140 const auto includePaths = d->includePaths;
141 for (
const QString &includePath : includePaths) {
142 QDirIterator it(includePath, {
143 QLatin1Char(
'*') + d->extension
145 QDir::Files | QDir::NoDotAndDotDot,
146 QDirIterator::Subdirectories);
147 while (it.hasNext()) {
148 QString path = it.next();
149 path.remove(includePath);
150 if (path.startsWith(u
'/')) {
154 if (d->cache->canLoadTemplate(path)) {
155 d->cache->loadByName(path, d->engine);
172 c->
setStash(d->cutelystVar, QVariant::fromValue(c));
173 const QVariantHash stash = c->
stash();
174 auto it = stash.constFind(QStringLiteral(
"template"));
175 QString templateFile;
176 if (it != stash.constEnd()) {
177 templateFile = it.value().toString();
179 if (c->action() && !c->action()->
reverse().isEmpty()) {
180 templateFile = c->action()->
reverse() + d->extension;
181 if (templateFile.startsWith(u
'/')) {
182 templateFile.remove(0, 1);
186 if (templateFile.isEmpty()) {
187 c->
error(QStringLiteral(
"Cannot render template, template name or template stash key not defined"));
192 qCDebug(CUTELYST_CUTELEE) <<
"Rendering template" << templateFile;
194 Cutelee::Context gc(stash);
196 auto localizer = std::make_shared<Cutelee::QtLocalizer>(c->
locale());
198 auto transIt = d->translators.constFind(c->
locale());
199 if (transIt != d->translators.constEnd()) {
200 localizer.get()->installTranslator(transIt.value(), transIt.key().name());
203 auto catalogIt = d->translationCatalogs.constBegin();
204 while (catalogIt != d->translationCatalogs.constEnd()) {
205 localizer.get()->loadCatalog(catalogIt.value(), catalogIt.key());
209 gc.setLocalizer(localizer);
211 Cutelee::Template tmpl = d->engine->loadByName(templateFile);
212 if (tmpl->error() != Cutelee::NoError) {
214 c->
error(QLatin1String(
"Error while rendering template: ") + tmpl->errorString());
218 QString content = tmpl->render(&gc);
219 if (tmpl->error() != Cutelee::NoError) {
221 c->
error(QLatin1String(
"Error while rendering template: ") + tmpl->errorString());
225 if (!d->wrapper.isEmpty()) {
226 Cutelee::Template wrapper = d->engine->loadByName(d->wrapper);
227 if (tmpl->error() != Cutelee::NoError) {
229 c->
error(QLatin1String(
"Error while rendering template: ") + tmpl->errorString());
233 Cutelee::SafeString safeContent(content,
true);
234 gc.insert(QStringLiteral(
"content"), safeContent);
235 content = wrapper->render(&gc);
237 if (wrapper->error() != Cutelee::NoError) {
239 c->
error(QLatin1String(
"Error while rendering template: ") + tmpl->errorString());
244 ret = content.toUtf8();
251 Q_ASSERT_X(translator,
"add translator to CuteleeView",
"invalid QTranslator object");
252 d->translators.insert(locale, translator);
263 Q_ASSERT_X(!path.isEmpty(),
"add translation catalog to CuteleeView",
"empty path");
264 Q_ASSERT_X(!catalog.isEmpty(),
"add translation catalog to CuteleeView",
"empty catalog name");
265 d->translationCatalogs.insert(catalog, path);
271 Q_ASSERT_X(!catalogs.empty(),
"add translation catalogs to GranteleeView",
"empty QHash");
272 d->translationCatalogs.unite(catalogs);
277 QVector<QLocale> locales;
279 if (Q_LIKELY(!filename.isEmpty() && !directory.isEmpty())) {
280 const QDir i18nDir(directory);
281 if (Q_LIKELY(i18nDir.exists())) {
282 const QString _prefix = prefix.isEmpty() ? QStringLiteral(
".") : prefix;
283 const QString _suffix = suffix.isEmpty() ? QStringLiteral(
".qm") : suffix;
284 const QStringList namesFilter = QStringList({filename + _prefix + QLatin1Char(
'*') + _suffix});
285 const QFileInfoList tsFiles = i18nDir.entryInfoList(namesFilter, QDir::Files);
286 if (Q_LIKELY(!tsFiles.empty())) {
287 locales.reserve(tsFiles.size());
288 for (
const QFileInfo &ts : tsFiles) {
289 const QString fn = ts.fileName();
290 const int prefIdx = fn.indexOf(_prefix);
291 const QString locString = fn.mid(prefIdx + _prefix.length(), fn.length() - prefIdx - _suffix.length() - _prefix.length());
292 QLocale loc(locString);
293 if (Q_LIKELY(loc.language() != QLocale::C)) {
294 auto trans =
new QTranslator(
this);
295 if (Q_LIKELY(trans->load(loc, filename, _prefix, directory))) {
298 qCDebug(CUTELYST_CUTELEE) <<
"Loaded translations for locale" << loc <<
"from" << ts.absoluteFilePath();
301 qCWarning(CUTELYST_CUTELEE) <<
"Can not load translations for locale" << loc;
304 qCWarning(CUTELYST_CUTELEE) <<
"Can not load translations for invalid locale string" << locString;
309 qCWarning(CUTELYST_CUTELEE) <<
"Can not find translation files for" << filename <<
"in directory" << directory;
312 qCWarning(CUTELYST_CUTELEE) <<
"Can not load translations from not existing directory:" << directory;
315 qCWarning(CUTELYST_CUTELEE) <<
"Can not load translations for empty file name or empty path.";
321 void CuteleeViewPrivate::initEngine()
324 const QByteArrayList dirs = QByteArrayList{ QByteArrayLiteral(CUTELYST_PLUGINS_DIR) } + qgetenv(
"CUTELYST_PLUGINS_DIR").split(
';');
325 for (
const QByteArray &dir : dirs) {
326 engine->addPluginPath(QString::fromLocal8Bit(dir));
329 engine->insertDefaultLibrary(QStringLiteral(
"cutelee_cutelyst"),
new CutelystCutelee(engine));
332 #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.
QMultiMap< QString, QString > ParamsMultiMap