2
* This file is part of the Nepomuk KDE project.
3
* Copyright (C) 2006-2009 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 Library General Public
7
* License as published by the Free Software Foundation; either
8
* version 2 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Library General Public License for more details.
15
* You should have received a copy of the GNU Library General Public License
16
* along with this library; see the file COPYING.LIB. If not, write to
17
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
* Boston, MA 02110-1301, USA.
21
#include "resourcefiltermodel.h"
22
#include "graphwrapper_p.h"
23
#include "resourcemanager.h"
25
#include <Soprano/Node>
26
#include <Soprano/Statement>
27
#include <Soprano/StatementIterator>
28
#include <Soprano/NodeIterator>
29
#include <Soprano/QueryResultIterator>
30
#include <Soprano/Client/DBusModel>
31
#include <Soprano/Client/DBusClient>
32
#include <Soprano/Client/LocalSocketClient>
33
#include <Soprano/Query/QueryLanguage>
34
#include <Soprano/Util/DummyModel>
35
#include <Soprano/Util/MutexModel>
36
#include <Soprano/Vocabulary/RDF>
37
#include <Soprano/Vocabulary/NRL>
38
#include <Soprano/Vocabulary/NAO>
40
#include <QtCore/QSet>
42
using namespace Soprano;
45
class Nepomuk::ResourceFilterModel::Private
48
GraphWrapper graphWrapper;
51
Nepomuk::ResourceFilterModel::ResourceFilterModel( ResourceManager* manager )
55
d->graphWrapper.setManager( manager );
59
Nepomuk::ResourceFilterModel::~ResourceFilterModel()
65
Soprano::Error::ErrorCode Nepomuk::ResourceFilterModel::addStatement( const Statement& statement )
67
Statement s( statement );
68
if( s.context().isEmpty() ) {
69
s.setContext( mainContext() );
71
return FilterModel::addStatement( s );
75
QUrl Nepomuk::ResourceFilterModel::mainContext()
77
return d->graphWrapper.currentGraph();
81
Soprano::Error::ErrorCode Nepomuk::ResourceFilterModel::updateModificationDate( const QUrl& resource, const QDateTime& date )
83
Error::ErrorCode c = removeAllStatements( resource, Soprano::Vocabulary::NAO::lastModified(), Soprano::Node() );
84
if ( c != Error::ErrorNone )
87
return addStatement( resource, Soprano::Vocabulary::NAO::lastModified(), LiteralValue( date ), mainContext() );
91
Soprano::Error::ErrorCode Nepomuk::ResourceFilterModel::updateProperty( const QUrl& resource, const QUrl& property, const Node& value )
93
if( !property.isValid() ) {
94
setError( "Cannot update invalid property", Error::ErrorInvalidArgument );
95
return Error::ErrorInvalidArgument;
98
if( !value.isValid() ) {
99
return removeProperty( resource, property );
102
StatementIterator it = listStatements( Statement( resource, property, Node() ) );
104
Statement s = it.current();
106
if ( s.object() == value ) {
107
// nothing to do. Yey!
108
return Error::ErrorNone;
111
removeStatement( s );
116
Error::ErrorCode c = addStatement( resource, property, value, mainContext() );
117
if ( c != Error::ErrorNone )
118
return updateModificationDate( resource );
124
Soprano::Error::ErrorCode Nepomuk::ResourceFilterModel::updateProperty( const QUrl& resource, const QUrl& property, const QList<Node>& values )
126
if( !property.isValid() ) {
127
setError( "Cannot update invalid property", Error::ErrorInvalidArgument );
128
return Error::ErrorInvalidArgument;
131
if( values.isEmpty() ) {
132
return removeProperty( resource, property );
135
QSet<Node> existingValuesSet = listStatements( Statement( resource, property, Node() ) ).iterateObjects().allNodes().toSet();
136
QSet<Node> valuesSet = values.toSet();
138
Error::ErrorCode c = Error::ErrorNone;
139
foreach( const Node &node, existingValuesSet - valuesSet ) {
140
if ( ( c = removeAllStatements( Statement( resource, property, node ) ) ) != Error::ErrorNone ) {
145
QSet<Node> newNodes = valuesSet- existingValuesSet;
146
if ( !newNodes.isEmpty() ) {
147
QUrl context = mainContext();
148
foreach( const Node &node, newNodes ) {
149
if ( ( c = addStatement( Statement( resource, property, node, context ) ) ) != Error::ErrorNone ) {
154
c = updateModificationDate( resource );
161
Soprano::Error::ErrorCode Nepomuk::ResourceFilterModel::removeProperty( const QUrl& resource, const QUrl& property )
163
if( !property.isValid() ) {
164
setError( "Cannot remove invalid property", Error::ErrorInvalidArgument );
165
return Error::ErrorInvalidArgument;
168
Soprano::Error::ErrorCode c = removeAllStatements( Statement( resource, property, Node() ) );
169
if ( c == Soprano::Error::ErrorNone )
170
return updateModificationDate( resource );
176
Soprano::Error::ErrorCode Nepomuk::ResourceFilterModel::ensureResource( const QUrl& resource, const QUrl& type )
178
if ( !containsAnyStatement( Statement( resource, Soprano::Vocabulary::RDF::type(), type ) ) ) {
179
Soprano::Error::ErrorCode c = addStatement( Statement( resource, Soprano::Vocabulary::RDF::type(), type, mainContext() ) );
180
if ( c == Soprano::Error::ErrorNone )
181
return updateModificationDate( resource );
187
return Error::ErrorNone;
191
#include "resourcefiltermodel.moc"