]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/knepomukrolesprovider.cpp
Merge remote-tracking branch 'origin/KDE/4.11' into KDE/4.12
[dolphin.git] / src / kitemviews / private / knepomukrolesprovider.cpp
1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "knepomukrolesprovider.h"
21
22 #include <KDebug>
23 #include <KGlobal>
24 #include <KLocale>
25
26 #include <Nepomuk2/Resource>
27 #include <Nepomuk2/Tag>
28 #include <Nepomuk2/Types/Property>
29 #include <Nepomuk2/Variant>
30
31 #include <QTime>
32
33 struct KNepomukRolesProviderSingleton
34 {
35 KNepomukRolesProvider instance;
36 };
37 K_GLOBAL_STATIC(KNepomukRolesProviderSingleton, s_nepomukRolesProvider)
38
39
40 KNepomukRolesProvider& KNepomukRolesProvider::instance()
41 {
42 return s_nepomukRolesProvider->instance;
43 }
44
45 KNepomukRolesProvider::~KNepomukRolesProvider()
46 {
47 }
48
49 QSet<QByteArray> KNepomukRolesProvider::roles() const
50 {
51 return m_roles;
52 }
53
54 QHash<QByteArray, QVariant> KNepomukRolesProvider::roleValues(const Nepomuk2::Resource& resource,
55 const QSet<QByteArray>& roles) const
56 {
57 if (!resource.isValid()) {
58 return QHash<QByteArray, QVariant>();
59 }
60
61 QHash<QByteArray, QVariant> values;
62
63 int width = -1;
64 int height = -1;
65
66 QHashIterator<QUrl, Nepomuk2::Variant> it(resource.properties());
67 while (it.hasNext()) {
68 it.next();
69
70 const Nepomuk2::Types::Property property = it.key();
71 const QByteArray role = roleForPropertyUri(property.uri());
72 if (role.isEmpty() || !roles.contains(role)) {
73 continue;
74 }
75
76 const Nepomuk2::Variant value = it.value();
77
78 if (role == "imageSize") {
79 // Merge the two Nepomuk properties for width and height
80 // as one string into the "imageSize" role
81 const QString uri = property.uri().toString();
82 if (uri.endsWith(QLatin1String("#width"))) {
83 width = value.toInt();
84 } else if (uri.endsWith(QLatin1String("#height"))) {
85 height = value.toInt();
86 }
87
88 if (width >= 0 && height >= 0) {
89 const QString widthAndHeight = QString::number(width) +
90 QLatin1String(" x ") +
91 QString::number(height);
92 values.insert(role, widthAndHeight);
93 }
94 } else if (role == "tags") {
95 const QString tags = tagsFromValues(value.toStringList());
96 values.insert(role, tags);
97 } else if (role == "orientation") {
98 const QString orientation = orientationFromValue(value.toInt());
99 values.insert(role, orientation);
100 } else if (role == "duration") {
101 const QString duration = durationFromValue(value.toInt());
102 values.insert(role, duration);
103 } else if (value.isResource()) {
104 const Nepomuk2::Resource resource = value.toResource();
105 values.insert(role, resource.genericLabel());
106 } else if (value.isResourceList()) {
107 const QList<Nepomuk2::Resource> resList = value.toResourceList();
108 QStringList strList;
109 foreach (const Nepomuk2::Resource& res, resList) {
110 strList << res.genericLabel();
111 }
112 values.insert(role, strList.join(QLatin1String(", ")));
113 } else {
114 values.insert(role, value.toString());
115 }
116 }
117
118 return values;
119 }
120
121 QByteArray KNepomukRolesProvider::roleForPropertyUri(const QUrl& uri) const
122 {
123 return m_roleForUri.value(uri);
124 }
125
126 KNepomukRolesProvider::KNepomukRolesProvider() :
127 m_roles(),
128 m_roleForUri()
129 {
130 struct UriInfo
131 {
132 const char* const uri;
133 const char* const role;
134 };
135
136 // Mapping from the URIs to the KFileItemModel roles. Note that this must not be
137 // a 1:1 mapping: One role may contain several URI-values (e.g. the URIs for height and
138 // width of an image are mapped to the role "imageSize")
139 static const UriInfo uriInfoList[] = {
140 { "http://www.semanticdesktop.org/ontologies/2007/08/15/nao#numericRating", "rating" },
141 { "http://www.semanticdesktop.org/ontologies/2007/08/15/nao#hasTag", "tags" },
142 { "http://www.semanticdesktop.org/ontologies/2007/08/15/nao#description", "comment" },
143 { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#wordCount", "wordCount" },
144 { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#lineCount", "lineCount" },
145 { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#width", "imageSize" },
146 { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#height", "imageSize" },
147 { "http://www.semanticdesktop.org/ontologies/2007/05/10/nexif#orientation", "orientation", },
148 { "http://www.semanticdesktop.org/ontologies/2009/02/19/nmm#performer", "artist" },
149 { "http://www.semanticdesktop.org/ontologies/2009/02/19/nmm#musicAlbum", "album" },
150 { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#duration", "duration" },
151 { "http://www.semanticdesktop.org/ontologies/2009/02/19/nmm#trackNumber", "track" },
152 { "http://www.semanticdesktop.org/ontologies/2010/04/30/ndo#copiedFrom", "copiedFrom" }
153 };
154
155 for (unsigned int i = 0; i < sizeof(uriInfoList) / sizeof(UriInfo); ++i) {
156 m_roleForUri.insert(QUrl(uriInfoList[i].uri), uriInfoList[i].role);
157 m_roles.insert(uriInfoList[i].role);
158 }
159 }
160
161 QString KNepomukRolesProvider::tagsFromValues(const QStringList& values) const
162 {
163 QString tags;
164
165 for (int i = 0; i < values.count(); ++i) {
166 if (i > 0) {
167 tags.append(QLatin1String(", "));
168 }
169
170 const Nepomuk2::Tag tag(values[i]);
171 tags += tag.genericLabel();
172 }
173
174 return tags;
175 }
176
177 QString KNepomukRolesProvider::orientationFromValue(int value) const
178 {
179 QString string;
180 switch (value) {
181 case 1: string = i18nc("@item:intable Image orientation", "Unchanged"); break;
182 case 2: string = i18nc("@item:intable Image orientation", "Horizontally flipped"); break;
183 case 3: string = i18nc("@item:intable image orientation", "180° rotated"); break;
184 case 4: string = i18nc("@item:intable image orientation", "Vertically flipped"); break;
185 case 5: string = i18nc("@item:intable image orientation", "Transposed"); break;
186 case 6: string = i18nc("@item:intable image orientation", "90° rotated"); break;
187 case 7: string = i18nc("@item:intable image orientation", "Transversed"); break;
188 case 8: string = i18nc("@item:intable image orientation", "270° rotated"); break;
189 default:
190 break;
191 }
192 return string;
193 }
194
195 QString KNepomukRolesProvider::durationFromValue(int value) const
196 {
197 QTime duration;
198 duration = duration.addSecs(value);
199 return duration.toString("hh:mm:ss");
200 }
201