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 <Nepomuk2/Resource>
27 #include <Nepomuk2/Tag>
28 #include <Nepomuk2/Types/Property>
29 #include <Nepomuk2/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 Nepomuk2::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
, Nepomuk2::Variant
> it(resource
.properties());
67 while (it
.hasNext()) {
70 const Nepomuk2::Types::Property property
= it
.key();
71 const QByteArray role
= m_roleForUri
.value(property
.uri());
72 if (role
.isEmpty() || !roles
.contains(role
)) {
76 const Nepomuk2::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(QLatin1String("#width"))) {
83 width
= value
.toInt();
84 } else if (uri
.endsWith(QLatin1String("#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 Nepomuk2::Resource resource
= value
.toResource();
105 values
.insert(role
, resource
.genericLabel());
107 values
.insert(role
, value
.toString());
114 KNepomukRolesProvider::KNepomukRolesProvider() :
120 const char* const uri
;
121 const char* const role
;
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" }
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
);
149 QString
KNepomukRolesProvider::tagsFromValues(const QStringList
& values
) const
153 for (int i
= 0; i
< values
.count(); ++i
) {
155 tags
.append(QLatin1String(", "));
158 const Nepomuk2::Tag
tag(values
[i
]);
159 tags
+= tag
.genericLabel();
165 QString
KNepomukRolesProvider::orientationFromValue(int value
) const
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;
183 QString
KNepomukRolesProvider::durationFromValue(int value
) const
186 duration
= duration
.addMSecs(value
);
187 return duration
.toString("hh:mm:ss");