MauiKit Calendar

agentconfiguration.cpp
1// SPDX-FileCopyrightText: 2021 Carl Schwan <carlschwan@kde.org>
2// SPDX-License-Identifier: LGPL-2.0-or-later
3
4#include "agentconfiguration.h"
5#include "kalendarapplication.h"
6
7#include <Akonadi/AgentConfigurationDialog>
8#include <Akonadi/AgentInstanceCreateJob>
9#include <Akonadi/AgentInstanceModel>
10#include <Akonadi/AgentManager>
11#include <Akonadi/AgentTypeModel>
12#include <KContacts/Addressee>
13#include <KContacts/ContactGroup>
14
15#include <KWindowSystem>
16#include <QDebug>
17#include <QPointer>
18#include <qobjectdefs.h>
19
20using namespace Akonadi;
21
22AgentConfiguration::AgentConfiguration(QObject *parent)
23 : QObject(parent)
24 , m_mode(KalendarApplication::Event)
25{
26 connect(Akonadi::AgentManager::self(), &Akonadi::AgentManager::instanceProgressChanged, this, &AgentConfiguration::processInstanceProgressChanged);
27 connect(Akonadi::AgentManager::self(), &Akonadi::AgentManager::instanceStatusChanged, this, &AgentConfiguration::processInstanceProgressChanged);
28}
29
30AgentConfiguration::~AgentConfiguration() = default;
31
32Akonadi::AgentFilterProxyModel *AgentConfiguration::availableAgents()
33{
34 if (m_availableAgents) {
35 return m_availableAgents;
36 }
37
38 auto agentInstanceModel = new AgentTypeModel(this);
39 m_availableAgents = new AgentFilterProxyModel(this);
40 if (m_mode == KalendarApplication::Contact) {
43 } else {
44 m_availableAgents->addMimeTypeFilter(QStringLiteral("text/calendar"));
45 }
46 m_availableAgents->setSourceModel(agentInstanceModel);
47 m_availableAgents->addCapabilityFilter(QStringLiteral("Resource")); // show only resources, no agents
48 return m_availableAgents;
49}
50
51Akonadi::AgentFilterProxyModel *AgentConfiguration::runningAgents()
52{
53 if (m_runningAgents) {
54 return m_runningAgents;
55 }
56
57 auto agentInstanceModel = new AgentInstanceModel(this);
58 m_runningAgents = new AgentFilterProxyModel(this);
59 if (m_mode == KalendarApplication::Contact) {
62 } else {
63 m_runningAgents->addMimeTypeFilter(QStringLiteral("text/calendar"));
64 }
65 m_runningAgents->setSourceModel(agentInstanceModel);
66 m_runningAgents->addCapabilityFilter(QStringLiteral("Resource")); // show only resources, no agents
67 return m_runningAgents;
68}
69
70void AgentConfiguration::createNew(int index)
71{
72 Q_ASSERT(m_availableAgents != nullptr);
73
74 const auto agentType = m_availableAgents->data(m_availableAgents->index(index, 0), AgentTypeModel::TypeRole).value<AgentType>();
75
76 if (agentType.isValid()) {
77 auto job = new Akonadi::AgentInstanceCreateJob(agentType, this);
78 job->configure(nullptr);
79 job->start();
80 }
81}
82
83void AgentConfiguration::edit(int index)
84{
85 Q_ASSERT(m_runningAgents != nullptr);
86
87 auto instance = m_runningAgents->data(m_runningAgents->index(index, 0), AgentInstanceModel::InstanceRole).value<AgentInstance>();
88 setupEdit(instance);
89}
90
91void AgentConfiguration::editIdentifier(const QString &resourceIdentifier)
92{
93 auto instance = Akonadi::AgentManager::self()->instance(resourceIdentifier);
94 setupEdit(instance);
95}
96
97void AgentConfiguration::setupEdit(Akonadi::AgentInstance instance)
98{
99 if (instance.isValid()) {
100 KWindowSystem::allowExternalProcessWindowActivation();
101 QPointer<AgentConfigurationDialog> dlg(new AgentConfigurationDialog(instance, nullptr));
102 dlg->exec();
103 delete dlg;
104 }
105}
106
107void AgentConfiguration::restart(int index)
108{
109 Q_ASSERT(m_runningAgents != nullptr);
110
111 auto instance = m_runningAgents->data(m_runningAgents->index(index, 0), AgentInstanceModel::InstanceRole).value<AgentInstance>();
112 setupRestart(instance);
113}
114
115void AgentConfiguration::restartIdentifier(const QString &resourceIdentifier)
116{
117 auto instance = Akonadi::AgentManager::self()->instance(resourceIdentifier);
118 setupRestart(instance);
119}
120
121void AgentConfiguration::setupRestart(Akonadi::AgentInstance instance)
122{
123 if (instance.isValid()) {
124 instance.restart();
125 }
126}
127
128void AgentConfiguration::remove(int index)
129{
130 Q_ASSERT(m_runningAgents != nullptr);
131
132 auto instance = m_runningAgents->data(m_runningAgents->index(index, 0), AgentInstanceModel::InstanceRole).value<AgentInstance>();
133 setupRemove(instance);
134}
135
136void AgentConfiguration::removeIdentifier(const QString &resourceIdentifier)
137{
138 auto instance = Akonadi::AgentManager::self()->instance(resourceIdentifier);
139 setupRemove(instance);
140}
141
142void AgentConfiguration::setupRemove(const Akonadi::AgentInstance &instance)
143{
144 if (instance.isValid()) {
146 }
147}
148
149void AgentConfiguration::processInstanceProgressChanged(const Akonadi::AgentInstance &instance)
150{
151 const QVariantMap instanceData = {
152 {QStringLiteral("instanceId"), instance.identifier()},
153 {QStringLiteral("progress"), instance.progress()}, // Not very reliable so beware
154 {QStringLiteral("status"), instance.status()},
155 };
156
157 Q_EMIT agentProgressChanged(instanceData);
158}
159
160KalendarApplication::Mode AgentConfiguration::mode() const
161{
162 return m_mode;
163}
164
165void AgentConfiguration::setMode(KalendarApplication::Mode mode)
166{
167 if (mode == m_mode) {
168 return;
169 }
170 m_mode = mode;
171 Q_EMIT modeChanged();
172
173 if (m_runningAgents) {
174 delete m_runningAgents;
175 m_runningAgents = nullptr;
176 Q_EMIT runningAgentsChanged();
177 }
178
179 if (m_availableAgents) {
180 delete m_availableAgents;
181 m_availableAgents = nullptr;
182 Q_EMIT availableAgentsChanged();
183 }
184}
void addMimeTypeFilter(const QString &mimeType)
void addCapabilityFilter(const QString &capability)
Status status() const
QString identifier() const
void instanceProgressChanged(const Akonadi::AgentInstance &instance)
static AgentManager * self()
void removeInstance(const AgentInstance &instance)
AgentInstance instance(const QString &identifier) const
void instanceStatusChanged(const Akonadi::AgentInstance &instance)
static QString mimeType()
static QString mimeType()
Q_EMITQ_EMIT
virtual QVariant data(const QModelIndex &index, int role) const const override
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
virtual void setSourceModel(QAbstractItemModel *sourceModel) override
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:35:11 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.