2 * SPDX-FileCopyrightText: 2012 Peter Penz <peter.penz19@gmail.com>
3 * SPDX-FileCopyrightText: 2013 Vishesh Handa <me@vhanda.in>
5 * SPDX-License-Identifier: GPL-2.0-or-later
8 #include "kbaloorolesprovider.h"
11 #include <KFileMetaData/PropertyInfo>
12 #include <KFileMetaData/UserMetaData>
18 struct KBalooRolesProviderSingleton
20 KBalooRolesProvider instance
;
22 Q_GLOBAL_STATIC(KBalooRolesProviderSingleton
, s_balooRolesProvider
)
25 KBalooRolesProvider
& KBalooRolesProvider::instance()
27 return s_balooRolesProvider
->instance
;
30 KBalooRolesProvider::~KBalooRolesProvider()
34 QSet
<QByteArray
> KBalooRolesProvider::roles() const
39 QHash
<QByteArray
, QVariant
> KBalooRolesProvider::roleValues(const Baloo::File
& file
,
40 const QSet
<QByteArray
>& roles
) const
42 QHash
<QByteArray
, QVariant
> values
;
44 using entry
= std::pair
<const KFileMetaData::Property::Property
&, const QVariant
&>;
46 const auto& propMap
= file
.properties();
47 auto rangeBegin
= propMap
.constKeyValueBegin();
49 while (rangeBegin
!= propMap
.constKeyValueEnd()) {
50 auto key
= (*rangeBegin
).first
;
51 const KFileMetaData::PropertyInfo
propertyInfo(key
);
52 const QByteArray role
= roleForProperty(propertyInfo
.name());
54 auto rangeEnd
= std::find_if(rangeBegin
, propMap
.constKeyValueEnd(),
55 [key
](const entry
& e
) { return e
.first
!= key
; });
57 if (role
.isEmpty() || !roles
.contains(role
)) {
58 rangeBegin
= rangeEnd
;
62 auto distance
= std::distance(rangeBegin
, rangeEnd
);
65 list
.reserve(static_cast<int>(distance
));
66 std::for_each(rangeBegin
, rangeEnd
, [&list
](const entry
& s
) { list
.append(s
.second
); });
67 values
.insert(role
, propertyInfo
.formatAsDisplayString(list
));
69 if (propertyInfo
.valueType() == QVariant::DateTime
) {
70 // Let dolphin format later Dates
71 values
.insert(role
, (*rangeBegin
).second
);
73 values
.insert(role
, propertyInfo
.formatAsDisplayString((*rangeBegin
).second
));
76 rangeBegin
= rangeEnd
;
79 KFileMetaData::UserMetaData
md(file
.path());
80 if (roles
.contains("tags")) {
81 values
.insert("tags", tagsFromValues(md
.tags()));
83 if (roles
.contains("rating")) {
84 values
.insert("rating", QString::number(md
.rating()));
86 if (roles
.contains("comment")) {
87 values
.insert("comment", md
.userComment());
89 if (roles
.contains("originUrl")) {
90 values
.insert("originUrl", md
.originUrl());
96 QByteArray
KBalooRolesProvider::roleForProperty(const QString
& property
) const
98 return m_roleForProperty
.value(property
);
101 KBalooRolesProvider::KBalooRolesProvider() :
107 const char* const property
;
108 const char* const role
;
111 // Mapping from the URIs to the KFileItemModel roles. Note that this must not be
112 // a 1:1 mapping: One role may contain several URI-values
113 static const PropertyInfo propertyInfoList
[] = {
114 { "rating", "rating" },
116 { "comment", "comment" },
117 { "title", "title" },
118 { "wordCount", "wordCount" },
119 { "lineCount", "lineCount" },
120 { "width", "width" },
121 { "height", "height" },
122 { "imageDateTime", "imageDateTime"},
123 { "imageOrientation", "orientation", },
124 { "artist", "artist" },
125 { "genre", "genre" },
126 { "album", "album" },
127 { "duration", "duration" },
128 { "bitRate", "bitrate" },
129 { "aspectRatio", "aspectRatio" },
130 { "frameRate", "frameRate" },
131 { "releaseYear", "releaseYear" },
132 { "trackNumber", "track" },
133 { "originUrl", "originUrl" }
136 for (unsigned int i
= 0; i
< sizeof(propertyInfoList
) / sizeof(PropertyInfo
); ++i
) {
137 m_roleForProperty
.insert(propertyInfoList
[i
].property
, propertyInfoList
[i
].role
);
138 m_roles
.insert(propertyInfoList
[i
].role
);
142 QString
KBalooRolesProvider::tagsFromValues(const QStringList
& values
) const
144 QStringList alphabeticalOrderTags
= values
;
146 coll
.setNumericMode(true);
147 std::sort(alphabeticalOrderTags
.begin(), alphabeticalOrderTags
.end(), [&](const QString
& s1
, const QString
& s2
){ return coll
.compare(s1
, s2
) < 0; });
148 return alphabeticalOrderTags
.join(QLatin1String(", "));