• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

Nepomuk-Core

  • sources
  • kde-4.12
  • kdelibs
  • nepomuk-core
  • services
  • storage
syncresource.cpp
Go to the documentation of this file.
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 "nie.h"
31 #include "nfo.h"
32 #include <Soprano/Vocabulary/RDF>
33 
34 #include <QtCore/QSharedData>
35 
36 class Nepomuk2::Sync::SyncResource::Private : public QSharedData {
37 public:
38  KUrl uri;
39 };
40 
41 
42 Nepomuk2::Sync::SyncResource::SyncResource()
43  : d( new Nepomuk2::Sync::SyncResource::Private )
44 {
45 }
46 
47 Nepomuk2::Sync::SyncResource::SyncResource(const KUrl& uri)
48  : d( new Nepomuk2::Sync::SyncResource::Private )
49 {
50  setUri( uri );
51 }
52 
53 Nepomuk2::Sync::SyncResource::SyncResource(const Nepomuk2::Sync::SyncResource& rhs)
54  : QMultiHash< KUrl, Soprano::Node >(rhs),
55  d( rhs.d )
56 {
57 }
58 
59 Nepomuk2::Sync::SyncResource::~SyncResource()
60 {
61 }
62 
63 Nepomuk2::Sync::SyncResource& Nepomuk2::Sync::SyncResource::operator=(const Nepomuk2::Sync::SyncResource& rhs)
64 {
65  d = rhs.d;
66  return *this;
67 }
68 
69 bool Nepomuk2::Sync::SyncResource::operator==(const Nepomuk2::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 > Nepomuk2::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 Nepomuk2::Sync::SyncResource::isFolder() const
96 {
97  return values( Soprano::Vocabulary::RDF::type() ).contains( Soprano::Node( Nepomuk2::Vocabulary::NFO::Folder() ) );
98 }
99 
100 
101 bool Nepomuk2::Sync::SyncResource::isFileDataObject() const
102 {
103  return values( Soprano::Vocabulary::RDF::type() ).contains( Soprano::Node( Nepomuk2::Vocabulary::NFO::FileDataObject() ) );
104 }
105 
106 
107 KUrl Nepomuk2::Sync::SyncResource::nieUrl() const
108 {
109  const QHash<KUrl, Soprano::Node>::const_iterator it = constFind( Nepomuk2::Vocabulary::NIE::url() );
110  if( it == constEnd() )
111  return KUrl();
112  else
113  return it.value().uri();
114 }
115 
116 
117 void Nepomuk2::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 Nepomuk2::Sync::SyncResource::uri() const
128 {
129  return d->uri;
130 }
131 
132 Soprano::Node Nepomuk2::Sync::SyncResource::uriNode() const
133 {
134  return d->uri.url().startsWith("_:") ? Soprano::Node(d->uri.url().mid(2)) : d->uri;
135 }
136 
137 
138 QList< Soprano::Node > Nepomuk2::Sync::SyncResource::property(const KUrl& url) const
139 {
140  return values(url);
141 }
142 
143 void Nepomuk2::Sync::SyncResource::removeObject(const KUrl& uri)
144 {
145  QMutableHashIterator<KUrl, Soprano::Node> iter( *this );
146  while( iter.hasNext() ) {
147  iter.next();
148 
149  if( iter.value().isResource() && iter.value().uri() == uri )
150  iter.remove();
151  }
152 }
153 
154 namespace {
155  // Blank nodes are stored as "_:identifier" in urls
156  QUrl getUri( const Soprano::Node & n ) {
157  if( n.isBlank() )
158  return QUrl( n.toN3() );
159  else
160  return n.uri();
161  }
162 }
163 // static
164 Nepomuk2::Sync::SyncResource Nepomuk2::Sync::SyncResource::fromStatementList(const QList< Soprano::Statement >& list)
165 {
166  if( list.isEmpty() )
167  return SyncResource();
168 
169  SyncResource res;
170  Soprano::Node subject = list.first().subject();
171  res.setUri( getUri(subject) );
172 
173  foreach( const Soprano::Statement & st, list ) {
174  if( st.subject() != subject )
175  continue;
176 
177  KUrl pred = st.predicate().uri();
178  Soprano::Node obj = st.object();
179 
180  if( !res.contains( pred, obj ) )
181  res.insert( pred, obj );
182  }
183 
184  return res;
185 }
186 
187 //
188 // ResourceHash
189 //
190 
191 // static
192 Nepomuk2::Sync::ResourceHash Nepomuk2::Sync::ResourceHash::fromGraph(const Soprano::Graph& graph)
193 {
194  return fromStatementList( graph.listStatements().allStatements() );
195 }
196 
197 // static
198 Nepomuk2::Sync::ResourceHash Nepomuk2::Sync::ResourceHash::fromStatementList(const QList< Soprano::Statement >& allStatements)
199 {
200  //
201  // Convert into multi hash for easier look up
202  //
203  QMultiHash<KUrl, Soprano::Statement> stHash;
204  stHash.reserve( allStatements.size() );
205  foreach( const Soprano::Statement & st, allStatements ) {
206  KUrl uri = getUri( st.subject() );
207  stHash.insert( uri, st );
208  }
209 
210  //
211  // Convert them into a better format --> SyncResource
212  //
213  const QList<KUrl> & uniqueUris = stHash.uniqueKeys();
214 
215  ResourceHash resources;
216  resources.reserve( uniqueUris.size() );
217 
218  foreach( const KUrl & resUri, uniqueUris ) {
219  SyncResource res = SyncResource::fromStatementList( stHash.values( resUri ) );
220  resources.insert( res.uri(), res );
221  }
222 
223  return resources;
224 }
225 
226 
227 QList< Soprano::Statement > Nepomuk2::Sync::ResourceHash::toStatementList() const
228 {
229  QList<Soprano::Statement> stList;
230  Q_FOREACH( const KUrl& uri, uniqueKeys() ) {
231  const SyncResource & res = value( uri );
232  stList += res.toStatementList();
233  }
234 
235  return stList;
236 }
237 
238 
239 bool Nepomuk2::Sync::SyncResource::isValid() const
240 {
241  return !d->uri.isEmpty() && !isEmpty();
242 }
243 
244 bool Nepomuk2::Sync::SyncResource::isBlank() const
245 {
246  //TODO: Figure out a way to optimize this - Converting a url to a string is expensive
247  return d->uri.url().startsWith(QLatin1String("_:"));
248 }
249 
250 
251 uint Nepomuk2::Sync::qHash(const Nepomuk2::Sync::SyncResource& res)
252 {
253  // WARNING: Do not use the uri to generate the hash.
254  // StoreResources depends on the fact that the uri is not used while generating the hash
255 
256  uint hash = 0;
257  QHashIterator<KUrl, Soprano::Node> it( res );
258  while( it.hasNext() ) {
259  it.next();
260 
261  hash ^= qHash( it.key() ) & qHash( it.value() );
262  }
263 
264  return hash;
265 }
266 
267 QDebug Nepomuk2::Sync::operator<<(QDebug dbg, const Nepomuk2::Sync::SyncResource& res)
268 {
269  return dbg << res.uri() << QMultiHash<KUrl, Soprano::Node>(res);
270 }
QMultiHash
Nepomuk2::Sync::SyncResource::operator=
SyncResource & operator=(const SyncResource &rhs)
Definition: syncresource.cpp:63
Nepomuk2::Sync::SyncResource::SyncResource
SyncResource()
Definition: syncresource.cpp:42
Nepomuk2::Sync::qHash
uint qHash(const SyncResource &res)
Definition: syncresource.cpp:251
Nepomuk2::Sync::operator<<
QDebug operator<<(QDebug dbg, const Nepomuk2::Sync::SyncResource &res)
Definition: syncresource.cpp:267
Nepomuk2::Sync::SyncResource::~SyncResource
virtual ~SyncResource()
Definition: syncresource.cpp:59
Nepomuk2::Sync::SyncResource::removeObject
void removeObject(const KUrl &uri)
Removes all the statements whose object is uri.
Definition: syncresource.cpp:143
Nepomuk2::Sync::SyncResource::fromStatementList
static SyncResource fromStatementList(const QList< Soprano::Statement > &list)
It uses the the first element's subject as the uri and ignores all further subjects.
Definition: syncresource.cpp:164
Nepomuk2::Sync::ResourceHash::toStatementList
QList< Soprano::Statement > toStatementList() const
Definition: syncresource.cpp:227
Nepomuk2::Sync::SyncResource::nieUrl
KUrl nieUrl() const
Definition: syncresource.cpp:107
QHash
Nepomuk2::Sync::SyncResource::isValid
bool isValid() const
Definition: syncresource.cpp:239
Nepomuk2::Sync::ResourceHash::fromStatementList
static ResourceHash fromStatementList(const QList< Soprano::Statement > &list)
Definition: syncresource.cpp:198
Nepomuk2::Sync::SyncResource::operator==
bool operator==(const SyncResource &res) const
Definition: syncresource.cpp:69
Nepomuk2::Sync::SyncResource::toStatementList
QList< Soprano::Statement > toStatementList() const
Definition: syncresource.cpp:75
Nepomuk2::Sync::SyncResource::property
QList< Soprano::Node > property(const KUrl &url) const
Definition: syncresource.cpp:138
Nepomuk2::Sync::ResourceHash::fromGraph
static ResourceHash fromGraph(const Soprano::Graph &graph)
Definition: syncresource.cpp:192
Nepomuk2::Sync::SyncResource::uri
KUrl uri() const
Definition: syncresource.cpp:127
Nepomuk2::Sync::SyncResource::isFileDataObject
bool isFileDataObject() const
Definition: syncresource.cpp:101
Nepomuk2::Sync::SyncResource::isFolder
bool isFolder() const
Definition: syncresource.cpp:95
Nepomuk2::Sync::SyncResource::setUri
void setUri(const Soprano::Node &node)
If node is resource node the uri is set to the node's uri Otherwise if node is a blank node then the ...
Definition: syncresource.cpp:117
Nepomuk2::Sync::SyncResource::isBlank
bool isBlank() const
Definition: syncresource.cpp:244
syncresource.h
Nepomuk2::Sync::SyncResource
A SyncResource is a convenient way of storing a set of properties and objects for a common subject...
Definition: syncresource.h:53
Nepomuk2::Sync::SyncResource::uriNode
Soprano::Node uriNode() const
Definition: syncresource.cpp:132
Nepomuk2::Sync::ResourceHash
A SyncResource is a convenient way of representing a list of Soprano::Statements or a Soprano::Graph...
Definition: syncresource.h:109
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:09 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Nepomuk-Core

Skip menu "Nepomuk-Core"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal