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

« back to all changes in this revision

Viewing changes to nepomuk/services/backupsync/lib/syncresource.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  Vishesh Handa <handa.vish@gmail.com>
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
 
 
23
 
#include "syncresource.h"
24
 
 
25
 
#include <Soprano/Node>
26
 
#include <Soprano/Graph>
27
 
#include <Soprano/Statement>
28
 
#include <Soprano/StatementIterator>
29
 
 
30
 
#include <Nepomuk/Vocabulary/NIE>
31
 
#include <Nepomuk/Vocabulary/NFO>
32
 
#include <Soprano/Vocabulary/RDF>
33
 
 
34
 
#include <QtCore/QSharedData>
35
 
 
36
 
class Nepomuk::Sync::SyncResource::Private : public QSharedData {
37
 
public:
38
 
    KUrl uri;
39
 
};
40
 
 
41
 
 
42
 
Nepomuk::Sync::SyncResource::SyncResource()
43
 
    : d( new Nepomuk::Sync::SyncResource::Private )
44
 
{
45
 
}
46
 
 
47
 
Nepomuk::Sync::SyncResource::SyncResource(const KUrl& uri)
48
 
    : d( new Nepomuk::Sync::SyncResource::Private )
49
 
{
50
 
    setUri( uri );
51
 
}
52
 
 
53
 
Nepomuk::Sync::SyncResource::SyncResource(const Nepomuk::Sync::SyncResource& rhs)
54
 
    : QMultiHash< KUrl, Soprano::Node >(rhs),
55
 
      d( rhs.d )
56
 
{
57
 
}
58
 
 
59
 
Nepomuk::Sync::SyncResource::~SyncResource()
60
 
{
61
 
}
62
 
 
63
 
Nepomuk::Sync::SyncResource& Nepomuk::Sync::SyncResource::operator=(const Nepomuk::Sync::SyncResource& rhs)
64
 
{
65
 
    d = rhs.d;
66
 
    return *this;
67
 
}
68
 
 
69
 
bool Nepomuk::Sync::SyncResource::operator==(const Nepomuk::Sync::SyncResource& res) const
70
 
{
71
 
    return d->uri == res.d->uri &&
72
 
    this->QHash<KUrl, Soprano::Node>::operator==( res );
73
 
}
74
 
 
75
 
QList< Soprano::Statement > Nepomuk::Sync::SyncResource::toStatementList() const
76
 
{
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;
82
 
        st.setSubject( sub );
83
 
        st.setPredicate( Soprano::Node( key ) );
84
 
 
85
 
        const QList<Soprano::Node>& objects = values( key );
86
 
        foreach( const Soprano::Node & node, objects ) {
87
 
            st.setObject( node );
88
 
            list.append( st );
89
 
        }
90
 
    }
91
 
    return list;
92
 
}
93
 
 
94
 
 
95
 
bool Nepomuk::Sync::SyncResource::isFolder() const
96
 
{
97
 
    return values( Soprano::Vocabulary::RDF::type() ).contains( Soprano::Node( Nepomuk::Vocabulary::NFO::Folder() ) );
98
 
}
99
 
 
100
 
 
101
 
bool Nepomuk::Sync::SyncResource::isFileDataObject() const
102
 
{
103
 
    return values( Soprano::Vocabulary::RDF::type() ).contains( Soprano::Node( Nepomuk::Vocabulary::NFO::FileDataObject() ) );
104
 
}
105
 
 
106
 
 
107
 
KUrl Nepomuk::Sync::SyncResource::nieUrl() const
108
 
{
109
 
    const QHash<KUrl, Soprano::Node>::const_iterator it = constFind( Nepomuk::Vocabulary::NIE::url() );
110
 
    if( it == constEnd() )
111
 
        return KUrl();
112
 
    else
113
 
        return it.value().uri();
114
 
}
115
 
 
116
 
 
117
 
void Nepomuk::Sync::SyncResource::setUri(const Soprano::Node& node)
118
 
{
119
 
    if( node.isResource() ) {
120
 
        d->uri = node.uri();
121
 
    }
122
 
    else if( node.isBlank() ) {
123
 
        d->uri = KUrl( node.toN3() );
124
 
    }
125
 
}
126
 
 
127
 
KUrl Nepomuk::Sync::SyncResource::uri() const
128
 
