1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
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. *
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. *
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 ***************************************************************************/
20 #include "knepomukrolesprovider_p.h"
25 #include <Nepomuk/Resource>
26 #include <Nepomuk/Tag>
27 #include <Nepomuk/Types/Property>
28 #include <Nepomuk/Variant>
30 struct KNepomukRolesProviderSingleton
32 KNepomukRolesProvider instance
;
34 K_GLOBAL_STATIC(KNepomukRolesProviderSingleton
, s_nepomukRolesProvider
)
37 KNepomukRolesProvider
& KNepomukRolesProvider::instance()
39 return s_nepomukRolesProvider
->instance
;
42 KNepomukRolesProvider::~KNepomukRolesProvider()
46 bool KNepomukRolesProvider::isNepomukRole(const QByteArray
& role
) const
48 return m_roles
.contains(role
);
51 QHash
<QByteArray
, QVariant
> KNepomukRolesProvider::roleValues(const QUrl
& url
,
52 const QSet
<QByteArray
>& roles
) const
54 const Nepomuk::Resource
resource(url
);
55 if (!resource
.isValid()) {
56 return QHash
<QByteArray
, QVariant
>();
59 QHash
<QByteArray
, QVariant
> values
;
64 QHashIterator
<QUrl
, Nepomuk::Variant
> it(resource
.properties());
65 while (it
.hasNext()) {
68 const Nepomuk::Types::Property property
= it
.key();
69 const QByteArray role
= m_roleForUri
.value(property
.uri());
70 if (role
.isEmpty() || !roles
.contains(role
)) {
74 const Nepomuk::Variant value
= it
.value();
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();
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
);
92 } else if (role
== "tags") {
93 const QString tags
= tagsFromValues(value
.toStringList());
94 values
.insert(role
, tags
);
96 values
.insert(role
, value
.toString());
103 KNepomukRolesProvider::KNepomukRolesProvider() :
109 const char* const uri
;
110 const char* const role
;
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" }
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
);
138 QString
KNepomukRolesProvider::tagsFromValues(const QStringList
& values
) const
142 for (int i
= 0; i
< values
.count(); ++i
) {
144 tags
.append(QLatin1String(", "));
147 const Nepomuk::Tag
tag(values
[i
]);
148 tags
+= tag
.genericLabel();