diff --git a/src/core/agentmanager.cpp b/src/core/agentmanager.cpp index eecea8e02..abff30488 100644 --- a/src/core/agentmanager.cpp +++ b/src/core/agentmanager.cpp @@ -1,427 +1,427 @@ /* Copyright (c) 2006-2008 Tobias Koenig 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. */ #include "agentmanager.h" #include "agentmanager_p.h" #include "vectorhelper.h" #include "agenttype_p.h" #include "agentinstance_p.h" #include "KDBusConnectionPool" #include "servermanager.h" #include "collection.h" #include #include using namespace Akonadi; // @cond PRIVATE AgentInstance AgentManagerPrivate::createInstance(const AgentType &type) { const QString &identifier = mManager->createAgentInstance(type.identifier()); if (identifier.isEmpty()) { return AgentInstance(); } return fillAgentInstanceLight(identifier); } void AgentManagerPrivate::agentTypeAdded(const QString &identifier) { // Ignore agent types we already know about, for example because we called // readAgentTypes before. if (mTypes.contains(identifier)) { return; } if (mTypes.isEmpty()) { // The Akonadi ServerManager assumes that the server is up and running as soon // as it knows about at least one agent type. // If we emit the typeAdded() signal here, it therefore thinks the server is // running. However, the AgentManager does not know about all agent types yet, // as the server might still have pending agentTypeAdded() signals, even though // it internally knows all agent types already. // This can cause situations where the client gets told by the ServerManager that // the server is running, yet the client will encounter an error because the // AgentManager doesn't know all types yet. // // Therefore, we read all agent types from the server here so they are known. readAgentTypes(); } const AgentType type = fillAgentType(identifier); if (type.isValid()) { mTypes.insert(identifier, type); emit mParent->typeAdded(type); } } void AgentManagerPrivate::agentTypeRemoved(const QString &identifier) { if (!mTypes.contains(identifier)) { return; } const AgentType type = mTypes.take(identifier); emit mParent->typeRemoved(type); } void AgentManagerPrivate::agentInstanceAdded(const QString &identifier) { const AgentInstance instance = fillAgentInstance(identifier); if (instance.isValid()) { // It is possible that this function is called when the instance is already // in our list we filled initially in the constructor. // This happens when the constructor is called during Akonadi startup, when // the agent processes are not fully loaded and have no D-Bus interface yet. // The server-side agent manager then emits the instance added signal when // the D-Bus interface for the agent comes up. // In this case, we simply notify that the instance status has changed. const bool newAgentInstance = !mInstances.contains(identifier); if (newAgentInstance) { mInstances.insert(identifier, instance); emit mParent->instanceAdded(instance); } else { mInstances.remove(identifier); mInstances.insert(identifier, instance); emit mParent->instanceStatusChanged(instance); } } } void AgentManagerPrivate::agentInstanceRemoved(const QString &identifier) { if (!mInstances.contains(identifier)) { return; } const AgentInstance instance = mInstances.take(identifier); emit mParent->instanceRemoved(instance); } void AgentManagerPrivate::agentInstanceStatusChanged(const QString &identifier, int status, const QString &msg) { if (!mInstances.contains(identifier)) { return; } AgentInstance &instance = mInstances[identifier]; instance.d->mStatus = status; instance.d->mStatusMessage = msg; emit mParent->instanceStatusChanged(instance); } void AgentManagerPrivate::agentInstanceProgressChanged(const QString &identifier, uint progress, const QString &msg) { if (!mInstances.contains(identifier)) { return; } AgentInstance &instance = mInstances[identifier]; instance.d->mProgress = progress; if (!msg.isEmpty()) { instance.d->mStatusMessage = msg; } emit mParent->instanceProgressChanged(instance); } void AgentManagerPrivate::agentInstanceWarning(const QString &identifier, const QString &msg) { if (!mInstances.contains(identifier)) { return; } AgentInstance &instance = mInstances[identifier]; emit mParent->instanceWarning(instance, msg); } void AgentManagerPrivate::agentInstanceError(const QString &identifier, const QString &msg) { if (!mInstances.contains(identifier)) { return; } AgentInstance &instance = mInstances[identifier]; emit mParent->instanceError(instance, msg); } void AgentManagerPrivate::agentInstanceOnlineChanged(const QString &identifier, bool state) { if (!mInstances.contains(identifier)) { return; } AgentInstance &instance = mInstances[identifier]; instance.d->mIsOnline = state; emit mParent->instanceOnline(instance, state); } void AgentManagerPrivate::agentInstanceNameChanged(const QString &identifier, const QString &name) { if (!mInstances.contains(identifier)) { return; } AgentInstance &instance = mInstances[identifier]; instance.d->mName = name; emit mParent->instanceNameChanged(instance); } void AgentManagerPrivate::readAgentTypes() { const QDBusReply types = mManager->agentTypes(); if (types.isValid()) { const QStringList lst = types.value(); for (const QString &type : lst) { const AgentType agentType = fillAgentType(type); if (agentType.isValid()) { mTypes.insert(type, agentType); emit mParent->typeAdded(agentType); } } } } void AgentManagerPrivate::readAgentInstances() { const QDBusReply instances = mManager->agentInstances(); if (instances.isValid()) { const QStringList lst = instances.value(); for (const QString &instance : lst) { const AgentInstance agentInstance = fillAgentInstance(instance); if (agentInstance.isValid()) { mInstances.insert(instance, agentInstance); emit mParent->instanceAdded(agentInstance); } } } } AgentType AgentManagerPrivate::fillAgentType(const QString &identifier) const { AgentType type; type.d->mIdentifier = identifier; type.d->mName = mManager->agentName(identifier); type.d->mDescription = mManager->agentComment(identifier); type.d->mIconName = mManager->agentIcon(identifier); type.d->mMimeTypes = mManager->agentMimeTypes(identifier); type.d->mCapabilities = mManager->agentCapabilities(identifier); type.d->mCustomProperties = mManager->agentCustomProperties(identifier); return type; } void AgentManagerPrivate::setName(const AgentInstance &instance, const QString &name) { mManager->setAgentInstanceName(instance.identifier(), name); } void AgentManagerPrivate::setOnline(const AgentInstance &instance, bool state) { mManager->setAgentInstanceOnline(instance.identifier(), state); } void AgentManagerPrivate::configure(const AgentInstance &instance, QWidget *parent) { qlonglong winId = 0; if (parent) { winId = (qlonglong)(parent->window()->winId()); } mManager->agentInstanceConfigure(instance.identifier(), winId); } void AgentManagerPrivate::synchronize(const AgentInstance &instance) { mManager->agentInstanceSynchronize(instance.identifier()); } void AgentManagerPrivate::synchronizeCollectionTree(const AgentInstance &instance) { mManager->agentInstanceSynchronizeCollectionTree(instance.identifier()); } AgentInstance AgentManagerPrivate::fillAgentInstance(const QString &identifier) const { AgentInstance instance; const QString agentTypeIdentifier = mManager->agentInstanceType(identifier); if (!mTypes.contains(agentTypeIdentifier)) { return instance; } instance.d->mType = mTypes.value(agentTypeIdentifier); instance.d->mIdentifier = identifier; instance.d->mName = mManager->agentInstanceName(identifier); instance.d->mStatus = mManager->agentInstanceStatus(identifier); instance.d->mStatusMessage = mManager->agentInstanceStatusMessage(identifier); instance.d->mProgress = mManager->agentInstanceProgress(identifier); instance.d->mIsOnline = mManager->agentInstanceOnline(identifier); return instance; } AgentInstance AgentManagerPrivate::fillAgentInstanceLight(const QString &identifier) const { AgentInstance instance; const QString agentTypeIdentifier = mManager->agentInstanceType(identifier); Q_ASSERT_X(mTypes.contains(agentTypeIdentifier), "fillAgentInstanceLight", "Requests non-existing agent type"); instance.d->mType = mTypes.value(agentTypeIdentifier); instance.d->mIdentifier = identifier; return instance; } void AgentManagerPrivate::serviceOwnerChanged(const QString &, const QString &oldOwner, const QString &) { if (oldOwner.isEmpty()) { if (mTypes.isEmpty()) { // just to be safe readAgentTypes(); } if (mInstances.isEmpty()) { readAgentInstances(); } } } void AgentManagerPrivate::createDBusInterface() { mTypes.clear(); mInstances.clear(); delete mManager; mManager = new org::freedesktop::Akonadi::AgentManager(ServerManager::serviceName(ServerManager::Control), QStringLiteral("/AgentManager"), KDBusConnectionPool::threadConnection(), mParent); QObject::connect(mManager, SIGNAL(agentTypeAdded(QString)), mParent, SLOT(agentTypeAdded(QString))); QObject::connect(mManager, SIGNAL(agentTypeRemoved(QString)), mParent, SLOT(agentTypeRemoved(QString))); QObject::connect(mManager, SIGNAL(agentInstanceAdded(QString)), mParent, SLOT(agentInstanceAdded(QString))); QObject::connect(mManager, SIGNAL(agentInstanceRemoved(QString)), mParent, SLOT(agentInstanceRemoved(QString))); QObject::connect(mManager, SIGNAL(agentInstanceStatusChanged(QString,int,QString)), mParent, SLOT(agentInstanceStatusChanged(QString,int,QString))); QObject::connect(mManager, SIGNAL(agentInstanceProgressChanged(QString,uint,QString)), mParent, SLOT(agentInstanceProgressChanged(QString,uint,QString))); QObject::connect(mManager, SIGNAL(agentInstanceNameChanged(QString,QString)), mParent, SLOT(agentInstanceNameChanged(QString,QString))); QObject::connect(mManager, SIGNAL(agentInstanceWarning(QString,QString)), mParent, SLOT(agentInstanceWarning(QString,QString))); QObject::connect(mManager, SIGNAL(agentInstanceError(QString,QString)), mParent, SLOT(agentInstanceError(QString,QString))); QObject::connect(mManager, SIGNAL(agentInstanceOnlineChanged(QString,bool)), mParent, SLOT(agentInstanceOnlineChanged(QString,bool))); if (mManager->isValid()) { readAgentTypes(); readAgentInstances(); } } AgentManager *AgentManagerPrivate::mSelf = nullptr; AgentManager::AgentManager() : QObject(nullptr) , d(new AgentManagerPrivate(this)) { // needed for queued connections on our signals qRegisterMetaType(); qRegisterMetaType(); d->createDBusInterface(); QDBusServiceWatcher *watcher = new QDBusServiceWatcher(ServerManager::serviceName(ServerManager::Control), KDBusConnectionPool::threadConnection(), QDBusServiceWatcher::WatchForOwnerChange, this); - connect(watcher, SIGNAL(serviceOwnerChanged(QString,QString,QString)), - this, SLOT(serviceOwnerChanged(QString,QString,QString))); + connect(watcher, &QDBusServiceWatcher::serviceOwnerChanged, + this, [this](const QString &arg1, const QString &arg2 , const QString &arg3) { d->serviceOwnerChanged(arg1, arg2, arg3); }); } // @endcond AgentManager::~AgentManager() { delete d; } AgentManager *AgentManager::self() { if (!AgentManagerPrivate::mSelf) { AgentManagerPrivate::mSelf = new AgentManager(); } return AgentManagerPrivate::mSelf; } AgentType::List AgentManager::types() const { // Maybe the Control process is up and ready but we haven't been to the event loop yet so serviceOwnerChanged wasn't called yet. // In that case make sure to do it here, to avoid going into Broken state. if (d->mTypes.isEmpty()) { d->readAgentTypes(); } return Akonadi::valuesToVector(d->mTypes); } AgentType AgentManager::type(const QString &identifier) const { return d->mTypes.value(identifier); } AgentInstance::List AgentManager::instances() const { return Akonadi::valuesToVector(d->mInstances); } AgentInstance AgentManager::instance(const QString &identifier) const { return d->mInstances.value(identifier); } void AgentManager::removeInstance(const AgentInstance &instance) { d->mManager->removeAgentInstance(instance.identifier()); } void AgentManager::synchronizeCollection(const Collection &collection) { synchronizeCollection(collection, false); } void AgentManager::synchronizeCollection(const Collection &collection, bool recursive) { const QString resId = collection.resource(); Q_ASSERT(!resId.isEmpty()); d->mManager->agentInstanceSynchronizeCollection(resId, collection.id(), recursive); } #include "moc_agentmanager.cpp" diff --git a/src/core/agentmanager.h b/src/core/agentmanager.h index 5b4acfba4..94f451c10 100644 --- a/src/core/agentmanager.h +++ b/src/core/agentmanager.h @@ -1,222 +1,221 @@ /* Copyright (c) 2006-2008 Tobias Koenig 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 AKONADI_AGENTMANAGER_H #define AKONADI_AGENTMANAGER_H #include "akonadicore_export.h" #include "agenttype.h" #include "agentinstance.h" #include namespace Akonadi { class AgentManagerPrivate; class Collection; /** * @short Provides an interface to retrieve agent types and manage agent instances. * * This singleton class can be used to create or remove agent instances or trigger * synchronization of collections. Furthermore it provides information about status * changes of the agents. * * @code * * Akonadi::AgentManager *manager = Akonadi::AgentManager::self(); * * Akonadi::AgentType::List types = manager->types(); * foreach ( const Akonadi::AgentType& type, types ) { * qDebug() << "Type:" << type.name() << type.description(); * } * * @endcode * * @author Tobias Koenig */ class AKONADICORE_EXPORT AgentManager : public QObject { friend class AgentInstance; friend class AgentInstanceCreateJobPrivate; friend class AgentManagerPrivate; Q_OBJECT public: /** * Returns the global instance of the agent manager. */ static AgentManager *self(); /** * Destroys the agent manager. */ ~AgentManager(); /** * Returns the list of all available agent types. */ AgentType::List types() const; /** * Returns the agent type with the given @p identifier or * an invalid agent type if the identifier does not exist. */ AgentType type(const QString &identifier) const; /** * Returns the list of all available agent instances. */ AgentInstance::List instances() const; /** * Returns the agent instance with the given @p identifier or * an invalid agent instance if the identifier does not exist. * * Note that because a resource is a special case of an agent, the * identifier of a resource is the same as that of its agent instance. * @param identifier identifier to choose the agent instance */ AgentInstance instance(const QString &identifier) const; /** * Removes the given agent @p instance. */ void removeInstance(const AgentInstance &instance); /** * Trigger a synchronization of the given collection by its owning resource agent. * * @param collection The collection to synchronize. */ void synchronizeCollection(const Collection &collection); /** * Trigger a synchronization of the given collection by its owning resource agent. * * @param collection The collection to synchronize. * @param recursive If true, the sub-collections are also synchronized * * @since 4.6 */ void synchronizeCollection(const Collection &collection, bool recursive); Q_SIGNALS: /** * This signal is emitted whenever a new agent type was installed on the system. * * @param type The new agent type. */ void typeAdded(const Akonadi::AgentType &type); /** * This signal is emitted whenever an agent type was removed from the system. * * @param type The removed agent type. */ void typeRemoved(const Akonadi::AgentType &type); /** * This signal is emitted whenever a new agent instance was created. * * @param instance The new agent instance. */ void instanceAdded(const Akonadi::AgentInstance &instance); /** * This signal is emitted whenever an agent instance was removed. * * @param instance The removed agent instance. */ void instanceRemoved(const Akonadi::AgentInstance &instance); /** * This signal is emitted whenever the status of an agent instance has * changed. * * @param instance The agent instance that status has changed. */ void instanceStatusChanged(const Akonadi::AgentInstance &instance); /** * This signal is emitted whenever the progress of an agent instance has * changed. * * @param instance The agent instance that progress has changed. */ void instanceProgressChanged(const Akonadi::AgentInstance &instance); /** * This signal is emitted whenever the name of the agent instance has changed. * * @param instance The agent instance that name has changed. */ void instanceNameChanged(const Akonadi::AgentInstance &instance); /** * This signal is emitted whenever the agent instance raised an error. * * @param instance The agent instance that raised the error. * @param message The i18n'ed error message. */ void instanceError(const Akonadi::AgentInstance &instance, const QString &message); /** * This signal is emitted whenever the agent instance raised a warning. * * @param instance The agent instance that raised the warning. * @param message The i18n'ed warning message. */ void instanceWarning(const Akonadi::AgentInstance &instance, const QString &message); /** * This signal is emitted whenever the online state of an agent changed. * * @param instance The agent instance that changed its online state. * @param online The new online state. * @since 4.2 */ void instanceOnline(const Akonadi::AgentInstance &instance, bool online); private: //@cond PRIVATE AgentManager(); AgentManagerPrivate *const d; Q_PRIVATE_SLOT(d, void agentTypeAdded(const QString &)) Q_PRIVATE_SLOT(d, void agentTypeRemoved(const QString &)) Q_PRIVATE_SLOT(d, void agentInstanceAdded(const QString &)) Q_PRIVATE_SLOT(d, void agentInstanceRemoved(const QString &)) Q_PRIVATE_SLOT(d, void agentInstanceStatusChanged(const QString &, int, const QString &)) Q_PRIVATE_SLOT(d, void agentInstanceProgressChanged(const QString &, uint, const QString &)) Q_PRIVATE_SLOT(d, void agentInstanceNameChanged(const QString &, const QString &)) Q_PRIVATE_SLOT(d, void agentInstanceWarning(const QString &, const QString &)) Q_PRIVATE_SLOT(d, void agentInstanceError(const QString &, const QString &)) Q_PRIVATE_SLOT(d, void agentInstanceOnlineChanged(const QString &, bool)) - Q_PRIVATE_SLOT(d, void serviceOwnerChanged(const QString &, const QString &, const QString &)) //@endcond }; } #endif diff --git a/src/core/models/agentinstancemodel.cpp b/src/core/models/agentinstancemodel.cpp index d3bc67c2b..01966c5e8 100644 --- a/src/core/models/agentinstancemodel.cpp +++ b/src/core/models/agentinstancemodel.cpp @@ -1,252 +1,252 @@ /* Copyright (c) 2006 Tobias Koenig 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. */ #include "agentinstancemodel.h" #include "agentinstance.h" #include "agentmanager.h" #include #include using namespace Akonadi; /** * @internal */ class Q_DECL_HIDDEN AgentInstanceModel::Private { public: Private(AgentInstanceModel *parent) : mParent(parent) { } AgentInstanceModel *mParent = nullptr; AgentInstance::List mInstances; void instanceAdded(const AgentInstance &); void instanceRemoved(const AgentInstance &); void instanceChanged(const AgentInstance &); }; void AgentInstanceModel::Private::instanceAdded(const AgentInstance &instance) { mParent->beginInsertRows(QModelIndex(), mInstances.count(), mInstances.count()); mInstances.append(instance); mParent->endInsertRows(); } void AgentInstanceModel::Private::instanceRemoved(const AgentInstance &instance) { const int index = mInstances.indexOf(instance); if (index == -1) { return; } mParent->beginRemoveRows(QModelIndex(), index, index); mInstances.removeAll(instance); mParent->endRemoveRows(); } void AgentInstanceModel::Private::instanceChanged(const AgentInstance &instance) { const int numberOfInstance(mInstances.count()); for (int i = 0; i < numberOfInstance; ++i) { if (mInstances[i] == instance) { //TODO why reassign it if equals ? mInstances[i] = instance; const QModelIndex idx = mParent->index(i, 0); emit mParent->dataChanged(idx, idx); return; } } } AgentInstanceModel::AgentInstanceModel(QObject *parent) : QAbstractItemModel(parent) , d(new Private(this)) { d->mInstances = AgentManager::self()->instances(); - connect(AgentManager::self(), SIGNAL(instanceAdded(Akonadi::AgentInstance)), - this, SLOT(instanceAdded(Akonadi::AgentInstance))); - connect(AgentManager::self(), SIGNAL(instanceRemoved(Akonadi::AgentInstance)), - this, SLOT(instanceRemoved(Akonadi::AgentInstance))); + connect(AgentManager::self(), &AgentManager::instanceAdded, + this, [this](const Akonadi::AgentInstance &inst) { d->instanceAdded(inst);}); + connect(AgentManager::self(), &AgentManager::instanceRemoved, + this, [this](const Akonadi::AgentInstance &inst) { d->instanceRemoved(inst);}); connect(AgentManager::self(), SIGNAL(instanceStatusChanged(Akonadi::AgentInstance)), this, SLOT(instanceChanged(Akonadi::AgentInstance))); connect(AgentManager::self(), SIGNAL(instanceProgressChanged(Akonadi::AgentInstance)), this, SLOT(instanceChanged(Akonadi::AgentInstance))); connect(AgentManager::self(), SIGNAL(instanceNameChanged(Akonadi::AgentInstance)), this, SLOT(instanceChanged(Akonadi::AgentInstance))); connect(AgentManager::self(), SIGNAL(instanceOnline(Akonadi::AgentInstance,bool)), this, SLOT(instanceChanged(Akonadi::AgentInstance))); } AgentInstanceModel::~AgentInstanceModel() { delete d; } QHash AgentInstanceModel::roleNames() const { QHash roles = QAbstractItemModel::roleNames(); roles.insert(StatusRole, "status"); roles.insert(StatusMessageRole, "statusMessage"); roles.insert(ProgressRole, "progress"); roles.insert(OnlineRole, "online"); return roles; } int AgentInstanceModel::columnCount(const QModelIndex &index) const { return index.isValid() ? 0 : 1; } int AgentInstanceModel::rowCount(const QModelIndex &index) const { return index.isValid() ? 0 : d->mInstances.count(); } QVariant AgentInstanceModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) { return QVariant(); } if (index.row() < 0 || index.row() >= d->mInstances.count()) { return QVariant(); } const AgentInstance &instance = d->mInstances[index.row()]; switch (role) { case Qt::DisplayRole: return instance.name(); case Qt::DecorationRole: return instance.type().icon(); case InstanceRole: { QVariant var; var.setValue(instance); return var; } case InstanceIdentifierRole: return instance.identifier(); case Qt::ToolTipRole: return QStringLiteral("