{
129
 
    return d->uri;
130
 
}
131
 
 
132
 
QList< Soprano::Node > Nepomuk::Sync::SyncResource::property(const KUrl& url) const
133
 
{
134
 
    return values(url);
135
 
}
136
 
 
137
 
void Nepomuk::Sync::SyncResource::removeObject(const KUrl& uri)
138
 
{
139
 
    QMutableHashIterator<KUrl, Soprano::Node> iter( *this );
140
 
    while( iter.hasNext() ) {
141
 
        iter.next();
142
 
 
143
 
        if( iter.value().isResource() && iter.value().uri() == uri )
144
 
            iter.remove();
145
 
    }
146
 
}
147
 
 
148
 
namespace {
149
 
    // Blank nodes are stored as "_:identifier" in urls
150
 
    QUrl getUri( const Soprano::Node & n ) {
151
 
        if( n.isBlank() )
152
 
            return QUrl( n.toN3() );
153
 
        else
154
 
            return n.uri();
155
 
    }
156
 
}
157
 
// static
158
 
Nepomuk::Sync::SyncResource Nepomuk::Sync::SyncResource::fromStatementList(const QList< Soprano::Statement >& list)
159
 
{
160
 
    if( list.isEmpty() )
161
 
        return SyncResource();
162
 
 
163
 
    SyncResource res;
164
 
    Soprano::Node subject = list.first().subject();
165
 
    res.setUri( getUri(subject) );
166
 
 
167
 
    foreach( const Soprano::Statement & st, list ) {
168
 
        if( st.subject() != subject )
169
 
            continue;
170
 
 
171
 
        KUrl pred = st.predicate().uri();
172
 
        Soprano::Node obj = st.object();
173
 
 
174
 
        if( !res.contains( pred, obj ) )
175
 
            res.insert( pred, obj );
176
 
    }
177
 
 
178
 
    return res;
179
 
}
180
 
 
181
 
//
182
 
// ResourceHash
183
 
//
184
 
 
185
 
// static
186
 
Nepomuk::Sync::ResourceHash Nepomuk::Sync::ResourceHash::fromGraph(const Soprano::Graph& graph)
187
 
{
188
 
    return fromStatementList( graph.listStatements().allStatements() );
189
 
}
190
 
 
191
 
// static
192
 
Nepomuk::Sync::ResourceHash Nepomuk::Sync::ResourceHash::fromStatementList(const QList< Soprano::Statement >& allStatements)
193
 
{
194
 
    //
195
 
    // Convert into multi hash for easier look up
196
 
    //
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 );
202
 
    }
203
 
 
204
 
    //
205
 
    // Convert them into a better format --> SyncResource
206
 
    //
207
 
    const QList<KUrl> & uniqueUris = stHash.uniqueKeys();
208
 
 
209
 
    ResourceHash resources;
210
 
    resources.reserve( uniqueUris.size() );
211
 
 
212
 
    foreach( const KUrl & resUri, uniqueUris ) {
213
 
        SyncResource res = SyncResource::fromStatementList( stHash.values( resUri ) );
214
 
        resources.insert( res.uri(), res );
215
 
    }
216
 
 
217
 
    return resources;
218
 
}
219
 
 
220
 
 
221
 
QList< Soprano::Statement > Nepomuk::Sync::ResourceHash::toStatementList() const
222
 
{
223
 
    QList<Soprano::Statement> stList;
224
 
    Q_FOREACH( const KUrl& uri, uniqueKeys() ) {
225
 
        const SyncResource & res = value( uri );
226
 
        stList += res.toStatementList();
227
 
    }
228
 
 
229
 
    return stList;
230
 
}
231
 
 
232
 
 
233
 
bool Nepomuk::Sync::SyncResource::isValid() const
234
 
{
235
 
    return !d->uri.isEmpty() && !isEmpty();
236
 
}
237
 
 
238
 
uint Nepomuk::Sync::qHash(const Nepomuk::Sync::SyncResource& res)
239
 
{
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
242
 
 
243
 
    uint hash = 0;
244
 
    QHashIterator<KUrl, Soprano::Node> it( res );
245
 
    while( it.hasNext() ) {
246
 
        it.next();
247
 
 
248
 
        hash ^= qHash( it.key() ) & qHash( it.value() );
249
 
    }
250
 
 
251
 
    return hash;
252
 
}