Cutelee 6.1.0
context.cpp
1/*
2 This file is part of the Cutelee template system.
3
4 Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either version
9 2.1 of the Licence, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library. If not, see <http://www.gnu.org/licenses/>.
18
19*/
20
21#include "context.h"
22
23#include "nulllocalizer_p.h"
24#include "rendercontext.h"
25#include "util.h"
26
27#include <memory>
28#include <QtCore/QStringList>
29
30using namespace Cutelee;
31
32namespace Cutelee
33{
35{
36 ContextPrivate(Context *context, const QVariantHash &variantHash)
37 : q_ptr(context), m_autoescape(true), m_mutating(false),
38 m_urlType(Context::AbsoluteUrls), m_renderContext(new RenderContext),
39 m_localizer(new NullLocalizer)
40 {
41 m_variantHashStack.append(variantHash);
42 }
43
44 ~ContextPrivate() { delete m_renderContext; }
45
46 Q_DECLARE_PUBLIC(Context)
47 Context *const q_ptr;
48
49 QList<QVariantHash> m_variantHashStack;
50 bool m_autoescape;
51 bool m_mutating;
52 QList<std::pair<QString, QString>> m_externalMedia;
53 Context::UrlType m_urlType;
54 QString m_relativeMediaPath;
55 RenderContext *const m_renderContext;
56 std::shared_ptr<AbstractLocalizer> m_localizer;
57};
58}
59
60Context::Context() : d_ptr(new ContextPrivate(this, QVariantHash())) {}
61
62Context::Context(const QVariantHash &variantHash)
63 : d_ptr(new ContextPrivate(this, variantHash))
64{
65}
66
68 : d_ptr(new ContextPrivate(this, QVariantHash()))
69{
70 *this = other;
71}
72
74{
75 if (&other == this)
76 return *this;
77 d_ptr->m_autoescape = other.d_ptr->m_autoescape;
78 d_ptr->m_externalMedia = other.d_ptr->m_externalMedia;
79 d_ptr->m_mutating = other.d_ptr->m_mutating;
80 d_ptr->m_variantHashStack = other.d_ptr->m_variantHashStack;
81 d_ptr->m_urlType = other.d_ptr->m_urlType;
82 d_ptr->m_relativeMediaPath = other.d_ptr->m_relativeMediaPath;
83 return *this;
84}
85
86Context::~Context() { delete d_ptr; }
87
88bool Context::autoEscape() const
89{
90 Q_D(const Context);
91 return d->m_autoescape;
92}
93
94void Context::setAutoEscape(bool autoescape)
95{
96 Q_D(Context);
97 d->m_autoescape = autoescape;
98}
99
101{
102 Q_D(const Context);
103 QVariant ret;
104
105 // return a variant from the stack.
106 for (const QVariantHash &h : d->m_variantHashStack) {
107 auto it = h.constFind(str);
108 if (it != h.constEnd()) {
109 ret = it.value();
110 // If the user passed a string into the context, turn it into a
111 // Cutelee::SafeString.
112 if (ret.userType() == qMetaTypeId<QString>()) {
113 ret = QVariant::fromValue<Cutelee::SafeString>(
114 getSafeString(ret.toString()));
115 }
116 return ret;
117 }
118 }
119
120 return ret;
121}
122
124{
125 Q_D(Context);
126
127 const QHash<QString, QVariant> hash;
128 d->m_variantHashStack.prepend(hash);
129}
130
132{
133 Q_D(Context);
134
135 d->m_variantHashStack.removeFirst();
136}
137
138void Context::insert(const QString &name, const QVariant &variant)
139{
140 Q_D(Context);
141
142 d->m_variantHashStack[0].insert(name, variant);
143}
144
145void Context::insert(const QString &name, QObject *object)
146{
147 Q_D(Context);
148
149 d->m_variantHashStack[0].insert(name, QVariant::fromValue(object));
150}
151
152QHash<QString, QVariant> Context::stackHash(int depth) const
153{
154 Q_D(const Context);
155
156 return d->m_variantHashStack.value(depth);
157}
158
159bool Context::isMutating() const
160{
161 Q_D(const Context);
162 return d->m_mutating;
163}
164
165void Context::setMutating(bool mutating)
166{
167 Q_D(Context);
168 d->m_mutating = mutating;
169}
170
171void Context::addExternalMedia(const QString &absolutePart,
172 const QString &relativePart)
173{
174 Q_D(Context);
175 d->m_externalMedia.append({absolutePart, relativePart});
176}
177
178QList<std::pair<QString, QString> > Context::externalMedia() const
179{
180 Q_D(const Context);
181 return d->m_externalMedia;
182}
183
184void Context::clearExternalMedia()
185{
186 Q_D(Context);
187 d->m_externalMedia.clear();
188}
189
191{
192 Q_D(Context);
193 d->m_urlType = type;
194}
195
197{
198 Q_D(const Context);
199 return d->m_urlType;
200}
201
203{
204 Q_D(Context);
205 d->m_relativeMediaPath = path;
206}
207
209{
210 Q_D(const Context);
211 return d->m_relativeMediaPath;
212}
213
215{
216 Q_D(const Context);
217 return d->m_renderContext;
218}
219
220void Context::setLocalizer(std::shared_ptr<AbstractLocalizer> localizer)
221{
222 Q_D(Context);
223 if (!localizer) {
224 d->m_localizer = std::shared_ptr<AbstractLocalizer>(new NullLocalizer);
225 return;
226 }
227 d->m_localizer = localizer;
228}
229
230std::shared_ptr<AbstractLocalizer> Context::localizer() const
231{
232 Q_D(const Context);
233 return d->m_localizer;
234}
The Context class holds the context to render a Template with.
Definition context.h:119
UrlType urlType() const
Definition context.cpp:196
virtual ~Context()
Definition context.cpp:86
RenderContext * renderContext() const
Definition context.cpp:214
QString relativeMediaPath() const
Definition context.cpp:208
std::shared_ptr< AbstractLocalizer > localizer() const
Definition context.cpp:230
void setUrlType(UrlType type)
Definition context.cpp:190
void insert(const QString &name, QObject *object)
Definition context.cpp:145
void setLocalizer(std::shared_ptr< AbstractLocalizer > localizer)
Definition context.cpp:220
Context & operator=(const Context &other)
Definition context.cpp:73
virtual QVariant lookup(const QString &str) const
Definition context.cpp:100
void setRelativeMediaPath(const QString &relativePath)
Definition context.cpp:202
QList< std::pair< QString, QString > > externalMedia() const
Definition context.cpp:178
@ AbsoluteUrls
Absolute URLs should be put in the template.
Definition context.h:243
Provides storage facility for state while rendering a template.
The Cutelee namespace holds all public Cutelee API.
Definition Mainpage.dox:8
Cutelee::SafeString getSafeString(const QVariant &input)
Definition util.cpp:108
Utility functions used throughout Cutelee.