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.h"
26 #include <Nepomuk/Resource>
27 #include <Nepomuk/Tag>
28 #include <Nepomuk/Types/Property>
29 #include <Nepomuk/Variant>
31 struct KNepomukRolesProviderSingleton
33 KNepomukRolesProvider instance
;
35 K_GLOBAL_STATIC(KNepomukRolesProviderSingleton
, s_nepomukRolesProvider
)
38 KNepomukRolesProvider
& KNepomukRolesProvider::instance()
40 return s_nepomukRolesProvider
->instance
;
43 KNepomukRolesProvider::~KNepomukRolesProvider()
47 QSet
<QByteArray
> KNepomukRolesProvider::roles() const
52 QHash
<QByteArray
, QVariant
> KNepomukRolesProvider::roleValues(const Nepomuk::Resource
& resource
,
53 const QSet
<QByteArray
>& roles
) const
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
);
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());
102 values
.insert(role
, value
.toString());
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("-"));
116 KNepomukRolesProvider::KNepomukRolesProvider() :
122 const char* const uri
;
123 const char* const role
;
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" }
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
);
151 QString
KNepomukRolesProvider::tagsFromValues(const QStringList
& values
) const
155 for (int i
= 0; i
< values
.count(); ++i
) {
157 tags
.append(QLatin1String(", "));
160 const Nepomuk::Tag
tag(values
[i
]);
161 tags
+= tag
.genericLabel();
167 QString
KNepomukRolesProvider::orientationFromValue(int value
) const
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;