]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kbaloorolesprovider.cpp
GIT_SILENT Sync po/docbooks with svn
[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 <QSize>
16
17 namespace
18 {
19 QString tagsFromValues(const QStringList &values)
20 {
21 if (values.size() == 1) {
22 return values.at(0);
23 }
24
25 QStringList alphabeticalOrderTags = values;
26 QCollator coll;
27 coll.setNumericMode(true);
28 std::sort(alphabeticalOrderTags.begin(), alphabeticalOrderTags.end(), [&](const QString &s1, const QString &s2) {
29 return coll.compare(s1, s2) < 0;
30 });
31 return alphabeticalOrderTags.join(QLatin1String(", "));
32 }
33
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") }
59 };
60 return map;
61 }
62 }
63
64 struct KBalooRolesProviderSingleton {
65 KBalooRolesProvider instance;
66 };
67 Q_GLOBAL_STATIC(KBalooRolesProviderSingleton, s_balooRolesProvider)
68
69 KBalooRolesProvider &KBalooRolesProvider::instance()
70 {
71 return s_balooRolesProvider->instance;
72 }
73
74 KBalooRolesProvider::~KBalooRolesProvider()
75 {
76 }
77
78 QSet<QByteArray> KBalooRolesProvider::roles() const
79 {
80 return m_roles;
81 }
82
83 QHash<QByteArray, QVariant> KBalooRolesProvider::roleValues(const Baloo::File &file, const QSet<QByteArray> &roles) const
84 {
85 QHash<QByteArray, QVariant> values;
86
87 using entry = std::pair<const KFileMetaData::Property::Property &, const QVariant &>;
88
89 const auto &propMap = file.properties();
90 auto rangeBegin = propMap.constKeyValueBegin();
91
92 while (rangeBegin != propMap.constKeyValueEnd()) {
93 auto key = (*rangeBegin).first;
94
95 auto rangeEnd = std::find_if(rangeBegin, propMap.constKeyValueEnd(), [key](const entry &e) {
96 return e.first != key;
97 });
98
99 const QByteArray role = propertyRoleMap().value(key);
100 if (role.isEmpty() || !roles.contains(role)) {
101 rangeBegin = rangeEnd;
102 continue;
103 }
104
105 const KFileMetaData::PropertyInfo propertyInfo(key);
106 auto distance = std::distance(rangeBegin, rangeEnd);
107 if (distance > 1) {
108 QVariantList list;
109 list.reserve(static_cast<int>(distance));
110 std::for_each(rangeBegin, rangeEnd, [&list](const entry &s) {
111 list.append(s.second);
112 });
113 values.insert(role, propertyInfo.formatAsDisplayString(list));
114 } else {
115 if (propertyInfo.valueType() == QMetaType::Type::QDateTime) {
116 // Let dolphin format later Dates
117 values.insert(role, (*rangeBegin).second);
118 } else {
119 values.insert(role, propertyInfo.formatAsDisplayString((*rangeBegin).second));
120 }
121 }
122 rangeBegin = rangeEnd;
123 }
124
125 if (roles.contains("dimensions")) {
126 bool widthOk = false;
127 bool heightOk = false;
128
129 const int width = propMap.value(KFileMetaData::Property::Width).toInt(&widthOk);
130 const int height = propMap.value(KFileMetaData::Property::Height).toInt(&heightOk);
131
132 if (widthOk && heightOk && width >= 0 && height >= 0) {
133 values.insert("dimensions", QSize(width, height));
134 }
135 }
136
137 KFileMetaData::UserMetaData::Attributes attributes;
138 if (roles.contains("tags")) {
139 attributes |= KFileMetaData::UserMetaData::Tags;
140 }
141 if (roles.contains("rating")) {
142 attributes |= KFileMetaData::UserMetaData::Rating;
143 }
144 if (roles.contains("comment")) {
145 attributes |= KFileMetaData::UserMetaData::Comment;
146 }
147 if (roles.contains("originUrl")) {
148 attributes |= KFileMetaData::UserMetaData::OriginUrl;
149 }
150
151 if (attributes == KFileMetaData::UserMetaData::None) {
152 return values;
153 }
154
155 KFileMetaData::UserMetaData md(file.path());
156 attributes = md.queryAttributes(attributes);
157
158 if (attributes & KFileMetaData::UserMetaData::Tags) {
159 values.insert("tags", tagsFromValues(md.tags()));
160 }
161 if (attributes & KFileMetaData::UserMetaData::Rating) {
162 values.insert("rating", QString::number(md.rating()));
163 }
164 if (attributes & KFileMetaData::UserMetaData::Comment) {
165 values.insert("comment", md.userComment());
166 }
167 if (attributes & KFileMetaData::UserMetaData::OriginUrl) {
168 values.insert("originUrl", md.originUrl());
169 }
170
171 return values;
172 }
173
174 KBalooRolesProvider::KBalooRolesProvider()
175 {
176 // Display roles filled from Baloo property cache
177 for (const auto &role : propertyRoleMap()) {
178 m_roles.insert(role);
179 }
180 m_roles.insert("dimensions");
181
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"));
187 }