aboutsummaryrefslogtreecommitdiff
path: root/libcontextsubscriber/src/contexttypeinfo.cpp
blob: e3eac1fa758ab4029194f091045ca761ceb8fcb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 * Copyright (C) 2008, 2009 Nokia Corporation.
 *
 * Contact: Marius Vollmer <marius.vollmer@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include "contexttypeinfo.h"
#include "logging.h"
#include "loggingfeatures.h"
#include "contexttyperegistryinfo.h"

/* Public */

/* The AssocTree in a ContextTypeInfo corresponds directly to what has
   been written in the property declarations.  For example, a property
   declaration of this form

     <key name="Foo.Bar"
          type="string"/>

   will yield a ContextTypeInfo with this tree:

     "string"

   while this declaration

     <key name="Foo.Bar">
       <type>
         <string-enum>
           <foo/>
           <bar/>
         </string-enum>
       </type>
     </key>

   yields this tree

     [ "string-enum", [ "foo" ], [ "bar" ] ]

   The first element in this list is the name of the type, the rest of
   the list are the parameters.

   The first form of just "string" is short-hand for [ "string" ],
   i.e., you can use it when the list of parameters is empty.
*/

/// Returns a ContexTypeInfo where old-style type names (INTEGER,
/// STRING, BOOL) are replaced with new-style type names (integer,
/// string, bool). Returns self if the ContextTypeInfo is already
/// new-style.
ContextTypeInfo ContextTypeInfo::ensureNewTypes()
{
    QString newName;
    QString oldName = name();

    if (oldName == "TRUTH")
        newName = "bool";
    else if (oldName == "INT" || oldName == "INTEGER")
        newName = "integer";
    else if (oldName == "STRING")
        newName = "string";
    else if (oldName == "DOUBLE")
        newName = "number";
    else
        return *this;

    return ContextTypeInfo(QVariant(newName));
}

/// Verifies if \a value is acceptable as a representative of the type that
/// this ContextTypeInfo object describes.
bool ContextTypeInfo::typeCheck(const QVariant &value) const
{
    QString me = name();

    // First check against the parametrized base type.
    ContextTypeInfo baseType(base());
    if (!baseType.isNull()) {
        // Combine type instance parameters with default parameters of the
        // base type.  (Suboptimal.)
        if (baseType.type() != QVariant::List)
            baseType = AssocTree(QVariantList() << baseType.name());
        foreach (AssocTree p, parameters()) {
            baseType = baseType.filterOut(p.name());
            baseType = AssocTree(baseType.toList() << p);
        }
        if (!baseType.typeCheck(value))
            return false;
    }

    // Now let's see our hardwired knowledge about our types.
    QVariant::Type vtype = value.type();
    if (me == "value") {
        return true;
    }
    else if (me == "bool") {
        bool ok = vtype == QVariant::Bool;
        if (!ok)
            contextWarning() << F_TYPES << value << "is not a bool";
        return ok;
    }
    else if (me == "number") {
        if (!value.canConvert(QVariant::Double)) {
            contextWarning() << F_TYPES << value << "is not a number";
            return false;
        }
        QVariant min = parameterValue("min");
        QVariant max = parameterValue("max");
        double v = value.toDouble();
        if (!min.isNull() && v < min.toDouble()) {
            contextWarning() << F_TYPES << v << "below minimum:" << min.toDouble();
            return false;
        }
        if (!max.isNull() && max.toDouble() < v) {
            contextWarning() << F_TYPES << v << "above maximum:" << max.toDouble();
            return false;
        }
        return true;
    }
    else if (me == "integer") {
        bool ok = (vtype == QVariant::Int ||
                   vtype == QVariant::UInt ||
                   vtype == QVariant::LongLong ||
                   vtype == QVariant::ULongLong);
        if (!ok)
            contextWarning() << F_TYPES << value << "is not an integer type";
        return ok;
    }
    else if (me == "string") {
        bool ok = vtype == QVariant::String;
        if (!ok)
            contextWarning() << F_TYPES << value << "is not a string";
        return ok;
    }
    else if (me == "list") {
        if (vtype != QVariant::List) {
            contextWarning() << F_TYPES << value << "is not a list";
            return false;
        }
        QVariant minlen = parameterValue("min");
        QVariant maxlen = parameterValue("max");
        ContextTypeInfo eltype = parameterValue("type");
        QVariantList vl = value.toList();
        if (!minlen.isNull() && vl.size() < minlen.toInt()) {
            contextWarning() << F_TYPES << value << "shorter than minimum";
            return false;
        }
        if (!maxlen.isNull() && vl.size() > maxlen.toInt()) {
            contextWarning() << F_TYPES << value << "length over maximum";
            return false;
        }
        if (!eltype.isNull())
            foreach (QVariant el, vl)
                if (!eltype.typeCheck(el)) {
                    contextWarning() << F_TYPES << "element" << el
                                     << "is of incorrect type";
                    return false;
                }
        return true;
    }
    else if (me == "map") {
        if (vtype != QVariant::Map) {
            contextWarning() << F_TYPES << value << "is not a map";
            return false;
        }
        QHash<QString, ContextTypeInfo> allowedKeys;
        bool othersAllowed = false;
        foreach (ContextTypeInfo keyInfo, parameters()) {
            QString keyname = keyInfo.name();
            if (keyname == "allow-other-keys") {
                othersAllowed = true;
                continue;
            }
            allowedKeys.insert(keyname, keyInfo.parameterValue("type"));
        }
        if (allowedKeys.size() == 0)
            return true;
        QMapIterator<QString, QVariant> it(value.toMap());
        while (it.hasNext()) {
            it.next();
            QString key = it.key();
            if (allowedKeys.contains(key)) {
                if (allowedKeys[key].isNull())
                    continue;
                if (!allowedKeys[key].typeCheck(it.value())) {
                    contextWarning() << F_TYPES << "value for" << key
                                     << "(" << it.value() << ")"
                                     << "is of incorrect type";
                    return false;
                }
            } else if (!othersAllowed) {
                contextWarning() << F_TYPES << "unknown key" << key;
                return false;
            }
        }
        return true;
    }
    else if (me == "string-enum") {
        QSet<QString> enumerators;
        foreach (AssocTree enumerator, parameters())
            enumerators.insert(enumerator.name());
        bool ok = enumerators.contains(value.toString());
        if (!ok)
            contextWarning() << F_TYPES
                             << value << "is not valid in this string-enum";
        return ok;
    }
    else if (me == "int-enum") {
        QSet<int> enumerators;
        foreach (AssocTree enumerator, parameters())
            enumerators.insert(enumerator.value("value").toInt());
        bool ok = enumerators.contains(value.toInt());
        if (!ok)
            contextWarning() << F_TYPES << value << "is not valid in this int-enum";
        return ok;
    }
    return true;
}

/// Returns the AssocTree with the type definition for this type.
AssocTree ContextTypeInfo::definition() const
{
    return ContextTypeRegistryInfo::instance()->typeDefinitionForName(name());
}

/* ContextStringEnumInfo
 */

QStringList ContextStringEnumInfo::choices() const
{
    QStringList result;
    foreach (const AssocTree &p, parameters()) {
        result.append (p.name());
    }
    return result;
}