delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / runtime / nepomuk / services / ontologyloader / ontologyloader.cpp
blob3093d2d446b30b10d0c2a7f5d4e4a9434d3b9e0a
1 /* This file is part of the KDE Project
2 Copyright (c) 2007 Sebastian Trueg <trueg@kde.org>
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.
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.
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.
19 #include "ontologyloader.h"
20 #include "ontologymanagermodel.h"
21 #include "ontologymanageradaptor.h"
22 #include "graphretriever.h"
24 #include <Soprano/Global>
25 #include <Soprano/Node>
26 #include <Soprano/Model>
27 #include <Soprano/PluginManager>
28 #include <Soprano/StatementIterator>
29 #include <Soprano/Parser>
31 #include <KDesktopFile>
32 #include <KConfigGroup>
33 #include <KDebug>
34 #include <KGlobal>
35 #include <KStandardDirs>
36 #include <KLocale>
37 #include <KDirWatch>
39 #include <QtCore/QFileInfo>
40 #include <QtCore/QTimer>
42 #include <kpluginfactory.h>
43 #include <kpluginloader.h>
45 NEPOMUK_EXPORT_SERVICE( Nepomuk::OntologyLoader, "nepomukontologyloader")
48 using namespace Soprano;
50 class Nepomuk::OntologyLoader::Private
52 public:
53 Private( OntologyLoader* p )
54 : forceOntologyUpdate( false ),
55 q( p ) {
58 OntologyManagerModel* model;
60 QTimer updateTimer;
61 bool forceOntologyUpdate;
62 QStringList desktopFilesToUpdate;
64 void updateOntology( const QString& filename );
66 private:
67 OntologyLoader* q;
71 void Nepomuk::OntologyLoader::Private::updateOntology( const QString& filename )
73 KDesktopFile df( filename );
75 // only update if the modification date of the ontology file changed (not the desktop file).
76 // ------------------------------------
77 QFileInfo ontoFileInf( df.readPath() );
78 QDateTime ontoLastModified = model->ontoModificationDate( df.readUrl() );
79 bool update = false;
81 if ( ontoLastModified < ontoFileInf.lastModified() ) {
82 kDebug() << "Ontology" << df.readUrl() << "needs updating.";
83 update = true;
85 else {
86 kDebug() << "Ontology" << df.readUrl() << "up to date.";
89 if( !update && forceOntologyUpdate ) {
90 kDebug() << "Ontology update forced.";
91 update = true;
94 if( update ) {
95 QString mimeType = df.desktopGroup().readEntry( "MimeType", QString() );
97 const Soprano::Parser* parser
98 = Soprano::PluginManager::instance()->discoverParserForSerialization( Soprano::mimeTypeToSerialization( mimeType ),
99 mimeType );
100 if ( !parser ) {
101 kDebug() << "No parser to handle" << df.readName() << "(" << mimeType << ")";
102 return;
105 kDebug() << "Parsing" << df.readPath();
107 Soprano::StatementIterator it = parser->parseFile( df.readPath(),
108 df.readUrl(),
109 Soprano::mimeTypeToSerialization( mimeType ),
110 mimeType );
111 if ( !parser->lastError() ) {
112 model->updateOntology( it, df.readUrl() );
113 emit q->ontologyUpdated( df.readUrl() );
115 else {
116 emit q->ontologyUpdateFailed( df.readUrl(), i18n( "Parsing of file %1 failed (%2)", df.readPath(), parser->lastError().message() ) );
123 Nepomuk::OntologyLoader::OntologyLoader( QObject* parent, const QList<QVariant>& )
124 : Service( parent ),
125 d( new Private(this) )
127 ( void )new OntologyManagerAdaptor( this );
129 d->model = new OntologyManagerModel( mainModel(), this );
130 connect( &d->updateTimer, SIGNAL(timeout()), this, SLOT(updateNextOntology()) );
132 // only update changed ontologies
133 updateLocalOntologies();
135 // watch both the global and local ontology folder for changes
136 KDirWatch* dirWatch = KDirWatch::self();
137 connect( dirWatch, SIGNAL( dirty(QString) ),
138 this, SLOT( updateLocalOntologies() ) );
139 connect( dirWatch, SIGNAL( created(QString) ),
140 this, SLOT( updateLocalOntologies() ) );
141 foreach( const QString& dir, KGlobal::dirs()->findDirs( "data", QString() ) ) {
142 // we only add the suffix here to make sure to also watch the non-existing local dir
143 kDebug() << "watching" << ( dir + "nepomuk/ontologies/" );
144 dirWatch->addDir( dir + "nepomuk/ontologies/", KDirWatch::WatchFiles );
149 Nepomuk::OntologyLoader::~OntologyLoader()
151 delete d;
155 void Nepomuk::OntologyLoader::updateLocalOntologies()
157 d->desktopFilesToUpdate = KGlobal::dirs()->findAllResources( "data", "nepomuk/ontologies/*.desktop" );
158 d->updateTimer.start(0);
162 void Nepomuk::OntologyLoader::updateAllLocalOntologies()
164 d->forceOntologyUpdate = true;
165 updateLocalOntologies();
169 void Nepomuk::OntologyLoader::updateNextOntology()
171 if( !d->desktopFilesToUpdate.isEmpty() ) {
172 d->updateOntology( d->desktopFilesToUpdate.takeFirst() );
174 else {
175 d->forceOntologyUpdate = false;
176 d->updateTimer.stop();
181 QString Nepomuk::OntologyLoader::findOntologyContext( const QString& uri )
183 return QString::fromAscii( d->model->findOntologyContext( QUrl::fromEncoded( uri.toAscii() ) ).toEncoded() );
187 void Nepomuk::OntologyLoader::importOntology( const QString& url )
189 connect( GraphRetriever::retrieve( url ), SIGNAL( result( KJob* ) ),
190 this, SLOT( slotGraphRetrieverResult( KJob* ) ) );
194 void Nepomuk::OntologyLoader::slotGraphRetrieverResult( KJob* job )
196 GraphRetriever* graphRetriever = static_cast<GraphRetriever*>( job );
197 if ( job->error() ) {
198 emit ontologyUpdateFailed( QString::fromAscii( graphRetriever->url().toEncoded() ), graphRetriever->errorString() );
200 else {
201 // TODO: find a way to check if the imported version of the ontology
202 // is newer than the already installed one
203 if ( d->model->updateOntology( graphRetriever->statements(), graphRetriever->url() ) ) {
204 emit ontologyUpdated( QString::fromAscii( graphRetriever->url().toEncoded() ) );
206 else {
207 emit ontologyUpdateFailed( QString::fromAscii( graphRetriever->url().toEncoded() ), d->model->lastError().message() );
212 #include "ontologyloader.moc"