• 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
  • libnepomukcore
  • types
entity.cpp
Go to the documentation of this file.
1 /* This file is part of the Nepomuk-KDE libraries
2  Copyright (c) 2007-2010 Sebastian Trueg <trueg@kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "entity.h"
21 #include "entity_p.h"
22 #include "resourcemanager.h"
23 
24 #include <QtCore/QHash>
25 #include <QtCore/QMutexLocker>
26 
27 #include <Soprano/QueryResultIterator>
28 #include <Soprano/Model>
29 #include <Soprano/Vocabulary/NRL>
30 #include <Soprano/Vocabulary/NAO>
31 #include <Soprano/Vocabulary/RDFS>
32 
33 #include <kicon.h>
34 
35 
36 Nepomuk2::Types::EntityPrivate::EntityPrivate( const QUrl& uri_ )
37  : mutex(QMutex::Recursive),
38  uri( uri_ ),
39  userVisible( true ),
40  available( uri_.isValid() ? -1 : 0 ),
41  ancestorsAvailable( uri_.isValid() ? -1 : 0 )
42 {
43 }
44 
45 
46 void Nepomuk2::Types::EntityPrivate::init()
47 {
48  QMutexLocker lock( &mutex );
49 
50  if ( available < 0 ) {
51  available = load() ? 1 : 0;
52  }
53 }
54 
55 
56 void Nepomuk2::Types::EntityPrivate::initAncestors()
57 {
58  QMutexLocker lock( &mutex );
59 
60  if ( ancestorsAvailable < 0 ) {
61  ancestorsAvailable = loadAncestors() ? 1 : 0;
62  }
63 }
64 
65 
66 bool Nepomuk2::Types::EntityPrivate::load()
67 {
68  const QString query = QString::fromLatin1( "select ?p ?o where { "
69  "graph ?g { <%1> ?p ?o . } . "
70  "{ ?g a %2 . } UNION { ?g a %3 . } . }" )
71  .arg( QString::fromAscii( uri.toEncoded() ),
72  Soprano::Node::resourceToN3( Soprano::Vocabulary::NRL::Ontology() ),
73  Soprano::Node::resourceToN3( Soprano::Vocabulary::NRL::KnowledgeBase() ) );
74 
75  Soprano::QueryResultIterator it
76  = ResourceManager::instance()->mainModel()->executeQuery( query, Soprano::Query::QueryLanguageSparql );
77  while ( it.next() ) {
78  QUrl property = it.binding( "p" ).uri();
79  Soprano::Node value = it.binding( "o" );
80 
81  if ( property == Soprano::Vocabulary::RDFS::label() ) {
82  if ( value.language().isEmpty() ) {
83  label = value.toString();
84  }
85  else if( value.language() == KGlobal::locale()->language() ) {
86  l10nLabel = value.toString();
87  }
88  }
89 
90  else if ( property == Soprano::Vocabulary::RDFS::comment() ) {
91  if ( value.language().isEmpty() ) {
92  comment = value.toString();
93  }
94  else if( value.language() == KGlobal::locale()->language() ) {
95  l10nComment = value.toString();
96  }
97  }
98 
99  else if ( property == Soprano::Vocabulary::NAO::hasSymbol() ) {
100  icon = KIcon( value.toString() );
101  }
102 
103  else if ( property == Soprano::Vocabulary::NAO::userVisible() ) {
104  userVisible = value.literal().toBool();
105  }
106 
107  else {
108  addProperty( property, value );
109  }
110  }
111 
112  return !it.lastError();
113 }
114 
115 
116 bool Nepomuk2::Types::EntityPrivate::loadAncestors()
117 {
118  const QString query = QString::fromLatin1( "select ?s ?p where { "
119  "graph ?g { ?s ?p <%1> . } . "
120  "{ ?g a %2 . } UNION { ?g a %3 . } . }" )
121  .arg( QString::fromAscii( uri.toEncoded() ),
122  Soprano::Node::resourceToN3( Soprano::Vocabulary::NRL::Ontology() ),
123  Soprano::Node::resourceToN3( Soprano::Vocabulary::NRL::KnowledgeBase() ) );
124 
125  Soprano::QueryResultIterator it
126  = ResourceManager::instance()->mainModel()->executeQuery( query, Soprano::Query::QueryLanguageSparql );
127  while ( it.next() ) {
128  addAncestorProperty( it.binding( "s" ).uri(), it.binding( "p" ).uri() );
129  }
130 
131  return !it.lastError();
132 }
133 
134 
135 
136 void Nepomuk2::Types::EntityPrivate::reset( bool )
137 {
138  QMutexLocker lock( &mutex );
139 
140  label.clear();
141  comment.clear();
142  l10nLabel.clear();
143  l10nComment.clear();;
144 
145  icon = QIcon();
146 
147  available = -1;
148  ancestorsAvailable = -1;
149 }
150 
151 
152 Nepomuk2::Types::Entity::Entity()
153 {
154 }
155 
156 
157 Nepomuk2::Types::Entity::Entity( const Entity& other )
158 {
159  d = other.d;
160 }
161 
162 
163 Nepomuk2::Types::Entity::~Entity()
164 {
165 }
166 
167 
168 Nepomuk2::Types::Entity& Nepomuk2::Types::Entity::operator=( const Entity& other )
169 {
170  d = other.d;
171  return *this;
172 }
173 
174 
175 QUrl Nepomuk2::Types::Entity::uri() const
176 {
177  return d ? d->uri : QUrl();
178 }
179 
180 
181 QString Nepomuk2::Types::Entity::name() const
182 {
183  return d ? (d->uri.fragment().isEmpty() ? d->uri.toString().section('/',-1) : d->uri.fragment() ) : QString();
184 }
185 
186 
187 QString Nepomuk2::Types::Entity::label( const QString& language )
188 {
189  if ( d ) {
190  d->init();
191 
192  if ( language == KGlobal::locale()->language() &&
193  !d->l10nLabel.isEmpty() ) {
194  return d->l10nLabel;
195  }
196  else if( !d->label.isEmpty() ) {
197  return d->label;
198  }
199  else {
200  return name();
201  }
202  }
203  else {
204  return QString();
205  }
206 }
207 
208 
209 QString Nepomuk2::Types::Entity::label( const QString& language ) const
210 {
211  return const_cast<Entity*>(this)->label( language );
212 }
213 
214 
215 QString Nepomuk2::Types::Entity::comment( const QString& language )
216 {
217  if ( d ) {
218  d->init();
219 
220  if ( language == KGlobal::locale()->language() &&
221  !d->l10nComment.isEmpty() ) {
222  return d->l10nComment;
223  }
224  else {
225  return d->comment;
226  }
227  }
228  else {
229  return QString();
230  }
231 }
232 
233 
234 QString Nepomuk2::Types::Entity::comment( const QString& language ) const
235 {
236  return const_cast<Entity*>(this)->comment( language );
237 }
238 
239 
240 QIcon Nepomuk2::Types::Entity::icon()
241 {
242  if ( d ) {
243  d->init();
244 
245  return d->icon;
246  }
247  else {
248  return QIcon();
249  }
250 }
251 
252 
253 QIcon Nepomuk2::Types::Entity::icon() const
254 {
255  return const_cast<Entity*>(this)->icon();
256 }
257 
258 
259 bool Nepomuk2::Types::Entity::isValid() const
260 {
261  return d ? d->uri.isValid() : false;
262 }
263 
264 
265 bool Nepomuk2::Types::Entity::isAvailable()
266 {
267  if ( d ) {
268  d->init();
269  return d->available == 1;
270  }
271  else {
272  return false;
273  }
274 }
275 
276 
277 bool Nepomuk2::Types::Entity::isAvailable() const
278 {
279  return const_cast<Entity*>(this)->isAvailable();
280 }
281 
282 
283 void Nepomuk2::Types::Entity::reset( bool recursive )
284 {
285  if( d )
286  d->reset( recursive );
287 }
288 
289 
290 bool Nepomuk2::Types::Entity::userVisible() const
291 {
292  if ( d ) {
293  d->init();
294  return d->userVisible;
295  }
296  else {
297  return true;
298  }
299 }
300 
301 
302 bool Nepomuk2::Types::Entity::operator==( const Entity& other ) const
303 {
304  // since we use one instace cache we can improve comparation operations
305  // intensly by not comparing URLs but pointers.
306  return( d.constData() == other.d.constData() );
307 }
308 
309 
310 bool Nepomuk2::Types::Entity::operator==( const QUrl& other ) const
311 {
312  if( d )
313  return( d->uri == other );
314  else
315  return other.isEmpty();
316 }
317 
318 
319 bool Nepomuk2::Types::Entity::operator!=( const Entity& other ) const
320 {
321  // since we use one instace cache we can improve comparation operations
322  // intensly by not comparing URLs but pointers.
323  return( d.constData() != other.d.constData() );
324 }
325 
326 
327 bool Nepomuk2::Types::Entity::operator!=( const QUrl& other ) const
328 {
329  if( d )
330  return( d->uri != other );
331  else
332  return !other.isEmpty();
333 }
entity.h
Nepomuk2::addProperty
KJob * addProperty(const QList< QUrl > &resources, const QUrl &property, const QVariantList &values, const KComponentData &component=KGlobal::mainComponent())
Add one or more property values to one or more resources.
Definition: datamanagement.cpp:36
Nepomuk2::Types::Entity::uri
QUrl uri() const
The URI of the resource.
Definition: entity.cpp:175
Nepomuk2::Types::Entity::icon
QIcon icon()
Retrieve the icon stored for the entity (nao:hasSymbol)
Definition: entity.cpp:240
Nepomuk2::Types::Entity::operator=
Entity & operator=(const Entity &)
Copy operator.
Definition: entity.cpp:168
Nepomuk2::Types::Entity::isAvailable
bool isAvailable()
Is this Entity available locally, i.e.
Definition: entity.cpp:265
Nepomuk2::Types::Entity::reset
void reset(bool recursive=false)
The Types classes are optimized for performance under the aasumption that ontologies never change dur...
Definition: entity.cpp:283
Nepomuk2::Types::Entity::operator==
bool operator==(const Entity &other) const
Compares two Entity instances.
Definition: entity.cpp:302
Nepomuk2::Types::Entity
Abstract base class for Class and Property;.
Definition: entity.h:54
Nepomuk2::Types::Entity::~Entity
virtual ~Entity()
Destructor.
Definition: entity.cpp:163
Nepomuk2::Types::Entity::userVisible
bool userVisible() const
nao:userVisible can be used to hide certain properties and resources of a certain type from the user...
Definition: entity.cpp:290
Nepomuk2::Types::Entity::Entity
Entity()
Create an invalid Entity instance.
Definition: entity.cpp:152
Nepomuk2::ResourceManager::instance
static ResourceManager * instance()
Definition: resourcemanager.cpp:270
resourcemanager.h
Nepomuk2::Types::Entity::comment
QString comment(const QString &language=KGlobal::locale() ->language())
Retrieve the comment of the entity (rdfs:comment)
Definition: entity.cpp:215
Nepomuk2::Types::Entity::name
QString name() const
The name of the resource.
Definition: entity.cpp:181
Nepomuk2::Types::Entity::label
QString label(const QString &language=KGlobal::locale() ->language())
Retrieve the label of the entity (rdfs:label)
Definition: entity.cpp:187
Nepomuk2::Types::Entity::d
QExplicitlySharedDataPointer< EntityPrivate > d
Definition: entity.h:250
Nepomuk2::Types::Entity::operator!=
bool operator!=(const Entity &other) const
Compares two Entity instances.
Definition: entity.cpp:319
Nepomuk2::Types::Entity::isValid
bool isValid() const
Is this a valid Entity, i.e.
Definition: entity.cpp:259
Nepomuk2::ResourceManager::mainModel
Soprano::Model * mainModel()
Retrieve the main data storage model.
Definition: resourcemanager.cpp:363
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:08 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