2
This file is part of the Nepomuk KDE project.
3
Copyright (C) 2010 Sebastian Trueg <trueg@kde.org>
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Lesser General Public
7
License as published by the Free Software Foundation; either
8
version 2.1 of the License, or (at your option) version 3, or any
9
later version accepted by the membership of KDE e.V. (or its
10
successor approved by the membership of KDE e.V.), which shall
11
act as a proxy defined in Section 6 of version 3 of the license.
13
This library is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
Lesser General Public License for more details.
18
You should have received a copy of the GNU Lesser General Public
19
License along with this library. If not, see <http://www.gnu.org/licenses/>.
22
#include "simpleresource.h"
24
#include <QtCore/QHashIterator>
25
#include <QtCore/QSharedData>
26
#include <QtCore/QVariant>
27
#include <QtCore/QDebug>
28
#include <QtCore/QDataStream>
30
#include <Soprano/Node>
31
#include <Soprano/LiteralValue>
32
#include <Soprano/Vocabulary/RDF>
40
// convert int to string (a...z,aa...az,ba....bz,...)
41
int idCnt = s_idCnt.fetchAndAddRelaxed(1);
44
const int rest = idCnt%26;
45
id.append('a' + rest);
50
const QUrl uri = QString(QLatin1String("_:") + id);
55
class Nepomuk::SimpleResource::Private : public QSharedData
59
PropertyHash m_properties;
62
Nepomuk::SimpleResource::SimpleResource(const QUrl& uri)
68
Nepomuk::SimpleResource::SimpleResource(const PropertyHash& properties)
72
setProperties(properties);
75
Nepomuk::SimpleResource::SimpleResource(const SimpleResource& other)
80
Nepomuk::SimpleResource::~SimpleResource()
84
Nepomuk::SimpleResource & Nepomuk::SimpleResource::operator=(const Nepomuk::SimpleResource &other)
90
QUrl Nepomuk::SimpleResource::uri() const
95
void Nepomuk::SimpleResource::setUri(const QUrl& uri)
98
d->m_uri = createBlankUri();
104
Soprano::Node convertIfBlankNode( const Soprano::Node & n ) {
105
if( n.isResource() && n.uri().toString().startsWith("_:") ) {
106
return Soprano::Node( n.uri().toString().mid(2) ); // "_:" take 2 characters
112
QList< Soprano::Statement > Nepomuk::SimpleResource::toStatementList() const
114
QList<Soprano::Statement> list;
115
QHashIterator<QUrl, QVariant> it( d->m_properties );
116
while( it.hasNext() ) {
119
Soprano::Node object;
120
if( it.value().type() == QVariant::Url )
121
object = it.value().toUrl();
123
object = Soprano::LiteralValue( it.value() );
125
list << Soprano::Statement( convertIfBlankNode( d->m_uri ),
127
convertIfBlankNode( object ) );
132
bool Nepomuk::SimpleResource::isValid() const
134
// We do not check if m_uri.isValid() as a blank uri of the form "_:daf" would be invalid
135
if(d->m_uri.isEmpty() || d->m_properties.isEmpty()) {
139
// properties cannot have empty values
140
PropertyHash::const_iterator end = d->m_properties.constEnd();
141
for(PropertyHash::const_iterator it = d->m_properties.constBegin(); it != end; ++it) {
142
if(!it.value().isValid()) {
150
bool Nepomuk::SimpleResource::operator==(const Nepomuk::SimpleResource &other) const
152
return d->m_uri == other.d->m_uri && d->m_properties == other.d->m_properties;
155
Nepomuk::PropertyHash Nepomuk::SimpleResource::properties() const
157
return d->m_properties;
160
bool Nepomuk::SimpleResource::contains(const QUrl &property) const
162
return d->m_properties.contains(property);
165
bool Nepomuk::SimpleResource::contains(const QUrl &property, const QVariant &value) const
167
return d->m_properties.contains(property, value);
170
bool Nepomuk::SimpleResource::containsNode(const QUrl &property, const Soprano::Node &node) const
173
return contains(property, node.literal().variant());
174
else if(node.isResource())
175
return contains(property, node.uri());
180
void Nepomuk::SimpleResource::setPropertyNode(const QUrl &property, const Soprano::Node &value)
182
d->m_properties.remove(property);
183
addPropertyNode(property, value);
186
void Nepomuk::SimpleResource::setProperty(const QUrl &property, const QVariant &value)
188
d->m_properties.remove(property);
189
addProperty(property, value);
192
void Nepomuk::SimpleResource::setProperty(const QUrl& property, const Nepomuk::SimpleResource& res)
194
setProperty(property, res.uri());
198
void Nepomuk::SimpleResource::setProperty(const QUrl &property, const QVariantList &values)
200
d->m_properties.remove(property);
201
foreach(const QVariant& v, values) {
202
addProperty(property, v);
206
void Nepomuk::SimpleResource::addProperty(const QUrl &property, const QVariant &value)
208
// QMultiHash even stores the same key/value pair multiple times!
209
if(!d->m_properties.contains(property, value))
210
d->m_properties.insertMulti(property, value);
213
void Nepomuk::SimpleResource::addProperty(const QUrl& property, const Nepomuk::SimpleResource& res)
215
addProperty(property, res.uri());
218
void Nepomuk::SimpleResource::addPropertyNode(const QUrl &property, const Soprano::Node &node)
220
if(node.isResource())
221
addProperty(property, QVariant(node.uri()));
222
else if(node.isLiteral())
223
addProperty(property, node.literal().variant());
227
void Nepomuk::SimpleResource::remove(const QUrl &property, const QVariant &value)
229
d->m_properties.remove(property, value);
232
void Nepomuk::SimpleResource::remove(const QUrl &property)
234
d->m_properties.remove(property);
237
void Nepomuk::SimpleResource::removeAll(const QUrl &property, const QVariant &value)
239
if(property.isEmpty()) {
240
if(value.isValid()) {
241
foreach(const QUrl& prop, d->m_properties.keys(value)) {
242
d->m_properties.remove(prop, value);
246
d->m_properties.clear();
249
else if(value.isValid()){
250
d->m_properties.remove(property, value);
253
d->m_properties.remove(property);
257
void Nepomuk::SimpleResource::addType(const QUrl &type)
259
addProperty(Soprano::Vocabulary::RDF::type(), type);
262
void Nepomuk::SimpleResource::setTypes(const QList<QUrl> &types)
265
foreach(const QUrl& type, types) {
268
setProperty(Soprano::Vocabulary::RDF::type(), values);
271
void Nepomuk::SimpleResource::setProperties(const Nepomuk::PropertyHash &properties)
273
d->m_properties = properties;
276
void Nepomuk::SimpleResource::clear()
278
d->m_properties.clear();
281
void Nepomuk::SimpleResource::addProperties(const Nepomuk::PropertyHash &properties)
283
d->m_properties += properties;
286
QVariantList Nepomuk::SimpleResource::property(const QUrl &property) const
288
return d->m_properties.values(property);
291
uint Nepomuk::qHash(const SimpleResource& res)
293
return qHash(res.uri());
296
QDebug Nepomuk::operator<<(QDebug dbg, const Nepomuk::SimpleResource& res)
298
return dbg << res.uri() << res.properties();
301
QDataStream & Nepomuk::operator<<(QDataStream & stream, const Nepomuk::SimpleResource& resource)
303
stream << resource.uri() << resource.properties();
307
QDataStream & Nepomuk::operator>>(QDataStream & stream, Nepomuk::SimpleResource& resource)
310
PropertyHash properties;
311
stream >> uri >> properties;
312
resource.setUri(uri);
313
resource.setProperties(properties);