From 5d27eb81cd038d1cf66f21efa24cb8c2ebd52557 Mon Sep 17 00:00:00 2001 From: Peter Penz Date: Fri, 6 Apr 2012 18:03:24 +0200 Subject: [PATCH] Update items if a Nepomuk property has been changed If a Nepomuk property has been changed (e.g. by changing the rating in the Information Panel) the corresponding item in the view must be updated. --- src/CMakeLists.txt | 10 +- src/kitemviews/kfileitemmodelrolesupdater.cpp | 105 ++++++- src/kitemviews/kfileitemmodelrolesupdater.h | 26 +- .../knepomukdatamanagement_export_p.h | 40 +++ src/kitemviews/knepomukresourcewatcher_p.h | 288 ++++++++++++++++++ src/kitemviews/knepomukrolesprovider.cpp | 3 +- src/kitemviews/knepomukrolesprovider_p.h | 12 +- 7 files changed, 465 insertions(+), 19 deletions(-) create mode 100644 src/kitemviews/knepomukdatamanagement_export_p.h create mode 100644 src/kitemviews/knepomukresourcewatcher_p.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9f9d38653c..55c657dceb 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -88,7 +88,14 @@ kde4_add_library(dolphinprivate SHARED ${dolphinprivate_LIB_SRCS}) target_link_libraries(dolphinprivate ${KDE4_KFILE_LIBS} konq ${KDE4_KNEWSTUFF3_LIBS}) if (Nepomuk_FOUND) - target_link_libraries(dolphinprivate ${NEPOMUK_LIBRARIES} ${NEPOMUK_QUERY_LIBRARIES} nepomukutils ${SOPRANO_LIBRARIES}) + target_link_libraries( + dolphinprivate + ${NEPOMUK_LIBRARIES} + ${NEPOMUK_QUERY_LIBRARIES} + nepomukdatamanagement + nepomukutils + ${SOPRANO_LIBRARIES} + ) endif (Nepomuk_FOUND) if(X11_Xrender_FOUND) @@ -198,6 +205,7 @@ 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 efcf6b4ccd..b81a4a1a3c 100644 --- a/src/kitemviews/kfileitemmodelrolesupdater.cpp +++ b/src/kitemviews/kfileitemmodelrolesupdater.cpp @@ -35,6 +35,7 @@ #ifdef HAVE_NEPOMUK #include "knepomukrolesprovider_p.h" + #include "knepomukresourcewatcher_p.h" #endif // Required includes for subItemsCount(): @@ -79,7 +80,8 @@ KFileItemModelRolesUpdater::KFileItemModelRolesUpdater(KFileItemModel* model, QO m_changedItemsTimer(0), m_changedItems() #ifdef HAVE_NEPOMUK - , m_resolveNepomukRoles(false) + , m_nepomukResourceWatcher(0), + m_nepomukUriItems() #endif { @@ -229,19 +231,39 @@ void KFileItemModelRolesUpdater::setRoles(const QSet& roles) #ifdef HAVE_NEPOMUK // Check whether there is at least one role that must be resolved - // with the help of Nepomuk. If this is the case, m_resolveNepomukRoles - // will be set to true and the (quite expensive) resolving will be done - // in KFileItemModelRolesUpdater::rolesData(). + // with the help of Nepomuk. If this is the case, a (quite expensive) + // resolving will be done in KFileItemModelRolesUpdater::rolesData() and + // the role gets watched for changes. const KNepomukRolesProvider& rolesProvider = KNepomukRolesProvider::instance(); - m_resolveNepomukRoles = false; + bool hasNepomukRole = false; QSetIterator it(roles); while (it.hasNext()) { const QByteArray& role = it.next(); if (rolesProvider.isNepomukRole(role)) { - m_resolveNepomukRoles = true; + hasNepomukRole = true; break; } } + + if (hasNepomukRole && !m_nepomukResourceWatcher) { + Q_ASSERT(m_nepomukUriItems.isEmpty()); + + m_nepomukResourceWatcher = new Nepomuk::ResourceWatcher(this); + connect(m_nepomukResourceWatcher, SIGNAL(propertyChanged(Nepomuk::Resource,Nepomuk::Types::Property,QVariantList,QVariantList)), + this, SLOT(applyChangedNepomukRoles(Nepomuk::Resource))); + connect(m_nepomukResourceWatcher, SIGNAL(propertyRemoved(Nepomuk::Resource,Nepomuk::Types::Property,QVariant)), + this, SLOT(applyChangedNepomukRoles(Nepomuk::Resource))); + connect(m_nepomukResourceWatcher, SIGNAL(propertyAdded(Nepomuk::Resource,Nepomuk::Types::Property,QVariant)), + this, SLOT(applyChangedNepomukRoles(Nepomuk::Resource))); + connect(m_nepomukResourceWatcher, SIGNAL(resourceCreated(Nepomuk::Resource,QList)), + this, SLOT(applyChangedNepomukRoles(Nepomuk::Resource))); + connect(m_nepomukResourceWatcher, SIGNAL(resourceRemoved(QUrl,QList)), + this, SLOT(applyChangedNepomukRoles(Nepomuk::Resource))); + } else if (!hasNepomukRole && m_nepomukResourceWatcher) { + delete m_nepomukResourceWatcher; + m_nepomukResourceWatcher = 0; + m_nepomukUriItems.clear(); + } #endif if (m_paused) { @@ -275,6 +297,30 @@ void KFileItemModelRolesUpdater::slotItemsInserted(const KItemRangeList& itemRan void KFileItemModelRolesUpdater::slotItemsRemoved(const KItemRangeList& itemRanges) { Q_UNUSED(itemRanges); + +#ifdef HAVE_NEPOMUK + if (m_nepomukResourceWatcher) { + // Don't let the ResourceWatcher watch for removed items + if (m_model->count() == 0) { + m_nepomukResourceWatcher->setResources(QList()); + m_nepomukUriItems.clear(); + } else { + QList newResources; + const QList oldResources = m_nepomukResourceWatcher->resources(); + foreach (const Nepomuk::Resource& resource, oldResources) { + const QUrl uri = resource.resourceUri(); + const KUrl itemUrl = m_nepomukUriItems.value(uri); + if (m_model->index(itemUrl) >= 0) { + newResources.append(resource); + } else { + m_nepomukUriItems.remove(uri); + } + } + m_nepomukResourceWatcher->setResources(newResources); + } + } +#endif + m_firstVisibleIndex = 0; m_lastVisibleIndex = -1; if (!hasPendingRoles()) { @@ -448,6 +494,31 @@ void KFileItemModelRolesUpdater::resolveChangedItems() startUpdating(itemRanges); } +void KFileItemModelRolesUpdater::applyChangedNepomukRoles(const Nepomuk::Resource& resource) +{ +#ifdef HAVE_NEPOMUK + const KUrl itemUrl = m_nepomukUriItems.value(resource.resourceUri()); + const KFileItem item = m_model->fileItem(itemUrl); + QHash data = rolesData(item); + + const KNepomukRolesProvider& rolesProvider = KNepomukRolesProvider::instance(); + QHashIterator it(rolesProvider.roleValues(resource, m_roles)); + while (it.hasNext()) { + it.next(); + data.insert(it.key(), it.value()); + } + + disconnect(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet)), + this, SLOT(slotItemsChanged(KItemRangeList,QSet))); + const int index = m_model->index(item); + m_model->setData(index, data); + connect(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet)), + this, SLOT(slotItemsChanged(KItemRangeList,QSet))); +#else + Q_UNUSED(resource); +#endif +} + void KFileItemModelRolesUpdater::startUpdating(const KItemRangeList& itemRanges) { // If no valid index range is given assume that all items are visible. @@ -780,13 +851,31 @@ QHash KFileItemModelRolesUpdater::rolesData(const KFileIte data.insert("iconOverlays", item.overlays()); #ifdef HAVE_NEPOMUK - if (m_resolveNepomukRoles) { + if (m_nepomukResourceWatcher) { const KNepomukRolesProvider& rolesProvider = KNepomukRolesProvider::instance(); - QHashIterator it(rolesProvider.roleValues(item.url(), m_roles)); + Nepomuk::Resource resource(item.url()); + QHashIterator it(rolesProvider.roleValues(resource, m_roles)); while (it.hasNext()) { it.next(); data.insert(it.key(), it.value()); } + + QUrl uri = resource.resourceUri(); + if (uri.isEmpty()) { + // TODO: Is there another way to explicitly create a resource? + // We need a resource to be able to track it for changes. + resource.setRating(0); + uri = resource.resourceUri(); + } + if (!uri.isEmpty() && !m_nepomukUriItems.contains(uri)) { + // TODO: Calling stop()/start() is a workaround until + // ResourceWatcher has been fixed. + m_nepomukResourceWatcher->stop(); + m_nepomukResourceWatcher->addResource(resource); + m_nepomukResourceWatcher->start(); + + m_nepomukUriItems.insert(uri, item.url()); + } } #endif diff --git a/src/kitemviews/kfileitemmodelrolesupdater.h b/src/kitemviews/kfileitemmodelrolesupdater.h index 47255b03fe..d228675114 100644 --- a/src/kitemviews/kfileitemmodelrolesupdater.h +++ b/src/kitemviews/kfileitemmodelrolesupdater.h @@ -21,11 +21,12 @@ #define KFILEITEMMODELROLESUPDATER_H #include -#include #include #include +#include + #include #include #include @@ -36,6 +37,21 @@ class KJob; class QPixmap; class QTimer; +#ifdef HAVE_NEPOMUK + namespace Nepomuk + { + class ResourceWatcher; + class Resource; + } +#else + // Required for the slot applyChangedNepomukRoles() that + // cannot be ifdefined due to moc. + namespace Nepomuk + { + class Resource; + } +#endif + /** * @brief Resolves expensive roles asynchronously and applies them to the KFileItemModel. * @@ -133,6 +149,8 @@ private slots: */ void resolveChangedItems(); + void applyChangedNepomukRoles(const Nepomuk::Resource& resource); + private: /** * Updates the roles for the given item ranges. The roles for the currently @@ -207,11 +225,9 @@ private: QSet m_changedItems; #ifdef HAVE_NEPOMUK - // True if roles must be resolved with the help of Nepomuk inside - // KFileItemModelRolesUpdater::rolesData(). - bool m_resolveNepomukRoles; + Nepomuk::ResourceWatcher* m_nepomukResourceWatcher; + mutable QHash m_nepomukUriItems; #endif - }; #endif diff --git a/src/kitemviews/knepomukdatamanagement_export_p.h b/src/kitemviews/knepomukdatamanagement_export_p.h new file mode 100644 index 0000000000..929a737c97 --- /dev/null +++ b/src/kitemviews/knepomukdatamanagement_export_p.h @@ -0,0 +1,40 @@ +/* This file is part of the KDE project + Copyright (C) 2007 David Faure + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef NEPOMUKDATAMANAGEMENT_EXPORT_H +#define NEPOMUKDATAMANAGEMENT_EXPORT_H + +/* needed for KDE_EXPORT and KDE_IMPORT macros */ +#include + +#ifndef NEPOMUK_DATA_MANAGEMENT_EXPORT +# if defined(MAKE_NEPOMUKDATAMANAGEMENT_LIB) + /* We are building this library */ +# define NEPOMUK_DATA_MANAGEMENT_EXPORT KDE_EXPORT +# else + /* We are using this library */ +# define NEPOMUK_DATA_MANAGEMENT_EXPORT KDE_IMPORT +# endif +#endif + +# ifndef NEPOMUK_DATA_MANAGEMENT_EXPORT_DEPRECATED +# define NEPOMUK_DATA_MANAGEMENT_EXPORT_DEPRECATED KDE_DEPRECATED NEPOMUK_DATA_MANAGEMENT_EXPORT +# endif + +#endif diff --git a/src/kitemviews/knepomukresourcewatcher_p.h b/src/kitemviews/knepomukresourcewatcher_p.h new file mode 100644 index 0000000000..9b4b8b71d2 --- /dev/null +++ b/src/kitemviews/knepomukresourcewatcher_p.h @@ -0,0 +1,288 @@ +/* + 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_p.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/knepomukrolesprovider.cpp b/src/kitemviews/knepomukrolesprovider.cpp index 1612d57b63..a1b4205e83 100644 --- a/src/kitemviews/knepomukrolesprovider.cpp +++ b/src/kitemviews/knepomukrolesprovider.cpp @@ -48,10 +48,9 @@ bool KNepomukRolesProvider::isNepomukRole(const QByteArray& role) const return m_roles.contains(role); } -QHash KNepomukRolesProvider::roleValues(const QUrl& url, +QHash KNepomukRolesProvider::roleValues(const Nepomuk::Resource& resource, const QSet& roles) const { - const Nepomuk::Resource resource(url); if (!resource.isValid()) { return QHash(); } diff --git a/src/kitemviews/knepomukrolesprovider_p.h b/src/kitemviews/knepomukrolesprovider_p.h index 7955093330..ca5e186ba7 100644 --- a/src/kitemviews/knepomukrolesprovider_p.h +++ b/src/kitemviews/knepomukrolesprovider_p.h @@ -20,11 +20,16 @@ #ifndef KNEPOMUKROLESPROVIDER_H #define KNEPOMUKROLESPROVIDER_H +#include + #include #include #include -class QUrl; +namespace Nepomuk +{ + class Resource; +} /** * @brief Allows accessing metadata of a file by providing KFileItemModel roles. @@ -32,7 +37,7 @@ class QUrl; * Is a helper class for KFileItemModelRolesUpdater to retrieve roles that * are only accessible with Nepomuk. */ -class KNepomukRolesProvider +class LIBDOLPHINPRIVATE_EXPORT KNepomukRolesProvider { public: static KNepomukRolesProvider& instance(); @@ -47,7 +52,8 @@ public: * @return Values for the roles \a roles that can be determined from the file * with the URL \a url. */ - QHash roleValues(const QUrl& url, const QSet& roles) const; + QHash roleValues(const Nepomuk::Resource& resource, + const QSet& roles) const; protected: KNepomukRolesProvider();