~ubuntu-branches/ubuntu/precise/grantlee/precise

« back to all changes in this revision

Viewing changes to corelib/util.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Harald Sitter
  • Date: 2010-06-11 23:41:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100611234145-oas7rhdrbwy8j55c
Tags: upstream-0.1.1
ImportĀ upstreamĀ versionĀ 0.1.1

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 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
  Library 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 "util.h"
 
22
 
 
23
QString Grantlee::unescapeStringLiteral( const QString &input )
 
24
{
 
25
  return input.mid( 1, input.size() - 2 )
 
26
         .replace( "\\\'", QChar( '\'' ) )
 
27
         .replace( "\\\"", QChar( '"' ) )
 
28
         .replace( "\\\\", QChar( '\\' ) );
 
29
}
 
30
 
 
31
bool Grantlee::variantIsTrue( const QVariant &variant )
 
32
{
 
33
 
 
34
  if ( !variant.isValid() )
 
35
    return false;
 
36
  switch ( variant.userType() ) {
 
37
  case QVariant::Bool: {
 
38
    return variant.toBool();
 
39
  }
 
40
  case QVariant::Int: {
 
41
    return ( variant.toInt() > 0 );
 
42
  }
 
43
  case QVariant::Double: {
 
44
    return ( variant.toDouble() > 0 );
 
45
  }
 
46
  case QMetaType::QObjectStar: {
 
47
    QObject *obj = variant.value<QObject *>();
 
48
    if ( !obj )
 
49
      return false;
 
50
 
 
51
    if ( obj->property( "__true__" ).isValid() ) {
 
52
      return obj->property( "__true__" ).toBool();
 
53
    }
 
54
    return true;
 
55
  }
 
56
  case QVariant::List: {
 
57
    return ( variant.toList().size() > 0 );
 
58
  }
 
59
  case QVariant::Hash: {
 
60
    return ( variant.toHash().size() > 0 );
 
61
  }
 
62
  }
 
63
 
 
64
  Grantlee::SafeString str = getSafeString( variant );
 
65
  return !str.get().isEmpty();
 
66
}
 
67
 
 
68
QVariantList Grantlee::variantToList( const QVariant &var )
 
69
{
 
70
 
 
71
  if ( !var.isValid() )
 
72
    return QVariantList();
 
73
 
 
74
  if ( var.type() == QVariant::List ) {
 
75
    return var.toList();
 
76
  }
 
77
 
 
78
  if ( var.type() == QVariant::Hash ) {
 
79
    QVariantHash varHash = var.toHash();
 
80
    QVariantList list;
 
81
 
 
82
    QVariantHash::const_iterator it = varHash.constBegin();
 
83
    for ( ; it != varHash.constEnd(); ++it )
 
84
      list << it.key();
 
85
 
 
86
    return list;
 
87
  }
 
88
 
 
89
  if ( var.userType() == QMetaType::QObjectStar ) {
 
90
    QObject *obj = var.value<QObject*>();
 
91
    if ( obj->property( "__list__" ).isValid() ) {
 
92
      return obj->property( "__list__" ).toList();
 
93
    }
 
94
  } else {
 
95
    return QVariantList() << var;
 
96
  }
 
97
  return QVariantList();
 
98
}
 
99
 
 
100
Grantlee::SafeString Grantlee::markSafe( const Grantlee::SafeString &input )
 
101
{
 
102
  Grantlee::SafeString sret = input;
 
103
  sret.setSafety( Grantlee::SafeString::IsSafe );
 
104
  return sret;
 
105
}
 
106
 
 
107
Grantlee::SafeString Grantlee::markForEscaping( const Grantlee::SafeString &input )
 
108
{
 
109
  Grantlee::SafeString temp = input;
 
110
  if ( input.isSafe() || input.needsEscape() )
 
111
    return input;
 
112
 
 
113
  temp.setNeedsEscape( true );
 
114
  return temp;
 
115
}
 
116
 
 
117
Grantlee::SafeString Grantlee::getSafeString( const QVariant &input )
 
118
{
 
119
  if ( input.userType() == qMetaTypeId<Grantlee::SafeString>() ) {
 
120
    return input.value<Grantlee::SafeString>();
 
121
  } else {
 
122
    return input.toString();
 
123
  }
 
124
}
 
125
 
 
126
bool Grantlee::isSafeString( const QVariant &input )
 
127
{
 
128
  int type = input.userType();
 
129
  return (( type == qMetaTypeId<Grantlee::SafeString>() )
 
130
          || type == QVariant::String );
 
131
}
 
132
 
 
133
bool Grantlee::supportedOutputType( const QVariant &input )
 
134
{
 
135
  QList<int> primitives;
 
136
  primitives << qMetaTypeId<Grantlee::SafeString>()
 
137
             << QVariant::Bool
 
138
             << QVariant::Int
 
139
             << QVariant::Double
 
140
             << QVariant::DateTime;
 
141
  return primitives.contains( input.userType() );
 
142
}
 
143
 
 
144
 
 
145
bool Grantlee::equals( const QVariant &lhs, const QVariant &rhs )
 
146
{
 
147
 
 
148
  // QVariant doesn't use operator== to compare its held data, so we do it manually instead for SafeString.
 
149
  bool equal = false;
 
150
  if ( lhs.userType() == qMetaTypeId<Grantlee::SafeString>() ) {
 
151
    if ( rhs.userType() == qMetaTypeId<Grantlee::SafeString>() ) {
 
152
      equal = ( lhs.value<Grantlee::SafeString>() == rhs.value<Grantlee::SafeString>() );
 
153
    } else if ( rhs.userType() == QVariant::String ) {
 
154
      equal = ( lhs.value<Grantlee::SafeString>() == rhs.toString() );
 
155
    }
 
156
  } else if ( rhs.userType() == qMetaTypeId<Grantlee::SafeString>() && lhs.userType() == QVariant::String ) {
 
157
    equal = ( rhs.value<Grantlee::SafeString>() == lhs.toString() );
 
158
  }  else {
 
159
    equal = (( lhs == rhs ) && ( lhs.userType() == rhs.userType() ) );
 
160
  }
 
161
  return equal;
 
162
}
 
163
 
 
164
Grantlee::SafeString Grantlee::toString( const QVariantList &list )
 
165
{
 
166
  QString output( '[' );
 
167
  QVariantList::const_iterator it = list.constBegin();
 
168
  const QVariantList::const_iterator end = list.constEnd();
 
169
  while ( it != end ) {
 
170
    QVariant item = *it;
 
171
    if ( isSafeString( item ) ) {
 
172
      output.append( "u\'" );
 
173
      output.append( getSafeString( item ) );
 
174
      output.append( '\'' );
 
175
      if (( it + 1 ) != end )
 
176
        output.append( ", " );
 
177
    }
 
178
    if ( item.type() == QVariant::List ) {
 
179
      output.append( toString( item.toList() ) );
 
180
      output.append( ", " );
 
181
    }
 
182
    ++it;
 
183
  }
 
184
 
 
185
  return output.append( ']' );
 
186
}
 
187