• 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
ontologyloader.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE Project
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 version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 */
18 
19 #include "ontologyloader.h"
20 #include "ontologymanagermodel.h"
21 #include "ontologymanageradaptor.h"
22 #include "graphretriever.h"
23 
24 #include <Soprano/Global>
25 #include <Soprano/Node>
26 #include <Soprano/Model>
27 #include <Soprano/PluginManager>
28 #include <Soprano/StatementIterator>
29 #include <Soprano/Parser>
30 #include <Soprano/QueryResultIterator>
31 
32 #include <KConfig>
33 #include <KConfigGroup>
34 #include <KDebug>
35 #include <KGlobal>
36 #include <KStandardDirs>
37 #include <KLocale>
38 #include <KDirWatch>
39 #include <kdbusconnectionpool.h>
40 
41 #include <QtCore/QFileInfo>
42 #include <QtCore/QTimer>
43 
44 #include <kpluginfactory.h>
45 #include <kpluginloader.h>
46 
47 
48 using namespace Soprano;
49 
50 
51 class Nepomuk2::OntologyLoader::Private
52 {
53 public:
54  Private( OntologyLoader* p )
55  : forceOntologyUpdate( false ),
56  someOntologyUpdated( false ),
57  q( p ) {
58  }
59 
60  OntologyManagerModel* model;
61 
62  QTimer updateTimer;
63  bool forceOntologyUpdate;
64  QStringList desktopFilesToUpdate;
65 
66  // true if at least one ontology has been updated
67  bool someOntologyUpdated;
68 
69  void updateOntology( const QString& filename );
70 
71 private:
72  OntologyLoader* q;
73 };
74 
75 
76 void Nepomuk2::OntologyLoader::Private::updateOntology( const QString& filename )
77 {
78  KConfig ontologyDescFile( filename );
79  KConfigGroup df( &ontologyDescFile, QLatin1String( "Ontology" ) );
80 
81  // only update if the modification date of the ontology file changed (not the desktop file).
82  // ------------------------------------
83  QFileInfo ontoFileInf( df.readEntry( QLatin1String("Path") ) );
84 
85 #ifdef Q_OS_WIN
86  if ( ! ontoFileInf.exists() ) {
87  // On Windows the Path setting is always the install directory which is not
88  // something like /usr/share/ontologies but something like
89  // r:\kderoot\build\shared-desktop-ontologies-mingw-i686\image\shared\ontologies
90  // so if you did not compile shared-desktop-ontologies yourself the Path
91  // is useless.
92  // We expect a default ontologies installation where the .ontology files are placed
93  // in the same directory as the .trig files.
94  QString alt_filename = filename;
95  QFileInfo ontoAlternative( alt_filename.replace( QLatin1String(".ontology"),
96  QLatin1String(".trig") ) );
97  ontoFileInf = ontoAlternative;
98  kDebug() << "Ontology path: " << filename << " does not exist. Using "
99  << alt_filename << " instead";
100  }
101 #endif
102  QString ontoNamespace = df.readEntry( QLatin1String("Namespace") );
103  QDateTime ontoLastModified = model->ontoModificationDate( ontoNamespace );
104  bool update = false;
105 
106  if ( ontoLastModified < ontoFileInf.lastModified() ) {
107  kDebug() << "Ontology" << ontoNamespace << "needs updating.";
108  update = true;
109  }
110  //else {
111  // kDebug() << "Ontology" << ontoNamespace << "up to date.";
112  //}
113 
114  if( !update && forceOntologyUpdate ) {
115  kDebug() << "Ontology update forced.";
116  update = true;
117  }
118 
119  if( update ) {
120  someOntologyUpdated = true;
121 
122  QString mimeType = df.readEntry( "MimeType", QString() );
123 
124  const Soprano::Parser* parser
125  = Soprano::PluginManager::instance()->discoverParserForSerialization( Soprano::mimeTypeToSerialization( mimeType ),
126  mimeType );
127  if ( !parser ) {
128  kDebug() << "No parser to handle" << df.readEntry( QLatin1String( "Name" ) ) << "(" << mimeType << ")";
129  return;
130  }
131 
132  kDebug() << "Parsing" << ontoFileInf.filePath();
133 
134  Soprano::StatementIterator it = parser->parseFile( ontoFileInf.filePath(),
135  ontoNamespace,
136  Soprano::mimeTypeToSerialization( mimeType ),
137  mimeType );
138  if ( !parser->lastError() ) {
139  model->updateOntology( it, ontoNamespace );
140  emit q->ontologyUpdated( ontoNamespace );
141  }
142  else {
143  kDebug() << "Parsing of file " << ontoFileInf.filePath() << "failed (" << parser->lastError().message() << ")";
144  emit q->ontologyUpdateFailed( ontoNamespace, i18n( "Parsing of file %1 failed (%2)", ontoFileInf.filePath(), parser->lastError().message() ) );
145  }
146  }
147 }
148 
149 
150 
151 Nepomuk2::OntologyLoader::OntologyLoader( Soprano::Model* model, QObject* parent )
152  : QObject( parent ),
153  d( new Private(this) )
154 {
155  // register ontology resource dir
156  KGlobal::dirs()->addResourceType( "xdgdata-ontology", 0, "ontology" );
157 
158  // export ourselves on DBus
159  ( void )new OntologyManagerAdaptor( this );
160  QDBusConnection con = KDBusConnectionPool::threadConnection();
161  con.registerObject( QLatin1String("/nepomukontologyloader"), this,
162  QDBusConnection::ExportAdaptors );
163 
164  // be backwards compatible
165  con.registerService( QLatin1String("org.kde.nepomuk.services.nepomukontologyloader") );
166 
167  d->model = new OntologyManagerModel( model, this );
168  connect( &d->updateTimer, SIGNAL(timeout()), this, SLOT(updateNextOntology()) );
169 
170  // watch both the global and local ontology folder for changes
171  KDirWatch* dirWatch = KDirWatch::self();
172  connect( dirWatch, SIGNAL( dirty(QString) ),
173  this, SLOT( updateLocalOntologies() ) );
174  connect( dirWatch, SIGNAL( created(QString) ),
175  this, SLOT( updateLocalOntologies() ) );
176 
177  foreach( const QString& dir, KGlobal::dirs()->resourceDirs( "xdgdata-ontology" ) ) {
178  kDebug() << "watching" << dir;
179  dirWatch->addDir( dir, KDirWatch::WatchFiles|KDirWatch::WatchSubDirs );
180  }
181 }
182 
183 
184 Nepomuk2::OntologyLoader::~OntologyLoader()
185 {
186  delete d;
187 }
188 
189 
190 void Nepomuk2::OntologyLoader::updateLocalOntologies()
191 {
192  d->someOntologyUpdated = false;
193  d->desktopFilesToUpdate = KGlobal::dirs()->findAllResources( "xdgdata-ontology", "*.ontology", KStandardDirs::Recursive|KStandardDirs::NoDuplicates );
194  if(d->desktopFilesToUpdate.isEmpty())
195  kError() << "No ontology files found! Make sure the shared-desktop-ontologies project is installed and XDG_DATA_DIRS is set properly.";
196  d->updateTimer.start(0);
197 }
198 
199 
200 void Nepomuk2::OntologyLoader::updateAllLocalOntologies()
201 {
202  d->forceOntologyUpdate = true;
203  updateLocalOntologies();
204 }
205 
206 
207 void Nepomuk2::OntologyLoader::updateNextOntology()
208 {
209  if( !d->desktopFilesToUpdate.isEmpty() ) {
210  d->updateOntology( d->desktopFilesToUpdate.takeFirst() );
211  }
212  else {
213  d->forceOntologyUpdate = false;
214  d->updateTimer.stop();
215  emit ontologyUpdateFinished(d->someOntologyUpdated);
216  }
217 }
218 
219 
220 QString Nepomuk2::OntologyLoader::findOntologyContext( const QString& uri )
221 {
222  return QString::fromAscii( d->model->findOntologyContext( QUrl::fromEncoded( uri.toAscii() ) ).toEncoded() );
223 }
224 
225 
226 void Nepomuk2::OntologyLoader::importOntology( const QString& url )
227 {
228  connect( GraphRetriever::retrieve( url ), SIGNAL( result( KJob* ) ),
229  this, SLOT( slotGraphRetrieverResult( KJob* ) ) );
230 }
231 
232 
233 void Nepomuk2::OntologyLoader::slotGraphRetrieverResult( KJob* job )
234 {
235  GraphRetriever* graphRetriever = static_cast<GraphRetriever*>( job );
236  if ( job->error() ) {
237  emit ontologyUpdateFailed( QString::fromAscii( graphRetriever->url().toEncoded() ), graphRetriever->errorString() );
238  }
239  else {
240  // TODO: find a way to check if the imported version of the ontology
241  // is newer than the already installed one
242  if ( d->model->updateOntology( graphRetriever->statements(), QUrl()/*graphRetriever->url()*/ ) ) {
243  emit ontologyUpdated( QString::fromAscii( graphRetriever->url().toEncoded() ) );
244  emit ontologyUpdateFinished(true);
245  }
246  else {
247  emit ontologyUpdateFailed( QString::fromAscii( graphRetriever->url().toEncoded() ), d->model->lastError().message() );
248  }
249  }
250 }
251 
252 #include "ontologyloader.moc"
Nepomuk2::GraphRetriever::url
QUrl url() const
Definition: graphretriever.cpp:98
Nepomuk2::OntologyLoader::importOntology
void importOntology(const QString &url)
Try to retrieve an ontology from the web.
Definition: ontologyloader.cpp:226
Nepomuk2::GraphRetriever
Utility class for retrieving RDF graphs from Web locations.
Definition: graphretriever.h:54
Nepomuk2::OntologyLoader::updateLocalOntologies
void updateLocalOntologies()
Update all installed ontologies that changed since the last update.
Definition: ontologyloader.cpp:190
Nepomuk2::OntologyLoader::findOntologyContext
QString findOntologyContext(const QString &uri)
Tries to find the ontology uri in the local Nepomuk store.
Definition: ontologyloader.cpp:220
graphretriever.h
QObject
Nepomuk2::OntologyLoader::~OntologyLoader
~OntologyLoader()
Definition: ontologyloader.cpp:184
Nepomuk2::OntologyManagerModel
Filter model to manage NRL ontologies.
Definition: ontologymanagermodel.h:35
Nepomuk2::OntologyLoader::updateAllLocalOntologies
void updateAllLocalOntologies()
Update all installed ontologies, independently of their status.
Definition: ontologyloader.cpp:200
ontologyloader.h
KJob
Nepomuk2::GraphRetriever::statements
Soprano::StatementIterator statements() const
Definition: graphretriever.cpp:121
ontologymanagermodel.h
Nepomuk2::GraphRetriever::retrieve
static GraphRetriever * retrieve(const QUrl &uri)
Definition: graphretriever.cpp:165
Nepomuk2::OntologyLoader::OntologyLoader
OntologyLoader(Soprano::Model *model, QObject *parent=0)
Definition: ontologyloader.cpp:151
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