Akonadi

manageaccountwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "manageaccountwidget.h"
8
9#include "agentconfigurationdialog.h"
10#include "agentfilterproxymodel.h"
11#include "agentinstance.h"
12#include "agentinstancecreatejob.h"
13#include "agentmanager.h"
14#include "agenttypedialog.h"
15
16#include "ui_manageaccountwidget.h"
17
18#include <KLocalizedString>
19#include <KMessageBox>
20#include <QAbstractItemDelegate>
21#include <QAbstractItemView>
22#include <QKeyEvent>
23#include <QPointer>
24
25using namespace Akonadi;
26
27class Akonadi::ManageAccountWidgetPrivate
28{
29public:
30 QString mSpecialCollectionIdentifier;
31
32 QStringList mMimeTypeFilter;
33 QStringList mCapabilityFilter;
34 QStringList mExcludeCapabilities;
35
36 Ui::ManageAccountWidget ui;
37};
38
39ManageAccountWidget::ManageAccountWidget(QWidget *parent)
40 : QWidget(parent)
41 , d(new Akonadi::ManageAccountWidgetPrivate)
42{
43 d->ui.setupUi(this);
44 connect(d->ui.mAddAccountButton, &QPushButton::clicked, this, &ManageAccountWidget::slotAddAccount);
45
46 connect(d->ui.mModifyAccountButton, &QPushButton::clicked, this, &ManageAccountWidget::slotModifySelectedAccount);
47
48 connect(d->ui.mRemoveAccountButton, &QPushButton::clicked, this, &ManageAccountWidget::slotRemoveSelectedAccount);
49 connect(d->ui.mRestartAccountButton, &QPushButton::clicked, this, &ManageAccountWidget::slotRestartSelectedAccount);
50
51 connect(d->ui.mAccountList, &Akonadi::AgentInstanceWidget::clicked, this, &ManageAccountWidget::slotAccountSelected);
52 connect(d->ui.mAccountList, &Akonadi::AgentInstanceWidget::doubleClicked, this, &ManageAccountWidget::slotModifySelectedAccount);
53
54 d->ui.mAccountList->view()->setSelectionMode(QAbstractItemView::SingleSelection);
55
56 connect(d->ui.mFilterAccount, &QLineEdit::textChanged, this, &ManageAccountWidget::slotSearchAgentType);
57
58 d->ui.mFilterAccount->installEventFilter(this);
59 slotAccountSelected(d->ui.mAccountList->currentAgentInstance());
60}
61
62ManageAccountWidget::~ManageAccountWidget() = default;
63
64void ManageAccountWidget::slotSearchAgentType(const QString &str)
65{
66 d->ui.mAccountList->agentFilterProxyModel()->setFilterRegularExpression(str);
67}
68
69void ManageAccountWidget::disconnectAddAccountButton()
70{
71 disconnect(d->ui.mAddAccountButton, &QPushButton::clicked, this, &ManageAccountWidget::slotAddAccount);
72}
73
74QPushButton *ManageAccountWidget::addAccountButton() const
75{
76 return d->ui.mAddAccountButton;
77}
78
79void ManageAccountWidget::setDescriptionLabelText(const QString &text)
80{
81 d->ui.label->setText(text);
82}
83
84bool ManageAccountWidget::eventFilter(QObject *obj, QEvent *event)
85{
86 if (event->type() == QEvent::KeyPress && obj == d->ui.mFilterAccount) {
87 auto key = static_cast<QKeyEvent *>(event);
88 if ((key->key() == Qt::Key_Enter) || (key->key() == Qt::Key_Return)) {
89 event->accept();
90 return true;
91 }
92 }
94}
95
96QAbstractItemView *ManageAccountWidget::view() const
97{
98 return d->ui.mAccountList->view();
99}
100
101void ManageAccountWidget::setSpecialCollectionIdentifier(const QString &identifier)
102{
103 d->mSpecialCollectionIdentifier = identifier;
104}
105
106void ManageAccountWidget::slotAddAccount()
107{
109
110 Akonadi::AgentFilterProxyModel *filter = dlg.agentFilterProxyModel();
111 for (const QString &filterStr : std::as_const(d->mMimeTypeFilter)) {
112 filter->addMimeTypeFilter(filterStr);
113 }
114 for (const QString &capa : std::as_const(d->mCapabilityFilter)) {
115 filter->addCapabilityFilter(capa);
116 }
117 for (const QString &capa : std::as_const(d->mExcludeCapabilities)) {
118 filter->excludeCapabilities(capa);
119 }
120 if (dlg.exec()) {
121 const Akonadi::AgentType agentType = dlg.agentType();
122
123 if (agentType.isValid()) {
124 auto job = new Akonadi::AgentInstanceCreateJob(agentType, this);
125 job->configure(this);
126 job->start();
127 }
128 }
129}
130
131QStringList ManageAccountWidget::excludeCapabilities() const
132{
133 return d->mExcludeCapabilities;
134}
135
136void ManageAccountWidget::setExcludeCapabilities(const QStringList &excludeCapabilities)
137{
138 d->mExcludeCapabilities = excludeCapabilities;
139 for (const QString &capability : std::as_const(d->mExcludeCapabilities)) {
140 d->ui.mAccountList->agentFilterProxyModel()->excludeCapabilities(capability);
141 }
142}
143
144void ManageAccountWidget::setItemDelegate(QAbstractItemDelegate *delegate)
145{
146 d->ui.mAccountList->view()->setItemDelegate(delegate);
147}
148
149QStringList ManageAccountWidget::capabilityFilter() const
150{
151 return d->mCapabilityFilter;
152}
153
154void ManageAccountWidget::setCapabilityFilter(const QStringList &capabilityFilter)
155{
156 d->mCapabilityFilter = capabilityFilter;
157 for (const QString &capability : std::as_const(d->mCapabilityFilter)) {
158 d->ui.mAccountList->agentFilterProxyModel()->addCapabilityFilter(capability);
159 }
160}
161
162QStringList ManageAccountWidget::mimeTypeFilter() const
163{
164 return d->mMimeTypeFilter;
165}
166
167void ManageAccountWidget::setMimeTypeFilter(const QStringList &mimeTypeFilter)
168{
169 d->mMimeTypeFilter = mimeTypeFilter;
170 for (const QString &mimeType : std::as_const(d->mMimeTypeFilter)) {
171 d->ui.mAccountList->agentFilterProxyModel()->addMimeTypeFilter(mimeType);
172 }
173}
174
175void ManageAccountWidget::slotModifySelectedAccount()
176{
177 Akonadi::AgentInstance instance = d->ui.mAccountList->currentAgentInstance();
178 if (instance.isValid()) {
179 QPointer<AgentConfigurationDialog> dlg(new AgentConfigurationDialog(instance, this));
180 dlg->exec();
181 delete dlg;
182 }
183}
184
185void ManageAccountWidget::slotRestartSelectedAccount()
186{
187 const Akonadi::AgentInstance instance = d->ui.mAccountList->currentAgentInstance();
188 if (instance.isValid()) {
189 instance.restart();
190 }
191}
192
193void ManageAccountWidget::slotRemoveSelectedAccount()
194{
195 const Akonadi::AgentInstance instance = d->ui.mAccountList->currentAgentInstance();
196
197 const int rc = KMessageBox::questionTwoActions(this,
198 i18n("Do you want to remove account '%1'?", instance.name()),
199 i18nc("@title:window", "Remove account?"),
202 if (rc == KMessageBox::ButtonCode::SecondaryAction) {
203 return;
204 }
205
206 if (instance.isValid()) {
208 }
209
210 slotAccountSelected(d->ui.mAccountList->currentAgentInstance());
211}
212
213void ManageAccountWidget::slotAccountSelected(const Akonadi::AgentInstance &current)
214{
215 if (current.isValid()) {
216 d->ui.mModifyAccountButton->setEnabled(!current.type().capabilities().contains(QLatin1StringView("NoConfig")));
217 d->ui.mRemoveAccountButton->setEnabled(d->mSpecialCollectionIdentifier != current.identifier());
218 // Restarting an agent is not possible if it's in Running status... (see AgentProcessInstance::restartWhenIdle)
219 d->ui.mRestartAccountButton->setEnabled((current.status() != 1));
220 } else {
221 d->ui.mModifyAccountButton->setEnabled(false);
222 d->ui.mRemoveAccountButton->setEnabled(false);
223 d->ui.mRestartAccountButton->setEnabled(false);
224 }
225}
226
227#include "moc_manageaccountwidget.cpp"
A proxy model for filtering AgentType or AgentInstance.
Job for creating new agent instances.
void doubleClicked(const Akonadi::AgentInstance &current)
This signal is emitted whenever there is a double click on an agent instance.
void clicked(const Akonadi::AgentInstance &current)
This signal is emitted whenever there is a click on an agent instance.
A representation of an agent instance.
void restart() const
Restart the agent process.
Status status() const
Returns the status of the agent instance.
bool isValid() const
Returns whether the agent instance object is valid.
QString identifier() const
Returns the unique identifier of the agent instance.
AgentType type() const
Returns the agent type of this instance.
QString name() const
Returns the user visible name of the agent instance.
static AgentManager * self()
Returns the global instance of the agent manager.
void removeInstance(const AgentInstance &instance)
Removes the given agent instance.
A dialog to select an available agent type.
A representation of an agent type.
bool isValid() const
Returns whether the agent type is valid.
QStringList capabilities() const
Returns the list of supported capabilities of the agent type.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
Helper integration between Akonadi and Qt.
ButtonCode questionTwoActions(QWidget *parent, const QString &text, const QString &title, const KGuiItem &primaryAction, const KGuiItem &secondaryAction, const QString &dontAskAgainName=QString(), Options options=Notify)
KGuiItem remove()
KGuiItem cancel()
A glue between Qt and the standard library.
void clicked(bool checked)
void accept()
void textChanged(const QString &text)
bool disconnect(const QMetaObject::Connection &connection)
virtual bool eventFilter(QObject *watched, QEvent *event)
T qobject_cast(QObject *object)
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
Key_Enter
QFuture< void > filter(QThreadPool *pool, Sequence &sequence, KeepFunctor &&filterFunction)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
virtual bool event(QEvent *event) override
void setupUi(QWidget *widget)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 14 2024 11:51:45 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.