Akonadi

manageaccountwidget.cpp
1 /*
2  Copyright (c) 2014-2020 Laurent Montel <[email protected]>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "manageaccountwidget.h"
21 
22 #include "agentinstance.h"
23 #include "agentinstancecreatejob.h"
24 #include "agenttypedialog.h"
25 #include "agentfilterproxymodel.h"
26 #include "agentmanager.h"
27 #include "agentconfigurationdialog.h"
28 
29 #include "ui_manageaccountwidget.h"
30 
31 #include <KMessageBox>
32 #include <KLocalizedString>
33 #include <KWindowSystem>
34 #include <QKeyEvent>
35 #include <QAbstractItemDelegate>
36 #include <QAbstractItemView>
37 #include <QPointer>
38 
39 using namespace Akonadi;
40 
41 class Akonadi::ManageAccountWidgetPrivate
42 {
43 public:
44  QString mSpecialCollectionIdentifier;
45 
46  QStringList mMimeTypeFilter;
47  QStringList mCapabilityFilter;
48  QStringList mExcludeCapabilities;
49 
50  Ui::ManageAccountWidget ui;
51 };
52 
53 ManageAccountWidget::ManageAccountWidget(QWidget *parent)
54  : QWidget(parent),
55  d(new Akonadi::ManageAccountWidgetPrivate)
56 {
57  d->ui.setupUi(this);
58  connect(d->ui.mAddAccountButton, &QPushButton::clicked, this, &ManageAccountWidget::slotAddAccount);
59 
60  connect(d->ui.mModifyAccountButton, &QPushButton::clicked, this, &ManageAccountWidget::slotModifySelectedAccount);
61 
62  connect(d->ui.mRemoveAccountButton, &QPushButton::clicked, this, &ManageAccountWidget::slotRemoveSelectedAccount);
63  connect(d->ui.mRestartAccountButton, &QPushButton::clicked, this, &ManageAccountWidget::slotRestartSelectedAccount);
64 
65  connect(d->ui.mAccountList, &Akonadi::AgentInstanceWidget::clicked, this, &ManageAccountWidget::slotAccountSelected);
66  connect(d->ui.mAccountList, &Akonadi::AgentInstanceWidget::doubleClicked, this, &ManageAccountWidget::slotModifySelectedAccount);
67 
68  d->ui.mAccountList->view()->setSelectionMode(QAbstractItemView::SingleSelection);
69 
70  connect(d->ui.mFilterAccount, &QLineEdit::textChanged, d->ui.mAccountList->agentFilterProxyModel(), &AgentFilterProxyModel::setFilterFixedString);
71  d->ui.mFilterAccount->installEventFilter(this);
72  slotAccountSelected(d->ui.mAccountList->currentAgentInstance());
73 }
74 
75 ManageAccountWidget::~ManageAccountWidget() = default;
76 
77 void ManageAccountWidget::disconnectAddAccountButton()
78 {
79  disconnect(d->ui.mAddAccountButton, &QPushButton::clicked, this, &ManageAccountWidget::slotAddAccount);
80 }
81 
82 QPushButton *ManageAccountWidget::addAccountButton() const
83 {
84  return d->ui.mAddAccountButton;
85 }
86 
87 void ManageAccountWidget::setDescriptionLabelText(const QString &text)
88 {
89  d->ui.label->setText(text);
90 }
91 
92 bool ManageAccountWidget::eventFilter(QObject *obj, QEvent *event)
93 {
94  if (event->type() == QEvent::KeyPress && obj == d->ui.mFilterAccount) {
95  QKeyEvent *key = static_cast<QKeyEvent *>(event);
96  if ((key->key() == Qt::Key_Enter) || (key->key() == Qt::Key_Return)) {
97  event->accept();
98  return true;
99  }
100  }
101  return QWidget::eventFilter(obj, event);
102 }
103 
104 QAbstractItemView *ManageAccountWidget::view() const
105 {
106  return d->ui.mAccountList->view();
107 }
108 
109 void ManageAccountWidget::setSpecialCollectionIdentifier(const QString &identifier)
110 {
111  d->mSpecialCollectionIdentifier = identifier;
112 }
113 
114 void ManageAccountWidget::slotAddAccount()
115 {
116  Akonadi::AgentTypeDialog dlg(this);
117 
118  Akonadi::AgentFilterProxyModel *filter = dlg.agentFilterProxyModel();
119  for (const QString &filterStr : qAsConst(d->mMimeTypeFilter)) {
120  filter->addMimeTypeFilter(filterStr);
121  }
122  for (const QString &capa : qAsConst(d->mCapabilityFilter)) {
123  filter->addCapabilityFilter(capa);
124  }
125  for (const QString &capa : qAsConst(d->mExcludeCapabilities)) {
126  filter->excludeCapabilities(capa);
127  }
128  if (dlg.exec()) {
129  const Akonadi::AgentType agentType = dlg.agentType();
130 
131  if (agentType.isValid()) {
132 
134  job->configure(this);
135  job->start();
136  }
137  }
138 }
139 
140 QStringList ManageAccountWidget::excludeCapabilities() const
141 {
142  return d->mExcludeCapabilities;
143 }
144 
145 void ManageAccountWidget::setExcludeCapabilities(const QStringList &excludeCapabilities)
146 {
147  d->mExcludeCapabilities = excludeCapabilities;
148  for (const QString &capability : qAsConst(d->mExcludeCapabilities)) {
149  d->ui.mAccountList->agentFilterProxyModel()->excludeCapabilities(capability);
150  }
151 }
152 
153 void ManageAccountWidget::setItemDelegate(QAbstractItemDelegate *delegate)
154 {
155  d->ui.mAccountList->view()->setItemDelegate(delegate);
156 }
157 
158 QStringList ManageAccountWidget::capabilityFilter() const
159 {
160  return d->mCapabilityFilter;
161 }
162 
163 void ManageAccountWidget::setCapabilityFilter(const QStringList &capabilityFilter)
164 {
165  d->mCapabilityFilter = capabilityFilter;
166  for (const QString &capability : qAsConst(d->mCapabilityFilter)) {
167  d->ui.mAccountList->agentFilterProxyModel()->addCapabilityFilter(capability);
168  }
169 }
170 
171 QStringList ManageAccountWidget::mimeTypeFilter() const
172 {
173  return d->mMimeTypeFilter;
174 }
175 
176 void ManageAccountWidget::setMimeTypeFilter(const QStringList &mimeTypeFilter)
177 {
178  d->mMimeTypeFilter = mimeTypeFilter;
179  for (const QString &mimeType : qAsConst(d->mMimeTypeFilter)) {
180  d->ui.mAccountList->agentFilterProxyModel()->addMimeTypeFilter(mimeType);
181  }
182 }
183 
184 void ManageAccountWidget::slotModifySelectedAccount()
185 {
186  Akonadi::AgentInstance instance = d->ui.mAccountList->currentAgentInstance();
187  if (instance.isValid()) {
189  QPointer<AgentConfigurationDialog> dlg(new AgentConfigurationDialog(instance, this));
190  dlg->exec();
191  delete dlg;
192  }
193 }
194 
195 void ManageAccountWidget::slotRestartSelectedAccount()
196 {
197  const Akonadi::AgentInstance instance = d->ui.mAccountList->currentAgentInstance();
198  if (instance.isValid()) {
199  instance.restart();
200  }
201 }
202 
203 void ManageAccountWidget::slotRemoveSelectedAccount()
204 {
205  const Akonadi::AgentInstance instance = d->ui.mAccountList->currentAgentInstance();
206 
207  const int rc = KMessageBox::questionYesNo(this,
208  i18n("Do you want to remove account '%1'?", instance.name()),
209  i18n("Remove account?"));
210  if (rc == KMessageBox::No) {
211  return;
212  }
213 
214  if (instance.isValid()) {
216  }
217 
218  slotAccountSelected(d->ui.mAccountList->currentAgentInstance());
219 }
220 
221 void ManageAccountWidget::slotAccountSelected(const Akonadi::AgentInstance &current)
222 {
223  if (current.isValid()) {
224  d->ui.mModifyAccountButton->setEnabled(!current.type().capabilities().contains(QLatin1String("NoConfig")));
225  d->ui.mRemoveAccountButton->setEnabled(d->mSpecialCollectionIdentifier != current.identifier());
226  // Restarting an agent is not possible if it's in Running status... (see AgentProcessInstance::restartWhenIdle)
227  d->ui.mRestartAccountButton->setEnabled((current.status() != 1));
228  } else {
229  d->ui.mModifyAccountButton->setEnabled(false);
230  d->ui.mRemoveAccountButton->setEnabled(false);
231  d->ui.mRestartAccountButton->setEnabled(false);
232  }
233 }
QEvent::Type type() const const
void setupUi(QWidget *widget)
void restart() const
Restart the agent process.
void addMimeTypeFilter(const QString &mimeType)
Accept agents supporting mimeType.
AgentType type() const
Returns the agent type of this instance.
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
bool isValid() const
Returns whether the agent type is valid.
void textChanged(const QString &text)
QString identifier() const
Returns the unique identifier of the agent instance.
void start() override
Starts the instance creation.
void removeInstance(const AgentInstance &instance)
Removes the given agent instance.
void excludeCapabilities(const QString &capability)
Excludes agents with the given capability.
A representation of an agent type.
bool isValid() const
Returns whether the agent instance object is valid.
void clicked(bool checked)
QStringList capabilities() const
Returns the list of supported capabilities of the agent type.
virtual bool eventFilter(QObject *watched, QEvent *event)
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)
int key() const const
QFuture< void > filter(Sequence &sequence, KeepFunctor filterFunction)
void accept()
Job for creating new agent instances.
static void allowExternalProcessWindowActivation(int pid=-1)
QString i18n(const char *text, const TYPE &arg...)
QString name() const
Returns the user visible name of the agent instance.
Helper integration between Akonadi and Qt.
A proxy model for filtering AgentType or AgentInstance.
A dialog to select an available agent type.
void doubleClicked(const Akonadi::AgentInstance &current)
This signal is emitted whenever there is a double click on an agent instance.
static AgentManager * self()
Returns the global instance of the agent manager.
void setText(const QString &text)
Status status() const
Returns the status of the agent instance.
void setFilterFixedString(const QString &pattern)
void configure(QWidget *parent=nullptr)
Setup the job to show agent configuration dialog once the agent instance has been successfully starte...
A representation of an agent instance.
void clicked(const Akonadi::AgentInstance &current)
This signal is emitted whenever there is a click on an agent instance.
void addCapabilityFilter(const QString &capability)
Accept agents with the given capability.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Jun 5 2020 23:08:55 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.