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)
44 d->engine =
new Cutelee::Engine(
this);
45 d->engine->addTemplateLoader(d->loader);
49 for (
const QByteArray &dir : dirs) {
50 d->engine->addPluginPath(QString::fromLocal8Bit(dir));
53 d->engine->insertDefaultLibrary(QStringLiteral(
"cutelee_cutelyst"),
new CutelystCutelee(d->engine));
55 auto app = qobject_cast<Application *>(parent);
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);
148 QDirIterator it(includePath, {
151 QDir::Files | QDir::NoDotAndDotDot,
152 QDirIterator::Subdirectories);
153 while (it.hasNext()) {
154 QString path = it.next();
155 path.remove(includePath);
160 if (d->cache->canLoadTemplate(path)) {
161 d->cache->loadByName(path, d->engine);
170 return !d->cache.isNull();
178 c->
setStash(d->cutelystVar, QVariant::fromValue(c));
179 const QVariantHash stash = c->
stash();
180 auto it = stash.constFind(QStringLiteral(
"template"));
181 QString templateFile;
182 if (it != stash.constEnd()) {
183 templateFile = it.value().toString();
185 if (c->action() && !c->action()->
reverse().isEmpty()) {
186 templateFile = c->action()->
reverse() + d->extension;
188 templateFile.remove(0, 1);
192 if (templateFile.isEmpty()) {
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) {
250 ret = content.toUtf8();
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);
283 QVector<QLocale> locales;
285 if (Q_LIKELY(!filename.isEmpty() && !directory.isEmpty())) {
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());
294 for (
const QFileInfo &ts : tsFiles) {
295 const QString fn = ts.fileName();
296 const int prefIdx = fn.
indexOf(_prefix);
297 const QString locString = fn.mid(prefIdx + _prefix.length(), fn.length() - prefIdx - _suffix.length() - _prefix.length());
298 QLocale loc(locString);
299 if (Q_LIKELY(loc.language() != QLocale::C)) {
300 auto trans =
new QTranslator(
this);
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"
QSharedPointer< T > create()
void addTranslator(const QLocale &locale, QTranslator *translator)
void setStash(const QString &key, const QVariant &value)
QString templateExtension() const
Returns the template extension.
QByteArray render(Context *c) const final
QString wrapper() const
Returns the template wrapper.
QStringList includePaths() const
Returns the list of include paths.
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
CuteleeView(QObject *parent=nullptr, const QString &name=QString())
Constructs a CuteleeView object with the given parent and name.
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.
void addTranslationCatalogs(const QHash< QString, QString > &catalogs)
void addTranslationCatalog(const QString &path, const QString &catalog)
bool isCaching() const
Returns true if caching is enabled.
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
int indexOf(const int &rx, int from) const const
void setBody(QIODevice *body)
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...
bool error() const
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"))
void stash(const QVariantHash &unite)