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>
20 QString
tagsFromValues(const QStringList
& values
)
22 if (values
.size() == 1) {
26 QStringList alphabeticalOrderTags
= values
;
28 coll
.setNumericMode(true);
29 std::sort(alphabeticalOrderTags
.begin(), alphabeticalOrderTags
.end(), [&](const QString
& s1
, const QString
& s2
){ return coll
.compare(s1
, s2
) < 0; });
30 return alphabeticalOrderTags
.join(QLatin1String(", "));
33 using Property
= KFileMetaData::Property::Property
;
34 // Mapping from the KFM::Property to the KFileItemModel roles.
35 const QHash
<Property
, QByteArray
> propertyRoleMap() {
36 static const auto map
= QHash
<Property
, QByteArray
> {
37 { Property::Rating
, QByteArrayLiteral("rating") },
38 { Property::Comment
, QByteArrayLiteral("comment") },
39 { Property::Title
, QByteArrayLiteral("title") },
40 { Property::WordCount
, QByteArrayLiteral("wordCount") },
41 { Property::LineCount
, QByteArrayLiteral("lineCount") },
42 { Property::Width
, QByteArrayLiteral("width") },
43 { Property::Height
, QByteArrayLiteral("height") },
44 { Property::ImageDateTime
, QByteArrayLiteral("imageDateTime") },
45 { Property::ImageOrientation
, QByteArrayLiteral("orientation") },
46 { Property::Artist
, QByteArrayLiteral("artist") },
47 { Property::Genre
, QByteArrayLiteral("genre") },
48 { Property::Album
, QByteArrayLiteral("album") },
49 { Property::Duration
, QByteArrayLiteral("duration") },
50 { Property::BitRate
, QByteArrayLiteral("bitrate") },
51 { Property::AspectRatio
, QByteArrayLiteral("aspectRatio") },
52 { Property::FrameRate
, QByteArrayLiteral("frameRate") },
53 { Property::ReleaseYear
, QByteArrayLiteral("releaseYear") },
54 { Property::TrackNumber
, QByteArrayLiteral("track") }
60 struct KBalooRolesProviderSingleton
62 KBalooRolesProvider instance
;
64 Q_GLOBAL_STATIC(KBalooRolesProviderSingleton
, s_balooRolesProvider
)
67 KBalooRolesProvider
& KBalooRolesProvider::instance()
69 return s_balooRolesProvider
->instance
;
72 KBalooRolesProvider::~KBalooRolesProvider()
76 QSet
<QByteArray
> KBalooRolesProvider::roles() const
81 QHash
<QByteArray
, QVariant
> KBalooRolesProvider::roleValues(const Baloo::File
& file
,
82 const QSet
<QByteArray
>& roles
) const
84 QHash
<QByteArray
, QVariant
> values
;
86 using entry
= std::pair
<const KFileMetaData::Property::Property
&, const QVariant
&>;
88 const auto& propMap
= file
.properties();
89 auto rangeBegin
= propMap
.constKeyValueBegin();
91 while (rangeBegin
!= propMap
.constKeyValueEnd()) {
92 auto key
= (*rangeBegin
).first
;
94 auto rangeEnd
= std::find_if(rangeBegin
, propMap
.constKeyValueEnd(),
95 [key
](const entry
& e
) { return e
.first
!= key
; });
97 const QByteArray role
= propertyRoleMap().value(key
);
98 if (role
.isEmpty() || !roles
.contains(role
)) {
99 rangeBegin
= rangeEnd
;
103 const KFileMetaData::PropertyInfo
propertyInfo(key
);
104 auto distance
= std::distance(rangeBegin
, rangeEnd
);
107 list
.reserve(static_cast<int>(distance
));
108 std::for_each(rangeBegin
, rangeEnd
, [&list
](const entry
& s
) { list
.append(s
.second
); });
109 values
.insert(role
, propertyInfo
.formatAsDisplayString(list
));
111 if (propertyInfo
.valueType() == QVariant::DateTime
) {
112 // Let dolphin format later Dates
113 values
.insert(role
, (*rangeBegin
).second
);
115 values
.insert(role
, propertyInfo
.formatAsDisplayString((*rangeBegin
).second
));
118 rangeBegin
= rangeEnd
;
121 if (roles
.contains("dimensions")) {
122 bool widthOk
= false;
123 bool heightOk
= false;
125 const int width
= propMap
.value(KFileMetaData::Property::Width
).toInt(&widthOk
);
126 const int height
= propMap
.value(KFileMetaData::Property::Height
).toInt(&heightOk
);
128 if (widthOk
&& heightOk
&& width
>= 0 && height
>= 0) {
129 values
.insert("dimensions", QSize(width
, height
));
133 KFileMetaData::UserMetaData::Attributes attributes
;
134 if (roles
.contains("tags")) {
135 attributes
|= KFileMetaData::UserMetaData::Tags
;
137 if (roles
.contains("rating")) {
138 attributes
|= KFileMetaData::UserMetaData::Rating
;
140 if (roles
.contains("comment")) {
141 attributes
|= KFileMetaData::UserMetaData::Comment
;
143 if (roles
.contains("originUrl")) {
144 attributes
|= KFileMetaData::UserMetaData::OriginUrl
;
147 if (attributes
== KFileMetaData::UserMetaData::None
) {
151 KFileMetaData::UserMetaData
md(file
.path());
152 attributes
= md
.queryAttributes(attributes
);
154 if (attributes
& KFileMetaData::UserMetaData::Tags
) {
155 values
.insert("tags", tagsFromValues(md
.tags()));
157 if (attributes
& KFileMetaData::UserMetaData::Rating
) {
158 values
.insert("rating", QString::number(md
.rating()));
160 if (attributes
& KFileMetaData::UserMetaData::Comment
) {
161 values
.insert("comment", md
.userComment());
163 if (attributes
& KFileMetaData::UserMetaData::OriginUrl
) {
164 values
.insert("originUrl", md
.originUrl());
170 KBalooRolesProvider::KBalooRolesProvider()
172 // Display roles filled from Baloo property cache
173 for (const auto& role
: propertyRoleMap()) {
174 m_roles
.insert(role
);
176 m_roles
.insert("dimensions");
178 // Display roles provided by UserMetaData
179 m_roles
.insert(QByteArrayLiteral("tags"));
180 m_roles
.insert(QByteArrayLiteral("rating"));
181 m_roles
.insert(QByteArrayLiteral("comment"));
182 m_roles
.insert(QByteArrayLiteral("originUrl"));