18 #include "cuteleeview_p.h"
19 #include "cutelystcutelee.h"
21 #include "application.h"
27 #include <cutelee/qtlocalizer.h>
30 #include <QDirIterator>
31 #include <QtCore/QLoggingCategory>
32 #include <QTranslator>
34 Q_LOGGING_CATEGORY(CUTELYST_CUTELEE,
"cutelyst.cutelee", QtWarningMsg)
36 using namespace Cutelyst;
44 d->engine =
new Cutelee::Engine(
this);
45 d->engine->addTemplateLoader(d->loader);
53 d->engine->insertDefaultLibrary(QStringLiteral(
"cutelee_cutelyst"),
new CutelystCutelee(d->engine));
58 setIncludePaths({ app->config(QStringLiteral(
"root")).toString() });
62 d->cutelystVar = app->
config(QStringLiteral(
"CUTELYST_VAR"), QStringLiteral(
"c")).
toString();
64 app->loadTranslations(QStringLiteral(
"plugin_view_cutelee"));
74 return d->includePaths;
80 d->loader->setTemplateDirs(paths);
81 d->includePaths = paths;
94 d->extension = extension;
115 if (enable != d->cache.isNull()) {
120 d->engine =
new Cutelee::Engine(
this);
124 d->engine->addTemplateLoader(d->cache);
127 d->engine->addTemplateLoader(d->loader);
151 QDir::Files | QDir::NoDotAndDotDot,
152 QDirIterator::Subdirectories);
153 while (it.hasNext()) {
160 if (d->cache->canLoadTemplate(path)) {
161 d->cache->loadByName(path, d->engine);
170 return !d->cache.isNull();
179 const QVariantHash stash = c->
stash();
180 auto it = stash.constFind(QStringLiteral(
"template"));
182 if (it != stash.constEnd()) {
183 templateFile = it.value().toString();
186 templateFile = c->action()->
reverse() + d->extension;
188 templateFile.
remove(0, 1);
193 c->
error(QStringLiteral(
"Cannot render template, template name or template stash key not defined"));
198 qCDebug(CUTELYST_CUTELEE) <<
"Rendering template" << templateFile;
200 Cutelee::Context gc(stash);
204 auto transIt = d->translators.constFind(c->
locale());
205 if (transIt != d->translators.constEnd()) {
206 localizer.data()->installTranslator(transIt.value(), transIt.key().name());
209 auto catalogIt = d->translationCatalogs.constBegin();
210 while (catalogIt != d->translationCatalogs.constEnd()) {
211 localizer.data()->loadCatalog(catalogIt.value(), catalogIt.key());
215 gc.setLocalizer(localizer);
217 Cutelee::Template tmpl = d->engine->loadByName(templateFile);
218 if (tmpl->error() != Cutelee::NoError) {
224 QString content = tmpl->render(&gc);
225 if (tmpl->error() != Cutelee::NoError) {
231 if (!d->wrapper.isEmpty()) {
232 Cutelee::Template
wrapper = d->engine->loadByName(d->wrapper);
233 if (tmpl->error() != Cutelee::NoError) {
239 Cutelee::SafeString safeContent(content,
true);
240 gc.insert(QStringLiteral(
"content"), safeContent);
241 content = wrapper->render(&gc);
243 if (wrapper->error() != Cutelee::NoError) {
257 Q_ASSERT_X(translator,
"add translator to CuteleeView",
"invalid QTranslator object");
258 d->translators.insert(locale, translator);
269 Q_ASSERT_X(!path.
isEmpty(),
"add translation catalog to CuteleeView",
"empty path");
270 Q_ASSERT_X(!catalog.
isEmpty(),
"add translation catalog to CuteleeView",
"empty catalog name");
271 d->translationCatalogs.insert(catalog, path);
277 Q_ASSERT_X(!catalogs.
empty(),
"add translation catalogs to GranteleeView",
"empty QHash");
278 d->translationCatalogs.unite(catalogs);
286 const QDir i18nDir(directory);
287 if (Q_LIKELY(i18nDir.
exists())) {
288 const QString _prefix = prefix.
isEmpty() ? QStringLiteral(
".") : prefix;
289 const QString _suffix = suffix.
isEmpty() ? QStringLiteral(
".qm") : suffix;
291 const QFileInfoList tsFiles = i18nDir.
entryInfoList(namesFilter, QDir::Files);
292 if (Q_LIKELY(!tsFiles.empty())) {
293 locales.
reserve(tsFiles.size());
295 const QString fn = ts.fileName();
296 const int prefIdx = fn.
indexOf(_prefix);
299 if (Q_LIKELY(loc.
language() != QLocale::C)) {
301 if (Q_LIKELY(trans->load(loc, filename, _prefix, directory))) {
304 qCDebug(CUTELYST_CUTELEE) <<
"Loaded translations for locale" << loc <<
"from" << ts.absoluteFilePath();
307 qCWarning(CUTELYST_CUTELEE) <<
"Can not load translations for locale" << loc;
310 qCWarning(CUTELYST_CUTELEE) <<
"Can not load translations for invalid locale string" << locString;
315 qCWarning(CUTELYST_CUTELEE) <<
"Can not find translation files for" << filename <<
"in directory" << directory;
318 qCWarning(CUTELYST_CUTELEE) <<
"Can not load translations from not existing directory:" << directory;
321 qCWarning(CUTELYST_CUTELEE) <<
"Can not load translations for empty file name or empty path.";
327 #include "moc_cuteleeview.cpp"
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
QSharedPointer< T > create()
void append(const T &value)
void addTranslator(const QLocale &locale, QTranslator *translator)
void setStash(const QString &key, const QVariant &value)
QString & remove(int position, int n)
bool error() const
Returns true if an error was set.
QByteArray render(Context *c) const final
QString fromLocal8Bit(const char *str, int size)
QFileInfoList entryInfoList(Filters filters, SortFlags sort) const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
Language language() const
void setIncludePaths(const QStringList &paths)
Sets the list of include paths which will be looked for when resolving templates files.
QVariant config(const QString &key, const QVariant &defaultValue=QVariant()) const
QVariant fromValue(const T &value)
void addTranslationCatalogs(const QHash< QString, QString > &catalogs)
void addTranslationCatalog(const QString &path, const QString &catalog)
QString mid(int position, int n) const
QString templateExtension() const
Returns the template extension.
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
QString wrapper() const
Returns the template wrapper.
void setCache(bool enable)
Sets if template caching should be done, this increases performance at the cost of higher memory usag...
bool isCaching() const
Returns true if caching is enabled.
Cutelyst View abstract view component
The Cutelyst Application.
void setBody(QIODevice *body)
Cutelee::Engine * engine() 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...
QStringList includePaths() const
Returns the list of include paths.
QVector< QLocale > loadTranslationsFromDir(const QString &filename, const QString &directory, const QString &prefix=QStringLiteral("."), const QString &suffix=QStringLiteral(".qm"))
void stash(const QVariantHash &unite)
QByteArray toUtf8() const