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"
30 #include <KActionCollection>
32 #include <KInputDialog>
34 #include <KMessageBox>
36 #include <QItemSelectionModel>
38 #include <boost/static_assert.hpp>
40 using namespace Akonadi;
50 } agentActionData[] = {
51 {
"akonadi_agentinstance_create", I18N_NOOP(
"&New Agent Instance..." ),
52 "folder-new", 0, SLOT(slotCreateAgentInstance())
54 {
"akonadi_agentinstance_delete", I18N_NOOP(
"&Delete Agent Instance" ),
55 "edit-delete", 0, SLOT(slotDeleteAgentInstance())
57 {
"akonadi_agentinstance_configure", I18N_NOOP(
"&Configure Agent Instance" ),
58 "configure", 0, SLOT(slotConfigureAgentInstance())
61 static const int numAgentActionData =
sizeof agentActionData /
sizeof *agentActionData;
68 class AgentActionManager::Private
79 i18nc(
"@title:window",
"New Agent Instance" ) );
83 ki18n(
"Could not create agent instance: %1" ) );
87 i18n(
"Agent instance creation failed" ) );
91 i18nc(
"@title:window",
"Delete Agent Instance?" ) );
95 i18n(
"Do you really want to delete the selected agent instance?" ) );
101 if ( mActions[ type ] ) {
102 mActions[ type ]->setEnabled( enable );
110 const bool createActionEnabled =
true;
111 bool deleteActionEnabled =
true;
112 bool configureActionEnabled =
true;
114 if ( instances.isEmpty() ) {
115 deleteActionEnabled =
false;
116 configureActionEnabled =
false;
119 if ( instances.count() == 1 ) {
121 if ( instance.
type().
capabilities().contains( QLatin1String(
"NoConfig" ) ) ) {
122 configureActionEnabled =
false;
130 emit q->actionStateUpdated();
137 if ( !mSelectionModel ) {
141 foreach (
const QModelIndex &index, mSelectionModel->selectedRows() ) {
145 instances << instance;
152 void slotCreateAgentInstance()
158 foreach (
const QString &mimeType, mMimeTypeFilter ) {
159 dlg.agentFilterProxyModel()->addMimeTypeFilter( mimeType );
162 foreach (
const QString &capability, mCapabilityFilter ) {
163 dlg.agentFilterProxyModel()->addCapabilityFilter( capability );
167 const AgentType agentType = dlg.agentType();
171 q->connect( job, SIGNAL(result(KJob*)), SLOT(slotAgentInstanceCreationResult(KJob*)) );
178 void slotDeleteAgentInstance()
181 if ( !instances.isEmpty() ) {
182 if ( KMessageBox::questionYesNo(
188 KStandardGuiItem::del(),
189 KStandardGuiItem::cancel(),
191 KMessageBox::Dangerous ) == KMessageBox::Yes ) {
200 void slotConfigureAgentInstance()
203 if ( instances.isEmpty() ) {
207 instances.first().configure( mParentWidget );
210 void slotAgentInstanceCreationResult( KJob *job )
212 if ( job->error() ) {
225 mContextTexts[ type ].insert( context, data );
232 mContextTexts[ type ].insert( context, data.toString() );
238 return mContextTexts[ type ].value( context );
242 KActionCollection *mActionCollection;
243 QWidget *mParentWidget;
244 QItemSelectionModel *mSelectionModel;
245 QVector<KAction*> mActions;
246 QStringList mMimeTypeFilter;
247 QStringList mCapabilityFilter;
249 typedef QHash<AgentActionManager::TextContext, QString> ContextTexts;
250 QHash<AgentActionManager::Type, ContextTexts> mContextTexts;
257 d( new Private( this ) )
259 d->mParentWidget = parent;
260 d->mActionCollection = actionCollection;
263 AgentActionManager::~AgentActionManager()
270 d->mSelectionModel = selectionModel;
271 connect( selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
272 SLOT(updateActions()) );
277 d->mMimeTypeFilter = mimeTypes;
282 d->mCapabilityFilter = capabilities;
287 Q_ASSERT( type >= 0 && type <
LastType );
288 Q_ASSERT( agentActionData[ type ].name );
289 if ( d->mActions[ type ] ) {
290 return d->mActions[ type ];
293 KAction *
action =
new KAction( d->mParentWidget );
294 action->setText( i18n( agentActionData[ type ].label ) );
296 if ( agentActionData[ type ].icon ) {
297 action->setIcon( KIcon( QString::fromLatin1( agentActionData[ type ].icon ) ) );
300 action->setShortcut( agentActionData[ type ].shortcut );
302 if ( agentActionData[ type ].slot ) {
303 connect( action, SIGNAL(triggered()), agentActionData[ type ].slot );
306 d->mActionCollection->addAction( QString::fromLatin1( agentActionData[ type ].name ), action );
307 d->mActions[ type ] =
action;
315 for (
int type = 0; type <
LastType; ++type ) {
322 Q_ASSERT( type >= 0 && type < LastType );
323 return d->mActions[ type ];
328 Q_ASSERT( type >= 0 && type < LastType );
330 const KAction *action = d->mActions[ type ];
337 disconnect( action, SIGNAL(triggered()),
this, agentActionData[ type ].slot );
339 connect( action, SIGNAL(triggered()), agentActionData[ type ].slot );
345 return d->selectedAgentInstances();
350 d->setContextText( type, context, text );
354 const KLocalizedString &text )
356 d->setContextText( type, context, text );
359 #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.