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);
47 Cutelee::registerMetaType<ParamsMultiMap>();
49 d->loader = std::shared_ptr<Cutelee::FileSystemTemplateLoader>(
new Cutelee::FileSystemTemplateLoader);
51 d->engine =
new Cutelee::Engine(
this);
52 d->engine->addTemplateLoader(d->loader);
60 d->engine->insertDefaultLibrary(QStringLiteral(
"cutelee_cutelyst"),
new CutelystCutelee(d->engine));
62 auto app = qobject_cast<Application *>(
parent);
69 d->cutelystVar = app->config(QStringLiteral(
"CUTELYST_VAR"), QStringLiteral(
"c")).toString();
71 app->loadTranslations(QStringLiteral(
"plugin_view_cutelee"));
81 return d->includePaths;
87 d->loader->setTemplateDirs(paths);
88 d->includePaths = paths;
92 QString CuteleeView::templateExtension()
const
101 d->extension = extension;
122 if (enable && d->cache) {
127 d->engine =
new Cutelee::Engine(
this);
130 d->cache = std::shared_ptr<Cutelee::CachingLoaderDecorator>(
new Cutelee::CachingLoaderDecorator(d->loader));
131 d->engine->addTemplateLoader(d->cache);
134 d->engine->addTemplateLoader(d->loader);
153 const auto includePaths = d->includePaths;
154 for (
const QString &includePath : includePaths) {
160 while (it.hasNext()) {
167 if (d->cache->canLoadTemplate(path)) {
168 d->cache->loadByName(path, d->engine);
186 const QVariantHash stash = c->
stash();
187 auto it = stash.constFind(QStringLiteral(
"template"));
189 if (it != stash.constEnd()) {
190 templateFile = it.value().toString();
193 templateFile = c->action()->
reverse() + d->extension;
195 templateFile.
remove(0, 1);
200 c->
error(QStringLiteral(
"Cannot render template, template name or template stash key not defined"));
205 qCDebug(CUTELYST_CUTELEE) <<
"Rendering template" << templateFile;
207 Cutelee::Context gc(stash);
209 auto localizer = std::shared_ptr<Cutelee::QtLocalizer>(
new Cutelee::QtLocalizer{c->
locale()});
211 auto transIt = d->translators.constFind(c->
locale());
212 if (transIt != d->translators.constEnd()) {
213 localizer.get()->installTranslator(transIt.value(), transIt.key().name());
216 auto catalogIt = d->translationCatalogs.constBegin();
217 while (catalogIt != d->translationCatalogs.constEnd()) {
218 localizer.get()->loadCatalog(catalogIt.value(), catalogIt.key());
222 gc.setLocalizer(localizer);
224 Cutelee::Template tmpl = d->engine->loadByName(templateFile);
225 if (tmpl->error() != Cutelee::NoError) {
231 QString content = tmpl->render(&gc);
232 if (tmpl->error() != Cutelee::NoError) {
238 if (!d->wrapper.isEmpty()) {
239 Cutelee::Template wrapper = d->engine->loadByName(d->wrapper);
240 if (tmpl->error() != Cutelee::NoError) {
246 Cutelee::SafeString safeContent(content,
true);
247 gc.insert(QStringLiteral(
"content"), safeContent);
248 content = wrapper->render(&gc);
250 if (wrapper->error() != Cutelee::NoError) {
264 Q_ASSERT_X(translator,
"add translator to CuteleeView",
"invalid QTranslator object");
265 d->translators.insert(locale, translator);
276 Q_ASSERT_X(!path.
isEmpty(),
"add translation catalog to CuteleeView",
"empty path");
277 Q_ASSERT_X(!catalog.
isEmpty(),
"add translation catalog to CuteleeView",
"empty catalog name");
278 d->translationCatalogs.insert(catalog, path);
284 Q_ASSERT_X(!catalogs.empty(),
"add translation catalogs to GranteleeView",
"empty QHash");
285 d->translationCatalogs.unite(catalogs);
293 const QDir i18nDir(directory);
294 if (Q_LIKELY(i18nDir.
exists())) {
295 const QString _prefix = prefix.
isEmpty() ? QStringLiteral(
".") : prefix;
296 const QString _suffix = suffix.
isEmpty() ? QStringLiteral(
".qm") : suffix;
299 if (Q_LIKELY(!tsFiles.empty())) {
300 locales.
reserve(tsFiles.size());
302 const QString fn = ts.fileName();
303 const int prefIdx = fn.
indexOf(_prefix);
308 if (Q_LIKELY(trans->load(loc, filename, _prefix, directory))) {
311 qCDebug(CUTELYST_CUTELEE) <<
"Loaded translations for locale" << loc <<
"from" << ts.absoluteFilePath();
314 qCWarning(CUTELYST_CUTELEE) <<
"Can not load translations for locale" << loc;
317 qCWarning(CUTELYST_CUTELEE) <<
"Can not load translations for invalid locale string" << locString;
322 qCWarning(CUTELYST_CUTELEE) <<
"Can not find translation files for" << filename <<
"in directory" << directory;
325 qCWarning(CUTELYST_CUTELEE) <<
"Can not load translations from not existing directory:" << directory;
328 qCWarning(CUTELYST_CUTELEE) <<
"Can not load translations for empty file name or empty path.";
334 #include "moc_cuteleeview.cpp"
void stash(const QVariantHash &unite)
bool error() const
Returns true if an error was set.
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
void setStash(const QString &key, const QVariant &value)
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)