~ubuntu-branches/ubuntu/utopic/kde4libs/utopic

« back to all changes in this revision

Viewing changes to nepomuk/core/resourcefiltermodel.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Kolberg
  • Date: 2012-03-03 00:25:28 UTC
  • mfrom: (1.14.15)
  • Revision ID: package-import@ubuntu.com-20120303002528-chhwyfluldkicy5k
Tags: 4:4.8.1-0ubuntu1
* New upstream release
  - Update symbol files

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * This file is part of the Nepomuk KDE project.
3
 
 * Copyright (C) 2006-2009 Sebastian Trueg <trueg@kde.org>
4
 
 *
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.
9
 
 *
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.
14
 
 *
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.
19
 
 */
20
 
 
21
 
#include "resourcefiltermodel.h"
22
 
#include "graphwrapper_p.h"
23
 
#include "resourcemanager.h"
24
 
 
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>
39
 
 
40
 
#include <QtCore/QSet>
41
 
 
42
 
using namespace Soprano;
43
 
 
44
 
 
45
 
class Nepomuk::ResourceFilterModel::Private
46
 
{
47
 
public:
48
 
    GraphWrapper graphWrapper;
49
 
};
50
 
 
51
 
Nepomuk::ResourceFilterModel::ResourceFilterModel( ResourceManager* manager )
52
 
    : FilterModel( 0 ),
53
 
      d(new Private())
54
 
{
55
 
    d->graphWrapper.setManager( manager );
56
 
}
57
 
 
58
 
 
59
 
Nepomuk::ResourceFilterModel::~ResourceFilterModel()
60
 
{
61
 
    delete d;
62
 
}
63
 
 
64
 
 
65
 
Soprano::Error::ErrorCode Nepomuk::ResourceFilterModel::addStatement( const Statement& statement )
66
 
{
67
 
    Statement s( statement );
68
 
    if( s.context().isEmpty() ) {
69
 
        s.setContext( mainContext() );
70
 
    }
71
 
    return FilterModel::addStatement( s );
72
 
}
73
 
 
74
 
 
75
 
QUrl Nepomuk::ResourceFilterModel::mainContext()
76
 
{
77
 
    return d->graphWrapper.currentGraph();
78
 
}
79
 
 
80
 
 
81
 
Soprano::Error::ErrorCode Nepomuk::ResourceFilterModel::updateModificationDate( const QUrl& resource, const QDateTime& date )
82
 
{
83
 
    Error::ErrorCode c = removeAllStatements( resource, Soprano::Vocabulary::NAO::lastModified(), Soprano::Node() );
84
 
    if ( c != Error::ErrorNone )
85
 
        return c;
86
 
    else
87
 
        return addStatement( resource, Soprano::Vocabulary::NAO::lastModified(), LiteralValue( date ), mainContext() );
88
 
}
89
 
 
90
 
 
91
 
Soprano::Error::ErrorCode Nepomuk::ResourceFilterModel::updateProperty( const QUrl& resource, const QUrl& property, const Node& value )
92
 
{
93
 
    if( !property.isValid() ) {
94
 
        setError( "Cannot update invalid property", Error::ErrorInvalidArgument );
95
 
        return Error::ErrorInvalidArgument;
96
 
    }
97
 
 
98
 
    if( !value.isValid() ) {
99
 
        return removeProperty( resource, property );
100
 
    }
101
 
 
102
 
    StatementIterator it = listStatements( Statement( resource, property, Node() ) );
103
 
    if ( it.next() ) {
104
 
        Statement s = it.current();
105
 
        it.close();
106
 
        if ( s.object() == value ) {
107
 
            // nothing to do. Yey!
108
 
            return Error::ErrorNone;
109
 
        }
110
 
        else {
111
 
            removeStatement( s );
112
 
        }
113
 
    }
114
 
 
115
 
    // update property
116
 
    Error::ErrorCode c = addStatement( resource, property, value, mainContext() );
117
 
    if ( c != Error::ErrorNone )
118
 
        return updateModificationDate( resource );
119
 
 
120
 
    return c;
121
 
}
122
 
 
123
 
 
124
 
Soprano::Error::ErrorCode Nepomuk::ResourceFilterModel::updateProperty( const QUrl& resource, const QUrl& property, const QList<Node>& values )
125
 
{
126
 
    if( !property.isValid() ) {
127
 
        setError( "Cannot update invalid property", Error::ErrorInvalidArgument );
128
 
        return Error::ErrorInvalidArgument;
129
 
    }
130
 
 
131
 
    if( values.isEmpty() ) {
132
 
        return removeProperty( resource, property );
133
 
    }
134
 
 
135
 
    QSet<Node> existingValuesSet = listStatements( Statement( resource, property, Node() ) ).iterateObjects().allNodes().toSet();
136
 
    QSet<Node> valuesSet = values.toSet();
137
 
 
138
 
    Error::ErrorCode c = Error::ErrorNone;
139
 
    foreach( const Node &node, existingValuesSet - valuesSet ) {
140
 
        if ( ( c = removeAllStatements( Statement( resource, property, node ) ) ) != Error::ErrorNone ) {
141
 
            return c;
142
 
        }
143
 
    }
144
 
 
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 ) {
150
 
                return c;
151
 
            }
152
 
        }
153
 
 
154
 
        c = updateModificationDate( resource );
155
 
    }
156
 
 
157
 
    return c;
158
 
}
159
 
 
160
 
 
161
 
Soprano::Error::ErrorCode Nepomuk::ResourceFilterModel::removeProperty( const QUrl& resource, const QUrl& property )
162
 
{
163
 
    if( !property.isValid() ) {
164
 
        setError( "Cannot remove invalid property", Error::ErrorInvalidArgument );
165
 
        return Error::ErrorInvalidArgument;
166
 
    }
167
 
 
168
 
    Soprano::Error::ErrorCode c = removeAllStatements( Statement( resource, property, Node() ) );
169
 
    if ( c == Soprano::Error::ErrorNone )
170
 
        return updateModificationDate( resource );
171
 
    else
172
 
        return c;
173
 
}
174
 
 
175
 
 
176
 
Soprano::Error::ErrorCode Nepomuk::ResourceFilterModel::ensureResource( const QUrl& resource, const QUrl& type )
177
 
{
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 );
182
 
         else
183
 
             return c;
184
 
    }
185
 
    else {
186
 
        clearError();
187
 
        return Error::ErrorNone;
188
 
    }
189
 
}
190
 
 
191
 
#include "resourcefiltermodel.moc"