Baloo Widgets

filemetadatautil.cpp
1 /*****************************************************************************
2  * Copyright (C) 2019 by Stefan BrĂ¼ns <[email protected]> *
3  * *
4  * This library is free software; you can redistribute it and/or *
5  * modify it under the terms of the GNU Library General Public *
6  * License as published by the Free Software Foundation; either *
7  * version 2 of the License, or (at your option) any later version. *
8  * *
9  * This library is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12  * Library General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU Library General Public License *
15  * along with this library; see the file COPYING.LIB. If not, write to *
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17  * Boston, MA 02110-1301, USA. *
18  *****************************************************************************/
19 
20 #include "filemetadatautil_p.h"
21 #include <KFileMetaData/UserMetaData>
22 #include <KFileMetaData/PropertyInfo>
23 
24 #include <algorithm>
25 
26 namespace Baloo
27 {
28 namespace Private
29 {
30 
31 QVariantMap convertUserMetaData(const KFileMetaData::UserMetaData& metaData)
32 {
33  using Attribute = KFileMetaData::UserMetaData::Attribute;
34  QVariantMap properties;
35 
36  QFlags<Attribute> attributes = metaData.queryAttributes(Attribute::Tags | Attribute::Rating |
37  Attribute::Comment | Attribute::OriginUrl);
38 
39  if (attributes & Attribute::Tags) {
40  QStringList tags = metaData.tags();
41  if (!tags.isEmpty()) {
42  properties.insert(QStringLiteral("tags"), tags);
43  }
44  }
45 
46  if (attributes & Attribute::Rating) {
47  int rating = metaData.rating();
48  if (rating) {
49  properties.insert(QStringLiteral("rating"), rating);
50  }
51  }
52 
53  if (attributes & Attribute::Comment) {
54  QString comment = metaData.userComment();
55  if (!comment.isEmpty()) {
56  properties.insert(QStringLiteral("userComment"), comment);
57  }
58  }
59 
60  if (attributes & Attribute::OriginUrl) {
61  const QString originUrl = metaData.originUrl().toDisplayString();
62  if (!originUrl.isEmpty()) {
63  properties.insert(QStringLiteral("originUrl"), originUrl);
64  }
65  }
66 
67  return properties;
68 }
69 
70 QVariantMap toNamedVariantMap(const KFileMetaData::PropertyMap& propMap)
71 {
72  QVariantMap map;
73  if (propMap.isEmpty()) {
74  return map;
75  }
76 
77  using entry = std::pair<const KFileMetaData::Property::Property&, const QVariant&>;
78 
79  auto begin = propMap.constKeyValueBegin();
80 
81  while (begin != propMap.constKeyValueEnd()) {
82  auto key = (*begin).first;
83  KFileMetaData::PropertyInfo property(key);
84  auto rangeEnd = std::find_if(begin, propMap.constKeyValueEnd(),
85  [key](const entry& e) { return e.first != key; });
86 
87  auto distance = std::distance(begin, rangeEnd);
88  if (distance > 1) {
89  QVariantList list;
90  list.reserve(static_cast<int>(distance));
91  std::for_each(begin, rangeEnd, [&list](const entry& s) { list.append(s.second); });
92  map.insert(property.name(), list);
93  } else {
94  map.insert(property.name(), (*begin).second);
95  }
96  begin = rangeEnd;
97  }
98 
99  return map;
100 }
101 
102 } // namespace KFMPrivate
103 } // namespace Baloo
void reserve(int alloc)
const QList< QKeySequence > & begin()
QString & insert(int position, QChar ch)
KGuiItem properties()
bool isEmpty() const const
bool isEmpty() const const
int distance(const GeoCoordinates &coord1, const GeoCoordinates &coord2)
void insert(int i, const T &value)
QFuture< void > map(Sequence &sequence, MapFunctor function)
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Oct 21 2021 23:07:11 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.