| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies). |
| 4 | ** Contact: http://www.qt-project.org/legal |
| 5 | ** |
| 6 | ** This file is part of the QtSystems module 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 The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #if !defined(QT_NO_GCONFLAYER) |
| 41 | |
| 42 | #include "gconflayer_p.h" |
| 43 | |
| 44 | #include <QtCore/QDataStream> |
| 45 | |
| 46 | QT_BEGIN_NAMESPACE |
| 47 | |
| 48 | Q_GLOBAL_STATIC(GConfLayer, gConfLayer) |
| 49 | |
| 50 | GConfLayer::GConfLayer() |
| 51 | { |
| 52 | GConfItem *gconfItem = new GConfItem(QString::fromLatin1("/"), true, this); |
| 53 | connect(gconfItem, SIGNAL(subtreeChanged(QString,QVariant)), this, SLOT(notifyChanged(QString,QVariant))); |
| 54 | } |
| 55 | |
| 56 | GConfLayer::~GConfLayer() |
| 57 | { |
| 58 | QMutableHashIterator<QString, GConfHandle *> i(m_handles); |
| 59 | while (i.hasNext()) { |
| 60 | i.next(); |
| 61 | doRemoveHandle(Handle(i.value())); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | QUuid GConfLayer::id() |
| 66 | { |
| 67 | return QVALUESPACE_GCONF_LAYER; |
| 68 | } |
| 69 | |
| 70 | QValueSpace::LayerOptions GConfLayer::layerOptions() const |
| 71 | { |
| 72 | return QValueSpace::PermanentLayer | QValueSpace::WritableLayer; |
| 73 | } |
| 74 | |
| 75 | GConfLayer *GConfLayer::instance() |
| 76 | { |
| 77 | return gConfLayer(); |
| 78 | } |
| 79 | |
| 80 | bool GConfLayer::value(Handle handle, QVariant *data) |
| 81 | { |
| 82 | QMutexLocker locker(&m_mutex); |
| 83 | GConfHandle *sh = gConfHandle(handle); |
| 84 | if (!sh) |
| 85 | return false; |
| 86 | |
| 87 | return getValue(InvalidHandle, sh->path, data); |
| 88 | } |
| 89 | |
| 90 | bool GConfLayer::value(Handle handle, const QString &subPath, QVariant *data) |
| 91 | { |
| 92 | QMutexLocker locker(&m_mutex); |
| 93 | return getValue(handle, subPath, data); |
| 94 | } |
| 95 | |
| 96 | bool GConfLayer::getValue(Handle handle, const QString &subPath, QVariant *data) |
| 97 | { |
| 98 | if (handle != InvalidHandle && !gConfHandle(handle)) |
| 99 | return false; |
| 100 | |
| 101 | QString path(subPath); |
| 102 | while (path.endsWith(QLatin1Char('/'))) |
| 103 | path.chop(1); |
| 104 | |
| 105 | if (handle != InvalidHandle) { |
| 106 | while (path.startsWith(QLatin1Char('/'))) |
| 107 | path = path.mid(1); |
| 108 | } |
| 109 | |
| 110 | int index = path.lastIndexOf(QLatin1Char('/'), -1); |
| 111 | bool createdHandle = false; |
| 112 | |
| 113 | QString value; |
| 114 | if (index == -1) { |
| 115 | value = path; |
| 116 | } else { |
| 117 | // want a value that is in a sub path under handle |
| 118 | value = path.mid(index + 1); |
| 119 | path.truncate(index); |
| 120 | |
| 121 | if (path.isEmpty()) |
| 122 | path.append(QLatin1Char('/')); |
| 123 | |
| 124 | handle = getItem(handle, path); |
| 125 | createdHandle = true; |
| 126 | } |
| 127 | |
| 128 | GConfHandle *sh = gConfHandle(handle); |
| 129 | if (!sh) |
| 130 | return false; |
| 131 | |
| 132 | QString fullPath(sh->path); |
| 133 | if (fullPath != QLatin1String("/") && !value.isEmpty()) |
| 134 | fullPath.append(QLatin1Char('/')); |
| 135 | |
| 136 | fullPath.append(value); |
| 137 | |
| 138 | GConfItem gconfItem(fullPath); |
| 139 | QVariant readValue = gconfItem.value(); |
| 140 | switch (readValue.type()) { |
| 141 | case QVariant::Invalid: |
| 142 | case QVariant::Bool: |
| 143 | case QVariant::Int: |
| 144 | case QVariant::Double: |
| 145 | case QVariant::StringList: |
| 146 | case QVariant::List: |
| 147 | *data = readValue; |
| 148 | break; |
| 149 | case QVariant::String: { |
| 150 | QString readString = readValue.toString(); |
| 151 | QDataStream readStream(QByteArray::fromBase64(readString.toLatin1())); |
| 152 | QVariant serializedValue; |
| 153 | readStream >> serializedValue; |
| 154 | if (serializedValue.isValid()) |
| 155 | *data = serializedValue; |
| 156 | else |
| 157 | *data = readValue; |
| 158 | break; |
| 159 | } |
| 160 | default: |
| 161 | break; |
| 162 | } |
| 163 | |
| 164 | if (createdHandle) |
| 165 | doRemoveHandle(handle); |
| 166 | |
| 167 | return data->isValid(); |
| 168 | } |
| 169 | |
| 170 | QSet<QString> GConfLayer::children(Handle handle) |
| 171 | { |
| 172 | QMutexLocker locker(&m_mutex); |
| 173 | |
| 174 | GConfHandle *sh = gConfHandle(handle); |
| 175 | if (!sh) |
| 176 | return QSet<QString>(); |
| 177 | |
| 178 | GConfItem gconfItem(sh->path); |
| 179 | |
| 180 | QSet<QString> ret; |
| 181 | foreach (const QString &child, gconfItem.listEntries() + gconfItem.listDirs()) { |
| 182 | const int index = child.lastIndexOf(QLatin1Char('/'), -1); |
| 183 | ret += child.mid(index + 1); |
| 184 | } |
| 185 | |
| 186 | return ret; |
| 187 | } |
| 188 | |
| 189 | QAbstractValueSpaceLayer::Handle GConfLayer::item(Handle parent, const QString &subPath) |
| 190 | { |
| 191 | QMutexLocker locker(&m_mutex); |
| 192 | return getItem(parent, subPath); |
| 193 | } |
| 194 | |
| 195 | QAbstractValueSpaceLayer::Handle GConfLayer::getItem(Handle parent, const QString &subPath) |
| 196 | { |
| 197 | QString fullPath; |
| 198 | |
| 199 | // Fail on invalid path. |
| 200 | if (subPath.isEmpty() || subPath.contains(QLatin1String("//"))) |
| 201 | return InvalidHandle; |
| 202 | |
| 203 | if (parent == InvalidHandle) { |
| 204 | fullPath = subPath; |
| 205 | } else { |
| 206 | GConfHandle *sh = gConfHandle(parent); |
| 207 | if (!sh) |
| 208 | return InvalidHandle; |
| 209 | |
| 210 | if (subPath == QLatin1String("/")) |
| 211 | fullPath = sh->path; |
| 212 | else if (sh->path.endsWith(QLatin1Char('/')) && subPath.startsWith(QLatin1Char('/'))) |
| 213 | fullPath = sh->path + subPath.mid(1); |
| 214 | else if (!sh->path.endsWith(QLatin1Char('/')) && !subPath.startsWith(QLatin1Char('/'))) |
| 215 | fullPath = sh->path + QLatin1Char('/') + subPath; |
| 216 | else |
| 217 | fullPath = sh->path + subPath; |
| 218 | } |
| 219 | |
| 220 | if (m_handles.contains(fullPath)) { |
| 221 | GConfHandle *sh = m_handles.value(fullPath); |
| 222 | ++sh->refCount; |
| 223 | return Handle(sh); |
| 224 | } |
| 225 | |
| 226 | // Create a new handle for path |
| 227 | GConfHandle *sh = new GConfHandle(fullPath); |
| 228 | m_handles.insert(fullPath, sh); |
| 229 | |
| 230 | return Handle(sh); |
| 231 | } |
| 232 | |
| 233 | void GConfLayer::setProperty(Handle handle, Properties properties) |
| 234 | { |
| 235 | QMutexLocker locker(&m_mutex); |
| 236 | |
| 237 | GConfHandle *sh = gConfHandle(handle); |
| 238 | if (!sh) |
| 239 | return; |
| 240 | |
| 241 | QString basePath = sh->path; |
| 242 | if (!basePath.endsWith(QLatin1Char('/'))) |
| 243 | basePath += QLatin1Char('/'); |
| 244 | |
| 245 | if (properties & QAbstractValueSpaceLayer::Publish) |
| 246 | m_monitoringHandles.insert(sh); |
| 247 | else |
| 248 | m_monitoringHandles.remove(sh); |
| 249 | } |
| 250 | |
| 251 | void GConfLayer::removeHandle(Handle handle) |
| 252 | { |
| 253 | QMutexLocker locker(&m_mutex); |
| 254 | doRemoveHandle(handle); |
| 255 | } |
| 256 | |
| 257 | void GConfLayer::doRemoveHandle(Handle handle) |
| 258 | { |
| 259 | GConfHandle *sh = gConfHandle(handle); |
| 260 | if (!sh) |
| 261 | return; |
| 262 | |
| 263 | if (--sh->refCount) |
| 264 | return; |
| 265 | |
| 266 | m_monitoringHandles.remove(sh); |
| 267 | m_handles.remove(sh->path); |
| 268 | |
| 269 | delete sh; |
| 270 | } |
| 271 | |
| 272 | bool GConfLayer::setValue(QValueSpacePublisher */*creator*/, Handle handle, const QString &subPath, const QVariant &data) |
| 273 | { |
| 274 | QMutexLocker locker(&m_mutex); |
| 275 | |
| 276 | GConfHandle *sh = gConfHandle(handle); |
| 277 | if (!sh) |
| 278 | return false; |
| 279 | |
| 280 | QString path(subPath); |
| 281 | while (path.endsWith(QLatin1Char('/'))) |
| 282 | path.chop(1); |
| 283 | |
| 284 | int index = path.lastIndexOf(QLatin1Char('/'), -1); |
| 285 | |
| 286 | bool createdHandle = false; |
| 287 | |
| 288 | QString value; |
| 289 | if (index == -1) { |
| 290 | value = path; |
| 291 | } else { |
| 292 | // want a value that is in a sub path under handle |
| 293 | value = path.mid(index + 1); |
| 294 | path.truncate(index); |
| 295 | |
| 296 | if (path.isEmpty()) |
| 297 | path.append(QLatin1Char('/')); |
| 298 | |
| 299 | sh = gConfHandle(getItem(Handle(sh), path)); |
| 300 | createdHandle = true; |
| 301 | } |
| 302 | |
| 303 | QString fullPath(sh->path); |
| 304 | if (fullPath != QLatin1String("/") && !value.isEmpty()) |
| 305 | fullPath.append(QLatin1Char('/')); |
| 306 | |
| 307 | fullPath.append(value); |
| 308 | |
| 309 | GConfItem gconfItem(fullPath); |
| 310 | switch (data.type()) { |
| 311 | case QVariant::Invalid: |
| 312 | case QVariant::Bool: |
| 313 | case QVariant::Int: |
| 314 | case QVariant::Double: |
| 315 | case QVariant::String: |
| 316 | case QVariant::StringList: |
| 317 | case QVariant::List: |
| 318 | gconfItem.set(data); |
| 319 | break; |
| 320 | default: |
| 321 | QByteArray byteArray; |
| 322 | QDataStream writeStream(&byteArray, QIODevice::WriteOnly); |
| 323 | writeStream << data; |
| 324 | QString serializedValue(QString::fromLatin1(byteArray.toBase64().data())); |
| 325 | gconfItem.set(serializedValue); |
| 326 | } |
| 327 | |
| 328 | if (createdHandle) |
| 329 | doRemoveHandle(Handle(sh)); |
| 330 | |
| 331 | return true; |
| 332 | } |
| 333 | |
| 334 | void GConfLayer::sync() |
| 335 | { |
| 336 | //Not needed |
| 337 | } |
| 338 | |
| 339 | bool GConfLayer::removeSubTree(QValueSpacePublisher * /*creator*/, Handle /*handle*/) |
| 340 | { |
| 341 | //Not needed |
| 342 | return false; |
| 343 | } |
| 344 | |
| 345 | bool GConfLayer::removeValue(QValueSpacePublisher */*creator*/, Handle handle, const QString &subPath) |
| 346 | { |
| 347 | QMutexLocker locker(&m_mutex); |
| 348 | |
| 349 | QString fullPath; |
| 350 | |
| 351 | GConfHandle *sh = gConfHandle(handle); |
| 352 | if (!sh) |
| 353 | return false; |
| 354 | |
| 355 | if (handle == InvalidHandle) { |
| 356 | fullPath = subPath; |
| 357 | } else { |
| 358 | if (subPath == QLatin1String("/")) |
| 359 | fullPath = sh->path; |
| 360 | else if (sh->path.endsWith(QLatin1Char('/')) && subPath.startsWith(QLatin1Char('/'))) |
| 361 | fullPath = sh->path + subPath.mid(1); |
| 362 | else if (!sh->path.endsWith(QLatin1Char('/')) && !subPath.startsWith(QLatin1Char('/'))) |
| 363 | fullPath = sh->path + QLatin1Char('/') + subPath; |
| 364 | else |
| 365 | fullPath = sh->path + subPath; |
| 366 | } |
| 367 | |
| 368 | GConfItem gconfItem(fullPath); |
| 369 | gconfItem.recursiveUnset(); |
| 370 | |
| 371 | return true; |
| 372 | } |
| 373 | |
| 374 | void GConfLayer::addWatch(QValueSpacePublisher *, Handle) |
| 375 | { |
| 376 | //Not needed |
| 377 | } |
| 378 | |
| 379 | void GConfLayer::removeWatches(QValueSpacePublisher *, Handle) |
| 380 | { |
| 381 | //Not needed |
| 382 | } |
| 383 | |
| 384 | bool GConfLayer::supportsInterestNotification() const |
| 385 | { |
| 386 | return false; |
| 387 | } |
| 388 | |
| 389 | bool GConfLayer::notifyInterest(Handle, bool) |
| 390 | { |
| 391 | //Not needed |
| 392 | return false; |
| 393 | } |
| 394 | |
| 395 | void GConfLayer::notifyChanged(const QString &key, const QVariant & /*value*/) |
| 396 | { |
| 397 | foreach (GConfHandle *handle, m_monitoringHandles.values()) { |
| 398 | if (key.startsWith(handle->path)) |
| 399 | emit handleChanged(Handle(handle)); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | QT_END_NAMESPACE |
| 404 | |
| 405 | #endif // QT_NO_GCONFLAYER |