%1

%2
").arg(instance.name(), instance.type().description()); case StatusRole: return instance.status(); case StatusMessageRole: return instance.statusMessage(); case ProgressRole: return instance.progress(); case OnlineRole: return instance.isOnline(); case TypeRole: { QVariant var; var.setValue(instance.type()); return var; } case TypeIdentifierRole: return instance.type().identifier(); case DescriptionRole: return instance.type().description(); case CapabilitiesRole: return instance.type().capabilities(); case MimeTypesRole: return instance.type().mimeTypes(); } return QVariant(); } QVariant AgentInstanceModel::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation == Qt::Vertical) { return QVariant(); } if (role != Qt::DisplayRole) { return QVariant(); } switch (section) { case 0: return i18nc("@title:column, name of a thing", "Name"); break; default: return QVariant(); break; } } QModelIndex AgentInstanceModel::index(int row, int column, const QModelIndex &) const { if (row < 0 || row >= d->mInstances.count()) { return QModelIndex(); } if (column != 0) { return QModelIndex(); } return createIndex(row, column); } QModelIndex AgentInstanceModel::parent(const QModelIndex &) const { return QModelIndex(); } Qt::ItemFlags AgentInstanceModel::flags(const QModelIndex &index) const { if (!index.isValid() || index.row() < 0 || index.row() >= d->mInstances.count()) { return QAbstractItemModel::flags(index); } return QAbstractItemModel::flags(index) | Qt::ItemIsEditable; } bool AgentInstanceModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (!index.isValid()) { return false; } if (index.row() < 0 || index.row() >= d->mInstances.count()) { return false; } AgentInstance &instance = d->mInstances[index.row()]; switch (role) { case OnlineRole: instance.setIsOnline(value.toBool()); emit dataChanged(index, index); return true; default: return false; } return false; } #include "moc_agentinstancemodel.cpp" diff --git a/src/core/models/agentinstancemodel.h b/src/core/models/agentinstancemodel.h index ce097f088..ba587d5c4 100644 --- a/src/core/models/agentinstancemodel.h +++ b/src/core/models/agentinstancemodel.h @@ -1,109 +1,107 @@ /* Copyright (c) 2006-2008 Tobias Koenig 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 AKONADI_AGENTINSTANCEMODEL_H #define AKONADI_AGENTINSTANCEMODEL_H #include "akonadicore_export.h" #include namespace Akonadi { /** * @short Provides a data model for agent instances. * * This class provides the interface of a QAbstractItemModel to * access all available agent instances: their name, identifier, * supported mimetypes and capabilities. * * @code * * Akonadi::AgentInstanceModel *model = new Akonadi::AgentInstanceModel( this ); * * QListView *view = new QListView( this ); * view->setModel( model ); * * @endcode * * To show only agent instances that match a given mime type or special * capabilities, use the AgentFilterProxyModel on top of this model. * * @author Tobias Koenig */ class AKONADICORE_EXPORT AgentInstanceModel : public QAbstractItemModel { Q_OBJECT public: /** * Describes the roles of this model. */ enum Roles { TypeRole = Qt::UserRole + 1, ///< The agent type itself TypeIdentifierRole, ///< The identifier of the agent type DescriptionRole, ///< A description of the agent type MimeTypesRole, ///< A list of supported mimetypes CapabilitiesRole, ///< A list of supported capabilities InstanceRole, ///< The agent instance itself InstanceIdentifierRole, ///< The identifier of the agent instance StatusRole, ///< The current status (numerical) of the instance StatusMessageRole, ///< A textual presentation of the current status ProgressRole, ///< The current progress (numerical in percent) of an operation OnlineRole, ///< The current online/offline status UserRole = Qt::UserRole + 42 ///< Role for user extensions }; /** * Creates a new agent instance model. * * @param parent The parent object. */ explicit AgentInstanceModel(QObject *parent = nullptr); /** * Destroys the agent instance model. */ virtual ~AgentInstanceModel(); QHash roleNames() const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; QModelIndex parent(const QModelIndex &index) const override; Qt::ItemFlags flags(const QModelIndex &index) const override; bool setData(const QModelIndex &index, const QVariant &value, int role) override; private: //@cond PRIVATE class Private; Private *const d; - Q_PRIVATE_SLOT(d, void instanceAdded(const Akonadi::AgentInstance &)) - Q_PRIVATE_SLOT(d, void instanceRemoved(const Akonadi::AgentInstance &)) Q_PRIVATE_SLOT(d, void instanceChanged(const Akonadi::AgentInstance &)) //@endcond }; } #endif