NepomukDaemons
ontologyloader.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "ontologyloader.h"
00020 #include "ontologymanagermodel.h"
00021 #include "ontologymanageradaptor.h"
00022 #include "graphretriever.h"
00023
00024 #include <Soprano/Global>
00025 #include <Soprano/Node>
00026 #include <Soprano/Model>
00027 #include <Soprano/PluginManager>
00028 #include <Soprano/StatementIterator>
00029 #include <Soprano/Parser>
00030
00031 #include <KDesktopFile>
00032 #include <KConfigGroup>
00033 #include <KDebug>
00034 #include <KGlobal>
00035 #include <KStandardDirs>
00036 #include <KLocale>
00037 #include <KDirWatch>
00038
00039 #include <QtCore/QFileInfo>
00040 #include <QtCore/QTimer>
00041
00042 #include <kpluginfactory.h>
00043 #include <kpluginloader.h>
00044
00045 NEPOMUK_EXPORT_SERVICE( Nepomuk::OntologyLoader, "nepomukontologyloader")
00046
00047
00048 using namespace Soprano;
00049
00050 class Nepomuk::OntologyLoader::Private
00051 {
00052 public:
00053 Private( OntologyLoader* p )
00054 : forceOntologyUpdate( false ),
00055 q( p ) {
00056 }
00057
00058 OntologyManagerModel* model;
00059
00060 QTimer updateTimer;
00061 bool forceOntologyUpdate;
00062 QStringList desktopFilesToUpdate;
00063
00064 void updateOntology( const QString& filename );
00065
00066 private:
00067 OntologyLoader* q;
00068 };
00069
00070
00071 void Nepomuk::OntologyLoader::Private::updateOntology( const QString& filename )
00072 {
00073 KDesktopFile df( filename );
00074
00075
00076
00077 QFileInfo ontoFileInf( df.readPath() );
00078 QDateTime ontoLastModified = model->ontoModificationDate( df.readUrl() );
00079 bool update = false;
00080
00081 if ( ontoLastModified < ontoFileInf.lastModified() ) {
00082 kDebug() << "Ontology" << df.readUrl() << "needs updating.";
00083 update = true;
00084 }
00085 else {
00086 kDebug() << "Ontology" << df.readUrl() << "up to date.";
00087 }
00088
00089 if( !update && forceOntologyUpdate ) {
00090 kDebug() << "Ontology update forced.";
00091 update = true;
00092 }
00093
00094 if( update ) {
00095 QString mimeType = df.desktopGroup().readEntry( "MimeType", QString() );
00096
00097 const Soprano::Parser* parser
00098 = Soprano::PluginManager::instance()->discoverParserForSerialization( Soprano::mimeTypeToSerialization( mimeType ),
00099 mimeType );
00100 if ( !parser ) {
00101 kDebug() << "No parser to handle" << df.readName() << "(" << mimeType << ")";
00102 return;
00103 }
00104
00105 kDebug() << "Parsing" << df.readPath();
00106
00107 Soprano::StatementIterator it = parser->parseFile( df.readPath(),
00108 df.readUrl(),
00109 Soprano::mimeTypeToSerialization( mimeType ),
00110 mimeType );
00111 if ( !parser->lastError() ) {
00112 model->updateOntology( it, df.readUrl() );
00113 emit q->ontologyUpdated( df.readUrl() );
00114 }
00115 else {
00116 emit q->ontologyUpdateFailed( df.readUrl(), i18n( "Parsing of file %1 failed (%2)", df.readPath(), parser->lastError().message() ) );
00117 }
00118 }
00119 }
00120
00121
00122
00123 Nepomuk::OntologyLoader::OntologyLoader( QObject* parent, const QList<QVariant>& )
00124 : Service( parent ),
00125 d( new Private(this) )
00126 {
00127 ( void )new OntologyManagerAdaptor( this );
00128
00129 d->model = new OntologyManagerModel( mainModel(), this );
00130 connect( &d->updateTimer, SIGNAL(timeout()), this, SLOT(updateNextOntology()) );
00131
00132
00133 updateLocalOntologies();
00134
00135
00136 KDirWatch* dirWatch = KDirWatch::self();
00137 connect( dirWatch, SIGNAL( dirty(QString) ),
00138 this, SLOT( updateLocalOntologies() ) );
00139 connect( dirWatch, SIGNAL( created(QString) ),
00140 this, SLOT( updateLocalOntologies() ) );
00141 foreach( const QString& dir, KGlobal::dirs()->findDirs( "data", QString() ) ) {
00142
00143 kDebug() << "watching" << ( dir + "nepomuk/ontologies/" );
00144 dirWatch->addDir( dir + "nepomuk/ontologies/", KDirWatch::WatchFiles );
00145 }
00146 }
00147
00148
00149 Nepomuk::OntologyLoader::~OntologyLoader()
00150 {
00151 delete d;
00152 }
00153
00154
00155 void Nepomuk::OntologyLoader::updateLocalOntologies()
00156 {
00157 d->desktopFilesToUpdate = KGlobal::dirs()->findAllResources( "data", "nepomuk/ontologies/*.desktop" );
00158 d->updateTimer.start(0);
00159 }
00160
00161
00162 void Nepomuk::OntologyLoader::updateAllLocalOntologies()
00163 {
00164 d->forceOntologyUpdate = true;
00165 updateLocalOntologies();
00166 }
00167
00168
00169 void Nepomuk::OntologyLoader::updateNextOntology()
00170 {
00171 if( !d->desktopFilesToUpdate.isEmpty() ) {
00172 d->updateOntology( d->desktopFilesToUpdate.takeFirst() );
00173 }
00174 else {
00175 d->forceOntologyUpdate = false;
00176 d->updateTimer.stop();
00177 }
00178 }
00179
00180
00181 QString Nepomuk::OntologyLoader::findOntologyContext( const QString& uri )
00182 {
00183 return QString::fromAscii( d->model->findOntologyContext( QUrl::fromEncoded( uri.toAscii() ) ).toEncoded() );
00184 }
00185
00186
00187 void Nepomuk::OntologyLoader::importOntology( const QString& url )
00188 {
00189 connect( GraphRetriever::retrieve( url ), SIGNAL( result( KJob* ) ),
00190 this, SLOT( slotGraphRetrieverResult( KJob* ) ) );
00191 }
00192
00193
00194 void Nepomuk::OntologyLoader::slotGraphRetrieverResult( KJob* job )
00195 {
00196 GraphRetriever* graphRetriever = static_cast<GraphRetriever*>( job );
00197 if ( job->error() ) {
00198 emit ontologyUpdateFailed( QString::fromAscii( graphRetriever->url().toEncoded() ), graphRetriever->errorString() );
00199 }
00200 else {
00201
00202
00203 if ( d->model->updateOntology( graphRetriever->statements(), graphRetriever->url() ) ) {
00204 emit ontologyUpdated( QString::fromAscii( graphRetriever->url().toEncoded() ) );
00205 }
00206 else {
00207 emit ontologyUpdateFailed( QString::fromAscii( graphRetriever->url().toEncoded() ), d->model->lastError().message() );
00208 }
00209 }
00210 }
00211
00212 #include "ontologyloader.moc"