]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kbaloorolesprovider.cpp
[KBalooRolesProvider] Support properties of type QStringList
[dolphin.git] / src / kitemviews / private / kbaloorolesprovider.cpp
1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2013 by Vishesh Handa <me@vhanda.in> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "kbaloorolesprovider.h"
22
23 #include <Baloo/File>
24 #include <KFileMetaData/PropertyInfo>
25 #include <KFileMetaData/UserMetaData>
26 #include <KFormat>
27 #include <KLocalizedString>
28
29 #include <QCollator>
30 #include <QDebug>
31 #include <QTime>
32
33 struct KBalooRolesProviderSingleton
34 {
35 KBalooRolesProvider instance;
36 };
37 Q_GLOBAL_STATIC(KBalooRolesProviderSingleton, s_balooRolesProvider)
38
39
40 KBalooRolesProvider& KBalooRolesProvider::instance()
41 {
42 return s_balooRolesProvider->instance;
43 }
44
45 KBalooRolesProvider::~KBalooRolesProvider()
46 {
47 }
48
49 QSet<QByteArray> KBalooRolesProvider::roles() const
50 {
51 return m_roles;
52 }
53
54 QHash<QByteArray, QVariant> KBalooRolesProvider::roleValues(const Baloo::File& file,
55 const QSet<QByteArray>& roles) const
56 {
57 QHash<QByteArray, QVariant> values;
58
59 int width = -1;
60 int height = -1;
61
62 QMapIterator<KFileMetaData::Property::Property, QVariant> it(file.properties());
63 while (it.hasNext()) {
64 it.next();
65
66 const KFileMetaData::PropertyInfo pi(it.key());
67 const QString property = pi.name();
68 const QByteArray role = roleForProperty(property);
69 if (role.isEmpty() || !roles.contains(role)) {
70 continue;
71 }
72
73 const QVariant value = it.value();
74
75 if (role == "imageSize") {
76 // Merge the two properties for width and height
77 // as one string into the "imageSize" role
78 if (property == QLatin1String("width")) {
79 width = value.toInt();
80 }
81 else if (property == QLatin1String("height")) {
82 height = value.toInt();
83 }
84
85 if (width >= 0 && height >= 0) {
86 QString widthAndHeight = QString::number(width);
87 widthAndHeight += QLatin1String(" x ");
88 widthAndHeight += QString::number(height);
89 values.insert(role, widthAndHeight);
90 }
91 } else if (role == "orientation") {
92 const QString orientation = orientationFromValue(value.toInt());
93 values.insert(role, orientation);
94 } else if (role == "duration") {
95 const QString duration = durationFromValue(value.toInt());
96 values.insert(role, duration);
97 } else if (role == "bitrate") {
98 const QString bitrate = bitrateFromValue(value.toInt());
99 values.insert(role, bitrate);
100 } else if (pi.valueType() == QVariant::StringList) {
101 values.insert(role, value.toStringList().join(QStringLiteral(", ")));
102 } else {
103 values.insert(role, value.toString());
104 }
105 }
106
107 KFileMetaData::UserMetaData md(file.path());
108 if (roles.contains("tags")) {
109 values.insert("tags", tagsFromValues(md.tags()));
110 }
111 if (roles.contains("rating")) {
112 values.insert("rating", QString::number(md.rating()));
113 }
114 if (roles.contains("comment")) {
115 values.insert("comment", md.userComment());
116 }
117 if (roles.contains("originUrl")) {
118 values.insert("originUrl", md.originUrl());
119 }
120
121 return values;
122 }
123
124 QByteArray KBalooRolesProvider::roleForProperty(const QString& property) const
125 {
126 return m_roleForProperty.value(property);
127 }
128
129 KBalooRolesProvider::KBalooRolesProvider() :
130 m_roles(),
131 m_roleForProperty()
132 {
133 struct PropertyInfo
134 {
135 const char* const property;
136 const char* const role;
137 };
138
139 // Mapping from the URIs to the KFileItemModel roles. Note that this must not be
140 // a 1:1 mapping: One role may contain several URI-values (e.g. the URIs for height and
141 // width of an image are mapped to the role "imageSize")
142 static const PropertyInfo propertyInfoList[] = {
143 { "rating", "rating" },
144 { "tag", "tags" },
145 { "comment", "comment" },
146 { "title", "title" },
147 { "wordCount", "wordCount" },
148 { "lineCount", "lineCount" },
149 { "width", "imageSize" },
150 { "height", "imageSize" },
151 { "imageDateTime", "imageDateTime"},
152 { "nexif.orientation", "orientation", },
153 { "artist", "artist" },
154 { "genre", "genre" },
155 { "album", "album" },
156 { "duration", "duration" },
157 { "bitRate", "bitrate" },
158 { "releaseYear", "releaseYear" },
159 { "trackNumber", "track" },
160 { "originUrl", "originUrl" }
161 };
162
163 for (unsigned int i = 0; i < sizeof(propertyInfoList) / sizeof(PropertyInfo); ++i) {
164 m_roleForProperty.insert(propertyInfoList[i].property, propertyInfoList[i].role);
165 m_roles.insert(propertyInfoList[i].role);
166 }
167 }
168
169 QString KBalooRolesProvider::tagsFromValues(const QStringList& values) const
170 {
171 QStringList alphabeticalOrderTags = values;
172 QCollator coll;
173 coll.setNumericMode(true);
174 std::sort(alphabeticalOrderTags.begin(), alphabeticalOrderTags.end(), [&](const QString& s1, const QString& s2){ return coll.compare(s1, s2) < 0; });
175 return alphabeticalOrderTags.join(QStringLiteral(", "));
176 }
177
178 QString KBalooRolesProvider::orientationFromValue(int value) const
179 {
180 QString string;
181 switch (value) {
182 case 1: string = i18nc("@item:intable Image orientation", "Unchanged"); break;
183 case 2: string = i18nc("@item:intable Image orientation", "Horizontally flipped"); break;
184 case 3: string = i18nc("@item:intable image orientation", "180° rotated"); break;
185 case 4: string = i18nc("@item:intable image orientation", "Vertically flipped"); break;
186 case 5: string = i18nc("@item:intable image orientation", "Transposed"); break;
187 case 6: string = i18nc("@item:intable image orientation", "90° rotated"); break;
188 case 7: string = i18nc("@item:intable image orientation", "Transversed"); break;
189 case 8: string = i18nc("@item:intable image orientation", "270° rotated"); break;
190 default:
191 break;
192 }
193 return string;
194 }
195
196 QString KBalooRolesProvider::durationFromValue(int value) const
197 {
198 QTime duration(0, 0, 0);
199 duration = duration.addSecs(value);
200 return duration.toString(QStringLiteral("hh:mm:ss"));
201 }
202
203
204 QString KBalooRolesProvider::bitrateFromValue(int value) const
205 {
206 KFormat form;
207 QString bitrate = i18nc("@label bitrate (per second)", "%1/s", form.formatByteSize(value, 1, KFormat::MetricBinaryDialect));
208 return bitrate;
209 }
210