~ubuntu-branches/ubuntu/quantal/kde-runtime/quantal

« back to all changes in this revision

Viewing changes to nepomuk/services/storage/lib/simpleresource.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2012-06-03 21:50:00 UTC
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: package-import@ubuntu.com-20120603215000-vn7oarsq0ynrydj5
Tags: upstream-4.8.80
Import upstream version 4.8.80

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) 2010 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 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.
12
 
 
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.
17
 
 
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/>.
20
 
*/
21
 
 
22
 
#include "simpleresource.h"
23
 
 
24
 
#include <QtCore/QHashIterator>
25
 
#include <QtCore/QSharedData>
26
 
#include <QtCore/QVariant>
27
 
#include <QtCore/QDebug>
28
 
#include <QtCore/QDataStream>
29
 
 
30
 
#include <Soprano/Node>
31
 
#include <Soprano/LiteralValue>
32
 
#include <Soprano/Vocabulary/RDF>
33
 
 
34
 
 
35
 
namespace {
36
 
QAtomicInt s_idCnt;
37
 
 
38
 
QUrl createBlankUri()
39
 
{
40
 
    // convert int to string (a...z,aa...az,ba....bz,...)
41
 
    int idCnt = s_idCnt.fetchAndAddRelaxed(1);
42
 
    QByteArray id;
43
 
    do {
44
 
        const int rest = idCnt%26;
45
 
        id.append('a' + rest);
46
 
        idCnt -= rest;
47
 
        idCnt /= 26;
48
 
    } while(idCnt > 0);
49
 
 
50
 
    const QUrl uri = QString(QLatin1String("_:") + id);
51
 
    return uri;
52
 
}
53
 
}
54
 
 
55
 
class Nepomuk::SimpleResource::Private : public QSharedData
56
 
{
57
 
public:
58
 
    QUrl m_uri;
59
 
    PropertyHash m_properties;
60
 
};
61
 
 
62
 
Nepomuk::SimpleResource::SimpleResource(const QUrl& uri)
63
 
{
64
 
    d = new Private();
65
 
    setUri(uri);
66
 
}
67
 
 
68
 
Nepomuk::SimpleResource::SimpleResource(const PropertyHash& properties)
69
 
{
70
 
    d = new Private();
71
 
    setUri(QUrl());
72
 
    setProperties(properties);
73
 
}
74
 
 
75
 
Nepomuk::SimpleResource::SimpleResource(const SimpleResource& other)
76
 
    : d(other.d)
77
 
{
78
 
}
79
 
 
80
 
Nepomuk::SimpleResource::~SimpleResource()
81
 
{
82
 
}
83
 
 
84
 
Nepomuk::SimpleResource & Nepomuk::SimpleResource::operator=(const Nepomuk::SimpleResource &other)
85
 
{
86
 
    d = other.d;
87
 
    return *this;
88
 
}
89
 
 
90
 
QUrl Nepomuk::SimpleResource::uri() const
91
 
{
92
 
    return d->m_uri;
93
 
}
94
 
 
95
 
void Nepomuk::SimpleResource::setUri(const QUrl& uri)
96
 
{
97
 
    if(uri.isEmpty())
98
 
        d->m_uri = createBlankUri();
99
 
    else
100
 
        d->m_uri = uri;
101
 
}
102
 
 
103
 
namespace {
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
107
 
        }
108
 
        return n;
109
 
    }
110
 
}
111
 
 
112
 
QList< Soprano::Statement > Nepomuk::SimpleResource::toStatementList() const
113
 
{
114
 
    QList<Soprano::Statement> list;
115
 
    QHashIterator<QUrl, QVariant> it( d->m_properties );
116
 
    while( it.hasNext() ) {
117
 
        it.next();
118
 
 
119
 
        Soprano::Node object;
120
 
        if( it.value().type() == QVariant::Url )
121
 
            object = it.value().toUrl();
122
 
        else
123
 
            object = Soprano::LiteralValue( it.value() );
124
 
 
125
 
        list << Soprano::Statement( convertIfBlankNode( d->m_uri ),
126
 
                                    it.key(),
127
 
                                    convertIfBlankNode( object ) );
128
 
    }
129
 
    return list;
130
 
}
131
 
 
132
 
bool Nepomuk::SimpleResource::isValid() const
133
 
{
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()) {
136
 
        return false;
137
 
    }
138
 
 
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()) {
143
 
            return false;
144
 
        }
145
 
    }
146
 
 
147
 
    return true;
148
 
}
149
 
 
150
 
bool Nepomuk::SimpleResource::operator==(const Nepomuk::SimpleResource &other) const
151
 
{
152
 
    return d->m_uri == other.d->m_uri && d->m_properties == other.d->m_properties;
153
 
}
154
 
 
155
 
Nepomuk::PropertyHash Nepomuk::SimpleResource::properties() const
156
 
{
157
 
    return d->m_properties;
158
 
}
159
 
 
160
 
bool Nepomuk::SimpleResource::contains(const QUrl &property) const
161
 
{
162
 
    return d->m_properties.contains(property);
163
 
}
164
 
 
165
 
bool Nepomuk::SimpleResource::contains(const QUrl &property, const QVariant &value) const
166
 
{
167
 
    return d->m_properties.contains(property, value);
168
 
}
169
 
 
170
 
