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;
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"