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>
19 QString
tagsFromValues(const QStringList
&values
)
21 if (values
.size() == 1) {
25 QStringList alphabeticalOrderTags
= values
;
27 coll
.setNumericMode(true);
28 std::sort(alphabeticalOrderTags
.begin(), alphabeticalOrderTags
.end(), [&](const QString
&s1
, const QString
&s2
) {
29 return coll
.compare(s1
, s2
) < 0;
31 return alphabeticalOrderTags
.join(QLatin1String(", "));
34 using Property
= KFileMetaData::Property::Property
;
35 // Mapping from the KFM::Property to the KFileItemModel roles.
36 const QHash
<Property
, QByteArray
> propertyRoleMap() {
37 static const auto map
= QHash
<Property
, QByteArray
> {
38 { Property::Rating
, QByteArrayLiteral("rating") },
39 { Property::Comment
, QByteArrayLiteral("comment") },
40 { Property::Title
, QByteArrayLiteral("title") },
41 { Property::Author
, QByteArrayLiteral("author") },
42 { Property::Publisher
, QByteArrayLiteral("publisher") },
43 { Property::PageCount
, QByteArrayLiteral("pageCount") },
44 { Property::WordCount
, QByteArrayLiteral("wordCount") },
45 { Property::LineCount
, QByteArrayLiteral("lineCount") },
46 { Property::Width
, QByteArrayLiteral("width") },
47 { Property::Height
, QByteArrayLiteral("height") },
48 { Property::ImageDateTime
, QByteArrayLiteral("imageDateTime") },
49 { Property::ImageOrientation
, QByteArrayLiteral("orientation") },
50 { Property::Artist
, QByteArrayLiteral("artist") },
51 { Property::Genre
, QByteArrayLiteral("genre") },
52 { Property::Album
, QByteArrayLiteral("album") },
53 { Property::Duration
, QByteArrayLiteral("duration") },
54 { Property::BitRate
, QByteArrayLiteral("bitrate") },
55 { Property::AspectRatio
, QByteArrayLiteral("aspectRatio") },
56 { Property::FrameRate
, QByteArrayLiteral("frameRate") },
57 { Property::ReleaseYear
, QByteArrayLiteral("releaseYear") },
58 { Property::TrackNumber
, QByteArrayLiteral("track") }
64 struct KBalooRolesProviderSingleton
{
65 KBalooRolesProvider instance
;
67 Q_GLOBAL_STATIC(KBalooRolesProviderSingleton
, s_balooRolesProvider
)
69 KBalooRolesProvider
&KBalooRolesProvider::instance()
71 return s_balooRolesProvider
->instance
;
74 KBalooRolesProvider::~KBalooRolesProvider()
78 QSet
<QByteArray
> KBalooRolesProvider::roles() const
83 QHash
<QByteArray
, QVariant
> KBalooRolesProvider::roleValues(const Baloo::File
&file
, const QSet
<QByteArray
> &roles
) const
85 QHash
<QByteArray
, QVariant
> values
;
87 using entry
= std::pair
<const KFileMetaData::Property::Property
&, const QVariant
&>;
89 const auto &propMap
= file
.properties();
90 auto rangeBegin
= propMap
.constKeyValueBegin();
92 while (rangeBegin
!= propMap
.constKeyValueEnd()) {
93 auto key
= (*rangeBegin
).first
;
95 auto rangeEnd
= std::find_if(rangeBegin
, propMap
.constKeyValueEnd(), [key
](const entry
&e
) {
96 return e
.first
!= key
;
99 const QByteArray role
= propertyRoleMap().value(key
);
100 if (role
.isEmpty() || !roles
.contains(role
)) {
101 rangeBegin
= rangeEnd
;
105 const KFileMetaData::PropertyInfo
propertyInfo(key
);
106 auto distance
= std::distance(rangeBegin
, rangeEnd
);
109 list
.reserve(static_cast<int>(distance
));
110 std::for_each(rangeBegin
, rangeEnd
, [&list
](const entry
&s
) {
111 list
.append(s
.second
);
113 values
.insert(role
, propertyInfo
.formatAsDisplayString(list
));
115 if (propertyInfo
.valueType() == QMetaType::Type::QDateTime
) {
116 // Let dolphin format later Dates
117 values
.insert(role
, (*rangeBegin
).second
);
119 values
.insert(role
, propertyInfo
.formatAsDisplayString((*rangeBegin
).second
));
122 rangeBegin
= rangeEnd
;
125 if (roles
.contains("dimensions")) {
126 bool widthOk
= false;
127 bool heightOk
= false;
129 const int width
= propMap
.value(KFileMetaData::Property::Width
).toInt(&widthOk
);
130 const int height
= propMap
.value(KFileMetaData::Property::Height
).toInt(&heightOk
);
132 if (widthOk
&& heightOk
&& width
>= 0 && height
>= 0) {
133 values
.insert("dimensions", QSize(width
, height
));
137 KFileMetaData::UserMetaData::Attributes attributes
;
138 if (roles
.contains("tags")) {
139 attributes
|= KFileMetaData::UserMetaData::Tags
;
141 if (roles
.contains("rating")) {
142 attributes
|= KFileMetaData::UserMetaData::Rating
;
144 if (roles
.contains("comment")) {
145 attributes
|= KFileMetaData::UserMetaData::Comment
;
147 if (roles
.contains("originUrl")) {
148 attributes
|= KFileMetaData::UserMetaData::OriginUrl
;
151 if (attributes
== KFileMetaData::UserMetaData::None
) {
155 KFileMetaData::UserMetaData
md(file
.path());
156 attributes
= md
.queryAttributes(attributes
);
158 if (attributes
& KFileMetaData::UserMetaData::Tags
) {
159 values
.insert("tags", tagsFromValues(md
.tags()));
161 if (attributes
& KFileMetaData::UserMetaData::Rating
) {
162 values
.insert("rating", QString::number(md
.rating()));
164 if (attributes
& KFileMetaData::UserMetaData::Comment
) {
165 values
.insert("comment", md
.userComment());
167 if (attributes
& KFileMetaData::UserMetaData::OriginUrl
) {
168 values
.insert("originUrl", md
.originUrl());
174 KBalooRolesProvider::KBalooRolesProvider()
176 // Display roles filled from Baloo property cache
177 for (const auto &role
: propertyRoleMap()) {
178 m_roles
.insert(role
);
180 m_roles
.insert("dimensions");
182 // Display roles provided by UserMetaData
183 m_roles
.insert(QByteArrayLiteral("tags"));
184 m_roles
.insert(QByteArrayLiteral("rating"));
185 m_roles
.insert(QByteArrayLiteral("comment"));
186 m_roles
.insert(QByteArrayLiteral("originUrl"));