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);
55 d->cutelystVar = app->config(QStringLiteral(
"CUTELYST_VAR"), QStringLiteral(
"c")).toString();
57 app->loadTranslations(QStringLiteral(
"plugin_view_cutelee"));
67 return d->includePaths;
73 d->loader->setTemplateDirs(paths);
74 d->includePaths = paths;
87 d->extension = extension;
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);
145 QDir::Files | QDir::NoDotAndDotDot,
146 QDirIterator::Subdirectories);
147 while (it.hasNext()) {
154 if (d->cache->canLoadTemplate(path)) {
155 d->cache->loadByName(path, d->engine);
173 const QVariantHash stash = c->
stash();
174 auto it = stash.constFind(QStringLiteral(
"template"));
176 if (it != stash.constEnd()) {
177 templateFile = it.value().toString();
180 templateFile = c->action()->
reverse() + d->extension;
182 templateFile.
remove(0, 1);
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) {
218 QString content = tmpl->render(&gc);
219 if (tmpl->error() != Cutelee::NoError) {
225 if (!d->wrapper.isEmpty()) {
226 Cutelee::Template
wrapper = d->engine->loadByName(d->wrapper);
227 if (tmpl->error() != Cutelee::NoError) {
233 Cutelee::SafeString safeContent(content,
true);
234 gc.insert(QStringLiteral(
"content"), safeContent);
235 content =
wrapper->render(&gc);
237 if (
wrapper->error() != Cutelee::NoError) {
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);
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;
285 const QFileInfoList tsFiles = i18nDir.
entryInfoList(namesFilter, QDir::Files);
286 if (Q_LIKELY(!tsFiles.empty())) {
287 locales.
reserve(tsFiles.size());
289 const QString fn = ts.fileName();
290 const int prefIdx = fn.
indexOf(_prefix);
293 if (Q_LIKELY(loc.
language() != QLocale::C)) {
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()
329 engine->insertDefaultLibrary(QStringLiteral(
"cutelee_cutelyst"),
new CutelystCutelee(engine));
332 #include "moc_cuteleeview.cpp" int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
bool error() const noexcept
Returns true if an error was set.
void append(const T &value)
void addTranslator(const QLocale &locale, QTranslator *translator)
Response * res() const noexcept
void setStash(const QString &key, const QVariant &value)
QString templateExtension() const
Returns the template extension.
QString & remove(int position, int n)
void addTranslationCatalogs(const QMultiHash< QString, QString > &catalogs)
QByteArray render(Context *c) const final
QString wrapper() const
Returns the template wrapper.
QString fromLocal8Bit(const char *str, int size)
bool exists() const const
void stash(const QVariantHash &unite)
QFileInfoList entryInfoList(QDir::Filters filters, QDir::SortFlags sort) const const
bool isEmpty() const const
QStringList includePaths() const
Returns the list of include paths.
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
QLocale::Language language() const const
The Cutelyst namespace holds all public Cutelyst API.
void setIncludePaths(const QStringList &paths)
Sets the list of include paths which will be looked for when resolving templates files.
QLocale locale() const noexcept
QVariant fromValue(const T &value)
void addTranslationCatalog(const QString &path, const QString &catalog)
bool isCaching() const
Returns true if caching is enabled.
QString mid(int position, int n) const const
Cutelee::Engine * engine() const
void setCache(bool enable)
Sets if template caching should be done, this increases performance at the cost of higher memory usag...
Cutelyst View abstract view component
The Cutelyst Application.
CuteleeView(QObject *parent=nullptr, const QString &name=QString())
Constructs a CuteleeView object with the given parent and name.
void setBody(QIODevice *body)
QObject * parent() const const
void setTemplateExtension(const QString &extension)
Sets the template extension, defaults to ".html".
void setWrapper(const QString &name)
Sets the template wrapper name, the template will be rendered into content variable in which the wrap...
QVector< QLocale > loadTranslationsFromDir(const QString &filename, const QString &directory, const QString &prefix=QStringLiteral("."), const QString &suffix=QStringLiteral(".qm"))
QByteArray toUtf8() const const