File indexing completed on 2024-11-24 04:50:45
0001 // SPDX-FileCopyrightText: 2021 Carl Schwan <carlschwan@kde.org> 0002 // SPDX-License-Identifier: LGPL-2.0-or-later 0003 0004 #include "agentconfiguration.h" 0005 0006 #include <Akonadi/AgentConfigurationDialog> 0007 #include <Akonadi/AgentInstanceCreateJob> 0008 #include <Akonadi/AgentInstanceModel> 0009 #include <Akonadi/AgentManager> 0010 #include <Akonadi/AgentTypeModel> 0011 0012 #include <KWindowSystem> 0013 #include <QPointer> 0014 0015 using namespace Akonadi; 0016 0017 AgentConfiguration::AgentConfiguration(QObject *parent) 0018 : QObject(parent) 0019 { 0020 connect(Akonadi::AgentManager::self(), &Akonadi::AgentManager::instanceProgressChanged, this, &AgentConfiguration::processInstanceProgressChanged); 0021 connect(Akonadi::AgentManager::self(), &Akonadi::AgentManager::instanceStatusChanged, this, &AgentConfiguration::processInstanceProgressChanged); 0022 } 0023 0024 AgentConfiguration::~AgentConfiguration() = default; 0025 0026 Akonadi::AgentFilterProxyModel *AgentConfiguration::availableAgents() 0027 { 0028 if (m_availableAgents) { 0029 return m_availableAgents; 0030 } 0031 0032 auto agentInstanceModel = new AgentTypeModel(this); 0033 m_availableAgents = new AgentFilterProxyModel(this); 0034 for (const auto &mimetype : std::as_const(m_mimetypes)) { 0035 m_availableAgents->addMimeTypeFilter(mimetype); 0036 } 0037 m_availableAgents->setSourceModel(agentInstanceModel); 0038 m_availableAgents->addCapabilityFilter(QStringLiteral("Resource")); // show only resources, no agents 0039 return m_availableAgents; 0040 } 0041 0042 Akonadi::AgentFilterProxyModel *AgentConfiguration::runningAgents() 0043 { 0044 if (m_runningAgents) { 0045 return m_runningAgents; 0046 } 0047 0048 auto agentInstanceModel = new AgentInstanceModel(this); 0049 m_runningAgents = new AgentFilterProxyModel(this); 0050 for (const auto &mimetype : std::as_const(m_mimetypes)) { 0051 m_runningAgents->addMimeTypeFilter(mimetype); 0052 } 0053 m_runningAgents->setSourceModel(agentInstanceModel); 0054 m_runningAgents->addCapabilityFilter(QStringLiteral("Resource")); // show only resources, no agents 0055 return m_runningAgents; 0056 } 0057 0058 void AgentConfiguration::createNew(int index) 0059 { 0060 Q_ASSERT(m_availableAgents != nullptr); 0061 0062 const auto agentType = m_availableAgents->data(m_availableAgents->index(index, 0), AgentTypeModel::TypeRole).value<AgentType>(); 0063 0064 if (agentType.isValid()) { 0065 auto job = new Akonadi::AgentInstanceCreateJob(agentType, this); 0066 job->configure(nullptr); 0067 job->start(); 0068 } 0069 } 0070 0071 void AgentConfiguration::edit(int index) 0072 { 0073 Q_ASSERT(m_runningAgents != nullptr); 0074 0075 auto instance = m_runningAgents->data(m_runningAgents->index(index, 0), AgentInstanceModel::InstanceRole).value<AgentInstance>(); 0076 setupEdit(instance); 0077 } 0078 0079 void AgentConfiguration::editIdentifier(const QString &resourceIdentifier) 0080 { 0081 auto instance = Akonadi::AgentManager::self()->instance(resourceIdentifier); 0082 setupEdit(instance); 0083 } 0084 0085 void AgentConfiguration::setupEdit(Akonadi::AgentInstance instance) 0086 { 0087 if (instance.isValid()) { 0088 QPointer<AgentConfigurationDialog> dlg(new AgentConfigurationDialog(instance, nullptr)); 0089 dlg->exec(); 0090 delete dlg; 0091 } 0092 } 0093 0094 void AgentConfiguration::restart(int index) 0095 { 0096 Q_ASSERT(m_runningAgents != nullptr); 0097 0098 auto instance = m_runningAgents->data(m_runningAgents->index(index, 0), AgentInstanceModel::InstanceRole).value<AgentInstance>(); 0099 setupRestart(instance); 0100 } 0101 0102 void AgentConfiguration::restartIdentifier(const QString &resourceIdentifier) 0103 { 0104 auto instance = Akonadi::AgentManager::self()->instance(resourceIdentifier); 0105 setupRestart(instance); 0106 } 0107 0108 void AgentConfiguration::setupRestart(Akonadi::AgentInstance instance) 0109 { 0110 if (instance.isValid()) { 0111 instance.restart(); 0112 } 0113 } 0114 0115 void AgentConfiguration::remove(int index) 0116 { 0117 Q_ASSERT(m_runningAgents != nullptr); 0118 0119 auto instance = m_runningAgents->data(m_runningAgents->index(index, 0), AgentInstanceModel::InstanceRole).value<AgentInstance>(); 0120 setupRemove(instance); 0121 } 0122 0123 void AgentConfiguration::removeIdentifier(const QString &resourceIdentifier) 0124 { 0125 auto instance = Akonadi::AgentManager::self()->instance(resourceIdentifier); 0126 setupRemove(instance); 0127 } 0128 0129 void AgentConfiguration::setupRemove(const Akonadi::AgentInstance &instance) 0130 { 0131 if (instance.isValid()) { 0132 Akonadi::AgentManager::self()->removeInstance(instance); 0133 } 0134 } 0135 0136 void AgentConfiguration::processInstanceProgressChanged(const Akonadi::AgentInstance &instance) 0137 { 0138 const QVariantMap instanceData = { 0139 {QStringLiteral("instanceId"), instance.identifier()}, 0140 {QStringLiteral("progress"), instance.progress()}, // Not very reliable so beware 0141 {QStringLiteral("status"), instance.status()}, 0142 }; 0143 0144 Q_EMIT agentProgressChanged(instanceData); 0145 } 0146 0147 QStringList AgentConfiguration::mimetypes() const 0148 { 0149 return m_mimetypes; 0150 } 0151 0152 void AgentConfiguration::setMimetypes(QStringList mimetypes) 0153 { 0154 if (mimetypes == m_mimetypes) { 0155 return; 0156 } 0157 m_mimetypes = mimetypes; 0158 Q_EMIT mimetypesChanged(); 0159 0160 if (m_runningAgents) { 0161 delete m_runningAgents; 0162 m_runningAgents = nullptr; 0163 Q_EMIT runningAgentsChanged(); 0164 } 0165 0166 if (m_availableAgents) { 0167 delete m_availableAgents; 0168 m_availableAgents = nullptr; 0169 Q_EMIT availableAgentsChanged(); 0170 } 0171 } 0172 0173 #include "moc_agentconfiguration.cpp"