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 QString
tagsFromValues(const QStringList
& values
)
20 if (values
.size() == 1) {
24 QStringList alphabeticalOrderTags
= values
;
26 coll
.setNumericMode(true);
27 std::sort(alphabeticalOrderTags
.begin(), alphabeticalOrderTags
.end(), [&](const QString
& s1
, const QString
& s2
){ return coll
.compare(s1
, s2
) < 0; });
28 return alphabeticalOrderTags
.join(QLatin1String(", "));
31 using Property
= KFileMetaData::Property::Property
;
32 // Mapping from the KFM::Property to the KFileItemModel roles.
33 const QHash
<Property
, QByteArray
> propertyRoleMap() {
34 static const auto map
= QHash
<Property
, QByteArray
> {
35 { Property::Rating
, QByteArrayLiteral("rating") },
36 { Property::Comment
, QByteArrayLiteral("comment") },
37 { Property::Title
, QByteArrayLiteral("title") },
38 { Property::Author
, QByteArrayLiteral("author") },
39 { Property::WordCount
, QByteArrayLiteral("wordCount") },
40 { Property::LineCount
, QByteArrayLiteral("lineCount") },
41 { Property::Width
, QByteArrayLiteral("width") },
42 { Property::Height
, QByteArrayLiteral("height") },
43 { Property::ImageDateTime
, QByteArrayLiteral("imageDateTime") },
44 { Property::ImageOrientation
, QByteArrayLiteral("orientation") },
45 { Property::Artist
, QByteArrayLiteral("artist") },
46 { Property::Genre
, QByteArrayLiteral("genre") },
47 { Property::Album
, QByteArrayLiteral("album") },
48 { Property::Duration
, QByteArrayLiteral("duration") },
49 { Property::BitRate
, QByteArrayLiteral("bitrate") },
50 { Property::AspectRatio
, QByteArrayLiteral("aspectRatio") },
51 { Property::FrameRate
, QByteArrayLiteral("frameRate") },
52 { Property::ReleaseYear
, QByteArrayLiteral("releaseYear") },
53 { Property::TrackNumber
, QByteArrayLiteral("track") }
59 struct KBalooRolesProviderSingleton
61 KBalooRolesProvider instance
;
63 Q_GLOBAL_STATIC(KBalooRolesProviderSingleton
, s_balooRolesProvider
)
66 KBalooRolesProvider
& KBalooRolesProvider::instance()
68 return s_balooRolesProvider
->instance
;
71 KBalooRolesProvider::~KBalooRolesProvider()
75 QSet
<QByteArray
> KBalooRolesProvider::roles() const
80 QHash
<QByteArray
, QVariant
> KBalooRolesProvider::roleValues(const Baloo::File
& file
,
81 const QSet
<QByteArray
>& roles
) const
83 QHash
<QByteArray
, QVariant
> values
;
85 using entry
= std::pair
<const KFileMetaData::Property::Property
&, const QVariant
&>;
87 const auto& propMap
= file
.properties();
88 auto rangeBegin
= propMap
.constKeyValueBegin();
90 while (rangeBegin
!= propMap
.constKeyValueEnd()) {
91 auto key
= (*rangeBegin
).first
;
93 auto rangeEnd
= std::find_if(rangeBegin
, propMap
.constKeyValueEnd(),
94 [key
](const entry
& e
) { return e
.first
!= key
; });
96 const QByteArray role
= propertyRoleMap().value(key
);
97 if (role
.isEmpty() || !roles
.contains(role
)) {
98 rangeBegin
= rangeEnd
;
102 const KFileMetaData::PropertyInfo
propertyInfo(key
);
103 auto distance
= std::distance(rangeBegin
, rangeEnd
);
106 list
.reserve(static_cast<int>(distance
));
107 std::for_each(rangeBegin
, rangeEnd
, [&list
](const entry
& s
) { list
.append(s
.second
); });
108 values
.insert(role
, propertyInfo
.formatAsDisplayString(list
));
110 if (propertyInfo
.valueType() == QVariant::DateTime
) {
111 // Let dolphin format later Dates
112 values
.insert(role
, (*rangeBegin
).second
);
114 values
.insert(role
, propertyInfo
.formatAsDisplayString((*rangeBegin
).second
));
117 rangeBegin
= rangeEnd
;
120 if (roles
.contains("dimensions")) {
121 bool widthOk
= false;
122 bool heightOk
= false;
124 const int width
= propMap
.value(KFileMetaData::Property::Width
).toInt(&widthOk
);
125 const int height
= propMap
.value(KFileMetaData::Property::Height
).toInt(&heightOk
);
127 if (widthOk
&& heightOk
&& width
>= 0 && height
>= 0) {
128 values
.insert("dimensions", QSize(width
, height
));
132 KFileMetaData::UserMetaData::Attributes attributes
;
133 if (roles
.contains("tags")) {
134 attributes
|= KFileMetaData::UserMetaData::Tags
;
136 if (roles
.contains("rating")) {
137 attributes
|= KFileMetaData::UserMetaData::Rating
;
139 if (roles
.contains("comment")) {
140 attributes
|= KFileMetaData::UserMetaData::Comment
;
142 if (roles
.contains("originUrl")) {
143 attributes
|= KFileMetaData::UserMetaData::OriginUrl
;
146 if (attributes
== KFileMetaData::UserMetaData::None
) {
150 KFileMetaData::UserMetaData
md(file
.path());
151 attributes
= md
.queryAttributes(attributes
);
153 if (attributes
& KFileMetaData::UserMetaData::Tags
) {
154 values
.insert("tags", tagsFromValues(md
.tags()));
156 if (attributes
& KFileMetaData::UserMetaData::Rating
) {
157 values
.insert("rating", QString::number(md
.rating()));
159 if (attributes
& KFileMetaData::UserMetaData::Comment
) {
160 values
.insert("comment", md
.userComment());
162 if (attributes
& KFileMetaData::UserMetaData::OriginUrl
) {
163 values
.insert("originUrl", md
.originUrl());
169 KBalooRolesProvider::KBalooRolesProvider()
171 // Display roles filled from Baloo property cache
172 for (const auto& role
: propertyRoleMap()) {
173 m_roles
.insert(role
);
175 m_roles
.insert("dimensions");
177 // Display roles provided by UserMetaData
178 m_roles
.insert(QByteArrayLiteral("tags"));
179 m_roles
.insert(QByteArrayLiteral("rating"));
180 m_roles
.insert(QByteArrayLiteral("comment"));
181 m_roles
.insert(QByteArrayLiteral("originUrl"));