Qt 4.8
Public Functions | Public Variables | List of all members
Maemo::IAPConfPrivate Class Reference

Public Functions

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

Public Variables

ConnSettings * settings
 

Detailed Description

Definition at line 54 of file iapconf.cpp.

Functions

◆ valueToVariant()

QVariant Maemo::IAPConfPrivate::valueToVariant ( ConnSettingsValue *  value)

Definition at line 134 of file iapconf.cpp.

Referenced by Maemo::IAPConf::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 }
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.
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
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

◆ variantToValue()

ConnSettingsValue * Maemo::IAPConfPrivate::variantToValue ( const QVariant variant)

Definition at line 62 of file iapconf.cpp.

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 }
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
QByteArray toByteArray() const
Returns the variant as a QByteArray if the variant has type() ByteArray or String (converted using QS...
Definition: qvariant.cpp:2383
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
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
Q_CORE_EXPORT void qWarning(const char *,...)
#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 size() const
Returns the number of items in the list.
Definition: qlist.h:137
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

Properties

◆ settings

ConnSettings* Maemo::IAPConfPrivate::settings

The documentation for this class was generated from the following file: