]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kbaloorolesprovider.cpp
Sort the tag-values alphabetically in the "Tags" column
[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 <QDebug>
24 #include <KLocalizedString>
25
26 #include <Baloo/File>
27 #include <KFileMetaData/PropertyInfo>
28 #include <KFileMetaData/UserMetaData>
29
30 #include <QTime>
31 #include <QMap>
32 #include <QCollator>
33
34 struct KBalooRolesProviderSingleton
35 {
36 KBalooRolesProvider instance;
37 };
38 Q_GLOBAL_STATIC(KBalooRolesProviderSingleton, s_balooRolesProvider)
39
40
41 KBalooRolesProvider& KBalooRolesProvider::instance()
42 {
43 return s_balooRolesProvider->instance;
44 }
45
46 KBalooRolesProvider::~KBalooRolesProvider()
47 {
48 }
49
50 QSet<QByteArray> KBalooRolesProvider::roles() const
51 {
52 return m_roles;
53 }
54
55 QHash<QByteArray, QVariant> KBalooRolesProvider::roleValues(const Baloo::File& file,
56 const QSet<QByteArray>& roles) const
57 {
58 QHash<QByteArray, QVariant> values;
59
60 int width = -1;
61 int height = -1;
62
63 QMapIterator<KFileMetaData::Property::Property, QVariant> it(file.properties());
64 while (it.hasNext()) {
65 it.next();
66
67 const KFileMetaData::PropertyInfo pi(it.key());
68 const QString property = pi.name();
69 const QByteArray role = roleForProperty(property);
70 if (role.isEmpty() || !roles.contains(role)) {
71 continue;
72 }
73
74 const QVariant value = it.value();
75
76 if (role == "imageSize") {
77 // Merge the two properties for width and height
78 // as one string into the "imageSize" role
79 if (property == QLatin1String("width")) {
80 width = value.toInt();
81 }
82 else if (property == QLatin1String("height")) {
83 height = value.toInt();
84 }
85
86 if (width >= 0 && height >= 0) {
87 QString widthAndHeight = QString::number(width);
88 widthAndHeight += QLatin1String(" x ");
89 widthAndHeight += QString::number(height);
90 values.insert(role, widthAndHeight);
91 }
92 } else if (role == "orientation") {
93 const QString orientation = orientationFromValue(value.toInt());
94 values.insert(role, orientation);
95 } else if (role == "duration") {
96 const QString duration = durationFromValue(value.toInt());
97 values.insert(role, duration);
98 } else {
99 values.insert(role, value.toString());
100 }
101 }
102
103 KFileMetaData::UserMetaData md(file.path());
104 if (roles.contains("tags")) {
105 values.insert("tags", tagsFromValues(md.tags()));
106 }
107 if (roles.contains("rating")) {
108 values.insert("rating", QString::number(md.rating()));
109 }
110 if (roles.contains("comment")) {
111 values.insert("comment", md.userComment());
112 }
113 if (roles.contains("originUrl")) {
114 values.insert("originUrl", md.originUrl());
115 }
116
117 return values;
118 }
119
120 QByteArray KBalooRolesProvider::roleForProperty(const QString& property) const
121 {
122 return m_roleForProperty.value(property);
123 }
124
125 KBalooRolesProvider::KBalooRolesProvider() :
126 m_roles(),
127 m_roleForProperty()
128 {
129 struct PropertyInfo
130 {
131 const char* const property;
132 const char* const role;
133 };
134
135 // Mapping from the URIs to the KFileItemModel roles. Note that this must not be
136 // a 1:1 mapping: One role may contain several URI-values (e.g. the URIs for height and
137 // width of an image are mapped to the role "imageSize")
138 static const PropertyInfo propertyInfoList[] = {
139 { "rating", "rating" },
140 { "tag", "tags" },
141 { "comment", "comment" },
142 { "title", "title" },
143 { "wordCount", "wordCount" },
144 { "lineCount", "lineCount" },
145 { "width", "imageSize" },
146 { "height", "imageSize" },
147 { "nexif.orientation", "orientation", },
148 { "artist", "artist" },
149 { "album", "album" },
150 { "duration", "duration" },
151 { "trackNumber", "track" },
152 { "originUrl", "originUrl" }
153 };
154
155 for (unsigned int i = 0; i < sizeof(propertyInfoList) / sizeof(PropertyInfo); ++i) {
156 m_roleForProperty.insert(propertyInfoList[i].property, propertyInfoList[i].role);
157 m_roles.insert(propertyInfoList[i].role);
158 }
159 }
160
161 QString KBalooRolesProvider::tagsFromValues(const QStringList& values) const
162 {
163 QStringList alphabeticalOrderTags = values;
164 QCollator coll;
165 coll.setNumericMode(true);
166 std::sort(alphabeticalOrderTags.begin(), alphabeticalOrderTags.end(), [&](const QString& s1, const QString& s2){ return coll.compare(s1, s2) < 0; });
167 return alphabeticalOrderTags.join(QStringLiteral(", "));
168 }
169
170 QString KBalooRolesProvider::orientationFromValue(int value) const
171 {
172 QString string;
173 switch (value) {
174 case 1: string = i18nc("@item:intable Image orientation", "Unchanged"); break;
175 case 2: string = i18nc("@item:intable Image orientation", "Horizontally flipped"); break;
176 case 3: string = i18nc("@item:intable image orientation", "180° rotated"); break;
177 case 4: string = i18nc("@item:intable image orientation", "Vertically flipped"); break;
178 case 5: string = i18nc("@item:intable image orientation", "Transposed"); break;
179 case 6: string = i18nc("@item:intable image orientation", "90° rotated"); break;
180 case 7: string = i18nc("@item:intable image orientation", "Transversed"); break;
181 case 8: string = i18nc("@item:intable image orientation", "270° rotated"); break;
182 default:
183 break;
184 }
185 return string;
186 }
187
188 QString KBalooRolesProvider::durationFromValue(int value) const
189 {
190 QTime duration(0, 0, 0);
191 duration = duration.addSecs(value);
192 return duration.toString(QStringLiteral("hh:mm:ss"));
193 }
194