]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/knepomukrolesprovider.cpp
Allow showing Nepomuk metadata inside views
[dolphin.git] / src / kitemviews / 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_p.h"
21
22 #include <KDebug>
23 #include <KGlobal>
24
25 #include <Nepomuk/Resource>
26 #include <Nepomuk/Tag>
27 #include <Nepomuk/Types/Property>
28 #include <Nepomuk/Variant>
29
30 struct KNepomukRolesProviderSingleton
31 {
32 KNepomukRolesProvider instance;
33 };
34 K_GLOBAL_STATIC(KNepomukRolesProviderSingleton, s_nepomukRolesProvider)
35
36
37 KNepomukRolesProvider& KNepomukRolesProvider::instance()
38 {
39 return s_nepomukRolesProvider->instance;
40 }
41
42 KNepomukRolesProvider::~KNepomukRolesProvider()
43 {
44 }
45
46 bool KNepomukRolesProvider::isNepomukRole(const QByteArray& role) const
47 {
48 return m_roles.contains(role);
49 }
50
51 QHash<QByteArray, QVariant> KNepomukRolesProvider::roleValues(const QUrl& url,
52 const QSet<QByteArray>& roles) const
53 {
54 const Nepomuk::Resource resource(url);
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 {
96 values.insert(role, value.toString());
97 }
98 }
99
100 return values;
101 }
102
103 KNepomukRolesProvider::KNepomukRolesProvider() :
104 m_roles(),
105 m_roleForUri()
106 {
107 struct UriInfo
108 {
109 const char* const uri;
110 const char* const role;
111 };
112
113 // Mapping from the URIs to the KFileItemModel roles. Note that this must not be
114 // a 1:1 mapping: One role may contain several URI-values (e.g. the URIs for height and
115 // width of an image are mapped to the role "imageSize")
116 static const UriInfo uriInfoList[] = {
117 { "http://www.semanticdesktop.org/ontologies/2007/08/15/nao#numericRating", "rating" },
118 { "http://www.semanticdesktop.org/ontologies/2007/08/15/nao#hasTag", "tags" },
119 { "http://www.semanticdesktop.org/ontologies/2007/08/15/nao#description", "comment" },
120 { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#wordCount", "wordCount" },
121 { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#lineCount", "lineCount" },
122 { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#width", "imageSize" },
123 { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#height", "imageSize" },
124 { "http://www.semanticdesktop.org/ontologies/2007/05/10/nexif#orientation", "orientation", },
125 { "http://www.semanticdesktop.org/ontologies/2009/02/19/nmm#performer", "artist" },
126 { "http://www.semanticdesktop.org/ontologies/2009/02/19/nmm#musicAlbum", "album" },
127 { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#duration", "duration" },
128 { "http://www.semanticdesktop.org/ontologies/2009/02/19/nmm#trackNumber", "track" },
129 { "http://www.semanticdesktop.org/ontologies/2010/04/30/ndo#copiedFrom", "copiedFrom" }
130 };
131
132 for (unsigned int i = 0; i < sizeof(uriInfoList) / sizeof(UriInfo); ++i) {
133 m_roleForUri.insert(QUrl(uriInfoList[i].uri), uriInfoList[i].role);
134 m_roles.insert(uriInfoList[i].role);
135 }
136 }
137
138 QString KNepomukRolesProvider::tagsFromValues(const QStringList& values) const
139 {
140 QString tags;
141
142 for (int i = 0; i < values.count(); ++i) {
143 if (i > 0) {
144 tags.append(QLatin1String(", "));
145 }
146
147 const Nepomuk::Tag tag(values[i]);
148 tags += tag.genericLabel();
149 }
150
151 return tags;
152 }
153