~timo-jyrinki/ubuntu/trusty/maliit-framework/fix_qt52

« back to all changes in this revision

Viewing changes to maliit-glib/maliitsettingdata.c

  • Committer: Package Import Robot
  • Author(s): Ricardo Salveti de Araujo, Sergio Schvezov, Ricardo Salveti de Araujo
  • Date: 2013-07-23 19:47:04 UTC
  • mfrom: (1.1.2) (1.2.1 experimental)
  • Revision ID: package-import@ubuntu.com-20130723194704-1lsy1kmlda069cea
Tags: 0.99.0+git20130615+97e8335-0ubuntu1
[ Sergio Schvezov ]
* New build from HEAD 97e8335.
* Packaging import from lp:phablet-extras/maliit-framework.

[ Ricardo Salveti de Araujo ]
* debian/control: adding vcs and fixing dependencies
* General package cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* This file is part of Maliit framework
2
 
 *
3
 
 * Copyright (C) 2012 Openismus GmbH
4
 
 *
5
 
 * Contact: maliit-discuss@lists.maliit.org
6
 
 *
7
 
 * This library is free software; you can redistribute it and/or
8
 
 * modify it under the terms of the GNU Lesser General Public
9
 
 * License as published by the Free Software Foundation; either
10
 
 * version 2.1 of the licence, or (at your option) any later version.
11
 
 *
12
 
 * This library is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 
 * Lesser General Public License for more details.
16
 
 *
17
 
 * You should have received a copy of the GNU Lesser General Public
18
 
 * License along with this library; if not, write to the
19
 
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20
 
 * Boston, MA 02111-1307, USA.
21
 
 */
22
 
 
23
 
#include "maliitsettingdata.h"
24
 
 
25
 
/**
26
 
 * SECTION:maliitsettingdata
27
 
 * @short_description: miscellaneous setting functions and types
28
 
 * @title: Maliit settings misc
29
 
 * @see_also: #MaliitSettingsEntry, #MaliitPluginSettings
30
 
 * @stability: Stable
31
 
 * @include: maliit/maliitsettingdata.h
32
 
 *
33
 
 * Here #MaliitSettingsEntryType, attribute keys and
34
 
 * maliit_validate_setting_value() are specified.
35
 
 */
36
 
 
37
 
 
38
 
/**
39
 
 * MaliitSettingsEntryType:
40
 
 * @MALIIT_STRING_TYPE: Entry is a string.
41
 
 * @MALIIT_INT_TYPE: Entry is an integer.
42
 
 * @MALIIT_BOOL_TYPE: Entry is a boolean.
43
 
 * @MALIIT_STRING_LIST_TYPE: Entry is a list of strings.
44
 
 * @MALIIT_INT_LIST_TYPE: Entry is a list of integers.
45
 
 *
46
 
 * This enum describes type of settings entry.
47
 
 */
48
 
static const GEnumValue maliit_settings_entry_type_values[] = {
49
 
    { MALIIT_STRING_TYPE, "MALIIT_STRING_TYPE", "string-type" },
50
 
    { MALIIT_INT_TYPE, "MALIIT_INT_TYPE", "int-type" },
51
 
    { MALIIT_BOOL_TYPE, "MALIIT_BOOL_TYPE", "bool-type" },
52
 
    { MALIIT_STRING_LIST_TYPE, "MALIIT_STRING_LIST_TYPE", "string-list-type" },
53
 
    { MALIIT_INT_LIST_TYPE, "MALIIT_INT_LIST_TYPE", "int-list-type" },
54
 
    { 0, NULL, NULL }
55
 
};
56
 
 
57
 
GType
58
 
maliit_settings_entry_type_get_type (void)
59
 
{
60
 
    static GType type = G_TYPE_INVALID;
61
 
 
62
 
    if (type == G_TYPE_INVALID) {
63
 
        type = g_enum_register_static ("MaliitSettingsEntryType",
64
 
                                       maliit_settings_entry_type_values);
65
 
    }
66
 
 
67
 
    return type;
68
 
}
69
 
 
70
 
static gboolean
71
 
check_single_value_domain(GVariant *value,
72
 
                          GVariant *domain)
73
 
{
74
 
  gsize array_len = g_variant_n_children(domain);
75
 
  gboolean equal = FALSE;
76
 
  gsize iter;
77
 
 
78
 
  for (iter = 0; iter < array_len && !equal; ++iter) {
79
 
    GVariant *child = g_variant_get_child_value(domain, iter);
80
 
 
81
 
    equal = g_variant_equal(value, child);
82
 
    g_variant_unref(child);
83
 
  }
84
 
  return equal;
85
 
}
86
 
 
87
 
static gboolean
88
 
check_variant_value_domain(GVariant *value,
89
 
                           GVariant *domain)
90
 
{
91
 
  if (!domain) {
92
 
    return TRUE;
93
 
  }
94
 
 
95
 
  if (!g_variant_is_of_type(domain, G_VARIANT_TYPE_ARRAY)) {
96
 
    return FALSE;
97
 
  }
98
 
 
99
 
  return check_single_value_domain(value, domain);
100
 
}
101
 
 
102
 
static gboolean
103
 
check_variant_array_domain(GVariant *values, GVariant *domain)
104
 
