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::Author
, QByteArrayLiteral("author") },
41 { Property::WordCount
, QByteArrayLiteral("wordCount") },
42 { Property::LineCount
, QByteArrayLiteral("lineCount") },
43 { Property::Width
, QByteArrayLiteral("width") },
44 { Property::Height
, QByteArrayLiteral("height") },
45 { Property::ImageDateTime
, QByteArrayLiteral("imageDateTime") },
46 { Property::ImageOrientation
, QByteArrayLiteral("orientation") },
47 { Property::Artist
, QByteArrayLiteral("artist") },
48 { Property::Genre
, QByteArrayLiteral("genre") },
49 { Property::Album
, QByteArrayLiteral("album") },
50 { Property::Duration
, QByteArrayLiteral("duration") },
51 { Property::BitRate
, QByteArrayLiteral("bitrate") },
52 { Property::AspectRatio
, QByteArrayLiteral("aspectRatio") },
53 { Property::FrameRate
, QByteArrayLiteral("frameRate") },
54 { Property::ReleaseYear
, QByteArrayLiteral("releaseYear") },
55 { Property::TrackNumber
, QByteArrayLiteral("track") }
61 struct KBalooRolesProviderSingleton
63 KBalooRolesProvider instance
;
65 Q_GLOBAL_STATIC(KBalooRolesProviderSingleton
, s_balooRolesProvider
)
68 KBalooRolesProvider
& KBalooRolesProvider::instance()
70 return s_balooRolesProvider
->instance
;
73 KBalooRolesProvider::~KBalooRolesProvider()
77 QSet
<QByteArray
> KBalooRolesProvider::roles() const
82 QHash
<QByteArray
, QVariant
> KBalooRolesProvider::roleValues(const Baloo::File
& file
,
83 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(),
96 [key
](const entry
& e
) { return e
.first
!= key
; });
98 const QByteArray role
= propertyRoleMap().value(key
);
99 if (role
.isEmpty() || !roles
.contains(role
)) {
100 rangeBegin
= rangeEnd
;
104 const KFileMetaData::PropertyInfo
propertyInfo(key
);
105 auto distance
= std::distance(rangeBegin
, rangeEnd
);
108 list
.reserve(static_cast<int>(distance
));
109 std::for_each(rangeBegin
, rangeEnd
, [&list
](const entry
& s
) { list
.append(s
.second
); });
110 values
.insert(role
, propertyInfo
.formatAsDisplayString(list
));
112 if (propertyInfo
.valueType() == QVariant::DateTime
) {
113 // Let dolphin format later Dates
114 values
.insert(role
, (*rangeBegin
).second
);
116 values
.insert(role
, propertyInfo
.formatAsDisplayString((*rangeBegin
).second
));
119 rangeBegin
= rangeEnd
;
122 if (roles
.contains("dimensions")) {
123 bool widthOk
= false;
124 bool heightOk
= false;
126 const int width
= propMap
.value(KFileMetaData::Property::Width
).toInt(&widthOk
);
127 const int height
= propMap
.value(KFileMetaData::Property::Height
).toInt(&heightOk
);
129 if (widthOk
&& heightOk
&& width
>= 0 && height
>= 0) {
130 values
.insert("dimensions", QSize(width
, height
));
134 KFileMetaData::UserMetaData::Attributes attributes
;
135 if (roles
.contains("tags")) {
136 attributes
|= KFileMetaData::UserMetaData::Tags
;
138 if (roles
.contains("rating")) {
139 attributes
|= KFileMetaData::UserMetaData::Rating
;
141 if (roles
.contains("comment")) {
142 attributes
|= KFileMetaData::UserMetaData::Comment
;
144 if (roles
.contains("originUrl")) {
145 attributes
|= KFileMetaData::UserMetaData::OriginUrl
;
148 if (attributes
== KFileMetaData::UserMetaData::None
) {
152 KFileMetaData::UserMetaData
md(file
.path());
153 attributes
= md
.queryAttributes(attributes
);
155 if (attributes
& KFileMetaData::UserMetaData::Tags
) {
156 values
.insert("tags", tagsFromValues(md
.tags()));
158 if (attributes
& KFileMetaData::UserMetaData::Rating
) {
159 values
.insert("rating", QString::number(md
.rating()));
161 if (attributes
& KFileMetaData::UserMetaData::Comment
) {
162 values
.insert("comment", md
.userComment());
164 if (attributes
& KFileMetaData::UserMetaData::OriginUrl
) {
165 values
.insert("originUrl", md
.originUrl());
171 KBalooRolesProvider::KBalooRolesProvider()
173 // Display roles filled from Baloo property cache
174 for (const auto& role
: propertyRoleMap()) {
175 m_roles
.insert(role
);
177 m_roles
.insert("dimensions");
179 // Display roles provided by UserMetaData
180 m_roles
.insert(QByteArrayLiteral("tags"));
181 m_roles
.insert(QByteArrayLiteral("rating"));
182 m_roles
.insert(QByteArrayLiteral("comment"));
183 m_roles
.insert(QByteArrayLiteral("originUrl"));