• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

NepomukDaemons

ontologyloader.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE Project
00002    Copyright (c) 2007 Sebastian Trueg <trueg@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016    Boston, MA 02110-1301, USA.
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     // FIXME: install watches for changed or newly installed ontologies
00074 //    QStringList dirs = KGlobal::dirs()->findDirs( "data", "nepomuk/ontologies" );
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     // update all installed ontologies
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     // only update if the modification date of the ontology file changed (not the desktop file).
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"

NepomukDaemons

Skip menu "NepomukDaemons"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • KCMShell
  • KNotify
  • KStyles
  • Nepomuk Daemons
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal