Qt 4.8
iapconf.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
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 
55 public:
56  ConnSettings *settings;
57 
58  ConnSettingsValue *variantToValue(const QVariant &variant);
59  QVariant valueToVariant(ConnSettingsValue *value);
60 };
61 
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::fromUtf8(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 
209 {
210  conn_settings_close(d_ptr->settings);
211  delete d_ptr;
212 }
213 
214 
216 {
217  ConnSettingsValue *val = conn_settings_get(d_ptr->settings,
218  QSTRING_TO_CONST_CSTR(key));
219 
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
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QVariant valueToVariant(ConnSettingsValue *value)
Definition: iapconf.cpp:134
QByteArray & append(char c)
Appends the character ch to this byte array.
ConnSettings * settings
Definition: iapconf.cpp:56
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QString toString() const
Returns the variant as a QString if the variant has type() String , Bool , ByteArray ...
Definition: qvariant.cpp:2270
QList< QVariant > toList() const
Returns the variant as a QVariantList if the variant has type() List or StringList ; otherwise return...
Definition: qvariant.cpp:2751
bool toBool() const
Returns the variant as a bool if the variant has type() Bool.
Definition: qvariant.cpp:2691
The QString class provides a Unicode character string.
Definition: qstring.h:83
QByteArray toByteArray() const
Returns the variant as a QByteArray if the variant has type() ByteArray or String (converted using QS...
Definition: qvariant.cpp:2383
IAPConf(const QString &iap_id)
Definition: iapconf.cpp:197
int toInt(bool *ok=0) const
Returns the variant as an int if the variant has type() Int , Bool , ByteArray , Char ...
Definition: qvariant.cpp:2625
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
ConnSettingsValue * variantToValue(const QVariant &variant)
Definition: iapconf.cpp:62
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
static QByteArray fromRawData(const char *, int size)
Constructs a QByteArray that uses the first size bytes of the data array.
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
Q_CORE_EXPORT void qWarning(const char *,...)
virtual ~IAPConf()
Definition: iapconf.cpp:208
#define QSTRING_TO_CONST_CSTR(str)
Definition: iapconf.cpp:49
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
Type type() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1901
int key
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
static void getAll(QList< QString > &all_iaps, bool return_path=false)
Definition: iapconf.cpp:226
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
double toDouble(bool *ok=0) const
Returns the variant as a double if the variant has type() Double , QMetaType::Float ...
Definition: qvariant.cpp:2710
const char * variant
IAPConfPrivate * d_ptr
Definition: iapconf.h:69
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
QVariant value(const QString &key) const
Definition: iapconf.cpp:215