Baloo

propertydata.cpp
1/*
2 This file is part of the KDE Baloo project.
3 SPDX-FileCopyrightText: 2019 Stefan BrĂ¼ns <stefan.bruens@rwth-aachen.de>
4
5 SPDX-License-Identifier: LGPL-2.1-or-later
6*/
7
8#include "propertydata.h"
9#include <QJsonArray>
10#include <QJsonValue>
11
12namespace Baloo
13{
14
15const QJsonObject propertyMapToJson(const KFileMetaData::PropertyMultiMap& properties)
16{
17 auto it = properties.cbegin();
18 QJsonObject jsonDict;
19
20 while (it != properties.cend()) {
21 auto property = it.key();
22 QString keyString = QString::number(static_cast<int>(property));
23
24 auto rangeEnd = properties.upperBound(property);
25
26 QJsonValue value;
27 // In case a key has multiple values, convert to QJsonArray
28 if (std::distance(it, rangeEnd) > 1) {
29 QJsonArray values;
30
31 // Last inserted is first element, for backwards compatible
32 // ordering prepend earlier elements
33 while (it != rangeEnd) {
34 values.insert(0, QJsonValue::fromVariant(it.value()));
35 ++it;
36 };
37
38 value = values;
39 } else {
40 auto type = it.value().userType();
41 if ((type == QMetaType::QVariantList) || (type == QMetaType::QStringList)) {
42 // if it is a QList<T>, recurse
43 auto list = it.value().toList();
44 QJsonArray values;
45 while (!list.isEmpty()) {
47 }
48 value = values;
49 } else {
50 value = QJsonValue::fromVariant(it.value());
51 }
52 }
53
54 jsonDict.insert(keyString, value);
55
56 // pivot to next key
57 it = rangeEnd;
58 }
59
60 return jsonDict;
61}
62
63const KFileMetaData::PropertyMultiMap jsonToPropertyMap(const QJsonObject& properties)
64{
65 KFileMetaData::PropertyMultiMap propertyMap;
66
67 auto it = properties.begin();
68 while (it != properties.end()) {
69 int propNum = it.key().toInt();
70 auto prop = static_cast<KFileMetaData::Property::Property>(propNum);
71
72 if (it.value().isArray()) {
73 const auto values = it.value().toArray();
74 for (const auto val : values) {
75 propertyMap.insert(prop, val.toVariant());
76 }
77
78 } else if (it.value().isDouble()) {
79 propertyMap.insert(prop, it.value().toDouble());
80 } else {
81 propertyMap.insert(prop, it.value().toString());
82 }
83 ++it;
84 }
85
86 return propertyMap;
87}
88
89} // namespace Baloo
Type type(const QSqlDatabase &db)
Implements storage for docIds without any associated data Instantiated for:
Definition coding.cpp:11
KIOCORE_EXPORT QStringList list(const QString &fileClass)
KGuiItem properties()
iterator insert(iterator before, const QJsonValue &value)
void push_back(const QJsonValue &value)
iterator insert(QLatin1StringView key, const QJsonValue &value)
QJsonValue fromVariant(const QVariant &variant)
bool isEmpty() const const
value_type takeLast()
T value(qsizetype i) const const
QString number(double n, char format, int precision)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:58 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.