2
This file is part of the Grantlee template system.
4
Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
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.
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.
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/>.
23
#include "nulllocalizer_p.h"
24
#include "rendercontext.h"
27
#include <QtCore/QStringList>
29
using namespace Grantlee;
35
ContextPrivate( Context *context, const QVariantHash &variantHash )
39
m_urlType( Context::AbsoluteUrls ),
40
m_renderContext( new RenderContext ),
41
m_localizer( new NullLocalizer )
43
m_variantHashStack.append( variantHash );
48
delete m_renderContext;
51
Q_DECLARE_PUBLIC( Context )
52
Context * const q_ptr;
54
QList<QVariantHash> m_variantHashStack;
57
QList<QPair<QString, QString> > m_externalMedia;
58
Context::UrlType m_urlType;
59
QString m_relativeMediaPath;
60
RenderContext * const m_renderContext;
61
AbstractLocalizer::Ptr m_localizer;
67
: d_ptr( new ContextPrivate( this, QVariantHash() ) )
71
Context::Context( const QVariantHash &variantHash )
72
: d_ptr( new ContextPrivate( this, variantHash ) )
76
Context::Context( const Context &other )
77
: d_ptr( new ContextPrivate( this, QVariantHash() ) )
83
Context& Context::operator=( const Context &other )
87
d_ptr->m_autoescape = other.d_ptr->m_autoescape;
88
d_ptr->m_externalMedia = other.d_ptr->m_externalMedia;
89
d_ptr->m_mutating = other.d_ptr->m_mutating;
90
d_ptr->m_variantHashStack = other.d_ptr->m_variantHashStack;
91
d_ptr->m_urlType = other.d_ptr->m_urlType;
92
d_ptr->m_relativeMediaPath = other.d_ptr->m_relativeMediaPath;
101
bool Context::autoEscape() const
103
Q_D( const Context );
104
return d->m_autoescape;
107
void Context::setAutoEscape( bool autoescape )
110
d->m_autoescape = autoescape;
113
QVariant Context::lookup( const QString &str ) const
115
Q_D( const Context );
117
// return a variant from the stack.
118
QListIterator<QVariantHash> i( d->m_variantHashStack );
119
while ( i.hasNext() ) {
120
const QVariantHash h = i.next();
121
if ( h.contains( str ) ) {
122
QVariant var = h.value( str );
123
// If the user passed a string into the context, turn it into a Grantlee::SafeString.
124
if ( var.type() == QVariant::String ) {
125
var = QVariant::fromValue<Grantlee::SafeString>( getSafeString( var.toString() ) );
138
const QHash<QString, QVariant> hash;
139
d->m_variantHashStack.prepend( hash );
146
d->m_variantHashStack.removeFirst();
149
void Context::insert( const QString &name, const QVariant &variant )
153
d->m_variantHashStack[0].insert( name, variant );
156
QHash<QString, QVariant> Context::stackHash( int depth ) const
158
Q_D( const Context );
160
return d->m_variantHashStack.value( depth );
163
bool Context::isMutating() const
165
Q_D( const Context );
166
return d->m_mutating;
169
void Context::setMutating( bool mutating )
172
d->m_mutating = mutating;
175
void Context::addExternalMedia( const QString &absolutePart, const QString &relativePart )
178
d->m_externalMedia.append( qMakePair( absolutePart, relativePart ) );
181
QList<QPair<QString, QString> > Context::externalMedia() const
183
Q_D( const Context );
184
return d->m_externalMedia;
187
void Context::clearExternalMedia()
190
d->m_externalMedia.clear();
193
void Context::setUrlType( Context::UrlType type )
199
Context::UrlType Context::urlType() const
201
Q_D( const Context );
205
void Context::setRelativeMediaPath( const QString &path )
208
d->m_relativeMediaPath = path;
211
QString Context::relativeMediaPath() const
213
Q_D( const Context );
214
return d->m_relativeMediaPath;
217
RenderContext* Context::renderContext() const
219
Q_D( const Context );
220
return d->m_renderContext;
223
void Context::setLocalizer( AbstractLocalizer::Ptr localizer )
227
d->m_localizer = AbstractLocalizer::Ptr( new NullLocalizer );
230
d->m_localizer = localizer;
233
AbstractLocalizer::Ptr Context::localizer() const
235
Q_D( const Context );
236
return d->m_localizer;