]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/knepomukrolesprovider.cpp
Inline renaming fixes
[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 #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 Nepomuk::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, Nepomuk::Variant> it(resource.properties());
67 while (it.hasNext()) {
68 it.next();
69
70 const Nepomuk::Types::Property property = it.key();
71 const QByteArray role = m_roleForUri.value(property.uri());
72 if (role.isEmpty() || !roles.contains(role)) {
73 continue;
74 }
75
76 const Nepomuk::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("#width")) {
83 width = value.toInt();
84 } else if (uri.endsWith("#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 Nepomuk::Resource resource = value.toResource();
105 values.insert(role, resource.genericLabel());
106 } else {
107 values.insert(role, value.toString());
108 }
109 }
110
111 // Assure that empty values get replaced by "-"
112 foreach (const QByteArray& role, roles) {
113 if (m_roles.contains(role) && values.value(role).toString().isEmpty()) {
114 values.insert(role, QLatin1String("-"));
115 }
116 }
117
118 return values;
119 }
120
121 KNepomukRolesProvider::KNepomukRolesProvider() :
122 m_roles(),
123 m_roleForUri()
124 {
125 struct UriInfo
126 {
127 const char* const uri;
128 const char* const role;
129 };
130
131 // Mapping from the URIs to the KFileItemModel roles. Note that this must not be
132 // a 1:1 mapping: One role may contain several URI-values (e.g. the URIs for height and
133 // width of an image are mapped to the role "imageSize")
134 static const UriInfo uriInfoList[] = {
135 { "http://www.semanticdesktop.org/ontologies/2007/08/15/nao#numericRating", "rating" },
136 { "http://www.semanticdesktop.org/ontologies/2007/08/15/nao#hasTag", "tags" },
137 { "http://www.semanticdesktop.org/ontologies/2007/08/15/nao#description", "comment" },
138 { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#wordCount", "wordCount" },
139 { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#lineCount", "lineCount" },
140 { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#width", "imageSize" },
141 { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#height", "imageSize" },
142 { "http://www.semanticdesktop.org/ontologies/2007/05/10/nexif#orientation", "orientation", },
143 { "http://www.semanticdesktop.org/ontologies/2009/02/19/nmm#performer", "artist" },
144 { "http://www.semanticdesktop.org/ontologies/2009/02/19/nmm#musicAlbum", "album" },
145 { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#duration", "duration" },
146 { "http://www.semanticdesktop.org/ontologies/2009/02/19/nmm#trackNumber", "track" },
147 { "http://www.semanticdesktop.org/ontologies/2010/04/30/ndo#copiedFrom", "copiedFrom" }
148 };
149
150 for (unsigned int i = 0; i < sizeof(uriInfoList) / sizeof(UriInfo); ++i) {
151 m_roleForUri.insert(QUrl(uriInfoList[i].uri), uriInfoList[i].role);
152 m_roles.insert(uriInfoList[i].role);
153 }
154 }
155
156 QString KNepomukRolesProvider::tagsFromValues(const QStringList& values) const
157 {
158 QString tags;
159
160 for (int i = 0; i < values.count(); ++i) {
161 if (i > 0) {
162 tags.append(QLatin1String(", "));
163 }
164
165 const Nepomuk::Tag tag(values[i]);
166 tags += tag.genericLabel();
167 }
168
169 return tags;
170 }
171
172 QString KNepomukRolesProvider::orientationFromValue(int value) const
173 {
174 QString string;
175 switch (value) {
176 case 1: string = i18nc("@item:intable Image orientation", "Unchanged"); break;
177 case 2: string = i18nc("@item:intable Image orientation", "Horizontally flipped"); break;
178 case 3: string = i18nc("@item:intable image orientation", "180° rotated"); break;
179 case 4: string = i18nc("@item:intable image orientation", "Vertically flipped"); break;
180 case 5: string = i18nc("@item:intable image orientation", "Transposed"); break;
181 case 6: string = i18nc("@item:intable image orientation", "90° rotated"); break;
182 case 7: string = i18nc("@item:intable image orientation", "Transversed"); break;
183 case 8: string = i18nc("@item:intable image orientation", "270° rotated"); break;
184 default:
185 break;
186 }
187 return string;
188 }
189
190 QString KNepomukRolesProvider::durationFromValue(int value) const
191 {
192 QTime duration;
193 duration = duration.addMSecs(value);
194 return duration.toString("hh:mm:ss");
195 }
196