]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/knepomukrolesprovider.cpp
Provide correct labels for Nepomuk resources
[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 <Nepomuk/Resource>
27 #include <Nepomuk/Tag>
28 #include <Nepomuk/Types/Property>
29 #include <Nepomuk/Variant>
30
31 struct KNepomukRolesProviderSingleton
32 {
33 KNepomukRolesProvider instance;
34 };
35 K_GLOBAL_STATIC(KNepomukRolesProviderSingleton, s_nepomukRolesProvider)
36
37
38 KNepomukRolesProvider& KNepomukRolesProvider::instance()
39 {
40 return s_nepomukRolesProvider->instance;
41 }
42
43 KNepomukRolesProvider::~KNepomukRolesProvider()
44 {
45 }
46
47 QSet<QByteArray> KNepomukRolesProvider::roles() const
48 {
49 return m_roles;
50 }
51
52 QHash<QByteArray, QVariant> KNepomukRolesProvider::roleValues(const Nepomuk::Resource& resource,
53 const QSet<QByteArray>& roles) const
54 {
55 if (!resource.isValid()) {
56 return QHash<QByteArray, QVariant>();
57 }
58
59 QHash<QByteArray, QVariant> values;
60
61 int width = -1;
62 int height = -1;
63
64 QHashIterator<QUrl, Nepomuk::Variant> it(resource.properties());
65 while (it.hasNext()) {
66 it.next();
67
68 const Nepomuk::Types::Property property = it.key();
69 const QByteArray role = m_roleForUri.value(property.uri());
70 if (role.isEmpty() || !roles.contains(role)) {
71 continue;
72 }
73
74 const Nepomuk::Variant value = it.value();
75
76 if (role == "imageSize") {
77 // Merge the two Nepomuk properties for width and height
78 // as one string into the "imageSize" role
79 const QString uri = property.uri().toString();
80 if (uri.endsWith("#width")) {
81 width = value.toInt();
82 } else if (uri.endsWith("#height")) {
83 height = value.toInt();
84 }
85
86 if (width >= 0 && height >= 0) {
87 const QString widthAndHeight = QString::number(width) +
88 QLatin1String(" x ") +
89 QString::number(height);
90 values.insert(role, widthAndHeight);
91 }
92 } else if (role == "tags") {
93 const QString tags = tagsFromValues(value.toStringList());
94 values.insert(role, tags);
95 } else if (role == "orientation") {
96 const QString orientation = orientationFromValue(value.toInt());
97 values.insert(role, orientation);
98 } else if (value.isResource()) {
99 const Nepomuk::Resource resource = value.toResource();
100 values.insert(role, resource.genericLabel());
101 } else {
102 values.insert(role, value.toString());
103 }
104 }
105
106 // Assure that empty values get replaced by "-"
107 foreach (const QByteArray& role, roles) {
108 if (m_roles.contains(role) && values.value(role).toString().isEmpty()) {
109 values.insert(role, QLatin1String("-"));
110 }
111 }
112
113 return values;
114 }
115
116 KNepomukRolesProvider::KNepomukRolesProvider() :
117 m_roles(),
118 m_roleForUri()
119 {
120 struct UriInfo
121 {
122 const char* const uri;
123 const char* const role;
124 };
125
126 // Mapping from the URIs to the KFileItemModel roles. Note that this must not be
127 // a 1:1 mapping: One role may contain several URI-values (e.g. the URIs for height and
128 // width of an image are mapped to the role "imageSize")
129 static const UriInfo uriInfoList[] = {
130 { "http://www.semanticdesktop.org/ontologies/2007/08/15/nao#numericRating", "rating" },
131 { "http://www.semanticdesktop.org/ontologies/2007/08/15/nao#hasTag", "tags" },
132 { "http://www.semanticdesktop.org/ontologies/2007/08/15/nao#description", "comment" },
133 { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#wordCount", "wordCount" },
134 { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#lineCount", "lineCount" },
135 { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#width", "imageSize" },
136 { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#height", "imageSize" },
137 { "http://www.semanticdesktop.org/ontologies/2007/05/10/nexif#orientation", "orientation", },
138 { "http://www.semanticdesktop.org/ontologies/2009/02/19/nmm#performer", "artist" },
139 { "http://www.semanticdesktop.org/ontologies/2009/02/19/nmm#musicAlbum", "album" },
140 { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#duration", "duration" },
141 { "http://www.semanticdesktop.org/ontologies/2009/02/19/nmm#trackNumber", "track" },
142 { "http://www.semanticdesktop.org/ontologies/2010/04/30/ndo#copiedFrom", "copiedFrom" }
143 };
144
145 for (unsigned int i = 0; i < sizeof(uriInfoList) / sizeof(UriInfo); ++i) {
146 m_roleForUri.insert(QUrl(uriInfoList[i].uri), uriInfoList[i].role);
147 m_roles.insert(uriInfoList[i].role);
148 }
149 }
150
151 QString KNepomukRolesProvider::tagsFromValues(const QStringList& values) const
152 {
153 QString tags;
154
155 for (int i = 0; i < values.count(); ++i) {
156 if (i > 0) {
157 tags.append(QLatin1String(", "));
158 }
159
160 const Nepomuk::Tag tag(values[i]);
161 tags += tag.genericLabel();
162 }
163
164 return tags;
165 }
166
167 QString KNepomukRolesProvider::orientationFromValue(int value) const
168 {
169 QString string;
170 switch (value) {
171 case 1: string = i18nc("@item:intable Image orientation", "Unchanged"); break;
172 case 2: string = i18nc("@item:intable Image orientation", "Horizontally flipped"); break;
173 case 3: string = i18nc("@item:intable image orientation", "180° rotated"); break;
174 case 4: string = i18nc("@item:intable image orientation", "Vertically flipped"); break;
175 case 5: string = i18nc("@item:intable image orientation", "Transposed"); break;
176 case 6: string = i18nc("@item:intable image orientation", "90° rotated"); break;
177 case 7: string = i18nc("@item:intable image orientation", "Transversed"); break;
178 case 8: string = i18nc("@item:intable image orientation", "270° rotated"); break;
179 default:
180 break;
181 }
182 return string;
183 }
184