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
00022 #include <Soprano/Global>
00023 #include <Soprano/Node>
00024 #include <Soprano/Model>
00025 #include <Soprano/PluginManager>
00026 #include <Soprano/StatementIterator>
00027 #include <Soprano/Parser>
00028
00029 #include <KDesktopFile>
00030 #include <KConfigGroup>
00031 #include <KDebug>
00032 #include <KGlobal>
00033 #include <KStandardDirs>
00034
00035 #include <QtCore/QFileInfo>
00036 #include <QtCore/QTimer>
00037
00038 #include <kpluginfactory.h>
00039 #include <kpluginloader.h>
00040
00041 NEPOMUK_EXPORT_SERVICE( Nepomuk::OntologyLoader, "nepomukontologyloader")
00042
00043
00044 using namespace Soprano;
00045
00046 class Nepomuk::OntologyLoader::Private
00047 {
00048 public:
00049 Private( OntologyLoader* p )
00050 : q( p ) {
00051 }
00052
00053 OntologyManagerModel* model;
00054
00055 QTimer updateTimer;
00056 QStringList desktopFilesToUpdate;
00057
00058 void updateOntology( const QString& filename );
00059
00060 private:
00061 OntologyLoader* q;
00062 };
00063
00064
00065 Nepomuk::OntologyLoader::OntologyLoader( QObject* parent, const QList<QVariant>& )
00066 : Service( parent ),
00067 d( new Private(this) )
00068 {
00069 d->model = new OntologyManagerModel( mainModel(), this );
00070 connect( &d->updateTimer, SIGNAL(timeout()), this, SLOT(updateNextOntology()) );
00071 updateAllOntologies();
00072
00073
00074
00075 }
00076
00077
00078 Nepomuk::OntologyLoader::~OntologyLoader()
00079 {
00080 delete d;
00081 }
00082
00083
00084 void Nepomuk::OntologyLoader::updateAllOntologies()
00085 {
00086 if ( !d->model ) {
00087 kDebug() << "No Nepomuk Model. Cannot update ontologies.";
00088 return;
00089 }
00090
00091
00092 d->desktopFilesToUpdate = KGlobal::dirs()->findAllResources( "data", "nepomuk/ontologies/*.desktop" );
00093 d->updateTimer.start(0);
00094 }
00095
00096
00097 void Nepomuk::OntologyLoader::updateNextOntology()
00098 {
00099 if( !d->desktopFilesToUpdate.isEmpty() ) {
00100 d->updateOntology( d->desktopFilesToUpdate.takeFirst() );
00101 }
00102 else {
00103 d->updateTimer.stop();
00104 }
00105 }
00106
00107
00108 void Nepomuk::OntologyLoader::Private::updateOntology( const QString& filename )
00109 {
00110 KDesktopFile df( filename );
00111
00112
00113
00114 QFileInfo ontoFileInf( df.readPath() );
00115 QDateTime ontoLastModified = model->ontoModificationDate( df.readUrl() );
00116 if ( ontoLastModified < ontoFileInf.lastModified() ) {
00117
00118 kDebug() << "Ontology" << df.readUrl() << "needs updating.";
00119
00120 QString mimeType = df.desktopGroup().readEntry( "MimeType", QString() );
00121
00122 const Soprano::Parser* parser
00123 = Soprano::PluginManager::instance()->discoverParserForSerialization( Soprano::mimeTypeToSerialization( mimeType ),
00124 mimeType );
00125 if ( !parser ) {
00126 kDebug() << "No parser to handle" << df.readName() << "(" << mimeType << ")";
00127 return;
00128 }
00129
00130 kDebug() << "Parsing" << df.readPath();
00131
00132 model->updateOntology( parser->parseFile( df.readPath(),
00133 df.readUrl(),
00134 Soprano::mimeTypeToSerialization( mimeType ),
00135 mimeType ),
00136 df.readUrl() );
00137 }
00138 else {
00139 kDebug() << "Ontology" << df.readUrl() << "up to date.";
00140 }
00141 }
00142
00143 #include "ontologyloader.moc"