summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/libconninet/src/iapconf.cpp
blob: 2e77fa568f68a8fa115c6aa718f169929b3f3b28 (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
  libconninet - Internet Connectivity support library

  Copyright (C) 2009-2010 Nokia Corporation. All rights reserved.

  Contact: Aapo Makela <aapo.makela@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 <stdlib.h>
#include <string.h>
#include <conn_settings.h>

#include "iapconf.h"

#define QSTRING_TO_CONST_CSTR(str) \
    str.toUtf8().constData()

namespace Maemo {

class IAPConfPrivate {
public:
    ConnSettings *settings;

    ConnSettingsValue *variantToValue(const QVariant &variant);
    QVariant valueToVariant(ConnSettingsValue *value);
};

ConnSettingsValue *IAPConfPrivate::variantToValue(const QVariant &variant)
{
    // Convert variant to ConnSettingsValue
    ConnSettingsValue *value = conn_settings_value_new();
    if (value == 0) {
        qWarning("IAPConf: Unable to create new ConnSettingsValue");
        return 0;
    }

    switch(variant.type()) {
        
    case QVariant::Invalid:
        value->type = CONN_SETTINGS_VALUE_INVALID;
        break;
    
    case QVariant::String: {
        char *valueStr = strdup(QSTRING_TO_CONST_CSTR(variant.toString()));
        value->type = CONN_SETTINGS_VALUE_STRING;
        value->value.string_val = valueStr;
        break;
    }
    
    case QVariant::Int:
        value->type = CONN_SETTINGS_VALUE_INT;
        value->value.int_val = variant.toInt();
        break;
    
    case QMetaType::Float:
    case QVariant::Double:
        value->type = CONN_SETTINGS_VALUE_DOUBLE;
        value->value.double_val = variant.toDouble();
        break;
    
    case QVariant::Bool:
        value->type = CONN_SETTINGS_VALUE_BOOL;
        value->value.bool_val = variant.toBool() ? 1 : 0;
        break;
    
    case QVariant::ByteArray: {
        QByteArray array = variant.toByteArray();
        value->type = CONN_SETTINGS_VALUE_BYTE_ARRAY;
        value->value.byte_array.len = array.size();
        value->value.byte_array.val = (unsigned char *)malloc(array.size());
        memcpy(value->value.byte_array.val, array.constData(), array.size());
        break;
    }
    
    case QVariant::List: {
        QVariantList list = variant.toList();
        ConnSettingsValue **list_val = (ConnSettingsValue **)malloc(
            (list.size() + 1) * sizeof(ConnSettingsValue *));

        for (int idx = 0; idx < list.size(); idx++) {
            list_val[idx] = variantToValue(list.at(idx));
        }
        list_val[list.size()] = 0;

        value->type = CONN_SETTINGS_VALUE_LIST;
        value->value.list_val = list_val;
        break;
    }
    
    default:
        qWarning("IAPConf: Can not handle QVariant of type %d",
                 variant.type());
        conn_settings_value_destroy(value);
        return 0;
    }

    return value;
}

QVariant IAPConfPrivate::valueToVariant(ConnSettingsValue *value)
{
    if (value == 0 || value->type == CONN_SETTINGS_VALUE_INVALID) {
        return QVariant();
    }

    switch(value->type) {
    
    case CONN_SETTINGS_VALUE_BOOL:
        return QVariant(value->value.bool_val ? true : false);
    
    case CONN_SETTINGS_VALUE_STRING:
        return QVariant(QString(value->value.string_val));
    
    case CONN_SETTINGS_VALUE_DOUBLE:
        return QVariant(value->value.double_val);
    
    case CONN_SETTINGS_VALUE_INT:
        return QVariant(value->value.int_val);
    
    case CONN_SETTINGS_VALUE_LIST: {
        // At least with GConf backend connsettings returns byte array as list
        // of ints, first check for that case
        if (value->value.list_val && value->value.list_val[0]) {
            bool canBeConvertedToByteArray = true;
            for (int idx = 0; value->value.list_val[idx]; idx++) {
                ConnSettingsValue *val = value->value.list_val[idx];
                if (val->type != CONN_SETTINGS_VALUE_INT
                     || val->value.int_val > 255
                     || val->value.int_val < 0) {
                    canBeConvertedToByteArray = false;
                    break;
                }
            }

            if (canBeConvertedToByteArray) {
                QByteArray array;
                for (int idx = 0; value->value.list_val[idx]; idx++) {
                    array.append(value->value.list_val[idx]->value.int_val);
                }
                return array;
            }

	    // Create normal list
	    QVariantList list;
	    for (int idx = 0; value->value.list_val[idx]; idx++) {
	      list.append(valueToVariant(value->value.list_val[idx]));
	    }
	    return list;
        }
    }
    
    case CONN_SETTINGS_VALUE_BYTE_ARRAY:
        return QByteArray::fromRawData((char *)value->value.byte_array.val, 
                                       value->value.byte_array.len);
    
    default:
        return QVariant();
    }
}

// Public class implementation

IAPConf::IAPConf(const QString &iap_id)
    : d_ptr(new IAPConfPrivate)
{
    d_ptr->settings = conn_settings_open(CONN_SETTINGS_CONNECTION,
                                         QSTRING_TO_CONST_CSTR(iap_id));
    if (d_ptr->settings == 0) {
        qWarning("IAPConf: Unable to open ConnSettings for %s", 
                 QSTRING_TO_CONST_CSTR(iap_id));
    }
}

IAPConf::~IAPConf()
{
    conn_settings_close(d_ptr->settings);
    delete d_ptr;
}

void IAPConf::setValue(const QString& key, const QVariant& value)
{
    // Invalid value means unsetting the given key
    if (!value.isValid()) {
        int err = conn_settings_unset(d_ptr->settings, 
                                      QSTRING_TO_CONST_CSTR(key));
        if (err != CONN_SETTINGS_E_NO_ERROR) {
            qWarning("IAPConf: unable to unset key %s: %s",
                     QSTRING_TO_CONST_CSTR(key),
                     conn_settings_error_text((ConnSettingsError)err));
        }
        return;
    }

    // Convert value to ConnSettingsValue
    ConnSettingsValue *val = d_ptr->variantToValue(value);
    if (val == 0) return;

    // Set value and handle errors
    int error = conn_settings_set(d_ptr->settings,
                                  QSTRING_TO_CONST_CSTR(key),
                                  val);
    if (error != CONN_SETTINGS_E_NO_ERROR) {
        qWarning("IAPConf: error in setting key %s: %s", 
                 QSTRING_TO_CONST_CSTR(key), 
                 conn_settings_error_text((ConnSettingsError)error));
    }

    // Destroy value
    conn_settings_value_destroy(val);
    return;
}

void IAPConf::set(const QString& key1, const QVariant& value1, 
                  const QString& key2, const QVariant& value2, 
                  const QString& key3, const QVariant& value3, 
                  const QString& key4, const QVariant& value4, 
                  const QString& key5, const QVariant& value5, 
                  const QString& key6, const QVariant& value6, 
                  const QString& key7, const QVariant& value7, 
                  const QString& key8, const QVariant& value8, 
                  const QString& key9, const QVariant& value9,
                  const QString& key10, const QVariant& value10)
{
    if (!key1.isEmpty()) setValue(key1, value1);
    if (!key2.isEmpty()) setValue(key2, value2);
    if (!key3.isEmpty()) setValue(key3, value3);
    if (!key4.isEmpty()) setValue(key4, value4);
    if (!key5.isEmpty()) setValue(key5, value5);
    if (!key6.isEmpty()) setValue(key6, value6);
    if (!key7.isEmpty()) setValue(key7, value7);
    if (!key8.isEmpty()) setValue(key8, value8);
    if (!key9.isEmpty()) setValue(key9, value9);
    if (!key10.isEmpty()) setValue(key10, value10);
}

QVariant IAPConf::value(const QString& key) const
{
    ConnSettingsValue *val = conn_settings_get(d_ptr->settings,
                                               QSTRING_TO_CONST_CSTR(key));

    QVariant variant = d_ptr->valueToVariant(val);
    conn_settings_value_destroy(val);
    return variant;
}

void IAPConf::clear(const char *default_path)
{
    Q_UNUSED(default_path); // default path is unused

    int error = conn_settings_remove(d_ptr->settings);
    if (error != CONN_SETTINGS_E_NO_ERROR) {
        qWarning("IAPConf: Error when removing IAP: %s",
                 conn_settings_error_text((ConnSettingsError)error));
    }
}

void IAPConf::clearAll()
{
    ConnSettings *settings = conn_settings_open(CONN_SETTINGS_CONNECTION,
                                                NULL);
    conn_settings_remove(settings);
    conn_settings_close(settings);
}


void IAPConf::getAll(QList<QString> &all_iaps, bool return_path)
{
    Q_UNUSED(return_path); // We don't use return path currently

    // Go through all available connections and add them to the list
    char **ids = conn_settings_list_ids(CONN_SETTINGS_CONNECTION);
    if (ids == 0) {
        // No ids found - nothing to do
        return;
    }

    for (int idx = 0; ids[idx]; idx++) {
        all_iaps.append(QString(ids[idx]));
        free(ids[idx]);
    }
    free(ids);
}


} // namespace Maemo