Baloo

propertydata.cpp
1 /*
2  This file is part of the KDE Baloo project.
3  SPDX-FileCopyrightText: 2019 Stefan BrĂ¼ns <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.1-or-later
6 */
7 
8 #include "propertydata.h"
9 #include <QJsonArray>
10 #include <QJsonValue>
11 
12 namespace Baloo
13 {
14 
15 const 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) {
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();
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 
63 const 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
QString number(int n, int base)
Type type(const QSqlDatabase &db)
KGuiItem properties()
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
QJsonObject::iterator insert(const QString &key, const QJsonValue &value)
Implements storage for docIds without any associated data Instantiated for:
Definition: coding.cpp:11
bool isEmpty() const const
QJsonValue fromVariant(const QVariant &variant)
T takeLast()
QVector< V > values(const QMultiHash< K, V > &c)
T value(int i) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Nov 29 2023 03:56:26 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.