20#include "agentactionmanager.h"
22#include "agentfilterproxymodel.h"
23#include "agentinstancecreatejob.h"
24#include "agentinstancemodel.h"
25#include "agentmanager.h"
26#include "agenttypedialog.h"
31#include <KActionCollection>
33#include <KLocalizedString>
36#include <QItemSelectionModel>
39#include <boost/static_assert.hpp>
51} agentActionData[] = {
52 {
"akonadi_agentinstance_create", I18N_NOOP(
"&New Agent Instance..."),
53 "folder-new", 0, SLOT(slotCreateAgentInstance())
55 {
"akonadi_agentinstance_delete", I18N_NOOP(
"&Delete Agent Instance"),
56 "edit-delete", 0, SLOT(slotDeleteAgentInstance())
58 {
"akonadi_agentinstance_configure", I18N_NOOP(
"&Configure Agent Instance"),
59 "configure", 0, SLOT(slotConfigureAgentInstance())
62static const int numAgentActionData =
sizeof agentActionData /
sizeof * agentActionData;
69class AgentActionManager::Private
80 i18nc(
"@title:window",
"New Agent Instance"));
84 ki18n(
"Could not create agent instance: %1"));
88 i18n(
"Agent instance creation failed"));
92 i18nc(
"@title:window",
"Delete Agent Instance?"));
96 i18n(
"Do you really want to delete the selected agent instance?"));
102 if (mActions[type]) {
103 mActions[type]->setEnabled(enable);
111 const bool createActionEnabled =
true;
112 bool deleteActionEnabled =
true;
113 bool configureActionEnabled =
true;
115 if (instances.isEmpty()) {
116 deleteActionEnabled =
false;
117 configureActionEnabled =
false;
120 if (instances.count() == 1) {
123 configureActionEnabled =
false;
127 enableAction(CreateAgentInstance, createActionEnabled);
128 enableAction(DeleteAgentInstance, deleteActionEnabled);
129 enableAction(ConfigureAgentInstance, configureActionEnabled);
131 emit q->actionStateUpdated();
138 if (!mSelectionModel) {
142 foreach (
const QModelIndex &index, mSelectionModel->selectedRows()) {
146 instances << instance;
153 void slotCreateAgentInstance()
159 foreach (
const QString &mimeType, mMimeTypeFilter) {
160 dlg->agentFilterProxyModel()->addMimeTypeFilter(mimeType);
163 foreach (
const QString &capability, mCapabilityFilter) {
164 dlg->agentFilterProxyModel()->addCapabilityFilter(capability);
167 if (dlg->exec() == QDialog::Accepted && dlg != 0) {
168 const AgentType agentType = dlg->agentType();
172 q->connect(job, SIGNAL(result(KJob*)), SLOT(slotAgentInstanceCreationResult(KJob*)));
180 void slotDeleteAgentInstance()
183 if (!instances.isEmpty()) {
184 if (KMessageBox::questionYesNo(
190 KStandardGuiItem::del(),
191 KStandardGuiItem::cancel(),
193 KMessageBox::Dangerous) == KMessageBox::Yes) {
202 void slotConfigureAgentInstance()
205 if (instances.isEmpty()) {
209 instances.first().configure(mParentWidget);
212 void slotAgentInstanceCreationResult(KJob *job)
227 mContextTexts[type].insert(context, data);
234 mContextTexts[type].insert(context, data.toString());
240 return mContextTexts[type].value(context);
244 KActionCollection *mActionCollection;
245 QWidget *mParentWidget;
246 QItemSelectionModel *mSelectionModel;
247 QVector<KAction *> mActions;
248 QStringList mMimeTypeFilter;
249 QStringList mCapabilityFilter;
251 typedef QHash<AgentActionManager::TextContext, QString> ContextTexts;
252 QHash<AgentActionManager::Type, ContextTexts> mContextTexts;
259 , d(new Private(this))
261 d->mParentWidget = parent;
262 d->mActionCollection = actionCollection;
272 d->mSelectionModel = selectionModel;
273 connect(selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
274 SLOT(updateActions()));
279 d->mMimeTypeFilter = mimeTypes;
284 d->mCapabilityFilter = capabilities;
289 Q_ASSERT(type >= 0 && type <
LastType);
290 Q_ASSERT(agentActionData[type].name);
291 if (d->mActions[type]) {
292 return d->mActions[type];
295 KAction *
action =
new KAction(d->mParentWidget);
296 action->setText(i18n(agentActionData[type].label));
298 if (agentActionData[type].icon) {
299 action->setIcon(KIcon(QString::fromLatin1(agentActionData[type].icon)));
302 action->setShortcut(agentActionData[type].shortcut);
304 if (agentActionData[type].slot) {
305 connect(
action, SIGNAL(triggered()), agentActionData[type].slot);
308 d->mActionCollection->addAction(QString::fromLatin1(agentActionData[type].name),
action);
309 d->mActions[type] =
action;
317 for (
int type = 0; type <
LastType; ++type) {
324 Q_ASSERT(type >= 0 && type <
LastType);
325 return d->mActions[type];
330 Q_ASSERT(type >= 0 && type <
LastType);
332 const KAction *
action = d->mActions[type];
339 disconnect(
action, SIGNAL(triggered()),
this, agentActionData[type].slot);
341 connect(
action, SIGNAL(triggered()), agentActionData[type].slot);
347 return d->selectedAgentInstances();
352 d->setContextText(type, context, text);
356 const KLocalizedString &text)
358 d->setContextText(type, context, text);
361#include "moc_agentactionmanager.cpp"
Manages generic actions for agent and agent instance views.
AgentActionManager(KActionCollection *actionCollection, QWidget *parent=0)
Creates a new agent action manager.
Akonadi::AgentInstance::List selectedAgentInstances() const
Returns the list of agent instances that are currently selected.
KAction * action(Type type) const
Returns the action of the given type, 0 if it has not been created (yet).
void interceptAction(Type type, bool intercept=true)
Sets whether the default implementation for the given action type shall be executed when the action i...
Type
Describes the supported actions.
@ CreateAgentInstance
Creates an agent instance.
@ LastType
Marks last action.
@ DeleteAgentInstance
Deletes the selected agent instance.
KAction * createAction(Type type)
Creates the action of the given type and adds it to the action collection specified in the constructo...
void setMimeTypeFilter(const QStringList &mimeTypes)
Sets the mime type filter that will be used when creating new agent instances.
void setContextText(Type type, TextContext context, const QString &text)
Sets the text of the action type for the given context.
~AgentActionManager()
Destroys the agent action manager.
void setSelectionModel(QItemSelectionModel *model)
Sets the agent selection model based on which the actions should operate.
void createAllActions()
Convenience method to create all standard actions.
TextContext
Describes the text context that can be customized.
@ DialogTitle
The window title of a dialog.
@ MessageBoxText
The text of a message box.
@ ErrorMessageTitle
The window title of an error message.
@ MessageBoxTitle
The window title of a message box.
@ ErrorMessageText
The text of an error message.
void setCapabilityFilter(const QStringList &capabilities)
Sets the capability filter that will be used when creating new agent instances.
Job for creating new agent instances.
void start()
Starts the instance creation.
void configure(QWidget *parent=0)
Setup the job to show agent configuration dialog once the agent instance has been successfully starte...
@ InstanceRole
The agent instance itself.
A representation of an agent instance.
bool isValid() const
Returns whether the agent instance object is valid.
AgentType type() const
Returns the agent type of this instance.
QList< AgentInstance > List
Describes a list of agent instances.
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.
FreeBusyManager::Singleton.