20 #include "agentactionmanager.h"
22 #include "agentfilterproxymodel.h"
23 #include "agentinstancecreatejob.h"
24 #include "agentinstancemodel.h"
25 #include "agentmanager.h"
26 #include "agenttypedialog.h"
27 #include "metatypes.h"
31 #include <KActionCollection>
33 #include <KInputDialog>
34 #include <KLocalizedString>
35 #include <KMessageBox>
37 #include <QItemSelectionModel>
40 #include <boost/static_assert.hpp>
42 using namespace Akonadi;
52 } agentActionData[] = {
53 {
"akonadi_agentinstance_create", I18N_NOOP(
"&New Agent Instance..." ),
54 "folder-new", 0, SLOT(slotCreateAgentInstance())
56 {
"akonadi_agentinstance_delete", I18N_NOOP(
"&Delete Agent Instance" ),
57 "edit-delete", 0, SLOT(slotDeleteAgentInstance())
59 {
"akonadi_agentinstance_configure", I18N_NOOP(
"&Configure Agent Instance" ),
60 "configure", 0, SLOT(slotConfigureAgentInstance())
63 static const int numAgentActionData =
sizeof agentActionData /
sizeof *agentActionData;
70 class AgentActionManager::Private
81 i18nc(
"@title:window",
"New Agent Instance" ) );
85 ki18n(
"Could not create agent instance: %1" ) );
89 i18n(
"Agent instance creation failed" ) );
93 i18nc(
"@title:window",
"Delete Agent Instance?" ) );
97 i18n(
"Do you really want to delete the selected agent instance?" ) );
103 if ( mActions[ type ] ) {
104 mActions[ type ]->setEnabled( enable );
112 const bool createActionEnabled =
true;
113 bool deleteActionEnabled =
true;
114 bool configureActionEnabled =
true;
116 if ( instances.isEmpty() ) {
117 deleteActionEnabled =
false;
118 configureActionEnabled =
false;
121 if ( instances.count() == 1 ) {
123 if ( instance.
type().
capabilities().contains( QLatin1String(
"NoConfig" ) ) ) {
124 configureActionEnabled =
false;
132 emit q->actionStateUpdated();
139 if ( !mSelectionModel ) {
143 foreach (
const QModelIndex &index, mSelectionModel->selectedRows() ) {
147 instances << instance;
154 void slotCreateAgentInstance()
160 foreach (
const QString &mimeType, mMimeTypeFilter ) {
161 dlg->agentFilterProxyModel()->addMimeTypeFilter( mimeType );
164 foreach (
const QString &capability, mCapabilityFilter ) {
165 dlg->agentFilterProxyModel()->addCapabilityFilter( capability );
168 if ( dlg->exec() == QDialog::Accepted && dlg != 0 ) {
169 const AgentType agentType = dlg->agentType();
173 q->connect( job, SIGNAL(result(KJob*)), SLOT(slotAgentInstanceCreationResult(KJob*)) );
181 void slotDeleteAgentInstance()
184 if ( !instances.isEmpty() ) {
185 if ( KMessageBox::questionYesNo(
191 KStandardGuiItem::del(),
192 KStandardGuiItem::cancel(),
194 KMessageBox::Dangerous ) == KMessageBox::Yes ) {
203 void slotConfigureAgentInstance()
206 if ( instances.isEmpty() ) {
210 instances.first().configure( mParentWidget );
213 void slotAgentInstanceCreationResult( KJob *job )
215 if ( job->error() ) {
228 mContextTexts[ type ].insert( context, data );
235 mContextTexts[ type ].insert( context, data.toString() );
241 return mContextTexts[ type ].value( context );
245 KActionCollection *mActionCollection;
246 QWidget *mParentWidget;
247 QItemSelectionModel *mSelectionModel;
248 QVector<KAction*> mActions;
249 QStringList mMimeTypeFilter;
250 QStringList mCapabilityFilter;
252 typedef QHash<AgentActionManager::TextContext, QString> ContextTexts;
253 QHash<AgentActionManager::Type, ContextTexts> mContextTexts;
260 d( new Private( this ) )
262 d->mParentWidget = parent;
263 d->mActionCollection = actionCollection;
266 AgentActionManager::~AgentActionManager()
273 d->mSelectionModel = selectionModel;
274 connect( selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
275 SLOT(updateActions()) );
280 d->mMimeTypeFilter = mimeTypes;
285 d->mCapabilityFilter = capabilities;
290 Q_ASSERT( type >= 0 && type <
LastType );
291 Q_ASSERT( agentActionData[ type ].name );
292 if ( d->mActions[ type ] ) {
293 return d->mActions[ type ];
296 KAction *
action =
new KAction( d->mParentWidget );
297 action->setText( i18n( agentActionData[ type ].label ) );
299 if ( agentActionData[ type ].icon ) {
300 action->setIcon( KIcon( QString::fromLatin1( agentActionData[ type ].icon ) ) );
303 action->setShortcut( agentActionData[ type ].shortcut );
305 if ( agentActionData[ type ].slot ) {
306 connect( action, SIGNAL(triggered()), agentActionData[ type ].slot );
309 d->mActionCollection->addAction( QString::fromLatin1( agentActionData[ type ].name ), action );
310 d->mActions[ type ] =
action;
318 for (
int type = 0; type <
LastType; ++type ) {
325 Q_ASSERT( type >= 0 && type < LastType );
326 return d->mActions[ type ];
331 Q_ASSERT( type >= 0 && type < LastType );
333 const KAction *action = d->mActions[ type ];
340 disconnect( action, SIGNAL(triggered()),
this, agentActionData[ type ].slot );
342 connect( action, SIGNAL(triggered()), agentActionData[ type ].slot );
348 return d->selectedAgentInstances();
353 d->setContextText( type, context, text );
357 const KLocalizedString &text )
359 d->setContextText( type, context, text );
362 #include "moc_agentactionmanager.cpp"
The agent instance itself.
Configures the selected agent instance.
QList< AgentInstance > List
Describes a list of agent instances.
Manages generic actions for agent and agent instance views.
void createAllActions()
Convenience method to create all standard actions.
The text of an error message.
void configure(QWidget *parent=0)
Setup the job to show agent configuration dialog once the agent instance has been successfully starte...
AgentType type() const
Returns the agent type of this instance.
KAction * action(Type type) const
Returns the action of the given type, 0 if it has not been created (yet).
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.
The window title of an error message.
The window title of a message box.
KAction * createAction(Type type)
Creates the action of the given type and adds it to the action collection specified in the constructo...
Creates an agent instance.
A representation of an agent type.
TextContext
Describes the text context that can be customized.
bool isValid() const
Returns whether the agent instance object is valid.
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.
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...
Job for creating new agent instances.
void setSelectionModel(QItemSelectionModel *model)
Sets the agent selection model based on which the actions should operate.
Deletes the selected agent instance.
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.
A representation of an agent instance.
AgentActionManager(KActionCollection *actionCollection, QWidget *parent=0)
Creates a new agent action manager.
The text of a message box.
void start()
Starts the instance creation.