File: settingdata.cpp

package info (click to toggle)
maliit-framework 2.3.0-3.2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,488 kB
  • sloc: cpp: 13,098; ansic: 2,506; xml: 299; sh: 34; makefile: 23; sed: 4
file content (141 lines) | stat: -rw-r--r-- 4,096 bytes parent folder | download | duplicates (2)
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
/* * This file is part of Maliit framework *
 *
 * Copyright (C) 2012 Mattia Barbon <mattia@develer.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
 * and appearing in the file LICENSE.LGPL included in the packaging
 * of this file.
 */

#include "maliit/settingdata.h"

namespace
{
    bool checkValueDomain(const QVariant &value, const QVariant &domain)
    {
        if (!domain.isValid())
            return true;
        if (!domain.canConvert(QVariant::List))
            return false;

        QVariantList domain_values = domain.toList();

        return domain_values.contains(value);
    }

    bool checkValueRange(const QVariant &value, const QVariant &range_min, const QVariant &range_max)
    {
        if (!range_min.isValid() && !range_max.isValid())
            return true;

        if (range_min.isValid()) {
            if (!range_min.canConvert(QVariant::Int))
                return false;
            if (range_min.toInt() > value.toInt())
                return false;
        }

        if (range_max.isValid()) {
            if (!range_max.canConvert(QVariant::Int))
                return false;
            if (range_max.toInt() < value.toInt())
                return false;
        }

        return true;
    }

    bool checkValueDomain(const QVariantList &values, const QVariant &domain)
    {
        if (!domain.isValid())
            return true;
        if (!domain.canConvert(QVariant::List))
            return false;

        const QVariantList &domain_values = domain.toList();

        Q_FOREACH (const QVariant &v, values)
            if (!domain_values.contains(v))
                return false;

        return true;
    }

    bool checkValueRange(const QVariantList &values, const QVariant &range_min, const QVariant &range_max)
    {
        if (!range_min.isValid() && !range_max.isValid())
            return true;

        Q_FOREACH (const QVariant &v, values)
            if (!checkValueRange(v, range_min, range_max))
                return false;

        return true;
    }

    bool checkIntList(const QVariant &value)
    {
        if (!value.canConvert<QVariantList>())
            return false;

        const QVariantList &values = value.toList();

        Q_FOREACH (const QVariant &v, values)
        {
            QVariant copy = v;

            if (!v.canConvert<int>() || !copy.convert(QVariant::Int))
                return false;
        }

        return true;
    }
}

bool validateSettingValue(Maliit::SettingEntryType type, const QVariantMap attributes, const QVariant &value)
{
    QVariant domain = attributes[Maliit::SettingEntryAttributes::valueDomain];
    QVariant range_min = attributes[Maliit::SettingEntryAttributes::valueRangeMin];
    QVariant range_max = attributes[Maliit::SettingEntryAttributes::valueRangeMax];
    QVariant copy = value;

    switch (type)
    {
    case Maliit::StringType:
        if (!value.canConvert<QString>())
            return false;
        if (!checkValueDomain(value, domain))
            return false;
        break;
    case Maliit::IntType:
        if (!value.canConvert<int>() || !copy.convert(QVariant::Int))
            return false;
        if (!checkValueDomain(value, domain))
            return false;
        if (!checkValueRange(value, range_min, range_max))
            return false;
        break;
    case Maliit::BoolType:
        if (!value.canConvert<bool>())
            return false;
        break;
    case Maliit::StringListType:
        if (!value.canConvert<QStringList>())
            return false;
        if (!checkValueDomain(value.toList(), domain))
            return false;
        break;
    case Maliit::IntListType:
        if (!checkIntList(value))
            return false;
        if (!checkValueDomain(value.toList(), domain))
            return false;
        if (!checkValueRange(value.toList(), range_min, range_max))
            return false;
        break;
    }

    return true;
}