Akonadi

manageaccountwidget.cpp
1 /*
2  SPDX-FileCopyrightText: 2014-2022 Laurent Montel <[email protected]>
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 <KWindowSystem>
21 #include <QAbstractItemDelegate>
22 #include <QAbstractItemView>
23 #include <QKeyEvent>
24 #include <QPointer>
25 
26 using namespace Akonadi;
27 
28 class Akonadi::ManageAccountWidgetPrivate
29 {
30 public:
31  QString mSpecialCollectionIdentifier;
32 
33  QStringList mMimeTypeFilter;
34  QStringList mCapabilityFilter;
35  QStringList mExcludeCapabilities;
36 
37  Ui::ManageAccountWidget ui;
38 };
39 
40 ManageAccountWidget::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  slotAccountSelected(d->ui.mAccountList->currentAgentInstance());
61 }
62 
63 ManageAccountWidget::~ManageAccountWidget() = default;
64 
65 void ManageAccountWidget::slotSearchAgentType(const QString &str)
66 {
67  d->ui.mAccountList->agentFilterProxyModel()->setFilterRegularExpression(str);
68 }
69 
70 void ManageAccountWidget::disconnectAddAccountButton()
71 {
72  disconnect(d->ui.mAddAccountButton, &QPushButton::clicked, this, &ManageAccountWidget::slotAddAccount);
73 }
74 
75 QPushButton *ManageAccountWidget::addAccountButton() const
76 {
77  return d->ui.mAddAccountButton;
78 }
79 
80 void ManageAccountWidget::setDescriptionLabelText(const QString &text)
81 {
82  d->ui.label->setText(text);
83 }
84 
85 bool ManageAccountWidget::eventFilter(QObject *obj, QEvent *event)
86 {
87  if (event->type() == QEvent::KeyPress && obj == d->ui.mFilterAccount) {
88  auto key = static_cast<QKeyEvent *>(event);
89  if ((key->key() == Qt::Key_Enter) || (key->key() == Qt::Key_Return)) {
90  event->accept();
91  return true;
92  }
93  }
94  return QWidget::eventFilter(obj, event);
95 }
96 
97 QAbstractItemView *ManageAccountWidget::view() const
98 {
99  return d->ui.mAccountList->view();
100 }
101 
102 void ManageAccountWidget::setSpecialCollectionIdentifier(const QString &identifier)
103 {
104  d->mSpecialCollectionIdentifier = identifier;
105 }
106 
107 void ManageAccountWidget::slotAddAccount()
108 {
109  Akonadi::AgentTypeDialog dlg(this);
110 
111  Akonadi::AgentFilterProxyModel *filter = dlg.agentFilterProxyModel();
112  for (const QString &filterStr : std::as_const(d->mMimeTypeFilter)) {
113  filter->addMimeTypeFilter(filterStr);
114  }
115  for (const QString &capa : std::as_const(d->mCapabilityFilter)) {
116  filter->addCapabilityFilter(capa);
117  }
118  for (const QString &capa : std::as_const(d->mExcludeCapabilities)) {
119  filter->excludeCapabilities(capa);
120  }
121  if (dlg.exec()) {
122  const Akonadi::AgentType agentType = dlg.agentType();
123 
124  if (agentType.isValid()) {
125  auto job = new Akonadi::AgentInstanceCreateJob(agentType, this);
126  job->configure(this);
127  job->start();
128  }
129  }
130 }
131 
132 QStringList ManageAccountWidget::excludeCapabilities() const
133 {
134  return d->mExcludeCapabilities;
135 }
136 
137 void ManageAccountWidget::setExcludeCapabilities(const QStringList &excludeCapabilities)
138 {
139  d->mExcludeCapabilities = excludeCapabilities;
140  for (const QString &capability : std::as_const(d->mExcludeCapabilities)) {
141  d->ui.mAccountList->agentFilterProxyModel()->excludeCapabilities(capability);
142  }
143 }
144 
145 void ManageAccountWidget::setItemDelegate(QAbstractItemDelegate *delegate)
146 {
147  d->ui.mAccountList->view()->setItemDelegate(delegate);
148 }
149 
150 QStringList ManageAccountWidget::capabilityFilter() const
151 {
152  return d->mCapabilityFilter;
153 }
154 
155 void ManageAccountWidget::setCapabilityFilter(const QStringList &capabilityFilter)
156 {
157  d->mCapabilityFilter = capabilityFilter;
158  for (const QString &capability : std::as_const(d->mCapabilityFilter)) {
159  d->ui.mAccountList->agentFilterProxyModel()->addCapabilityFilter(capability);
160  }
161 }
162 
163 QStringList ManageAccountWidget::mimeTypeFilter() const
164 {
165  return d->mMimeTypeFilter;
166 }
167 
168 void ManageAccountWidget::setMimeTypeFilter(const QStringList &mimeTypeFilter)
169 {
170  d->mMimeTypeFilter = mimeTypeFilter;
171  for (const QString &mimeType : std::as_const(d->mMimeTypeFilter)) {
172  d->ui.mAccountList->agentFilterProxyModel()->addMimeTypeFilter(mimeType);
173  }
174 }
175 
176 void ManageAccountWidget::slotModifySelectedAccount()
177 {
178  Akonadi::AgentInstance instance = d->ui.mAccountList->currentAgentInstance();
179  if (instance.isValid()) {
181  QPointer<AgentConfigurationDialog> dlg(new AgentConfigurationDialog(instance, this));
182  dlg->exec();
183  delete dlg;
184  }
185 }
186 
187 void ManageAccountWidget::slotRestartSelectedAccount()
188 {
189  const Akonadi::AgentInstance instance = d->ui.mAccountList->currentAgentInstance();
190  if (instance.isValid()) {
191  instance.restart();
192  }
193 }
194 
195 void ManageAccountWidget::slotRemoveSelectedAccount()
196 {
197  const Akonadi::AgentInstance instance = d->ui.mAccountList->currentAgentInstance();
198 
199  const int rc = KMessageBox::questionYesNo(this,
200  i18n("Do you want to remove account '%1'?", instance.name()),
201  i18n("Remove account?"),
204  if (rc == KMessageBox::No) {
205  return;
206  }
207 
208  if (instance.isValid()) {
210  }
211 
212  slotAccountSelected(d->ui.mAccountList->currentAgentInstance());
213 }
214 
215 void ManageAccountWidget::slotAccountSelected(const Akonadi::AgentInstance &current)
216 {
217  if (current.isValid()) {
218  d->ui.mModifyAccountButton->setEnabled(!current.type().capabilities().contains(QLatin1String("NoConfig")));
219  d->ui.mRemoveAccountButton->setEnabled(d->mSpecialCollectionIdentifier != current.identifier());
220  // Restarting an agent is not possible if it's in Running status... (see AgentProcessInstance::restartWhenIdle)
221  d->ui.mRestartAccountButton->setEnabled((current.status() != 1));
222  } else {
223  d->ui.mModifyAccountButton->setEnabled(false);
224  d->ui.mRemoveAccountButton->setEnabled(false);
225  d->ui.mRestartAccountButton->setEnabled(false);
226  }
227 }
QString identifier() const
Returns the unique identifier of the agent instance.
void clicked(const Akonadi::AgentInstance &current)
This signal is emitted whenever there is a click on an agent instance.
bool isValid() const
Returns whether the agent type is valid.
Status status() const
Returns the status of the agent instance.
AgentType type() const
Returns the agent type of this instance.
A representation of an agent type.
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
void clicked(bool checked)
KGuiItem remove()
void removeInstance(const AgentInstance &instance)
Removes the given agent instance.
virtual bool eventFilter(QObject *watched, QEvent *event)
QStringList capabilities() const
Returns the list of supported capabilities of the agent type.
ButtonCode questionYesNo(QWidget *parent, const QString &text, const QString &caption=QString(), const KGuiItem &buttonYes=KStandardGuiItem::yes(), const KGuiItem &buttonNo=KStandardGuiItem::no(), const QString &dontAskAgainName=QString(), Options options=Notify)
void restart() const
Restart the agent process.
QString i18n(const char *text, const TYPE &arg...)
void textChanged(const QString &text)
QFuture< void > filter(Sequence &sequence, KeepFunctor filterFunction)
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
Key_Enter
void setupUi(QWidget *widget)
QString name() const
Returns the user visible name of the agent instance.
void doubleClicked(const Akonadi::AgentInstance &current)
This signal is emitted whenever there is a double click on an agent instance.
A proxy model for filtering AgentType or AgentInstance.
static AgentManager * self()
Returns the global instance of the agent manager.
A dialog to select an available agent type.
static void allowExternalProcessWindowActivation(int pid=-1)
KGuiItem cancel()
bool isValid() const
Returns whether the agent instance object is valid.
A representation of an agent instance.
void setText(const QString &text)
Job for creating new agent instances.
void accept()
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Mon Jun 27 2022 04:01:06 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.