Baloo Widgets

filemetadatautil.cpp
1 /*
2  SPDX-FileCopyrightText: 2019 Stefan BrĂ¼ns <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "filemetadatautil_p.h"
8 #include <KFileMetaData/PropertyInfo>
9 #include <KFileMetaData/UserMetaData>
10 
11 #include <algorithm>
12 
13 namespace Baloo
14 {
15 namespace Private
16 {
17 QVariantMap convertUserMetaData(const KFileMetaData::UserMetaData &metaData)
18 {
19  using Attribute = KFileMetaData::UserMetaData::Attribute;
20  QVariantMap properties;
21 
22  QFlags<Attribute> attributes = metaData.queryAttributes(Attribute::Tags | Attribute::Rating | Attribute::Comment | Attribute::OriginUrl);
23 
24  if (attributes & Attribute::Tags) {
25  QStringList tags = metaData.tags();
26  if (!tags.isEmpty()) {
27  properties.insert(QStringLiteral("tags"), tags);
28  }
29  }
30 
31  if (attributes & Attribute::Rating) {
32  int rating = metaData.rating();
33  if (rating) {
34  properties.insert(QStringLiteral("rating"), rating);
35  }
36  }
37 
38  if (attributes & Attribute::Comment) {
39  QString comment = metaData.userComment();
40  if (!comment.isEmpty()) {
41  properties.insert(QStringLiteral("userComment"), comment);
42  }
43  }
44 
45  if (attributes & Attribute::OriginUrl) {
46  const QString originUrl = metaData.originUrl().toDisplayString();
47  if (!originUrl.isEmpty()) {
48  properties.insert(QStringLiteral("originUrl"), originUrl);
49  }
50  }
51 
52  return properties;
53 }
54 
55 QVariantMap toNamedVariantMap(const KFileMetaData::PropertyMultiMap &propMap)
56 {
57  QVariantMap map;
58  if (propMap.isEmpty()) {
59  return map;
60  }
61 
62  using entry = std::pair<const KFileMetaData::Property::Property &, const QVariant &>;
63 
64  auto begin = propMap.constKeyValueBegin();
65 
66  while (begin != propMap.constKeyValueEnd()) {
67  auto key = (*begin).first;
68  KFileMetaData::PropertyInfo property(key);
69  auto rangeEnd = std::find_if(begin, propMap.constKeyValueEnd(), [key](const entry &e) {
70  return e.first != key;
71  });
72 
73  auto distance = std::distance(begin, rangeEnd);
74  if (distance > 1) {
75  QVariantList list;
76  list.reserve(static_cast<int>(distance));
77  std::for_each(begin, rangeEnd, [&list](const entry &s) {
78  list.append(s.second);
79  });
80  map.insert(property.name(), list);
81  } else {
82  map.insert(property.name(), (*begin).second);
83  }
84  begin = rangeEnd;
85  }
86 
87  return map;
88 }
89 
90 } // namespace KFMPrivate
91 } // namespace Baloo
void append(const T &value)
KGuiItem properties()
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
const QList< QKeySequence > & begin()
void reserve(int alloc)
KOSM_EXPORT double distance(const std::vector< const OSM::Node * > &path, Coordinate coord)
bool isEmpty() const const
bool isEmpty() const const
QFuture< void > map(Sequence &sequence, MapFunctor function)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri Aug 19 2022 04:12:56 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.