6 #include "application.h" 9 #include "cuteleeview_p.h" 10 #include "cutelystcutelee.h" 13 #include <cutelee/metatype.h> 14 #include <cutelee/qtlocalizer.h> 16 #include <QDirIterator> 18 #include <QTranslator> 19 #include <QtCore/QLoggingCategory> 21 Q_LOGGING_CATEGORY(CUTELYST_CUTELEE,
"cutelyst.cutelee", QtWarningMsg)
26 return object.value(property);
30 return object->property(property.toLatin1().constData());
34 :
View(new CuteleeViewPrivate, parent, name)
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);
56 app->config(QStringLiteral(
"CUTELYST_VAR"), QStringLiteral(
"c")).toString();
58 app->loadTranslations(QStringLiteral(
"plugin_view_cutelee"));
68 return d->includePaths;
74 d->loader->setTemplateDirs(paths);
75 d->includePaths = paths;
88 d->extension = extension;
109 if (enable && d->cache) {
114 d->engine =
new Cutelee::Engine(
this);
117 d->cache = std::make_shared<Cutelee::CachingLoaderDecorator>(d->loader);
118 d->engine->addTemplateLoader(d->cache);
121 d->engine->addTemplateLoader(d->loader);
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(
188 "Cannot render template, template name or template stash key not defined"));
193 qCDebug(CUTELYST_CUTELEE) <<
"Rendering template" << templateFile;
195 Cutelee::Context gc(stash);
197 auto localizer = std::make_shared<Cutelee::QtLocalizer>(c->
locale());
199 auto transIt = d->translators.constFind(c->
locale());
200 if (transIt != d->translators.constEnd()) {
201 localizer.get()->installTranslator(transIt.value(), transIt.key().name());
204 auto catalogIt = d->translationCatalogs.constBegin();
205 while (catalogIt != d->translationCatalogs.constEnd()) {
206 localizer.get()->loadCatalog(catalogIt.value(), catalogIt.key());
210 gc.setLocalizer(localizer);
212 Cutelee::Template tmpl = d->engine->loadByName(templateFile);
213 if (tmpl->error() != Cutelee::NoError) {
219 QString content = tmpl->render(&gc);
220 if (tmpl->error() != Cutelee::NoError) {
226 if (!d->wrapper.isEmpty()) {
227 Cutelee::Template
wrapper = d->engine->loadByName(d->wrapper);
228 if (tmpl->error() != Cutelee::NoError) {
234 Cutelee::SafeString safeContent(content,
true);
235 gc.insert(QStringLiteral(
"content"), safeContent);
236 content =
wrapper->render(&gc);
238 if (
wrapper->error() != Cutelee::NoError) {
252 Q_ASSERT_X(translator,
"add translator to CuteleeView",
"invalid QTranslator object");
253 d->translators.insert(locale, translator);
264 Q_ASSERT_X(!path.
isEmpty(),
"add translation catalog to CuteleeView",
"empty path");
265 Q_ASSERT_X(!catalog.
isEmpty(),
"add translation catalog to CuteleeView",
"empty catalog name");
266 d->translationCatalogs.insert(catalog, path);
272 Q_ASSERT_X(!catalogs.empty(),
"add translation catalogs to GranteleeView",
"empty QHash");
273 d->translationCatalogs.unite(catalogs);
284 const QDir i18nDir(directory);
285 if (Q_LIKELY(i18nDir.
exists())) {
286 const QString _prefix = prefix.
isEmpty() ? QStringLiteral(
".") : prefix;
287 const QString _suffix = suffix.
isEmpty() ? QStringLiteral(
".qm") : suffix;
291 if (Q_LIKELY(!tsFiles.empty())) {
292 locales.
reserve(tsFiles.size());
294 const QString fn = ts.fileName();
295 const int prefIdx = fn.
indexOf(_prefix);
302 if (Q_LIKELY(trans->load(loc, filename, _prefix, directory))) {
305 qCDebug(CUTELYST_CUTELEE) <<
"Loaded translations for locale" << loc
306 <<
"from" << ts.absoluteFilePath();
309 qCWarning(CUTELYST_CUTELEE)
310 <<
"Can not load translations for locale" << loc;
313 qCWarning(CUTELYST_CUTELEE)
314 <<
"Can not load translations for invalid locale string" << locString;
319 qCWarning(CUTELYST_CUTELEE) <<
"Can not find translation files for" << filename
320 <<
"in directory" << directory;
323 qCWarning(CUTELYST_CUTELEE)
324 <<
"Can not load translations from not existing directory:" << directory;
327 qCWarning(CUTELYST_CUTELEE)
328 <<
"Can not load translations for empty file name or empty path.";
334 void CuteleeViewPrivate::initEngine()
338 qgetenv(
"CUTELYST_PLUGINS_DIR").split(
';');
343 engine->insertDefaultLibrary(QStringLiteral(
"cutelee_cutelyst"),
new CutelystCutelee(engine));
346 #include "moc_cuteleeview.cpp" qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
bool error() const noexcept
Returns true if an error was set.
void addTranslator(const QLocale &locale, QTranslator *translator)
void reserve(qsizetype size)
QVariant fromValue(T &&value)
Response * res() const noexcept
void setStash(const QString &key, const QVariant &value)
QString templateExtension() const
Returns the template extension.
void addTranslationCatalogs(const QMultiHash< QString, QString > &catalogs)
QByteArray render(Context *c) const final
QString wrapper() const
Returns the template wrapper.
bool exists() const const
QString fromLocal8Bit(QByteArrayView str)
void stash(const QVariantHash &unite)
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QFileInfoList entryInfoList(Filters filters, SortFlags sort) const const
bool isEmpty() const const
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
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
QString & remove(QChar ch, Qt::CaseSensitivity cs)
void addTranslationCatalog(const QString &path, const QString &catalog)
bool isCaching() const
Returns true if caching is enabled.
QString mid(qsizetype position, qsizetype n) const const
Cutelee::Engine * engine() const
void append(QList< T > &&value)
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
qsizetype length() const const
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)
T qobject_cast(QObject *object)
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