2
This file is part of the Nepomuk KDE project.
3
Copyright (C) 2010 Vishesh Handa <handa.vish@gmail.com>
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/>.
23
#include "syncresource.h"
25
#include <Soprano/Node>
26
#include <Soprano/Graph>
27
#include <Soprano/Statement>
28
#include <Soprano/StatementIterator>
30
#include <Nepomuk/Vocabulary/NIE>
31
#include <Nepomuk/Vocabulary/NFO>
32
#include <Soprano/Vocabulary/RDF>
34
#include <QtCore/QSharedData>
36
class Nepomuk::Sync::SyncResource::Private : public QSharedData {
42
Nepomuk::Sync::SyncResource::SyncResource()
43
: d( new Nepomuk::Sync::SyncResource::Private )
47
Nepomuk::Sync::SyncResource::SyncResource(const KUrl& uri)
48
: d( new Nepomuk::Sync::SyncResource::Private )
53
Nepomuk::Sync::SyncResource::SyncResource(const Nepomuk::Sync::SyncResource& rhs)
54
: QMultiHash< KUrl, Soprano::Node >(rhs),
59
Nepomuk::Sync::SyncResource::~SyncResource()
63
Nepomuk::Sync::SyncResource& Nepomuk::Sync::SyncResource::operator=(const Nepomuk::Sync::SyncResource& rhs)
69
bool Nepomuk::Sync::SyncResource::operator==(const Nepomuk::Sync::SyncResource& res) const
71
return d->uri == res.d->uri &&
72
this->QHash<KUrl, Soprano::Node>::operator==( res );
75
QList< Soprano::Statement > Nepomuk::Sync::SyncResource::toStatementList() const
77
QList<Soprano::Statement> list;
78
const QList<KUrl> & keys = uniqueKeys();
79
foreach( const KUrl & key, keys ) {
80
Soprano::Statement st;
81
Soprano::Node sub = d->uri.url().startsWith("_:") ? Soprano::Node(d->uri.url().mid(2)) : d->uri;
83
st.setPredicate( Soprano::Node( key ) );
85
const QList<Soprano::Node>& objects = values( key );
86
foreach( const Soprano::Node & node, objects ) {
95
bool Nepomuk::Sync::SyncResource::isFolder() const
97
return values( Soprano::Vocabulary::RDF::type() ).contains( Soprano::Node( Nepomuk::Vocabulary::NFO::Folder() ) );
101
bool Nepomuk::Sync::SyncResource::isFileDataObject() const
103
return values( Soprano::Vocabulary::RDF::type() ).contains( Soprano::Node( Nepomuk::Vocabulary::NFO::FileDataObject() ) );
107
KUrl Nepomuk::Sync::SyncResource::nieUrl() const
109
const QHash<KUrl, Soprano::Node>::const_iterator it = constFind( Nepomuk::Vocabulary::NIE::url() );
110
if( it == constEnd() )
113
return it.value().uri();
117
void Nepomuk::Sync::SyncResource::setUri(const Soprano::Node& node)
119
if( node.isResource() ) {
122
else if( node.isBlank() ) {
123
d->uri = KUrl( node.toN3() );
127
KUrl Nepomuk::Sync::SyncResource::uri() const
132
QList< Soprano::Node > Nepomuk::Sync::SyncResource::property(const KUrl& url) const
137
void Nepomuk::Sync::SyncResource::removeObject(const KUrl& uri)
139
QMutableHashIterator<KUrl, Soprano::Node> iter( *this );
140
while( iter.hasNext() ) {
143
if( iter.value().isResource() && iter.value().uri() == uri )
149
// Blank nodes are stored as "_:identifier" in urls
150
QUrl getUri( const Soprano::Node & n ) {
152
return QUrl( n.toN3() );
158
Nepomuk::Sync::SyncResource Nepomuk::Sync::SyncResource::fromStatementList(const QList< Soprano::Statement >& list)
161
return SyncResource();
164
Soprano::Node subject = list.first().subject();
165
res.setUri( getUri(subject) );
167
foreach( const Soprano::Statement & st, list ) {
168
if( st.subject() != subject )
171
KUrl pred = st.predicate().uri();
172
Soprano::Node obj = st.object();
174
if( !res.contains( pred, obj ) )
175
res.insert( pred, obj );
186
Nepomuk::Sync::ResourceHash Nepomuk::Sync::ResourceHash::fromGraph(const Soprano::Graph& graph)
188
return fromStatementList( graph.listStatements().allStatements() );
192
Nepomuk::Sync::ResourceHash Nepomuk::Sync::ResourceHash::fromStatementList(const QList< Soprano::Statement >& allStatements)
195
// Convert into multi hash for easier look up
197
QMultiHash<KUrl, Soprano::Statement> stHash;
198
stHash.reserve( allStatements.size() );
199
foreach( const Soprano::Statement & st, allStatements ) {
200
KUrl uri = getUri( st.subject() );
201
stHash.insert( uri, st );
205
// Convert them into a better format --> SyncResource
207
const QList<KUrl> & uniqueUris = stHash.uniqueKeys();
209
ResourceHash resources;
210
resources.reserve( uniqueUris.size() );
212
foreach( const KUrl & resUri, uniqueUris ) {
213
SyncResource res = SyncResource::fromStatementList( stHash.values( resUri ) );
214
resources.insert( res.uri(), res );
221
QList< Soprano::Statement > Nepomuk::Sync::ResourceHash::toStatementList() const
223
QList<Soprano::Statement> stList;
224
Q_FOREACH( const KUrl& uri, uniqueKeys() ) {
225
const SyncResource & res = value( uri );
226
stList += res.toStatementList();
233
bool Nepomuk::Sync::SyncResource::isValid() const
235
return !d->uri.isEmpty() && !isEmpty();
238
uint Nepomuk::Sync::qHash(const Nepomuk::Sync::SyncResource& res)
240
// WARNING: Do not use the uri to generate the hash.
241
// StoreResources depends on the fact that the uri is not used while generating the hash
244
QHashIterator<KUrl, Soprano::Node> it( res );
245
while( it.hasNext() ) {
248
hash ^= qHash( it.key() ) & qHash( it.value() );