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>
33 struct KNepomukRolesProviderSingleton
35 KNepomukRolesProvider instance
;
37 K_GLOBAL_STATIC(KNepomukRolesProviderSingleton
, s_nepomukRolesProvider
)
40 KNepomukRolesProvider
& KNepomukRolesProvider::instance()
42 return s_nepomukRolesProvider
->instance
;
45 KNepomukRolesProvider::~KNepomukRolesProvider()
49 QSet
<QByteArray
> KNepomukRolesProvider::roles() const
54 QHash
<QByteArray
, QVariant
> KNepomukRolesProvider::roleValues(const Nepomuk::Resource
& resource
,
55 const QSet
<QByteArray
>& roles
) const
57 if (!resource
.isValid()) {
58 return QHash
<QByteArray
, QVariant
>();
61 QHash
<QByteArray
, QVariant
> values
;
66 QHashIterator
<QUrl
, Nepomuk::Variant
> it(resource
.properties());
67 while (it
.hasNext()) {
70 const Nepomuk::Types::Property property
= it
.key();
71 const QByteArray role
= m_roleForUri
.value(property
.uri());
72 if (role
.isEmpty() || !roles
.contains(role
)) {
76 const Nepomuk::Variant value
= it
.value();
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();
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
);
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());
107 values
.insert(role
, value
.toString());
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("-"));
121 KNepomukRolesProvider::KNepomukRolesProvider() :
127 const char* const uri
;
128 const char* const role
;
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" }
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
);
156 QString
KNepomukRolesProvider::tagsFromValues(const QStringList
& values
) const
160 for (int i
= 0; i
< values
.count(); ++i
) {
162 tags
.append(QLatin1String(", "));
165 const Nepomuk::Tag
tag(values
[i
]);
166 tags
+= tag
.genericLabel();
172 QString
KNepomukRolesProvider::orientationFromValue(int value
) const
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;
190 QString
KNepomukRolesProvider::durationFromValue(int value
) const
193 duration
= duration
.addMSecs(value
);
194 return duration
.toString("hh:mm:ss");