]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kbaloorolesprovider.cpp
Fix icon resize animation
[dolphin.git] / src / kitemviews / private / kbaloorolesprovider.cpp
1 /*
2 * SPDX-FileCopyrightText: 2012 Peter Penz <peter.penz19@gmail.com>
3 * SPDX-FileCopyrightText: 2013 Vishesh Handa <me@vhanda.in>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8 #include "kbaloorolesprovider.h"
9
10 #include <Baloo/File>
11 #include <KFileMetaData/PropertyInfo>
12 #include <KFileMetaData/UserMetaData>
13
14 #include <QCollator>
15 #include <QDebug>
16 #include <QSize>
17 #include <QTime>
18
19 namespace {
20 QString tagsFromValues(const QStringList& values)
21 {
22 if (values.size() == 1) {
23 return values.at(0);
24 }
25
26 QStringList alphabeticalOrderTags = values;
27 QCollator coll;
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(", "));
31 }
32
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") }
55 };
56 return map;
57 }
58 }
59
60 struct KBalooRolesProviderSingleton
61 {
62 KBalooRolesProvider instance;
63 };
64 Q_GLOBAL_STATIC(KBalooRolesProviderSingleton, s_balooRolesProvider)
65
66
67 KBalooRolesProvider& KBalooRolesProvider::instance()
68 {
69 return s_balooRolesProvider->instance;
70 }
71
72 KBalooRolesProvider::~KBalooRolesProvider()
73 {
74 }
75
76 QSet<QByteArray> KBalooRolesProvider::roles() const
77 {
78 return m_roles;
79 }
80
81 QHash<QByteArray, QVariant> KBalooRolesProvider::roleValues(const Baloo::File& file,
82 const QSet<QByteArray>& roles) const
83 {
84 QHash<QByteArray, QVariant> values;
85
86 using entry = std::pair<const KFileMetaData::Property::Property&, const QVariant&>;
87
88 const auto& propMap = file.properties();
89 auto rangeBegin = propMap.constKeyValueBegin();
90
91 while (rangeBegin != propMap.constKeyValueEnd()) {
92 auto key = (*rangeBegin).first;
93
94 auto rangeEnd = std::find_if(rangeBegin, propMap.constKeyValueEnd(),
95 [key](const entry& e) { return e.first != key; });
96
97 const QByteArray role = propertyRoleMap().value(key);
98 if (role.isEmpty() || !roles.contains(role)) {
99 rangeBegin = rangeEnd;
100 continue;
101 }
102
103 const KFileMetaData::PropertyInfo propertyInfo(key);
104 auto distance = std::distance(rangeBegin, rangeEnd);
105 if (distance > 1) {
106 QVariantList list;
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));
110 } else {
111 if (propertyInfo.valueType() == QVariant::DateTime) {
112 // Let dolphin format later Dates
113 values.insert(role, (*rangeBegin).second);
114 } else {
115 values.insert(role, propertyInfo.formatAsDisplayString((*rangeBegin).second));
116 }
117 }
118 rangeBegin = rangeEnd;
119 }
120
121 if (roles.contains("dimensions")) {
122 bool widthOk = false;
123 bool heightOk = false;
124
125 const int width = propMap.value(KFileMetaData::Property::Width).toInt(&widthOk);
126 const int height = propMap.value(KFileMetaData::Property::Height).toInt(&heightOk);
127
128 if (widthOk && heightOk && width >= 0 && height >= 0) {
129 values.insert("dimensions", QSize(width, height));
130 }
131 }
132
133 KFileMetaData::UserMetaData::Attributes attributes;
134 if (roles.contains("tags")) {
135 attributes |= KFileMetaData::UserMetaData::Tags;
136 }
137 if (roles.contains("rating")) {
138 attributes |= KFileMetaData::UserMetaData::Rating;
139 }
140 if (roles.contains("comment")) {
141 attributes |= KFileMetaData::UserMetaData::Comment;
142 }
143 if (roles.contains("originUrl")) {
144 attributes |= KFileMetaData::UserMetaData::OriginUrl;
145 }
146
147 if (attributes == KFileMetaData::UserMetaData::None) {
148 return values;
149 }
150
151 KFileMetaData::UserMetaData md(file.path());
152 attributes = md.queryAttributes(attributes);
153
154 if (attributes & KFileMetaData::UserMetaData::Tags) {
155 values.insert("tags", tagsFromValues(md.tags()));
156 }
157 if (attributes & KFileMetaData::UserMetaData::Rating) {
158 values.insert("rating", QString::number(md.rating()));
159 }
160 if (attributes & KFileMetaData::UserMetaData::Comment) {
161 values.insert("comment", md.userComment());
162 }
163 if (attributes & KFileMetaData::UserMetaData::OriginUrl) {
164 values.insert("originUrl", md.originUrl());
165 }
166
167 return values;
168 }
169
170 KBalooRolesProvider::KBalooRolesProvider()
171 {
172 // Display roles filled from Baloo property cache
173 for (const auto& role : propertyRoleMap()) {
174 m_roles.insert(role);
175 }
176 m_roles.insert("dimensions");
177
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"));
183 }
184