{
105
 
  gsize iter;
106
 
  gsize array_len;
107
 
  gboolean correct;
108
 
 
109
 
  if (!domain) {
110
 
    return TRUE;
111
 
  }
112
 
 
113
 
  if (!g_variant_is_of_type(domain, G_VARIANT_TYPE_ARRAY)) {
114
 
    return FALSE;
115
 
  }
116
 
 
117
 
  array_len = g_variant_n_children(values);
118
 
  correct = TRUE;
119
 
  for (iter = 0; iter < array_len && correct; ++iter) {
120
 
    GVariant *child = g_variant_get_child_value(values, iter);
121
 
 
122
 
    correct = check_single_value_domain(child, domain);
123
 
    g_variant_unref (child);
124
 
  }
125
 
  return correct;
126
 
}
127
 
 
128
 
static gboolean
129
 
check_single_value_range(GVariant *value,
130
 
                         GVariant *range_min,
131
 
                         GVariant *range_max)
132
 
{
133
 
  if (range_min) {
134
 
    if (!g_variant_is_of_type(range_min, G_VARIANT_TYPE_INT32) ||
135
 
        g_variant_get_int32(range_min) > g_variant_get_int32(value)) {
136
 
      return FALSE;
137
 
    }
138
 
  }
139
 
 
140
 
  if (range_max) {
141
 
    if (!g_variant_is_of_type(range_max, G_VARIANT_TYPE_INT32) ||
142
 
        g_variant_get_int32(range_max) < g_variant_get_int32(value)) {
143
 
      return FALSE;
144
 
    }
145
 
  }
146
 
 
147
 
  return TRUE;
148
 
}
149
 
 
150
 
static gboolean
151
 
check_variant_value_range(GVariant *value,
152
 
                          GVariant *range_min,
153
 
                          GVariant *range_max)
154
 
{
155
 
  if (!range_min && !range_max) {
156
 
    return TRUE;
157
 
  }
158
 
 
159
 
  return check_single_value_range(value, range_min, range_max);
160
 
}
161
 
 
162
 
static gboolean
163
 
check_variant_array_range(GVariant *values,
164
 
                          GVariant *range_min,
165
 
                          GVariant *range_max)
166
 
{
167
 
  gsize iter;
168
 
  gsize values_len;
169
 
  gboolean correct;
170
 
 
171
 
  if (!range_min && !range_max) {
172
 
    return TRUE;
173
 
  }
174
 
 
175
 
  correct = TRUE;
176
 
  values_len = g_variant_n_children(values);
177
 
  for (iter = 0; iter < values_len && correct; ++iter) {
178
 
    GVariant *child = g_variant_get_child_value(values, iter);
179
 
 
180
 
    correct = check_single_value_range(child, range_min, range_max);
181
 
    g_variant_unref (child);
182
 
  }
183
 
 
184
 
  return correct;
185
 
}
186
 
 
187
 
static gboolean
188
 
check_variant_array_is_int_array(GVariant *values)
189
 
{
190
 
  gsize array_len = g_variant_n_children(values);
191
 
  gsize iter;
192
 
  gboolean correct = TRUE;
193
 
 
194
 
  for (iter = 0; iter < array_len && correct; ++iter) {
195
 
    GVariant *child = g_variant_get_child_value(values, iter);
196
 
 
197
 
    correct = g_variant_is_of_type(child, G_VARIANT_TYPE_INT32);
198
 
    g_variant_unref(child);
199
 
  }
200
 
  return correct;
201
 
}
202
 
 
203
 
/**
204
 
 * maliit_validate_setting_value:
205
 
 * @type: a #MaliitSettingsEntryType to validate against.
206
 
 * @attributes: (transfer none) (element-type utf8 GLib.Variant): Attributes to validate.
207
 
 * @value: (transfer none): A #GVariant to validate.
208
 
 *
209
 
 * Validate the value for a plugin setting entry.
210
 
 *
211
 
 * Returns: %TRUE if @value and @attributes are valid for given @type.
212
 
 */
213
 
gboolean
214
 
maliit_validate_setting_value(MaliitSettingsEntryType  type,
215
 
                              GHashTable             *attributes,
216
 
                              GVariant               *value)
217
 
{
218
 
    GVariant *domain = g_hash_table_lookup(attributes,
219
 
                                           MALIIT_SETTING_VALUE_DOMAIN);
220
 
    GVariant *range_min = g_hash_table_lookup(attributes,
221
 
                                              MALIIT_SETTING_VALUE_RANGE_MIN);
222
 
    GVariant *range_max = g_hash_table_lookup(attributes,
223
 
                                              MALIIT_SETTING_VALUE_RANGE_MAX);
224
 
 
225
 
    switch (type)
226
 
    {
227
 
    case MALIIT_STRING_TYPE:
228
 
        return (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING) &&
229
 
                check_variant_value_domain(value, domain));
230
 
 
231
 
    case MALIIT_INT_TYPE:
232
 
      return (g_variant_is_of_type(value, G_VARIANT_TYPE_INT32) &&
233
 
              check_variant_value_domain(value, domain) &&
234
 
              check_variant_value_range(value, range_min, range_max));
235
 
 
236
 
    case MALIIT_BOOL_TYPE:
237
 
      return g_variant_is_of_type(value, G_VARIANT_TYPE_BOOLEAN);
238
 
 
239
 
    case MALIIT_STRING_LIST_TYPE:
240
 
      return (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING_ARRAY) &&
241
 
              check_variant_array_domain(value, domain));
242
 
 
243
 
    case MALIIT_INT_LIST_TYPE:
244
 
      return (g_variant_is_of_type(value, G_VARIANT_TYPE_ARRAY) &&
245
 
              check_variant_array_is_int_array(value) &&
246
 
              check_variant_array_domain(value, domain) &&
247
 
              check_variant_array_range(value, range_min, range_max));
248
 
 
249
 
    default:
250
 
      return FALSE;
251
 
    }
252
 
}