From 5cf40dba79301e2d54d63c8da4607325159f26b6 Mon Sep 17 00:00:00 2001 From: Peter Penz Date: Sun, 6 May 2012 21:49:47 +0200 Subject: Remove kde-runtime dependency "nepomukdatamanagement" Follow the suggestion from Vishesh Handa and copy the ResourceWatcher code from kde-runtime instead of creating a dependency. This will of course get removed again as soon as Nepomuk's resource watcher will get a public API. --- src/CMakeLists.txt | 11 +- src/kitemviews/kfileitemmodelrolesupdater.cpp | 2 +- src/kitemviews/private/knepomukresourcewatcher.h | 288 ------------------- src/kitemviews/private/nepomuk/ReadMe.txt | 3 + .../nepomuk/org.kde.nepomuk.ResourceWatcher.xml | 14 + .../org.kde.nepomuk.ResourceWatcherConnection.xml | 65 +++++ src/kitemviews/private/nepomuk/resourcewatcher.cpp | 299 +++++++++++++++++++ src/kitemviews/private/nepomuk/resourcewatcher.h | 319 +++++++++++++++++++++ 8 files changed, 710 insertions(+), 291 deletions(-) delete mode 100644 src/kitemviews/private/knepomukresourcewatcher.h create mode 100644 src/kitemviews/private/nepomuk/ReadMe.txt create mode 100644 src/kitemviews/private/nepomuk/org.kde.nepomuk.ResourceWatcher.xml create mode 100644 src/kitemviews/private/nepomuk/org.kde.nepomuk.ResourceWatcherConnection.xml create mode 100644 src/kitemviews/private/nepomuk/resourcewatcher.cpp create mode 100644 src/kitemviews/private/nepomuk/resourcewatcher.h (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3a06d6093..c00d29c20 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -77,7 +77,16 @@ if (Nepomuk_FOUND) set(dolphinprivate_LIB_SRCS ${dolphinprivate_LIB_SRCS} kitemviews/private/knepomukrolesprovider.cpp + kitemviews/private/nepomuk/resourcewatcher.cpp ) + + qt4_add_dbus_interface(dolphinprivate_LIB_SRCS + kitemviews/private/nepomuk/org.kde.nepomuk.ResourceWatcherConnection.xml + resourcewatcherconnectioninterface) + + qt4_add_dbus_interface(dolphinprivate_LIB_SRCS + kitemviews/private/nepomuk/org.kde.nepomuk.ResourceWatcher.xml + resourcewatchermanagerinterface) endif (Nepomuk_FOUND) kde4_add_kcfg_files(dolphinprivate_LIB_SRCS @@ -103,7 +112,6 @@ if (Nepomuk_FOUND) dolphinprivate ${NEPOMUK_LIBRARIES} ${NEPOMUK_QUERY_LIBRARIES} - nepomukdatamanagement nepomukutils ${SOPRANO_LIBRARIES} ) @@ -219,7 +227,6 @@ if (Nepomuk_FOUND) ${NEPOMUK_LIBRARIES} ${SOPRANO_LIBRARIES} ${NEPOMUK_QUERY_LIBRARIES} - nepomukdatamanagement nepomukutils ) endif (Nepomuk_FOUND) diff --git a/src/kitemviews/kfileitemmodelrolesupdater.cpp b/src/kitemviews/kfileitemmodelrolesupdater.cpp index 9e94d1191..a1773840d 100644 --- a/src/kitemviews/kfileitemmodelrolesupdater.cpp +++ b/src/kitemviews/kfileitemmodelrolesupdater.cpp @@ -39,7 +39,7 @@ #ifdef HAVE_NEPOMUK #include "private/knepomukrolesprovider.h" - #include "private/knepomukresourcewatcher.h" + #include "private/nepomuk/resourcewatcher.h" #endif // Required includes for subItemsCount(): diff --git a/src/kitemviews/private/knepomukresourcewatcher.h b/src/kitemviews/private/knepomukresourcewatcher.h deleted file mode 100644 index 3f6643fc8..000000000 --- a/src/kitemviews/private/knepomukresourcewatcher.h +++ /dev/null @@ -1,288 +0,0 @@ -/* - This file is part of the Nepomuk KDE project. - Copyright (C) 2011 Vishesh Handa - Copyright (C) 2011 Sebastian Trueg - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -*/ - - -#ifndef RESOURCEWATCHER_H -#define RESOURCEWATCHER_H - -#include -#include -#include - -#include -#include - -#include "knepomukdatamanagement_export.h" - -namespace Nepomuk { - - /** - * \class ResourceWatcher resourcewatcher.h - * - * \brief Selectively monitor the nepomuk repository for changes. - * - * Resources may be monitored on the basis of types, properties, and uris. - * - * Changes may be monitored in one of the following ways: - * -# By resources - - * Specify the exact resources that should be watched. Any changes made to the specified resources - * (Excluding \ref nepomuk_dms_metadata) will be notified through the propertyAdded() and propertyRemoved() - * signals. Notifications will also be sent if any of the watched resources is deleted. - * -# By resources and properties - - * Specify the exact resources and their properties. Any changes made to the specified resources - * which touch one of the specified properties will be notified through the propertyAdded() and propertyRemoved() - * signals. - * -# By types - - * Specific types may be specified via add/setType. If types are set, then notifications will be - * sent for all new resources of that type. This includes property changes and resource creation and removal. - * TODO: add flags that allow to only watch for resource creation and removal. - * -# By types and properties - - * Both the types and properties may be specified. Notifications will be sent for property changes - * in resource with the specified types. - * - * \section nepomuk_rw_examples Resource Watcher Usage Example - * - * The following code creates a new ResourceWatcher, configures it to listen to changes on the \c nmm:performer - * property on one specific resource \c res. - * - * \code - * Nepomuk::ResourceWatcher* watcher = new Nepomuk::ResourceWatcher(this); - * watcher->addResource(res); - * watcher->addProperty(NMM:performer()); - * connect(watcher, SIGNAL(propertyAdded(Nepomuk::Resource, Nepomuk::Types::Property, QVariant)), - * this, SLOT(slotPropertyChanged())); - * connect(watcher, SIGNAL(propertyRemoved(Nepomuk::Resource, Nepomuk::Types::Property, QVariant)), - * this, SLOT(slotPropertyChanged())); - * rwatcher->start(); - * \endcode - * - * \author Vishesh Handa , Sebastian Trueg - * - * \ingroup nepomuk_datamanagement - */ - class NEPOMUK_DATA_MANAGEMENT_EXPORT ResourceWatcher : public QObject - { - Q_OBJECT - - public: - /** - * \brief Create a new %ResourceWatcher instance. - * - * This instance will not emit any signals before it has been configured - * and started. - */ - ResourceWatcher( QObject* parent = 0 ); - - /** - * \brief Destructor. - */ - virtual ~ResourceWatcher(); - - /** - * \brief Add a type to be watched. - * - * Every resource of this type will be watched for changes. - * - * \sa setTypes() - */ - void addType( const Types::Class & type ); - - /** - * \brief Add a resource to be watched. - * - * Every change to this resource will be - * signalled, depending on the configured properties(). - * - * \sa setResources() - */ - void addResource( const Nepomuk::Resource & res ); - - /** - * \brief Add a property to be watched. - * - * Every change to a value of this property - * will be signalled, depending on the configured resources() or types(). - * - * \sa setProperties() - */ - void addProperty( const Types::Property & property ); - - /** - * \brief Set the types to be watched. - * - * Every resource having one of these types will be watched for changes. - * - * \sa addType() - */ - void setTypes( const QList & types_ ); - - /** - * \brief Set the resources to be watched. - * - * Every change to one of these resources will be - * signalled, depending on the configured properties(). - * - * \sa addResource() - */ - void setResources( const QList & resources_ ); - - /** - * \brief Set the properties to be watched. - * - * Every change to a value of any of these properties - * will be signalled, depending on the configured resources() or types(). - * - * \sa addProperty() - */ - void setProperties( const QList & properties_ ); - - /** - * \brief The types that have been configured via addType() and setTypes(). - * - * Every resource having one of these types will be watched - * for changes. - */ - QList types() const; - - /** - * \brief The resources that have been configured via addResource() and setResources(). - * - * Every change to one of these resources will be - * signalled, depending on the configured properties(). - */ - QList resources() const; - - /** - * \brief The properties that have been configured via addProperty() and setProperties(). - * - * Every change to a value of any of these properties - * will be signalled, depending on the configured resources() or types(). - */ - QList properties() const; - - public Q_SLOTS: - /** - * \brief Start the signalling of changes. - * - * Before calling this method no signal will be emitted. In - * combination with stop() this allows to suspend the watching. - * Calling start() multiple times has no effect. - */ - bool start(); - - /** - * \brief Stop the signalling of changes. - * - * Allows to stop the watcher which has been started - * via start(). Calling stop() multiple times has no effect. - */ - void stop(); - - Q_SIGNALS: - /** - * \brief This signal is emitted when a new resource is created. - * \param resource The newly created resource. - * \param types The types the new resource has. If types() have been configured this list will always - * contain one of the configured types. - */ - void resourceCreated( const Nepomuk::Resource & resource, const QList& types ); //FIXME: Use either Resource or uri, not a mix - - /** - * \brief This signal is emitted when a resource is deleted. - * \param uri The resource URI of the removed resource. - * \param types The types the removed resource had. If types() have been configured this list will always - * contain one of the configured types. - */ - void resourceRemoved( const QUrl & uri, const QList& types ); - - /** - * \brief This signal is emitted when a type has been added to a resource. This does not include creation which - * is signalled via resourceCreated(). It only applies to changes in a resource's types. - * \param res The changed resource. - * \param type The newly added type. If types() have been configured it will be one of them. - */ - void resourceTypeAdded( const Nepomuk::Resource & res, const Types::Class & type ); - - /** - * \brief This signal is emitted when a type has been removed from a resource. - * - * This does not include removal of entire resources which is signalled via resourceRemoved(). - * It only applies to changes in a resource's types. - * \param res The changed resource. - * \param type The removed type. If types() have been configured it will be one of them. - */ - void resourceTypeRemoved( const Nepomuk::Resource & res, const Types::Class & type ); - - /** - * \brief This signal is emitted when a property value is added. - * \param resource The changed resource. - * \param property The property which has a new value. - * \param value The newly added property value. - */ - void propertyAdded( const Nepomuk::Resource & resource, - const Nepomuk::Types::Property & property, - const QVariant & value ); - - /** - * \brief This signal is emitted when a property value is removed. - * \param resource The changed resource. - * \param property The property which was changed. - * \param value The removed property value. - */ - void propertyRemoved( const Nepomuk::Resource & resource, - const Nepomuk::Types::Property & property, - const QVariant & value ); - - /** - * \brief This signal is emitted when a property value is changed. - * - * This signal cannot be emitted for all changes. It doesn't work if a property is first - * removed and then set, cause the Data Mangement Service does not maintain an internal - * cache for the purpose of emitting the propertyChanged signal. - * - * Specially, since one could theoretically take forever between the removal and the - * setting of the property. - * - * \param resource The changed resource. - * \param property The property which was changed. - * \param oldValue The removed property value. - */ - void propertyChanged( const Nepomuk::Resource & resource, - const Nepomuk::Types::Property & property, - const QVariantList & oldValue, - const QVariantList & newValue ); - - private Q_SLOTS: - void slotResourceCreated(const QString& res, const QStringList& types); - void slotResourceRemoved(const QString& res, const QStringList& types); - void slotResourceTypeAdded(const QString& res, const QString& type); - void slotResourceTypeRemoved(const QString& res, const QString& type); - void slotPropertyAdded(const QString& res, const QString& prop, const QDBusVariant& object); - void slotPropertyRemoved(const QString& res, const QString& prop, const QDBusVariant& object); - void slotPropertyChanged(const QString& res, const QString& prop, - const QVariantList & oldObjs, - const QVariantList & newObjs); - private: - class Private; - Private * d; - }; -} - -#endif // RESOURCEWATCHER_H diff --git a/src/kitemviews/private/nepomuk/ReadMe.txt b/src/kitemviews/private/nepomuk/ReadMe.txt new file mode 100644 index 000000000..c070f3090 --- /dev/null +++ b/src/kitemviews/private/nepomuk/ReadMe.txt @@ -0,0 +1,3 @@ +The files in this directory are a copy of kde-runtime/nepomuk. They +are updated manually until the Nepomuk resoure-watcher is made +public in a future release of the Nepomuk API. diff --git a/src/kitemviews/private/nepomuk/org.kde.nepomuk.ResourceWatcher.xml b/src/kitemviews/private/nepomuk/org.kde.nepomuk.ResourceWatcher.xml new file mode 100644 index 000000000..7f414443b --- /dev/null +++ b/src/kitemviews/private/nepomuk/org.kde.nepomuk.ResourceWatcher.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + diff --git a/src/kitemviews/private/nepomuk/org.kde.nepomuk.ResourceWatcherConnection.xml b/src/kitemviews/private/nepomuk/org.kde.nepomuk.ResourceWatcherConnection.xml new file mode 100644 index 000000000..7848343a7 --- /dev/null +++ b/src/kitemviews/private/nepomuk/org.kde.nepomuk.ResourceWatcherConnection.xml @@ -0,0 +1,65 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/kitemviews/private/nepomuk/resourcewatcher.cpp b/src/kitemviews/private/nepomuk/resourcewatcher.cpp new file mode 100644 index 000000000..2dcf567ca --- /dev/null +++ b/src/kitemviews/private/nepomuk/resourcewatcher.cpp @@ -0,0 +1,299 @@ +/* + This file is part of the Nepomuk KDE project. + Copyright (C) 2011 Vishesh Handa + Copyright (C) 2011-2012 Sebastian Trueg + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + + +#include "resourcewatcher.h" +#include "resourcewatcherconnectioninterface.h" +#include "resourcewatchermanagerinterface.h" + +#include + +#include +#include + +#include +#include + +namespace { + QString convertUri(const QUrl& uri) { + return KUrl(uri).url(); + } + + QStringList convertUris(const QList& uris) { + QStringList cs; + foreach(const QUrl& uri, uris) { + cs << convertUri(uri); + } + return cs; + } + + QList convertUris(const QStringList& uris) { + QList us; + foreach(const QString& uri, uris) { + us << KUrl(uri); + } + return us; + } +} + +class Nepomuk::ResourceWatcher::Private { +public: + QList m_types; + QList m_resources; + QList m_properties; + + org::kde::nepomuk::ResourceWatcherConnection * m_connectionInterface; + org::kde::nepomuk::ResourceWatcher * m_watchManagerInterface; +}; + +Nepomuk::ResourceWatcher::ResourceWatcher(QObject* parent) + : QObject(parent), + d(new Private) +{ + d->m_watchManagerInterface + = new org::kde::nepomuk::ResourceWatcher( "org.kde.nepomuk.DataManagement", + "/resourcewatcher", + QDBusConnection::sessionBus() ); + d->m_connectionInterface = 0; +} + +Nepomuk::ResourceWatcher::~ResourceWatcher() +{ + stop(); + delete d; +} + +bool Nepomuk::ResourceWatcher::start() +{ + stop(); + + // + // Convert to list of strings + // + QList uris = convertUris(d->m_resources); + QList props = convertUris(d->m_properties); + QList types_ = convertUris(d->m_types); + + // + // Watch for the RW service to (re-)appear and then re-connect to make sure we always get updates + // We create this watcher even if we fail to connect below. Thus, once the rw service comes up we + // can re-attach. + // + connect(ResourceManager::instance(), SIGNAL(nepomukSystemStarted()), this, SLOT(start())); + + // + // Create the dbus object to watch + // + QDBusPendingReply reply = d->m_watchManagerInterface->watch( uris, props, types_ ); + QDBusObjectPath path = reply.value(); + + if(!path.path().isEmpty()) { + d->m_connectionInterface = new org::kde::nepomuk::ResourceWatcherConnection( "org.kde.nepomuk.DataManagement", + path.path(), + QDBusConnection::sessionBus() ); + connect( d->m_connectionInterface, SIGNAL(propertyAdded(QString,QString,QVariantList)), + this, SLOT(slotPropertyAdded(QString,QString,QVariantList)) ); + connect( d->m_connectionInterface, SIGNAL(propertyRemoved(QString,QString,QVariantList)), + this, SLOT(slotPropertyRemoved(QString,QString,QVariantList)) ); + connect( d->m_connectionInterface, SIGNAL(resourceCreated(QString,QStringList)), + this, SLOT(slotResourceCreated(QString,QStringList)) ); + connect( d->m_connectionInterface, SIGNAL(propertyChanged(QString,QString,QVariantList,QVariantList)), + this, SLOT(slotPropertyChanged(QString,QString,QVariantList,QVariantList)) ); + connect( d->m_connectionInterface, SIGNAL(resourceRemoved(QString,QStringList)), + this, SLOT(slotResourceRemoved(QString,QStringList)) ); + connect( d->m_connectionInterface, SIGNAL(resourceTypesAdded(QString,QStringList)), + this, SLOT(slotResourceTypesAdded(QString,QStringList)) ); + connect( d->m_connectionInterface, SIGNAL(resourceTypesRemoved(QString,QStringList)), + this, SLOT(slotResourceTypesRemoved(QString,QStringList)) ); + + kDebug() << "Successfully connected to watch service"; + return true; + } + else { + kDebug() << "Failed to connect to watch service" << reply.error().message(); + return false; + } +} + +void Nepomuk::ResourceWatcher::stop() +{ + if (d->m_connectionInterface) { + d->m_connectionInterface->close(); + delete d->m_connectionInterface; + d->m_connectionInterface = 0; + } + + disconnect(ResourceManager::instance(), SIGNAL(nepomukSystemStarted()), this, SLOT(start())); +} + +void Nepomuk::ResourceWatcher::addProperty(const Nepomuk::Types::Property& property) +{ + d->m_properties << property.uri(); + if(d->m_connectionInterface) { + d->m_connectionInterface->addProperty(convertUri(property.uri())); + } +} + +void Nepomuk::ResourceWatcher::addResource(const Nepomuk::Resource& res) +{ + d->m_resources << res.resourceUri(); + if(d->m_connectionInterface) { + d->m_connectionInterface->addResource(convertUri(res.resourceUri())); + } +} + +void Nepomuk::ResourceWatcher::addType(const Nepomuk::Types::Class& type) +{ + d->m_types << type.uri(); + if(d->m_connectionInterface) { + d->m_connectionInterface->addType(convertUri(type.uri())); + } +} + +void Nepomuk::ResourceWatcher::removeProperty(const Nepomuk::Types::Property& property) +{ + d->m_properties.removeAll(property.uri()); + if(d->m_connectionInterface) { + d->m_connectionInterface->removeProperty(convertUri(property.uri())); + } +} + +void Nepomuk::ResourceWatcher::removeResource(const Nepomuk::Resource& res) +{ + d->m_resources.removeAll(res.resourceUri()); + if(d->m_connectionInterface) { + d->m_connectionInterface->removeResource(convertUri(res.resourceUri())); + } +} + +void Nepomuk::ResourceWatcher::removeType(const Nepomuk::Types::Class& type) +{ + d->m_types.removeAll(type.uri()); + if(d->m_connectionInterface) { + d->m_connectionInterface->removeType(convertUri(type.uri())); + } +} + +QList< Nepomuk::Types::Property > Nepomuk::ResourceWatcher::properties() const +{ + QList< Nepomuk::Types::Property > props; + foreach(const QUrl& uri, d->m_properties) + props << Types::Property(uri); + return props; +} + +QList Nepomuk::ResourceWatcher::resources() const +{ + QList resources; + foreach(const QUrl& uri, d->m_resources) + resources << Resource::fromResourceUri(uri); + return resources; +} + +QList< Nepomuk::Types::Class > Nepomuk::ResourceWatcher::types() const +{ + QList types; + foreach(const QUrl& uri, d->m_types) + types << Types::Class(uri); + return types; +} + +void Nepomuk::ResourceWatcher::setProperties(const QList< Nepomuk::Types::Property >& properties_) +{ + d->m_properties.clear(); + foreach(const Nepomuk::Types::Property& p, properties_) { + d->m_properties << p.uri(); + } + + if(d->m_connectionInterface) { + d->m_connectionInterface->setProperties(convertUris(d->m_properties)); + } +} + +void Nepomuk::ResourceWatcher::setResources(const QList< Nepomuk::Resource >& resources_) +{ + d->m_resources.clear(); + foreach(const Nepomuk::Resource& res, resources_) { + d->m_resources << res.resourceUri(); + } + + if(d->m_connectionInterface) { + d->m_connectionInterface->setResources(convertUris(d->m_resources)); + } +} + +void Nepomuk::ResourceWatcher::setTypes(const QList< Nepomuk::Types::Class >& types_) +{ + d->m_types.clear(); + foreach(const Nepomuk::Types::Class& t, types_) { + d->m_types << t.uri(); + } + + if(d->m_connectionInterface) { + d->m_connectionInterface->setTypes(convertUris(d->m_types)); + } +} + +void Nepomuk::ResourceWatcher::slotResourceCreated(const QString &res, const QStringList &types) +{ + emit resourceCreated(Nepomuk::Resource::fromResourceUri(KUrl(res)), convertUris(types)); +} + +void Nepomuk::ResourceWatcher::slotResourceRemoved(const QString &res, const QStringList &types) +{ + emit resourceRemoved(KUrl(res), convertUris(types)); +} + +void Nepomuk::ResourceWatcher::slotResourceTypesAdded(const QString &res, const QStringList &types) +{ + foreach(const QString& type, types) { + emit resourceTypeAdded(KUrl(res), KUrl(type)); + } +} + +void Nepomuk::ResourceWatcher::slotResourceTypesRemoved(const QString &res, const QStringList &types) +{ + foreach(const QString& type, types) { + emit resourceTypeRemoved(KUrl(res), KUrl(type)); + } +} + +void Nepomuk::ResourceWatcher::slotPropertyAdded(const QString& res, const QString& prop, const QVariantList &objects) +{ + foreach(const QVariant& v, objects) { + emit propertyAdded( Resource::fromResourceUri(KUrl(res)), Types::Property( KUrl(prop) ), v ); + } +} + +void Nepomuk::ResourceWatcher::slotPropertyRemoved(const QString& res, const QString& prop, const QVariantList &objects) +{ + foreach(const QVariant& v, objects) { + emit propertyRemoved( Resource::fromResourceUri(KUrl(res)), Types::Property( KUrl(prop) ), v ); + } +} + +void Nepomuk::ResourceWatcher::slotPropertyChanged(const QString& res, const QString& prop, const QVariantList& oldObjs, const QVariantList& newObjs) +{ + emit propertyChanged( Resource::fromResourceUri(KUrl(res)), Types::Property( KUrl(prop) ), + oldObjs, newObjs ); +} + +#include "resourcewatcher.moc" + diff --git a/src/kitemviews/private/nepomuk/resourcewatcher.h b/src/kitemviews/private/nepomuk/resourcewatcher.h new file mode 100644 index 000000000..96a75ef94 --- /dev/null +++ b/src/kitemviews/private/nepomuk/resourcewatcher.h @@ -0,0 +1,319 @@ +/* + This file is part of the Nepomuk KDE project. + Copyright (C) 2011 Vishesh Handa + Copyright (C) 2011 Sebastian Trueg + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + + +#ifndef RESOURCEWATCHER_H +#define RESOURCEWATCHER_H + +#include +#include +#include + +#include +#include + +//#include "nepomukdatamanagement_export.h" + +namespace Nepomuk { + + /** + * \class ResourceWatcher resourcewatcher.h + * + * \brief Selectively monitor the nepomuk repository for changes. + * + * Resources may be monitored on the basis of types, properties, and uris. + * + * Changes may be monitored in one of the following ways: + * -# By resources - + * Specify the exact resources that should be watched. Any changes made to the specified resources + * (Excluding \ref nepomuk_dms_metadata) will be notified through the propertyAdded() and propertyRemoved() + * signals. Notifications will also be sent if any of the watched resources is deleted. + * -# By resources and properties - + * Specify the exact resources and their properties. Any changes made to the specified resources + * which touch one of the specified properties will be notified through the propertyAdded() and propertyRemoved() + * signals. + * -# By types - + * Specific types may be specified via add/setType. If types are set, then notifications will be + * sent for all new resources of that type. This includes property changes and resource creation and removal. + * TODO: add flags that allow to only watch for resource creation and removal. + * -# By types and properties - + * Both the types and properties may be specified. Notifications will be sent for property changes + * in resource with the specified types. + * + * \section nepomuk_rw_examples Resource Watcher Usage Example + * + * The following code creates a new ResourceWatcher, configures it to listen to changes on the \c nmm:performer + * property on one specific resource \c res. + * + * \code + * Nepomuk::ResourceWatcher* watcher = new Nepomuk::ResourceWatcher(this); + * watcher->addResource(res); + * watcher->addProperty(NMM:performer()); + * connect(watcher, SIGNAL(propertyAdded(Nepomuk::Resource, Nepomuk::Types::Property, QVariant)), + * this, SLOT(slotPropertyChanged())); + * connect(watcher, SIGNAL(propertyRemoved(Nepomuk::Resource, Nepomuk::Types::Property, QVariant)), + * this, SLOT(slotPropertyChanged())); + * rwatcher->start(); + * \endcode + * + * \author Vishesh Handa , Sebastian Trueg + * + * \ingroup nepomuk_datamanagement + */ + class ResourceWatcher : public QObject + { + Q_OBJECT + + public: + /** + * \brief Create a new %ResourceWatcher instance. + * + * This instance will not emit any signals before it has been configured + * and started. + */ + ResourceWatcher( QObject* parent = 0 ); + + /** + * \brief Destructor. + */ + virtual ~ResourceWatcher(); + + public Q_SLOTS: + /** + * \brief Add a type to be watched. + * + * Every resource of this type will be watched for changes. + * + * \sa setTypes() + */ + void addType( const Types::Class & type ); + + /** + * \brief Add a resource to be watched. + * + * Every change to this resource will be + * signalled, depending on the configured properties(). + * + * \sa setResources() + */ + void addResource( const Nepomuk::Resource & res ); + + /** + * \brief Add a property to be watched. + * + * Every change to a value of this property + * will be signalled, depending on the configured resources() or types(). + * + * \sa setProperties() + */ + void addProperty( const Types::Property & property ); + + /** + * \brief Remove a type to be watched. + * + * Every resource of this type will be watched for changes. + * + * \sa setTypes() + */ + void removeType( const Types::Class & type ); + + /** + * \brief Remove a resource to be watched. + * + * Every change to this resource will be + * signalled, depending on the configured properties(). + * + * \sa setResources() + */ + void removeResource( const Nepomuk::Resource & res ); + + /** + * \brief Remove a property to be watched. + * + * Every change to a value of this property + * will be signalled, depending on the configured resources() or types(). + * + * \sa setProperties() + */ + void removeProperty( const Types::Property & property ); + + /** + * \brief Set the types to be watched. + * + * Every resource having one of these types will be watched for changes. + * + * \sa addType() + */ + void setTypes( const QList & types_ ); + + /** + * \brief Set the resources to be watched. + * + * Every change to one of these resources will be + * signalled, depending on the configured properties(). + * + * \sa addResource() + */ + void setResources( const QList & resources_ ); + + /** + * \brief Set the properties to be watched. + * + * Every change to a value of any of these properties + * will be signalled, depending on the configured resources() or types(). + * + * \sa addProperty() + */ + void setProperties( const QList & properties_ ); + + /** + * \brief The types that have been configured via addType() and setTypes(). + * + * Every resource having one of these types will be watched + * for changes. + */ + QList types() const; + + /** + * \brief The resources that have been configured via addResource() and setResources(). + * + * Every change to one of these resources will be + * signalled, depending on the configured properties(). + */ + QList resources() const; + + /** + * \brief The properties that have been configured via addProperty() and setProperties(). + * + * Every change to a value of any of these properties + * will be signalled, depending on the configured resources() or types(). + */ + QList properties() const; + + /** + * \brief Start the signalling of changes. + * + * Before calling this method no signal will be emitted. In + * combination with stop() this allows to suspend the watching. + * Calling start() multiple times has no effect. + */ + bool start(); + + /** + * \brief Stop the signalling of changes. + * + * Allows to stop the watcher which has been started + * via start(). Calling stop() multiple times has no effect. + */ + void stop(); + + Q_SIGNALS: + /** + * \brief This signal is emitted when a new resource is created. + * \param resource The newly created resource. + * \param types The types the new resource has. If types() have been configured this list will always + * contain one of the configured types. + */ + void resourceCreated( const Nepomuk::Resource & resource, const QList& types ); //FIXME: Use either Resource or uri, not a mix + + /** + * \brief This signal is emitted when a resource is deleted. + * \param uri The resource URI of the removed resource. + * \param types The types the removed resource had. If types() have been configured this list will always + * contain one of the configured types. + */ + void resourceRemoved( const QUrl & uri, const QList& types ); + + /** + * \brief This signal is emitted when a type has been added to a resource. This does not include creation which + * is signalled via resourceCreated(). It only applies to changes in a resource's types. + * \param res The changed resource. + * \param type The newly added type. If types() have been configured it will be one of them. + */ + void resourceTypeAdded( const Nepomuk::Resource & res, const Nepomuk::Types::Class & type ); + + /** + * \brief This signal is emitted when a type has been removed from a resource. + * + * This does not include removal of entire resources which is signalled via resourceRemoved(). + * It only applies to changes in a resource's types. + * \param res The changed resource. + * \param type The removed type. If types() have been configured it will be one of them. + */ + void resourceTypeRemoved( const Nepomuk::Resource & res, const Nepomuk::Types::Class & type ); + + /** + * \brief This signal is emitted when a property value is added. + * \param resource The changed resource. + * \param property The property which has a new value. + * \param value The newly added property value. + */ + void propertyAdded( const Nepomuk::Resource & resource, + const Nepomuk::Types::Property & property, + const QVariant & value ); + + /** + * \brief This signal is emitted when a property value is removed. + * \param resource The changed resource. + * \param property The property which was changed. + * \param value The removed property value. + */ + void propertyRemoved( const Nepomuk::Resource & resource, + const Nepomuk::Types::Property & property, + const QVariant & value ); + + /** + * \brief This signal is emitted when a property value is changed. + * + * This signal is essentially a combination of the propertyAdded and propertyRemoved signals. + * + * Be aware that removing and then adding a property will result in two separate + * propertyChanged signals. They are never combined. + * + * Specially, since one could theoretically take forever between the removal and the + * setting of the property. + * + * \param resource The changed resource. + * \param property The property which was changed. + * \param addedValues The values that have been added. + * \param removedValues The values that have been removed. + */ + void propertyChanged( const Nepomuk::Resource & resource, + const Nepomuk::Types::Property & property, + const QVariantList & addedValues, + const QVariantList & removedValues ); + + private Q_SLOTS: + void slotResourceCreated(const QString& res, const QStringList& types); + void slotResourceRemoved(const QString& res, const QStringList& types); + void slotResourceTypesAdded(const QString& res, const QStringList& types); + void slotResourceTypesRemoved(const QString& res, const QStringList& types); + void slotPropertyAdded(const QString& res, const QString& prop, const QVariantList& objects); + void slotPropertyRemoved(const QString& res, const QString& prop, const QVariantList& objects); + void slotPropertyChanged(const QString& res, const QString& prop, + const QVariantList & oldObjs, + const QVariantList & newObjs); + private: + class Private; + Private * d; + }; +} + +#endif // RESOURCEWATCHER_H -- cgit v1.3