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