Akonadi

agentactionmanager.cpp
1 /*
2  Copyright (c) 2010 Tobias Koenig <[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 "agentactionmanager.h"
21 
22 #include "agentfilterproxymodel.h"
23 #include "agentinstancecreatejob.h"
24 #include "agentinstancemodel.h"
25 #include "agentmanager.h"
26 #include "agenttypedialog.h"
27 #include "metatypes.h"
28 
29 
30 #include <QAction>
31 #include <QIcon>
32 #include <KActionCollection>
33 #include <KLocalizedString>
34 #include <KMessageBox>
35 
36 #include <QItemSelectionModel>
37 #include <QPointer>
38 
39 using namespace Akonadi;
40 
41 //@cond PRIVATE
42 
43 static const struct {
44  const char *name;
45  const char *label;
46  const char *icon;
47  int shortcut;
48  const char *slot;
49 } agentActionData[] = {
50  {
51  "akonadi_agentinstance_create", I18N_NOOP("&New Agent Instance..."),
52  "folder-new", 0, SLOT(slotCreateAgentInstance())
53  },
54  {
55  "akonadi_agentinstance_delete", I18N_NOOP("&Delete Agent Instance"),
56  "edit-delete", 0, SLOT(slotDeleteAgentInstance())
57  },
58  {
59  "akonadi_agentinstance_configure", I18N_NOOP("&Configure Agent Instance"),
60  "configure", 0, SLOT(slotConfigureAgentInstance())
61  }
62 };
63 static const int numAgentActionData = sizeof agentActionData / sizeof * agentActionData;
64 
65 static_assert(numAgentActionData == AgentActionManager::LastType,
66  "agentActionData table does not match AgentActionManager types");
67 
71 class Q_DECL_HIDDEN AgentActionManager::Private
72 {
73 public:
74  Private(AgentActionManager *parent)
75  : q(parent)
76  , mActionCollection(nullptr)
77  , mParentWidget(nullptr)
78  , mSelectionModel(nullptr)
79  {
80  mActions.fill(nullptr, AgentActionManager::LastType);
81 
84  i18nc("@title:window", "New Agent Instance"));
85 
88  ki18n("Could not create agent instance: %1"));
89 
92  i18n("Agent instance creation failed"));
93 
96  i18nc("@title:window", "Delete Agent Instance?"));
97 
100  i18n("Do you really want to delete the selected agent instance?"));
101  }
102 
103  void enableAction(AgentActionManager::Type type, bool enable)
104  {
105  Q_ASSERT(type >= 0 && type < AgentActionManager::LastType);
106  if (QAction *act = mActions[type]) {
107  act->setEnabled(enable);
108  }
109  }
110 
111  void updateActions()
112  {
113  const AgentInstance::List instances = selectedAgentInstances();
114 
115  const bool createActionEnabled = true;
116  bool deleteActionEnabled = true;
117  bool configureActionEnabled = true;
118 
119  if (instances.isEmpty()) {
120  deleteActionEnabled = false;
121  configureActionEnabled = false;
122  }
123 
124  if (instances.count() == 1) {
125  const AgentInstance instance = instances.first();
126  if (instance.type().capabilities().contains(QLatin1String("NoConfig"))) {
127  configureActionEnabled = false;
128  }
129  }
130 
131  enableAction(CreateAgentInstance, createActionEnabled);
132  enableAction(DeleteAgentInstance, deleteActionEnabled);
133  enableAction(ConfigureAgentInstance, configureActionEnabled);
134 
135  Q_EMIT q->actionStateUpdated();
136  }
137 
138  AgentInstance::List selectedAgentInstances() const
139  {
140  AgentInstance::List instances;
141 
142  if (!mSelectionModel) {
143  return instances;
144  }
145 
146  const QModelIndexList lstModelIndex = mSelectionModel->selectedRows();
147  for (const QModelIndex &index : lstModelIndex ) {
148  const AgentInstance instance =
149  index.data(AgentInstanceModel::InstanceRole).value<AgentInstance>();
150  if (instance.isValid()) {
151  instances << instance;
152  }
153  }
154 
155  return instances;
156  }
157 
158  void slotCreateAgentInstance()
159  {
161  dlg->setWindowTitle(contextText(AgentActionManager::CreateAgentInstance,
163 
164  for (const QString &mimeType : qAsConst(mMimeTypeFilter)) {
165  dlg->agentFilterProxyModel()->addMimeTypeFilter(mimeType);
166  }
167 
168  for (const QString &capability : qAsConst(mCapabilityFilter)) {
169  dlg->agentFilterProxyModel()->addCapabilityFilter(capability);
170  }
171 
172  if (dlg->exec() == QDialog::Accepted) {
173  const AgentType agentType = dlg->agentType();
174 
175  if (agentType.isValid()) {
176  AgentInstanceCreateJob *job = new AgentInstanceCreateJob(agentType, q);
177  q->connect(job, &KJob::result, q, [this](KJob *job) { slotAgentInstanceCreationResult(job); });
178  job->configure(mParentWidget);
179  job->start();
180  }
181  }
182  delete dlg;
183  }
184 
185  void slotDeleteAgentInstance()
186  {
187  const AgentInstance::List instances = selectedAgentInstances();
188  if (!instances.isEmpty()) {
190  mParentWidget,
197  QString(),
198  KMessageBox::Dangerous) == KMessageBox::Yes) {
199 
200  for (const AgentInstance &instance : instances) {
201  AgentManager::self()->removeInstance(instance);
202  }
203  }
204  }
205  }
206 
207  void slotConfigureAgentInstance()
208  {
209  AgentInstance::List instances = selectedAgentInstances();
210  if (instances.isEmpty()) {
211  return;
212  }
213 
214  instances.first().configure(mParentWidget);
215  }
216 
217  void slotAgentInstanceCreationResult(KJob *job)
218  {
219  if (job->error()) {
221  mParentWidget,
226  }
227  }
228 
229  void setContextText(AgentActionManager::Type type,
230  AgentActionManager::TextContext context, const QString &data)
231  {
232  mContextTexts[type].insert(context, data);
233  }
234 
235  void setContextText(AgentActionManager::Type type,
237  {
238  mContextTexts[type].insert(context, data.toString());
239  }
240 
241  QString contextText(AgentActionManager::Type type,
242  AgentActionManager::TextContext context) const
243  {
244  return mContextTexts[type].value(context);
245  }
246 
247  AgentActionManager *q = nullptr;
248  KActionCollection *mActionCollection = nullptr;
249  QWidget *mParentWidget = nullptr;
250  QItemSelectionModel *mSelectionModel = nullptr;
251  QVector<QAction *> mActions;
252  QStringList mMimeTypeFilter;
253  QStringList mCapabilityFilter;
254 
257 };
258 
259 //@endcond
260 
262  : QObject(parent)
263  , d(new Private(this))
264 {
265  d->mParentWidget = parent;
266  d->mActionCollection = actionCollection;
267 }
268 
270 {
271  delete d;
272 }
273 
275 {
276  d->mSelectionModel = selectionModel;
277  connect(selectionModel, &QItemSelectionModel::selectionChanged,
278  this, [this]() { d->updateActions(); });
279 }
280 
282 {
283  d->mMimeTypeFilter = mimeTypes;
284 }
285 
287 {
288  d->mCapabilityFilter = capabilities;
289 }
290 
292 {
293  Q_ASSERT(type >= 0 && type < LastType);
294  Q_ASSERT(agentActionData[type].name);
295  if (QAction *act = d->mActions[type]) {
296  return act;
297  }
298 
299  QAction *action = new QAction(d->mParentWidget);
300  action->setText(i18n(agentActionData[type].label));
301 
302  if (agentActionData[type].icon) {
303  action->setIcon(QIcon::fromTheme(QString::fromLatin1(agentActionData[type].icon)));
304  }
305 
306  action->setShortcut(agentActionData[type].shortcut);
307 
308  if (agentActionData[type].slot) {
309  connect(action, SIGNAL(triggered()), agentActionData[type].slot);
310  }
311 
312  d->mActionCollection->addAction(QString::fromLatin1(agentActionData[type].name), action);
313  d->mActions[type] = action;
314  d->updateActions();
315 
316  return action;
317 }
318 
320 {
321  for (int type = 0; type < LastType; ++type) {
322  auto action = createAction(static_cast<Type>(type));
323  Q_UNUSED(action);
324  }
325 }
326 
327 QAction *AgentActionManager::action(Type type) const
328 {
329  Q_ASSERT(type >= 0 && type < LastType);
330  return d->mActions[type];
331 }
332 
333 void AgentActionManager::interceptAction(Type type, bool intercept)
334 {
335  Q_ASSERT(type >= 0 && type < LastType);
336 
337  const QAction *action = d->mActions[type];
338 
339  if (!action) {
340  return;
341  }
342 
343  if (intercept) {
344  disconnect(action, SIGNAL(triggered()), this, agentActionData[type].slot);
345  } else {
346  connect(action, SIGNAL(triggered()), agentActionData[type].slot);
347  }
348 }
349 
351 {
352  return d->selectedAgentInstances();
353 }
354 
355 void AgentActionManager::setContextText(Type type, TextContext context, const QString &text)
356 {
357  d->setContextText(type, context, text);
358 }
359 
360 void AgentActionManager::setContextText(Type type, TextContext context,
361  const KLocalizedString &text)
362 {
363  d->setContextText(type, context, text);
364 }
365 
366 #include "moc_agentactionmanager.cpp"
void setText(const QString &text)
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
QString toString() const
QString name(const QVariant &location)
Manages generic actions for agent and agent instance views.
QVector< AgentInstance > List
Describes a list of agent instances.
void createAllActions()
Convenience method to create all standard actions.
virtual QString errorString() const
AgentType type() const
Returns the agent type of this instance.
void setIcon(const QIcon &icon)
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
The window title of a dialog.
bool isValid() const
Returns whether the agent type is valid.
Type
Describes the supported actions.
void removeInstance(const AgentInstance &instance)
Removes the given agent instance.
KGuiItem cancel()
The window title of an error message.
QAction * createAction(Type type)
Creates the action of the given type and adds it to the action collection specified in the constructo...
The window title of a message box.
KGuiItem del()
A representation of an agent type.
TextContext
Describes the text context that can be customized.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString label(StandardShortcut id)
bool isValid() const
Returns whether the agent instance object is valid.
#define I18N_NOOP(text)
void error(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
QAction * action(Type type) const
Returns the action of the given type, 0 if it has not been created (yet).
QStringList capabilities() const
Returns the list of supported capabilities of the agent type.
void setCapabilityFilter(const QStringList &capabilities)
Sets the capability filter that will be used when creating new agent instances.
Akonadi::AgentInstance::List selectedAgentInstances() const
Returns the list of agent instances that are currently selected.
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 setMimeTypeFilter(const QStringList &mimeTypes)
Sets the mime type filter that will be used when creating new agent instances.
void interceptAction(Type type, bool intercept=true)
Sets whether the default implementation for the given action type shall be executed when the action i...
void setShortcut(const QKeySequence &shortcut)
Job for creating new agent instances.
QString i18n(const char *text, const TYPE &arg...)
Definition: item.h:44
AgentActionManager(KActionCollection *actionCollection, QWidget *parent=nullptr)
Creates a new agent action manager.
void setSelectionModel(QItemSelectionModel *model)
Sets the agent selection model based on which the actions should operate.
Deletes the selected agent instance.
Helper integration between Akonadi and Qt.
virtual Q_SCRIPTABLE void start()=0
void setContextText(Type type, TextContext context, const QString &text)
Sets the text of the action type for the given context.
A dialog to select an available agent type.
static AgentManager * self()
Returns the global instance of the agent manager.
QString fromLatin1(const char *str, int size)
KLocalizedString KI18N_EXPORT ki18n(const char *text)
QIcon fromTheme(const QString &name)
const QList< QKeySequence > & shortcut(StandardShortcut id)
A representation of an agent instance.
void result(KJob *job)
~AgentActionManager()
Destroys the agent action manager.
int error() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Jun 5 2020 23:08:53 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.