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 "agentinstancefilterproxymodel.h"
14#include "agentmanager.h"
15#include "agenttypedialog.h"
16
17#include "ui_manageaccountwidget.h"
18
19#include <KLocalizedString>
20#include <KMessageBox>
21#include <QAbstractItemDelegate>
22#include <QAbstractItemView>
23#include <QKeyEvent>
24#include <QPointer>
25
26using namespace Akonadi;
27
28class Akonadi::ManageAccountWidgetPrivate
29{
30public:
31 QString mSpecialCollectionIdentifier;
32
33 QStringList mMimeTypeFilter;
34 QStringList mCapabilityFilter;
35 QStringList mExcludeCapabilities;
36
37 Ui::ManageAccountWidget ui;
38};
39
40ManageAccountWidget::ManageAccountWidget(QWidget *parent)
41 : QWidget(parent)
42 , d(new Akonadi::ManageAccountWidgetPrivate)
43{
44 d->ui.setupUi(this);
45 connect(d->ui.mAddAccountButton, &QPushButton::clicked, this, &ManageAccountWidget::slotAddAccount);
46
47 connect(d->ui.mModifyAccountButton, &QPushButton::clicked, this, &ManageAccountWidget::slotModifySelectedAccount);
48
49 connect(d->ui.mRemoveAccountButton, &QPushButton::clicked, this, &ManageAccountWidget::slotRemoveSelectedAccount);
50 connect(d->ui.mRestartAccountButton, &QPushButton::clicked, this, &ManageAccountWidget::slotRestartSelectedAccount);
51
52 connect(d->ui.mAccountList, &Akonadi::AgentInstanceWidget::clicked, this, &ManageAccountWidget::slotAccountSelected);
53 connect(d->ui.mAccountList, &Akonadi::AgentInstanceWidget::doubleClicked, this, &ManageAccountWidget::slotModifySelectedAccount);
54
55 d->ui.mAccountList->view()->setSelectionMode(QAbstractItemView::SingleSelection);
56
57 connect(d->ui.mFilterAccount, &QLineEdit::textChanged, this, &ManageAccountWidget::slotSearchAgentType);
58
59 d->ui.mFilterAccount->installEventFilter(this);
60 d->ui.accountOnCurrentActivity->setVisible(false);
61 slotAccountSelected(d->ui.mAccountList->currentAgentInstance());
62}
63
64ManageAccountWidget::~ManageAccountWidget() = default;
65
66void ManageAccountWidget::slotSearchAgentType(const QString &str)
67{
68 d->ui.mAccountList->agentInstanceFilterProxyModel()->setFilterRegularExpression(str);
69}
70
71void ManageAccountWidget::disconnectAddAccountButton()
72{
73 disconnect(d->ui.mAddAccountButton, &QPushButton::clicked, this, &ManageAccountWidget::slotAddAccount);
74}
75
76bool ManageAccountWidget::enablePlasmaActivities() const
77{
78 return d->ui.mAccountList->enablePlasmaActivities();
79}
80
81void ManageAccountWidget::setEnablePlasmaActivities(bool newEnablePlasmaActivities)
82{
83 d->ui.accountOnCurrentActivity->setVisible(newEnablePlasmaActivities);
84 d->ui.mAccountList->setEnablePlasmaActivities(newEnablePlasmaActivities);
85}
86
87AccountActivitiesAbstract *ManageAccountWidget::accountActivitiesAbstract() const
88{
89 return d->ui.mAccountList->accountActivitiesAbstract();
90}
91
92void ManageAccountWidget::setAccountActivitiesAbstract(AccountActivitiesAbstract *abstract)
93{
94 d->ui.mAccountList->setAccountActivitiesAbstract(abstract);
95}
96
97QPushButton *ManageAccountWidget::addAccountButton() const
98{
99 return d->ui.mAddAccountButton;
100}
101
102void ManageAccountWidget::setDescriptionLabelText(const QString &text)
103{
104 d->ui.label->setText(text);
105}
106
107bool ManageAccountWidget::eventFilter(QObject *obj, QEvent *event)
108{
109 if (event->type() == QEvent::KeyPress && obj == d->ui.mFilterAccount) {
110 auto key = static_cast<QKeyEvent *>(event);
111 if ((key->key() == Qt::Key_Enter) || (key->key() == Qt::Key_Return)) {
112 event->accept();
113 return true;
114 }
115 }
116 return QWidget::eventFilter(obj, event);
117}
118
119QAbstractItemView *ManageAccountWidget::view() const
120{
121 return d->ui.mAccountList->view();
122}
123
124void ManageAccountWidget::setSpecialCollectionIdentifier(const QString &identifier)
125{
126 d->mSpecialCollectionIdentifier = identifier;
127}
128
129void ManageAccountWidget::slotAddAccount()
130{
131 Akonadi::AgentTypeDialog dlg(this);
132
133 Akonadi::AgentFilterProxyModel *filter = dlg.agentFilterProxyModel();
134 for (const QString &filterStr : std::as_const(d->mMimeTypeFilter)) {
135 filter->addMimeTypeFilter(filterStr);
136 }
137 for (const QString &capa : std::as_const(d->mCapabilityFilter)) {
138 filter->addCapabilityFilter(capa);
139 }
140 for (const QString &capa : std::as_const(d->mExcludeCapabilities)) {
141 filter->excludeCapabilities(capa);
142 }
143 if (dlg.exec()) {
144 const Akonadi::AgentType agentType = dlg.agentType();
145
146 if (agentType.isValid()) {
147 auto job = new Akonadi::AgentInstanceCreateJob(agentType, this);
148 job->configure(this);
149 job->start();
150 }
151 }
152}
153
154QStringList ManageAccountWidget::excludeCapabilities() const
155{
156 return d->mExcludeCapabilities;
157}
158
159void ManageAccountWidget::setExcludeCapabilities(const QStringList &excludeCapabilities)
160{
161 d->mExcludeCapabilities = excludeCapabilities;
162 for (const QString &capability : std::as_const(d->mExcludeCapabilities)) {
163 d->ui.mAccountList->agentInstanceFilterProxyModel()->excludeCapabilities(capability);
164 }
165}
166
167void ManageAccountWidget::setItemDelegate(QAbstractItemDelegate *delegate)
168{
169 d->ui.mAccountList->view()->setItemDelegate(delegate);
170}
171
172QStringList ManageAccountWidget::capabilityFilter() const
173{
174 return d->mCapabilityFilter;
175}
176
177void ManageAccountWidget::setCapabilityFilter(const QStringList &capabilityFilter)
178{
179 d->mCapabilityFilter = capabilityFilter;
180 for (const QString &capability : std::as_const(d->mCapabilityFilter)) {
181 d->ui.mAccountList->agentInstanceFilterProxyModel()->addCapabilityFilter(capability);
182 }
183}
184
185QStringList ManageAccountWidget::mimeTypeFilter() const
186{
187 return d->mMimeTypeFilter;
188}
189
190void ManageAccountWidget::setMimeTypeFilter(const QStringList &mimeTypeFilter)
191{
192 d->mMimeTypeFilter = mimeTypeFilter;
193 for (const QString &mimeType : std::as_const(d->mMimeTypeFilter)) {
194 d->ui.mAccountList->agentInstanceFilterProxyModel()->addMimeTypeFilter(mimeType);
195 }
196}
197
198void ManageAccountWidget::slotModifySelectedAccount()
199{
200 Akonadi::AgentInstance instance = d->ui.mAccountList->currentAgentInstance();
201 if (instance.isValid()) {
202 QPointer<AgentConfigurationDialog> dlg(new AgentConfigurationDialog(instance, this));
203 dlg->exec();
204 delete dlg;
205 }
206}
207
208void ManageAccountWidget::slotRestartSelectedAccount()
209{
210 const Akonadi::AgentInstance instance = d->ui.mAccountList->currentAgentInstance();
211 if (instance.isValid()) {
212 instance.restart();
213 }
214}
215
216void ManageAccountWidget::slotRemoveSelectedAccount()
217{
218 const Akonadi::AgentInstance instance = d->ui.mAccountList->currentAgentInstance();
219
220 const int rc = KMessageBox::questionTwoActions(this,
221 i18n("Do you want to remove account '%1'?", instance.name()),
222 i18nc("@title:window", "Remove account?"),
225 if (rc == KMessageBox::ButtonCode::SecondaryAction) {
226 return;
227 }
228
229 if (instance.isValid()) {
231 }
232
233 slotAccountSelected(d->ui.mAccountList->currentAgentInstance());
234}
235
236void ManageAccountWidget::slotAccountSelected(const Akonadi::AgentInstance &current)
237{
238 if (current.isValid()) {
239 d->ui.mModifyAccountButton->setEnabled(!current.type().capabilities().contains(QLatin1StringView("NoConfig")));
240 d->ui.mRemoveAccountButton->setEnabled(d->mSpecialCollectionIdentifier != current.identifier());
241 // Restarting an agent is not possible if it's in Running status... (see AgentProcessInstance::restartWhenIdle)
242 d->ui.mRestartAccountButton->setEnabled((current.status() != 1));
243 } else {
244 d->ui.mModifyAccountButton->setEnabled(false);
245 d->ui.mRemoveAccountButton->setEnabled(false);
246 d->ui.mRestartAccountButton->setEnabled(false);
247 }
248}
249
250#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()
void clicked(bool checked)
void textChanged(const QString &text)
bool disconnect(const QMetaObject::Connection &connection)
virtual bool eventFilter(QObject *watched, QEvent *event)
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 Dec 6 2024 12:01:00 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.