1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
6 | **
|
---|
7 | ** This file is part of the plugins of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at qt-info@nokia.com.
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 |
|
---|
43 | #include <stdlib.h>
|
---|
44 | #include <string.h>
|
---|
45 | #include <conn_settings.h>
|
---|
46 |
|
---|
47 | #include "iapconf.h"
|
---|
48 |
|
---|
49 | #define QSTRING_TO_CONST_CSTR(str) \
|
---|
50 | str.toUtf8().constData()
|
---|
51 |
|
---|
52 | namespace Maemo {
|
---|
53 |
|
---|
54 | class IAPConfPrivate {
|
---|
55 | public:
|
---|
56 | ConnSettings *settings;
|
---|
57 |
|
---|
58 | ConnSettingsValue *variantToValue(const QVariant &variant);
|
---|
59 | QVariant valueToVariant(ConnSettingsValue *value);
|
---|
60 | };
|
---|
61 |
|
---|
62 | ConnSettingsValue *IAPConfPrivate::variantToValue(const QVariant &variant)
|
---|
63 | {
|
---|
64 | // Convert variant to ConnSettingsValue
|
---|
65 | ConnSettingsValue *value = conn_settings_value_new();
|
---|
66 | if (value == 0) {
|
---|
67 | qWarning("IAPConf: Unable to create new ConnSettingsValue");
|
---|
68 | return 0;
|
---|
69 | }
|
---|
70 |
|
---|
71 | switch(variant.type()) {
|
---|
72 |
|
---|
73 | case QVariant::Invalid:
|
---|
74 | value->type = CONN_SETTINGS_VALUE_INVALID;
|
---|
75 | break;
|
---|
76 |
|
---|
77 | case QVariant::String: {
|
---|
78 | char *valueStr = strdup(QSTRING_TO_CONST_CSTR(variant.toString()));
|
---|
79 | value->type = CONN_SETTINGS_VALUE_STRING;
|
---|
80 | value->value.string_val = valueStr;
|
---|
81 | break;
|
---|
82 | }
|
---|
83 |
|
---|
84 | case QVariant::Int:
|
---|
85 | value->type = CONN_SETTINGS_VALUE_INT;
|
---|
86 | value->value.int_val = variant.toInt();
|
---|
87 | break;
|
---|
88 |
|
---|
89 | case QMetaType::Float:
|
---|
90 | case QVariant::Double:
|
---|
91 | value->type = CONN_SETTINGS_VALUE_DOUBLE;
|
---|
92 | value->value.double_val = variant.toDouble();
|
---|
93 | break;
|
---|
94 |
|
---|
95 | case QVariant::Bool:
|
---|
96 | value->type = CONN_SETTINGS_VALUE_BOOL;
|
---|
97 | value->value.bool_val = variant.toBool() ? 1 : 0;
|
---|
98 | break;
|
---|
99 |
|
---|
100 | case QVariant::ByteArray: {
|
---|
101 | QByteArray array = variant.toByteArray();
|
---|
102 | value->type = CONN_SETTINGS_VALUE_BYTE_ARRAY;
|
---|
103 | value->value.byte_array.len = array.size();
|
---|
104 | value->value.byte_array.val = (unsigned char *)malloc(array.size());
|
---|
105 | memcpy(value->value.byte_array.val, array.constData(), array.size());
|
---|
106 | break;
|
---|
107 | }
|
---|
108 |
|
---|
109 | case QVariant::List: {
|
---|
110 | QVariantList list = variant.toList();
|
---|
111 | ConnSettingsValue **list_val = (ConnSettingsValue **)malloc(
|
---|
112 | (list.size() + 1) * sizeof(ConnSettingsValue *));
|
---|
113 |
|
---|
114 | for (int idx = 0; idx < list.size(); idx++) {
|
---|
115 | list_val[idx] = variantToValue(list.at(idx));
|
---|
116 | }
|
---|
117 | list_val[list.size()] = 0;
|
---|
118 |
|
---|
119 | value->type = CONN_SETTINGS_VALUE_LIST;
|
---|
120 | value->value.list_val = list_val;
|
---|
121 | break;
|
---|
122 | }
|
---|
123 |
|
---|
124 | default:
|
---|
125 | qWarning("IAPConf: Can not handle QVariant of type %d",
|
---|
126 | variant.type());
|
---|
127 | conn_settings_value_destroy(value);
|
---|
128 | return 0;
|
---|
129 | }
|
---|
130 |
|
---|
131 | return value;
|
---|
132 | }
|
---|
133 |
|
---|
134 | QVariant IAPConfPrivate::valueToVariant(ConnSettingsValue *value)
|
---|
135 | {
|
---|
136 | if (value == 0 || value->type == CONN_SETTINGS_VALUE_INVALID) {
|
---|
137 | return QVariant();
|
---|
138 | }
|
---|
139 |
|
---|
140 | switch(value->type) {
|
---|
141 |
|
---|
142 | case CONN_SETTINGS_VALUE_BOOL:
|
---|
143 | return QVariant(value->value.bool_val ? true : false);
|
---|
144 |
|
---|
145 | case CONN_SETTINGS_VALUE_STRING:
|
---|
146 | return QVariant(QString(value->value.string_val));
|
---|
147 |
|
---|
148 | case CONN_SETTINGS_VALUE_DOUBLE:
|
---|
149 | return QVariant(value->value.double_val);
|
---|
150 |
|
---|
151 | case CONN_SETTINGS_VALUE_INT:
|
---|
152 | return QVariant(value->value.int_val);
|
---|
153 |
|
---|
154 | case CONN_SETTINGS_VALUE_LIST: {
|
---|
155 | // At least with GConf backend connsettings returns byte array as list
|
---|
156 | // of ints, first check for that case
|
---|
157 | if (value->value.list_val && value->value.list_val[0]) {
|
---|
158 | bool canBeConvertedToByteArray = true;
|
---|
159 | for (int idx = 0; value->value.list_val[idx]; idx++) {
|
---|
160 | ConnSettingsValue *val = value->value.list_val[idx];
|
---|
161 | if (val->type != CONN_SETTINGS_VALUE_INT
|
---|
162 | || val->value.int_val > 255
|
---|
163 | || val->value.int_val < 0) {
|
---|
164 | canBeConvertedToByteArray = false;
|
---|
165 | break;
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | if (canBeConvertedToByteArray) {
|
---|
170 | QByteArray array;
|
---|
171 | for (int idx = 0; value->value.list_val[idx]; idx++) {
|
---|
172 | array.append(value->value.list_val[idx]->value.int_val);
|
---|
173 | }
|
---|
174 | return array;
|
---|
175 | }
|
---|
176 |
|
---|
177 | // Create normal list
|
---|
178 | QVariantList list;
|
---|
179 | for (int idx = 0; value->value.list_val[idx]; idx++) {
|
---|
180 | list.append(valueToVariant(value->value.list_val[idx]));
|
---|
181 | }
|
---|
182 | return list;
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | case CONN_SETTINGS_VALUE_BYTE_ARRAY:
|
---|
187 | return QByteArray::fromRawData((char *)value->value.byte_array.val,
|
---|
188 | value->value.byte_array.len);
|
---|
189 |
|
---|
190 | default:
|
---|
191 | return QVariant();
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | // Public class implementation
|
---|
196 |
|
---|
197 | IAPConf::IAPConf(const QString &iap_id)
|
---|
198 | : d_ptr(new IAPConfPrivate)
|
---|
199 | {
|
---|
200 | d_ptr->settings = conn_settings_open(CONN_SETTINGS_CONNECTION,
|
---|
201 | QSTRING_TO_CONST_CSTR(iap_id));
|
---|
202 | if (d_ptr->settings == 0) {
|
---|
203 | qWarning("IAPConf: Unable to open ConnSettings for %s",
|
---|
204 | QSTRING_TO_CONST_CSTR(iap_id));
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | IAPConf::~IAPConf()
|
---|
209 | {
|
---|
210 | conn_settings_close(d_ptr->settings);
|
---|
211 | delete d_ptr;
|
---|
212 | }
|
---|
213 |
|
---|
214 |
|
---|
215 | QVariant IAPConf::value(const QString& key) const
|
---|
216 | {
|
---|
217 | ConnSettingsValue *val = conn_settings_get(d_ptr->settings,
|
---|
218 | QSTRING_TO_CONST_CSTR(key));
|
---|
219 |
|
---|
220 | QVariant variant = d_ptr->valueToVariant(val);
|
---|
221 | conn_settings_value_destroy(val);
|
---|
222 | return variant;
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 | void IAPConf::getAll(QList<QString> &all_iaps, bool return_path)
|
---|
227 | {
|
---|
228 | Q_UNUSED(return_path); // We don't use return path currently
|
---|
229 |
|
---|
230 | // Go through all available connections and add them to the list
|
---|
231 | char **ids = conn_settings_list_ids(CONN_SETTINGS_CONNECTION);
|
---|
232 | if (ids == 0) {
|
---|
233 | // No ids found - nothing to do
|
---|
234 | return;
|
---|
235 | }
|
---|
236 |
|
---|
237 | for (int idx = 0; ids[idx]; idx++) {
|
---|
238 | all_iaps.append(QString(ids[idx]));
|
---|
239 | free(ids[idx]);
|
---|
240 | }
|
---|
241 | free(ids);
|
---|
242 | }
|
---|
243 |
|
---|
244 |
|
---|
245 | } // namespace Maemo
|
---|