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