| 1 |
/* |
| 2 |
* libkysdk-appconf2's Library |
| 3 |
* |
| 4 |
* Copyright (C) 2024, KylinSoft Co., Ltd. |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 3 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this library. If not, see <https://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
* Authors: Zhenyu Wang <[email protected]> |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "kconf2.h" |
| 24 |
#include <QDebug> |
| 25 |
#include <QByteArray> |
| 26 |
#include <string> |
| 27 |
#include <iostream> |
| 28 |
#include <libkysettings.h> |
| 29 |
namespace kdk { |
| 30 |
|
| 31 |
enum KCONF2TYPE { |
| 32 |
KCONF2_TYPE_BOOLEAN = 'b', |
| 33 |
KCONF2_TYPE_BYTE = 'y', |
| 34 |
KCONF2_TYPE_INT16 = 'n', |
| 35 |
KCONF2_TYPE_UINT16 = 'q', |
| 36 |
KCONF2_TYPE_INT32 = 'i', |
| 37 |
KCONF2_TYPE_UINT32 = 'u', |
| 38 |
KCONF2_TYPE_INT64 = 'x', |
| 39 |
KCONF2_TYPE_UINT64 = 't', |
| 40 |
KCONF2_TYPE_HANDLE = 'h', |
| 41 |
KCONF2_TYPE_DOUBLE = 'd', |
| 42 |
KCONF2_TYPE_STRING = 's', |
| 43 |
KCONF2_TYPE_OBJECT_PATH = 'o', |
| 44 |
KCONF2_TYPE_SIGNATURE = 'g', |
| 45 |
KCONF2_TYPE_VARIANT = 'v', |
| 46 |
KCONF2_TYPE_MAYBE = 'm', |
| 47 |
KCONF2_TYPE_ARRAY = 'a', |
| 48 |
KCONF2_TYPE_TUPLE = '(', |
| 49 |
KCONF2_TYPE_DICT_ENTRY = '{' |
| 50 |
}; |
| 51 |
|
| 52 |
class KConf2Private : public QObject |
| 53 |
{ |
| 54 |
Q_OBJECT |
| 55 |
Q_DECLARE_PUBLIC(KConf2) |
| 56 |
|
| 57 |
public: |
| 58 |
KConf2Private(KConf2* parent); |
| 59 |
QVariant typesToQvariant(const char *key, const char *value) const; |
| 60 |
QVariant stringListToQvariant(char **strList) const; |
| 61 |
char** strvFromQVariant(const QVariant& variant); |
| 62 |
|
| 63 |
private: |
| 64 |
KConf2 *q_ptr; |
| 65 |
KSettings* m_pGsettings = nullptr; |
| 66 |
long signal_handler_id; |
| 67 |
}; |
| 68 |
|
| 69 |
void k_free(char **str) |
| 70 |
{ |
| 71 |
for (int i = 0; str[i]; i++) |
| 72 |
free(str[i]); |
| 73 |
} |
| 74 |
|
| 75 |
QString qtifyName(const char *name) |
| 76 |
{ |
| 77 |
bool next_cap = false; |
| 78 |
QString result; |
| 79 |
|
| 80 |
while (*name) { |
| 81 |
if (*name == '-') { |
| 82 |
next_cap = true; |
| 83 |
} else if (next_cap) { |
| 84 |
result.append(QChar(*name).toUpper().toLatin1()); |
| 85 |
next_cap = false; |
| 86 |
} else { |
| 87 |
result.append(*name); |
| 88 |
} |
| 89 |
|
| 90 |
name++; |
| 91 |
} |
| 92 |
|
| 93 |
return result; |
| 94 |
} |
| 95 |
|
| 96 |
static void settingChanged(KSettings *ukuistyle_settings, const char *key, void *user_data) |
| 97 |
{ |
| 98 |
KConf2 *self = (KConf2 *)user_data; |
| 99 |
|
| 100 |
QMetaObject::invokeMethod(self, "changed", Qt::QueuedConnection, Q_ARG(QString, qtifyName(key))); |
| 101 |
} |
| 102 |
|
| 103 |
KConf2::KConf2(const QByteArray &schema_id, const QString version, QObject *parent) |
| 104 |
:d_ptr(new KConf2Private(this)), |
| 105 |
QObject(parent) |
| 106 |
{ |
| 107 |
Q_D(KConf2); |
| 108 |
if (!d->m_pGsettings) |
| 109 |
{ |
| 110 |
if(version.isEmpty()) |
| 111 |
d->m_pGsettings = kdk_conf2_new(schema_id, NULL); |
| 112 |
else |
| 113 |
d->m_pGsettings = kdk_conf2_new(schema_id, version.toStdString().c_str()); |
| 114 |
} |
| 115 |
d->signal_handler_id = kdk_conf2_connect_signal(d->m_pGsettings, "changed",(KCallBack)settingChanged, this); |
| 116 |
} |
| 117 |
|
| 118 |
KConf2::~KConf2() |
| 119 |
{ |
| 120 |
Q_D(KConf2); |
| 121 |
kdk_conf2_ksettings_destroy(d->m_pGsettings); |
| 122 |
} |
| 123 |
|
| 124 |
QVariant KConf2::get(const QString &key) const |
| 125 |
{ |
| 126 |
Q_D(const KConf2); |
| 127 |
|
| 128 |
char*type = kdk_conf2_get_type(d->m_pGsettings, key.toUtf8().data()); |
| 129 |
if(strcmp(type, "as") == 0) |
| 130 |
{ |
| 131 |
char** strList = kdk_conf2_get_strv(d->m_pGsettings, key.toUtf8().data()); |
| 132 |
if(strList == NULL) |
| 133 |
{ |
| 134 |
free(type); |
| 135 |
free(strList); |
| 136 |
qWarning()<<"There is no value for key"; |
| 137 |
} |
| 138 |
else |
| 139 |
{ |
| 140 |
QVariant qvalue = d->stringListToQvariant(strList); |
| 141 |
free(strList); |
| 142 |
free(type); |
| 143 |
return qvalue; |
| 144 |
} |
| 145 |
} |
| 146 |
else |
| 147 |
{ |
| 148 |
char *value = kdk_conf2_get_value(d->m_pGsettings, key.toUtf8().data()); |
| 149 |
|
| 150 |
if(value == NULL ) |
| 151 |
{ |
| 152 |
qWarning()<<"There is no value for key"; |
| 153 |
free(value); |
| 154 |
free(type); |
| 155 |
return QVariant(); |
| 156 |
} |
| 157 |
else |
| 158 |
{ |
| 159 |
QVariant qvalue = d->typesToQvariant(key.toUtf8().data(), value); |
| 160 |
free(value); |
| 161 |
free(type); |
| 162 |
return qvalue; |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
void KConf2::set(const QString &key, const QVariant &value) |
| 168 |
{ |
| 169 |
if (!this->trySet(key, value)) |
| 170 |
qWarning("unable to set key '%s' to value '%s'", key.toUtf8().constData(), value.toString().toUtf8().constData()); |
| 171 |
} |
| 172 |
|
| 173 |
bool KConf2::trySet(const QString &key, const QVariant &value) |
| 174 |
{ |
| 175 |
Q_D(KConf2); |
| 176 |
if (!d->m_pGsettings) |
| 177 |
return false; |
| 178 |
|
| 179 |
bool success = false; |
| 180 |
|
| 181 |
char *type = kdk_conf2_get_type(d->m_pGsettings, key.toUtf8().data()); |
| 182 |
if(strcmp(type, "as") == 0) |
| 183 |
{ |
| 184 |
char** strv = d->strvFromQVariant(value); |
| 185 |
success = kdk_conf2_set_strv(d->m_pGsettings, key.toStdString().c_str(), strv); |
| 186 |
} |
| 187 |
else |
| 188 |
{ |
| 189 |
switch (*type) { |
| 190 |
case KCONF2_TYPE_BOOLEAN: |
| 191 |
{ |
| 192 |
qDebug()<<value.toInt(); |
| 193 |
success = kdk_conf2_set_boolean(d->m_pGsettings, key.toUtf8().data(), 0); |
| 194 |
break; |
| 195 |
} |
| 196 |
case KCONF2_TYPE_INT32: |
| 197 |
success = kdk_conf2_set_int(d->m_pGsettings, key.toUtf8().data(), value.toInt()); |
| 198 |
break; |
| 199 |
case KCONF2_TYPE_INT64: |
| 200 |
success = kdk_conf2_set_int64(d->m_pGsettings, key.toUtf8().data(), value.toLongLong()); |
| 201 |
break; |
| 202 |
case KCONF2_TYPE_UINT64: |
| 203 |
success = kdk_conf2_set_uint64(d->m_pGsettings, key.toUtf8().data(), value.toULongLong()); |
| 204 |
break; |
| 205 |
case KCONF2_TYPE_DOUBLE: |
| 206 |
success = kdk_conf2_set_double(d->m_pGsettings, key.toUtf8().data(), value.toDouble()); |
| 207 |
break; |
| 208 |
case KCONF2_TYPE_STRING: |
| 209 |
default: |
| 210 |
success = kdk_conf2_set_string(d->m_pGsettings, key.toUtf8().data(), value.toString().toStdString().c_str()); |
| 211 |
break; |
| 212 |
} |
| 213 |
} |
| 214 |
return success; |
| 215 |
} |
| 216 |
|
| 217 |
QStringList KConf2::keys() const |
| 218 |
{ |
| 219 |
Q_D(const KConf2); |
| 220 |
QStringList list; |
| 221 |
|
| 222 |
char **keys = kdk_conf2_list_keys(d->m_pGsettings); |
| 223 |
for (int i = 0; keys[i]; i++) |
| 224 |
list.append(qtifyName(keys[i])); |
| 225 |
|
| 226 |
k_free(keys); |
| 227 |
|
| 228 |
return list; |
| 229 |
} |
| 230 |
|
| 231 |
void KConf2::reset(const QString &qkey) |
| 232 |
{ |
| 233 |
Q_D(KConf2); |
| 234 |
if (!d->m_pGsettings) |
| 235 |
return; |
| 236 |
|
| 237 |
kdk_conf2_reset(d->m_pGsettings, qkey.toUtf8().data()); |
| 238 |
} |
| 239 |
|
| 240 |
bool KConf2::isSettingsAvailable(const QString &schema_id) |
| 241 |
{ |
| 242 |
bool result = false; |
| 243 |
if (kdk_conf2_new(schema_id.toUtf8().data(), NULL)) |
| 244 |
{ |
| 245 |
result = true; |
| 246 |
} |
| 247 |
return result; |
| 248 |
} |
| 249 |
|
| 250 |
QStringList KConf2::schemas(const QString &app, const QString &version) const |
| 251 |
{ |
| 252 |
Q_D(const KConf2); |
| 253 |
QStringList list; |
| 254 |
|
| 255 |
char **schemas = kdk_conf2_list_schemas(app.toUtf8().data(),version.toUtf8().data()); |
| 256 |
if(schemas == NULL) |
| 257 |
{ |
| 258 |
qWarning()<<"The obtained list schemas is empty"; |
| 259 |
} |
| 260 |
else |
| 261 |
{ |
| 262 |
for (int i = 0; schemas[i]; i++) |
| 263 |
list.append(qtifyName(schemas[i])); |
| 264 |
|
| 265 |
k_free(schemas); |
| 266 |
} |
| 267 |
return list; |
| 268 |
} |
| 269 |
|
| 270 |
QString KConf2::getDescrition(QString &key) |
| 271 |
{ |
| 272 |
Q_D(KConf2); |
| 273 |
char *value = kdk_conf2_get_descrition(d->m_pGsettings, key.toUtf8().data()); |
| 274 |
|
| 275 |
QString str = QString::fromUtf8(value); |
| 276 |
|
| 277 |
free(value); |
| 278 |
return str; |
| 279 |
} |
| 280 |
|
| 281 |
QString KConf2::getSummary(QString &key) |
| 282 |
{ |
| 283 |
Q_D(KConf2); |
| 284 |
char *value = kdk_conf2_get_summary(d->m_pGsettings, key.toUtf8().data()); |
| 285 |
|
| 286 |
QString str = QString::fromUtf8(value); |
| 287 |
|
| 288 |
free(value); |
| 289 |
|
| 290 |
return str; |
| 291 |
} |
| 292 |
|
| 293 |
QString KConf2::getType(QString &key) |
| 294 |
{ |
| 295 |
Q_D(KConf2); |
| 296 |
char *value = kdk_conf2_get_type(d->m_pGsettings, key.toUtf8().data()); |
| 297 |
|
| 298 |
QString str = QString::fromUtf8(value); |
| 299 |
|
| 300 |
free(value); |
| 301 |
return str; |
| 302 |
} |
| 303 |
|
| 304 |
bool KConf2::isWritable(QString &key) |
| 305 |
{ |
| 306 |
Q_D(KConf2); |
| 307 |
if (!d->m_pGsettings) |
| 308 |
return false; |
| 309 |
|
| 310 |
bool success = false; |
| 311 |
|
| 312 |
success = kdk_conf2_is_writable(d->m_pGsettings, key.toUtf8().data()); |
| 313 |
return success; |
| 314 |
} |
| 315 |
|
| 316 |
QString KConf2::getId() |
| 317 |
{ |
| 318 |
Q_D(KConf2); |
| 319 |
char *value = kdk_conf2_get_id(d->m_pGsettings); |
| 320 |
QString str = QString::fromUtf8(value); |
| 321 |
|
| 322 |
free(value); |
| 323 |
return str; |
| 324 |
} |
| 325 |
|
| 326 |
QString KConf2::getVersion() |
| 327 |
{ |
| 328 |
Q_D(KConf2); |
| 329 |
char *value = kdk_conf2_get_version(d->m_pGsettings); |
| 330 |
QString str = QString::fromUtf8(value); |
| 331 |
|
| 332 |
free(value); |
| 333 |
return str; |
| 334 |
} |
| 335 |
|
| 336 |
QStringList KConf2::children() const |
| 337 |
{ |
| 338 |
Q_D(const KConf2); |
| 339 |
QStringList list; |
| 340 |
|
| 341 |
char **keys = kdk_conf2_list_children(d->m_pGsettings); |
| 342 |
for (int i = 0; keys[i]; i++) |
| 343 |
list.append(qtifyName(keys[i])); |
| 344 |
|
| 345 |
k_free(keys); |
| 346 |
|
| 347 |
return list; |
| 348 |
} |
| 349 |
|
| 350 |
KConf2 *KConf2::getChild(QString &name) |
| 351 |
{ |
| 352 |
Q_D(KConf2); |
| 353 |
KSettings *settings = kdk_conf2_get_child(d->m_pGsettings, name.toUtf8().data()); |
| 354 |
KConf2* conf = new KConf2(kdk_conf2_get_id(settings)); |
| 355 |
return conf; |
| 356 |
} |
| 357 |
|
| 358 |
KConf2Private::KConf2Private(KConf2 *parent) |
| 359 |
:q_ptr(parent) |
| 360 |
{ |
| 361 |
setParent(parent); |
| 362 |
} |
| 363 |
|
| 364 |
QVariant KConf2Private::typesToQvariant(const char *key, const char *value) const |
| 365 |
{ |
| 366 |
Q_Q(const KConf2); |
| 367 |
char *type = kdk_conf2_get_type(m_pGsettings, key); |
| 368 |
switch (*type) |
| 369 |
{ |
| 370 |
case KCONF2_TYPE_BOOLEAN: |
| 371 |
return QVariant(value); |
| 372 |
|
| 373 |
case KCONF2_TYPE_BYTE: |
| 374 |
return QVariant(QString(value)); |
| 375 |
|
| 376 |
case KCONF2_TYPE_INT16: |
| 377 |
return QVariant(QString(value).toShort()); |
| 378 |
|
| 379 |
case KCONF2_TYPE_UINT16: |
| 380 |
return QVariant(QString(value).toUShort()); |
| 381 |
|
| 382 |
case KCONF2_TYPE_INT32: |
| 383 |
return QVariant(QString(value).toInt()); |
| 384 |
|
| 385 |
case KCONF2_TYPE_UINT32: |
| 386 |
return QVariant(QString(value).toUInt()); |
| 387 |
|
| 388 |
case KCONF2_TYPE_INT64: |
| 389 |
return QVariant(QString(value).toLongLong()); |
| 390 |
|
| 391 |
case KCONF2_TYPE_UINT64: |
| 392 |
return QVariant(QString(value).toULongLong()); |
| 393 |
|
| 394 |
case KCONF2_TYPE_DOUBLE: |
| 395 |
return QVariant(QString(value).toDouble()); |
| 396 |
|
| 397 |
case KCONF2_TYPE_STRING: |
| 398 |
return QVariant(QString(value)); |
| 399 |
|
| 400 |
case KCONF2_TYPE_ARRAY: |
| 401 |
default: |
| 402 |
return QVariant(); |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
QVariant KConf2Private::stringListToQvariant(char **strList) const |
| 407 |
{ |
| 408 |
QStringList list; |
| 409 |
for (int i = 0; strList[i]; i++) |
| 410 |
list.append((strList[i])); |
| 411 |
return QVariant(list); |
| 412 |
} |
| 413 |
|
| 414 |
char **KConf2Private::strvFromQVariant(const QVariant &variant) |
| 415 |
{ |
| 416 |
QStringList strList = variant.toStringList(); |
| 417 |
if(strList.isEmpty()) |
| 418 |
return nullptr; |
| 419 |
char** result = new char*[strList.count()+1]; |
| 420 |
for (int i = 0; i < strList.count(); ++i) { |
| 421 |
QByteArray byteArray = strList.at(i).toUtf8(); |
| 422 |
const int size = byteArray.size() + 1; |
| 423 |
result[i] = new char[size]; |
| 424 |
strncpy(result[i], byteArray.constData(), size); |
| 425 |
result[i][size - 1] = '\0'; |
| 426 |
} |
| 427 |
result[strList.count()] = nullptr; |
| 428 |
return result; |
| 429 |
} |
| 430 |
|
| 431 |
} |
| 432 |
|
| 433 |
#include "kconf2.moc" |
| 434 |
#include "moc_kconf2.cpp" |