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>
14 #include <KLocalizedString>
20 struct KBalooRolesProviderSingleton
22 KBalooRolesProvider instance
;
24 Q_GLOBAL_STATIC(KBalooRolesProviderSingleton
, s_balooRolesProvider
)
27 KBalooRolesProvider
& KBalooRolesProvider::instance()
29 return s_balooRolesProvider
->instance
;
32 KBalooRolesProvider::~KBalooRolesProvider()
36 QSet
<QByteArray
> KBalooRolesProvider::roles() const
41 QHash
<QByteArray
, QVariant
> KBalooRolesProvider::roleValues(const Baloo::File
& file
,
42 const QSet
<QByteArray
>& roles
) const
44 QHash
<QByteArray
, QVariant
> values
;
46 using entry
= std::pair
<const KFileMetaData::Property::Property
&, const QVariant
&>;
48 const auto& propMap
= file
.properties();
49 auto rangeBegin
= propMap
.constKeyValueBegin();
51 while (rangeBegin
!= propMap
.constKeyValueEnd()) {
52 auto key
= (*rangeBegin
).first
;
53 const KFileMetaData::PropertyInfo
propertyInfo(key
);
54 const QByteArray role
= roleForProperty(propertyInfo
.name());
56 auto rangeEnd
= std::find_if(rangeBegin
, propMap
.constKeyValueEnd(),
57 [key
](const entry
& e
) { return e
.first
!= key
; });
59 if (role
.isEmpty() || !roles
.contains(role
)) {
60 rangeBegin
= rangeEnd
;
64 auto distance
= std::distance(rangeBegin
, rangeEnd
);
67 list
.reserve(static_cast<int>(distance
));
68 std::for_each(rangeBegin
, rangeEnd
, [&list
](const entry
& s
) { list
.append(s
.second
); });
69 values
.insert(role
, propertyInfo
.formatAsDisplayString(list
));
71 if (propertyInfo
.valueType() == QVariant::DateTime
) {
72 // Let dolphin format later Dates
73 values
.insert(role
, (*rangeBegin
).second
);
75 values
.insert(role
, propertyInfo
.formatAsDisplayString((*rangeBegin
).second
));
78 rangeBegin
= rangeEnd
;
81 KFileMetaData::UserMetaData
md(file
.path());
82 if (roles
.contains("tags")) {
83 values
.insert("tags", tagsFromValues(md
.tags()));
85 if (roles
.contains("rating")) {
86 values
.insert("rating", QString::number(md
.rating()));
88 if (roles
.contains("comment")) {
89 values
.insert("comment", md
.userComment());
91 if (roles
.contains("originUrl")) {
92 values
.insert("originUrl", md
.originUrl());
98 QByteArray
KBalooRolesProvider::roleForProperty(const QString
& property
) const
100 return m_roleForProperty
.value(property
);
103 KBalooRolesProvider::KBalooRolesProvider() :
109 const char* const property
;
110 const char* const role
;
113 // Mapping from the URIs to the KFileItemModel roles. Note that this must not be
114 // a 1:1 mapping: One role may contain several URI-values
115 static const PropertyInfo propertyInfoList
[] = {
116 { "rating", "rating" },
118 { "comment", "comment" },
119 { "title", "title" },
120 { "wordCount", "wordCount" },
121 { "lineCount", "lineCount" },
122 { "width", "width" },
123 { "height", "height" },
124 { "imageDateTime", "imageDateTime"},
125 { "imageOrientation", "orientation", },
126 { "artist", "artist" },
127 { "genre", "genre" },
128 { "album", "album" },
129 { "duration", "duration" },
130 { "bitRate", "bitrate" },
131 { "aspectRatio", "aspectRatio" },
132 { "frameRate", "frameRate" },
133 { "releaseYear", "releaseYear" },
134 { "trackNumber", "track" },
135 { "originUrl", "originUrl" }
138 for (unsigned int i
= 0; i
< sizeof(propertyInfoList
) / sizeof(PropertyInfo
); ++i
) {
139 m_roleForProperty
.insert(propertyInfoList
[i
].property
, propertyInfoList
[i
].role
);
140 m_roles
.insert(propertyInfoList
[i
].role
);
144 QString
KBalooRolesProvider::tagsFromValues(const QStringList
& values
) const
146 QStringList alphabeticalOrderTags
= values
;
148 coll
.setNumericMode(true);
149 std::sort(alphabeticalOrderTags
.begin(), alphabeticalOrderTags
.end(), [&](const QString
& s1
, const QString
& s2
){ return coll
.compare(s1
, s2
) < 0; });
150 return alphabeticalOrderTags
.join(QLatin1String(", "));