KTextTemplate

templateloader.cpp
1/*
2 This file is part of the KTextTemplate library
3
4 SPDX-FileCopyrightText: 2009, 2010 Stephen Kelly <steveire@gmail.com>
5
6 SPDX-License-Identifier: LGPL-2.1-or-later
7
8*/
9
10#include "templateloader.h"
11
12#include "engine.h"
13#include "exception.h"
14#include "nulllocalizer_p.h"
15
16#include <QDir>
17#include <QFile>
18#include <QFileInfo>
19
20using namespace KTextTemplate;
21
23
24namespace KTextTemplate
25{
26class FileSystemTemplateLoaderPrivate
27{
28 FileSystemTemplateLoaderPrivate(FileSystemTemplateLoader *loader, QSharedPointer<AbstractLocalizer> localizer)
29 : q_ptr(loader)
30 , m_localizer(localizer ? localizer : QSharedPointer<AbstractLocalizer>(new NullLocalizer))
31 {
32 }
33 Q_DECLARE_PUBLIC(FileSystemTemplateLoader)
34 FileSystemTemplateLoader *const q_ptr;
35
36 QString m_themeName;
37 QStringList m_templateDirs;
38 const QSharedPointer<AbstractLocalizer> m_localizer;
39};
40}
41
44 , d_ptr(new FileSystemTemplateLoaderPrivate(this, localizer))
45{
46}
47
49{
50 for (const QString &dir : templateDirs())
51 d_ptr->m_localizer->unloadCatalog(dir + QLatin1Char('/') + themeName());
52 delete d_ptr;
53}
54
55InMemoryTemplateLoader::InMemoryTemplateLoader()
57{
58}
59
60InMemoryTemplateLoader::~InMemoryTemplateLoader() = default;
61
63{
65 for (const QString &dir : templateDirs())
66 d->m_localizer->unloadCatalog(dir + QLatin1Char('/') + d->m_themeName);
67 d->m_themeName = themeName;
68 for (const QString &dir : templateDirs())
69 d->m_localizer->loadCatalog(dir + QLatin1Char('/') + themeName, themeName);
70}
71
73{
75 return d->m_themeName;
76}
77
79{
81
82 for (const QString &dir : templateDirs())
83 d->m_localizer->unloadCatalog(dir + QLatin1Char('/') + d->m_themeName);
84 d->m_templateDirs = dirs;
85 for (const QString &dir : templateDirs())
86 d->m_localizer->loadCatalog(dir + QLatin1Char('/') + d->m_themeName, d->m_themeName);
87}
88
90{
92 return d->m_templateDirs;
93}
94
96{
98 auto i = 0;
99 QFile file;
100
101 while (!file.exists()) {
102 if (i >= d->m_templateDirs.size())
103 break;
104
105 file.setFileName(d->m_templateDirs.at(i) + QLatin1Char('/') + d->m_themeName + QLatin1Char('/') + name);
106 ++i;
107 }
108
109 if (!file.exists() || !file.open(QIODevice::ReadOnly | QIODevice::Text)) {
110 return false;
111 }
112 file.close();
113 return true;
114}
115
116Template FileSystemTemplateLoader::loadByName(const QString &fileName, Engine const *engine) const
117{
119 auto i = 0;
120 QFile file;
121
122 while (!file.exists()) {
123 if (i >= d->m_templateDirs.size())
124 break;
125
126 file.setFileName(d->m_templateDirs.at(i) + QLatin1Char('/') + d->m_themeName + QLatin1Char('/') + fileName);
127 const QFileInfo fi(file);
128
129 if (file.exists() && !fi.canonicalFilePath().contains(QDir(d->m_templateDirs.at(i)).canonicalPath()))
130 return {};
131 ++i;
132 }
133
134 if (!file.exists() || !file.open(QIODevice::ReadOnly | QIODevice::Text)) {
135 return {};
136 }
137
138 QTextStream fstream(&file);
140 const auto fileContent = fstream.readAll();
141
142 return engine->newTemplate(fileContent, fileName);
143}
144
145std::pair<QString, QString> FileSystemTemplateLoader::getMediaUri(const QString &fileName) const
146{
148 auto i = 0;
149 QFile file;
150 while (!file.exists()) {
151 if (i >= d->m_templateDirs.size())
152 break;
153
154 file.setFileName(d->m_templateDirs.at(i) + QLatin1Char('/') + d->m_themeName + QLatin1Char('/') + fileName);
155
156 const QFileInfo fi(file);
157 if (!fi.canonicalFilePath().contains(QDir(d->m_templateDirs.at(i)).canonicalPath())) {
158 ++i;
159 continue;
160 }
161
162 if (file.exists()) {
163 auto path = fi.absoluteFilePath();
164 path.chop(fileName.size());
165 return std::make_pair(path, fileName);
166 }
167 ++i;
168 }
169 return {};
170}
171
172void InMemoryTemplateLoader::setTemplate(const QString &name, const QString &content)
173{
174 m_namedTemplates.insert(name, content);
175}
176
178{
179 return m_namedTemplates.contains(name);
180}
181
183{
184 const auto it = m_namedTemplates.constFind(name);
185 if (it != m_namedTemplates.constEnd()) {
186 return engine->newTemplate(it.value(), name);
187 }
188 throw KTextTemplate::Exception(TagSyntaxError, QStringLiteral("Couldn't load template %1. Template does not exist.").arg(name));
189}
190
191std::pair<QString, QString> InMemoryTemplateLoader::getMediaUri(const QString &fileName) const
192{
193 Q_UNUSED(fileName)
194 // This loader doesn't make any media available yet.
195 return {};
196}
Interface for implementing an internationalization system.
An retrieval interface to a storage location for Template objects.
virtual ~AbstractTemplateLoader()
Destructor.
KTextTemplate::Engine is the main entry point for creating KTextTemplate Templates.
Definition engine.h:110
Template newTemplate(const QString &content, const QString &name) const
Create a new Template with the content content identified by name.
Definition engine.cpp:332
An exception for use when implementing template tags.
Definition exception.h:74
The FileSystemTemplateLoader loads Templates from the file system.
Template loadByName(const QString &name, Engine const *engine) const override
Load a Template called name.
FileSystemTemplateLoader(const QSharedPointer< AbstractLocalizer > localizer={})
Constructor.
void setTheme(const QString &themeName)
Sets the theme of this loader to themeName.
bool canLoadTemplate(const QString &name) const override
Return true if a Template identified by name exists and can be loaded.
QStringList templateDirs() const
The directories this TemplateLoader looks in for template files.
void setTemplateDirs(const QStringList &dirs)
Sets the directories to look for template files to dirs.
QString themeName() const
The themeName of this TemplateLoader.
~FileSystemTemplateLoader() override
Destructor.
std::pair< QString, QString > getMediaUri(const QString &fileName) const override
Return a complete URI for media identified by fileName.
bool canLoadTemplate(const QString &name) const override
Return true if a Template identified by name exists and can be loaded.
void setTemplate(const QString &name, const QString &content)
Add a template content to this Loader.
std::pair< QString, QString > getMediaUri(const QString &fileName) const override
Return a complete URI for media identified by fileName.
Template loadByName(const QString &name, Engine const *engine) const override
Load a Template called name.
The Template class is a tree of nodes which may be rendered.
Definition template.h:85
The KTextTemplate namespace holds all public KTextTemplate API.
Definition Mainpage.dox:8
QString canonicalPath() const const
bool exists(const QString &fileName)
bool open(FILE *fh, OpenMode mode, FileHandleFlags handleFlags)
void setFileName(const QString &name)
virtual void close() override
QString absoluteFilePath() const const
QString canonicalFilePath() const const
const_iterator constEnd() const const
const_iterator constFind(const Key &key) const const
bool contains(const Key &key) const const
iterator insert(const Key &key, const T &value)
void chop(qsizetype n)
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
qsizetype size() const const
QString readAll()
void setEncoding(QStringConverter::Encoding encoding)
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:17:29 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.