bool Nepomuk::SimpleResource::containsNode(const QUrl &property, const Soprano::Node &node) const
171
 
{
172
 
    if(node.isLiteral())
173
 
        return contains(property, node.literal().variant());
174
 
    else if(node.isResource())
175
 
        return contains(property, node.uri());
176
 
    else
177
 
        return false;
178
 
}
179
 
 
180
 
void Nepomuk::SimpleResource::setPropertyNode(const QUrl &property, const Soprano::Node &value)
181
 
{
182
 
    d->m_properties.remove(property);
183
 
    addPropertyNode(property, value);
184
 
}
185
 
 
186
 
void Nepomuk::SimpleResource::setProperty(const QUrl &property, const QVariant &value)
187
 
{
188
 
    d->m_properties.remove(property);
189
 
    addProperty(property, value);
190
 
}
191
 
 
192
 
void Nepomuk::SimpleResource::setProperty(const QUrl& property, const Nepomuk::SimpleResource& res)
193
 
{
194
 
    setProperty(property, res.uri());
195
 
}
196
 
 
197
 
 
198
 
void Nepomuk::SimpleResource::setProperty(const QUrl &property, const QVariantList &values)
199
 
{
200
 
    d->m_properties.remove(property);
201
 
    foreach(const QVariant& v, values) {
202
 
        addProperty(property, v);
203
 
    }
204
 
}
205
 
 
206
 
void Nepomuk::SimpleResource::addProperty(const QUrl &property, const QVariant &value)
207
 
{
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);
211
 
}
212
 
 
213
 
void Nepomuk::SimpleResource::addProperty(const QUrl& property, const Nepomuk::SimpleResource& res)
214
 
{
215
 
    addProperty(property, res.uri());
216
 
}
217
 
 
218
 
void Nepomuk::SimpleResource::addPropertyNode(const QUrl &property, const Soprano::Node &node)
219
 
{
220
 
    if(node.isResource())
221
 
        addProperty(property, QVariant(node.uri()));
222
 
    else if(node.isLiteral())
223
 
        addProperty(property, node.literal().variant());
224
 
    // else do nothing
225
 
}
226
 
 
227
 
void Nepomuk::SimpleResource::remove(const QUrl &property, const QVariant &value)
228
 
{
229
 
    d->m_properties.remove(property, value);
230
 
}
231
 
 
232
 
void Nepomuk::SimpleResource::remove(const QUrl &property)
233
 
{
234
 
    d->m_properties.remove(property);
235
 
}
236
 
 
237
 
void Nepomuk::SimpleResource::removeAll(const QUrl &property, const QVariant &value)
238
 
{
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);
243
 
            }
244
 
        }
245
 
        else {
246
 
            d->m_properties.clear();
247
 
        }
248
 
    }
249
 
    else if(value.isValid()){
250
 
        d->m_properties.remove(property, value);
251
 
    }
252
 
    else {
253
 
        d->m_properties.remove(property);
254
 
    }
255
 
}
256
 
 
257
 
void Nepomuk::SimpleResource::addType(const QUrl &type)
258
 
{
259
 
    addProperty(Soprano::Vocabulary::RDF::type(), type);
260
 
}
261
 
 
262
 
void Nepomuk::SimpleResource::setTypes(const QList<QUrl> &types)
263
 
{
264
 
    QVariantList values;
265
 
    foreach(const QUrl& type, types) {
266
 
        values << type;
267
 
    }
268
 
    setProperty(Soprano::Vocabulary::RDF::type(), values);
269
 
}
270
 
 
271
 
void Nepomuk::SimpleResource::setProperties(const Nepomuk::PropertyHash &properties)
272
 
{
273
 
    d->m_properties = properties;
274
 
}
275
 
 
276
 
void Nepomuk::SimpleResource::clear()
277
 
{
278
 
    d->m_properties.clear();
279
 
}
280
 
 
281
 
void Nepomuk::SimpleResource::addProperties(const Nepomuk::PropertyHash &properties)
282
 
{
283
 
    d->m_properties += properties;
284
 
}
285
 
 
286
 
QVariantList Nepomuk::SimpleResource::property(const QUrl &property) const
287
 
{
288
 
    return d->m_properties.values(property);
289
 
}
290
 
 
291
 
uint Nepomuk::qHash(const SimpleResource& res)
292
 
{
293
 
    return qHash(res.uri());
294
 
}
295
 
 
296
 
QDebug Nepomuk::operator<<(QDebug dbg, const Nepomuk::SimpleResource& res)
297
 
{
298
 
    return dbg << res.uri() << res.properties();
299
 
}
300
 
 
301
 
QDataStream & Nepomuk::operator<<(QDataStream & stream, const Nepomuk::SimpleResource& resource)
302
 
{
303
 
    stream << resource.uri() << resource.properties();
304
 
    return stream;
305
 
}
306
 
 
307
 
QDataStream & Nepomuk::operator>>(QDataStream & stream, Nepomuk::SimpleResource& resource)
308
 
{
309
 
    QUrl uri;
310
 
    PropertyHash properties;
311
 
    stream >> uri >> properties;
312
 
    resource.setUri(uri);
313
 
    resource.setProperties(properties);
314
 
    return stream;
315
 
}