~ubuntu-branches/ubuntu/trusty/grantlee/trusty

« back to all changes in this revision

Viewing changes to templates/lib/context.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2010-12-07 08:09:15 UTC
  • Revision ID: james.westby@ubuntu.com-20101207080915-ee6wcs8nv625su4b
Tags: 0.1.7-0ubuntu2
* Backport upstream commits 128272d4f65d7f02372cb606c148817c2f15a78d and
  5a71ed118f6fae67a4a26ab867ab7575e5a3e34c to moderate test requirements so
  it will build on armel
* Include missing grantlee_i18ntags.so and grantlee_mutabletags.so in
  libgrantlee-core0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  This file is part of the Grantlee 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 <QtCore/QStringList>
 
28
 
 
29
using namespace Grantlee;
 
30
 
 
31
namespace Grantlee
 
32
{
 
33
class ContextPrivate
 
34
{
 
35
  ContextPrivate( Context *context, const QVariantHash &variantHash )
 
36
      : q_ptr( context ),
 
37
        m_autoescape( true ),
 
38
        m_mutating( false ),
 
39
        m_urlType( Context::AbsoluteUrls ),
 
40
        m_renderContext( new RenderContext ),
 
41
        m_localizer( new NullLocalizer )
 
42
  {
 
43
    m_variantHashStack.append( variantHash );
 
44
  }
 
45
 
 
46
  ~ContextPrivate()
 
47
  {
 
48
    delete m_renderContext;
 
49
  }
 
50
 
 
51
  Q_DECLARE_PUBLIC( Context )
 
52
  Context * const q_ptr;
 
53
 
 
54
  QList<QVariantHash> m_variantHashStack;
 
55
  bool m_autoescape;
 
56
  bool m_mutating;
 
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;
 
62
};
 
63
 
 
64
}
 
65
 
 
66
Context::Context()
 
67
    : d_ptr( new ContextPrivate( this, QVariantHash() ) )
 
68
{
 
69
}
 
70
 
 
71
Context::Context( const QVariantHash &variantHash )
 
72
    : d_ptr( new ContextPrivate( this, variantHash ) )
 
73
{
 
74
}
 
75
 
 
76
Context::Context( const Context &other )
 
77
    : d_ptr( new ContextPrivate( this, QVariantHash() ) )
 
78
{
 
79
  *this = other;
 
80
}
 
81
 
 
82
 
 
83
Context& Context::operator=( const Context &other )
 
84
{
 
85
  if ( &other == this )
 
86
    return *this;
 
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;
 
93
  return *this;
 
94
}
 
95
 
 
96
Context::~Context()
 
97
{
 
98
  delete d_ptr;
 
99
}
 
100
 
 
101
bool Context::autoEscape() const
 
102
{
 
103
  Q_D( const Context );
 
104
  return d->m_autoescape;
 
105
}
 
106
 
 
107
void Context::setAutoEscape( bool autoescape )
 
108
{
 
109
  Q_D( Context );
 
110
  d->m_autoescape = autoescape;
 
111
}
 
112
 
 
113
QVariant Context::lookup( const QString &str ) const
 
114
{
 
115
  Q_D( const Context );
 
116
 
 
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() ) );
 
126
      }
 
127
      return var;
 
128
    }
 
129
  }
 
130
 
 
131
  return QVariant();
 
132
}
 
133
 
 
134
void Context::push()
 
135
{
 
136
  Q_D( Context );
 
137
 
 
138
  const QHash<QString, QVariant> hash;
 
139
  d->m_variantHashStack.prepend( hash );
 
140
}
 
141
 
 
142
void Context::pop()
 
143
{
 
144
  Q_D( Context );
 
145
 
 
146
  d->m_variantHashStack.removeFirst();
 
147
}
 
148
 
 
149
void Context::insert( const QString &name, const QVariant &variant )
 
150
{
 
151
  Q_D( Context );
 
152
 
 
153
  d->m_variantHashStack[0].insert( name, variant );
 
154
}
 
155
 
 
156
QHash<QString, QVariant> Context::stackHash( int depth ) const
 
157
{
 
158
  Q_D( const Context );
 
159
 
 
160
  return d->m_variantHashStack.value( depth );
 
161
}
 
162
 
 
163
bool Context::isMutating() const
 
164
{
 
165
  Q_D( const Context );
 
166
  return d->m_mutating;
 
167
}
 
168
 
 
169
void Context::setMutating( bool mutating )
 
170
{
 
171
  Q_D( Context );
 
172
  d->m_mutating = mutating;
 
173
}
 
174
 
 
175
void Context::addExternalMedia( const QString &absolutePart, const QString &relativePart )
 
176
{
 
177
  Q_D( Context );
 
178
  d->m_externalMedia.append( qMakePair( absolutePart, relativePart ) );
 
179
}
 
180
 
 
181
QList<QPair<QString, QString> > Context::externalMedia() const
 
182
{
 
183
  Q_D( const Context );
 
184
  return d->m_externalMedia;
 
185
}
 
186
 
 
187
void Context::clearExternalMedia()
 
188
{
 
189
  Q_D( Context );
 
190
  d->m_externalMedia.clear();
 
191
}
 
192
 
 
193
void Context::setUrlType( Context::UrlType type )
 
194
{
 
195
  Q_D( Context );
 
196
  d->m_urlType = type;
 
197
}
 
198
 
 
199
Context::UrlType Context::urlType() const
 
200
{
 
201
  Q_D( const Context );
 
202
  return d->m_urlType;
 
203
}
 
204
 
 
205
void Context::setRelativeMediaPath( const QString &path )
 
206
{
 
207
  Q_D( Context );
 
208
  d->m_relativeMediaPath = path;
 
209
}
 
210
 
 
211
QString Context::relativeMediaPath() const
 
212
{
 
213
  Q_D( const Context );
 
214
  return d->m_relativeMediaPath;
 
215
}
 
216
 
 
217
RenderContext* Context::renderContext() const
 
218
{
 
219
  Q_D( const Context );
 
220
  return d->m_renderContext;
 
221
}
 
222
 
 
223
void Context::setLocalizer( AbstractLocalizer::Ptr localizer )
 
224
{
 
225
  Q_D( Context );
 
226
  if ( !localizer ) {
 
227
    d->m_localizer = AbstractLocalizer::Ptr( new NullLocalizer );
 
228
    return;
 
229
  }
 
230
  d->m_localizer = localizer;
 
231
}
 
232
 
 
233
AbstractLocalizer::Ptr Context::localizer() const
 
234
{
 
235
  Q_D( const Context );
 
236
  return d->m_localizer;
 
237
}