• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepimlibs API Reference
  • KDE Home
  • Contact Us
 

akonadi

  • sources
  • kde-4.12
  • kdepimlibs
  • akonadi
agentactionmanager.cpp
1 /*
2  Copyright (c) 2010 Tobias Koenig <tokoe@kde.org>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "agentactionmanager.h"
21 
22 #include "agentfilterproxymodel.h"
23 #include "agentinstancecreatejob.h"
24 #include "agentinstancemodel.h"
25 #include "agentmanager.h"
26 #include "agenttypedialog.h"
27 #include "metatypes.h"
28 
29 #include <KAction>
30 #include <KIcon>
31 #include <KActionCollection>
32 #include <KDebug>
33 #include <KInputDialog>
34 #include <KLocalizedString>
35 #include <KMessageBox>
36 
37 #include <QItemSelectionModel>
38 #include <QPointer>
39 
40 #include <boost/static_assert.hpp>
41 
42 using namespace Akonadi;
43 
44 //@cond PRIVATE
45 
46 static const struct {
47  const char *name;
48  const char *label;
49  const char *icon;
50  int shortcut;
51  const char *slot;
52 } agentActionData[] = {
53  { "akonadi_agentinstance_create", I18N_NOOP( "&New Agent Instance..." ),
54  "folder-new", 0, SLOT(slotCreateAgentInstance())
55  },
56  { "akonadi_agentinstance_delete", I18N_NOOP( "&Delete Agent Instance" ),
57  "edit-delete", 0, SLOT(slotDeleteAgentInstance())
58  },
59  { "akonadi_agentinstance_configure", I18N_NOOP( "&Configure Agent Instance" ),
60  "configure", 0, SLOT(slotConfigureAgentInstance())
61  }
62 };
63 static const int numAgentActionData = sizeof agentActionData / sizeof *agentActionData;
64 
65 BOOST_STATIC_ASSERT( numAgentActionData == AgentActionManager::LastType );
66 
70 class AgentActionManager::Private
71 {
72  public:
73  Private( AgentActionManager *parent ) :
74  q( parent ),
75  mSelectionModel( 0 )
76  {
77  mActions.fill( 0, AgentActionManager::LastType );
78 
79  setContextText( AgentActionManager::CreateAgentInstance,
80  AgentActionManager::DialogTitle,
81  i18nc( "@title:window", "New Agent Instance" ) );
82 
83  setContextText( AgentActionManager::CreateAgentInstance,
84  AgentActionManager::ErrorMessageText,
85  ki18n( "Could not create agent instance: %1" ) );
86 
87  setContextText( AgentActionManager::CreateAgentInstance,
88  AgentActionManager::ErrorMessageTitle,
89  i18n( "Agent instance creation failed" ) );
90 
91  setContextText( AgentActionManager::DeleteAgentInstance,
92  AgentActionManager::MessageBoxTitle,
93  i18nc( "@title:window", "Delete Agent Instance?" ) );
94 
95  setContextText( AgentActionManager::DeleteAgentInstance,
96  AgentActionManager::MessageBoxText,
97  i18n( "Do you really want to delete the selected agent instance?" ) );
98  }
99 
100  void enableAction( AgentActionManager::Type type, bool enable )
101  {
102  Q_ASSERT( type >= 0 && type < AgentActionManager::LastType );
103  if ( mActions[ type ] ) {
104  mActions[ type ]->setEnabled( enable );
105  }
106  }
107 
108  void updateActions()
109  {
110  const AgentInstance::List instances = selectedAgentInstances();
111 
112  const bool createActionEnabled = true;
113  bool deleteActionEnabled = true;
114  bool configureActionEnabled = true;
115 
116  if ( instances.isEmpty() ) {
117  deleteActionEnabled = false;
118  configureActionEnabled = false;
119  }
120 
121  if ( instances.count() == 1 ) {
122  const AgentInstance instance = instances.first();
123  if ( instance.type().capabilities().contains( QLatin1String( "NoConfig" ) ) ) {
124  configureActionEnabled = false;
125  }
126  }
127 
128  enableAction( CreateAgentInstance, createActionEnabled );
129  enableAction( DeleteAgentInstance, deleteActionEnabled );
130  enableAction( ConfigureAgentInstance, configureActionEnabled );
131 
132  emit q->actionStateUpdated();
133  }
134 
135  AgentInstance::List selectedAgentInstances() const
136  {
137  AgentInstance::List instances;
138 
139  if ( !mSelectionModel ) {
140  return instances;
141  }
142 
143  foreach ( const QModelIndex &index, mSelectionModel->selectedRows() ) {
144  const AgentInstance instance =
145  index.data( AgentInstanceModel::InstanceRole ).value<AgentInstance>();
146  if ( instance.isValid() ) {
147  instances << instance;
148  }
149  }
150 
151  return instances;
152  }
153 
154  void slotCreateAgentInstance()
155  {
156  QPointer<Akonadi::AgentTypeDialog> dlg( new Akonadi::AgentTypeDialog( mParentWidget ) );
157  dlg->setCaption( contextText( AgentActionManager::CreateAgentInstance,
158  AgentActionManager::DialogTitle ) );
159 
160  foreach ( const QString &mimeType, mMimeTypeFilter ) {
161  dlg->agentFilterProxyModel()->addMimeTypeFilter( mimeType );
162  }
163 
164  foreach ( const QString &capability, mCapabilityFilter ) {
165  dlg->agentFilterProxyModel()->addCapabilityFilter( capability );
166  }
167 
168  if ( dlg->exec() == QDialog::Accepted && dlg != 0 ) {
169  const AgentType agentType = dlg->agentType();
170 
171  if ( agentType.isValid() ) {
172  AgentInstanceCreateJob *job = new AgentInstanceCreateJob( agentType, q );
173  q->connect( job, SIGNAL(result(KJob*)), SLOT(slotAgentInstanceCreationResult(KJob*)) );
174  job->configure( mParentWidget );
175  job->start();
176  }
177  }
178  delete dlg;
179  }
180 
181  void slotDeleteAgentInstance()
182  {
183  const AgentInstance::List instances = selectedAgentInstances();
184  if ( !instances.isEmpty() ) {
185  if ( KMessageBox::questionYesNo(
186  mParentWidget,
187  contextText( AgentActionManager::DeleteAgentInstance,
188  AgentActionManager::MessageBoxText ),
189  contextText( AgentActionManager::DeleteAgentInstance,
190  AgentActionManager::MessageBoxTitle ),
191  KStandardGuiItem::del(),
192  KStandardGuiItem::cancel(),
193  QString(),
194  KMessageBox::Dangerous ) == KMessageBox::Yes ) {
195 
196  foreach ( const AgentInstance &instance, instances ) {
197  AgentManager::self()->removeInstance( instance );
198  }
199  }
200  }
201  }
202 
203  void slotConfigureAgentInstance()
204  {
205  AgentInstance::List instances = selectedAgentInstances();
206  if ( instances.isEmpty() ) {
207  return;
208  }
209 
210  instances.first().configure( mParentWidget );
211  }
212 
213  void slotAgentInstanceCreationResult( KJob *job )
214  {
215  if ( job->error() ) {
216  KMessageBox::error(
217  mParentWidget,
218  contextText( AgentActionManager::CreateAgentInstance,
219  AgentActionManager::ErrorMessageText ).arg( job->errorString() ),
220  contextText( AgentActionManager::CreateAgentInstance,
221  AgentActionManager::ErrorMessageTitle ) );
222  }
223  }
224 
225  void setContextText( AgentActionManager::Type type,
226  AgentActionManager::TextContext context, const QString &data )
227  {
228  mContextTexts[ type ].insert( context, data );
229  }
230 
231  void setContextText( AgentActionManager::Type type,
232  AgentActionManager::TextContext context, const KLocalizedString &data )
233  {
234 
235  mContextTexts[ type ].insert( context, data.toString() );
236  }
237 
238  QString contextText( AgentActionManager::Type type,
239  AgentActionManager::TextContext context ) const
240  {
241  return mContextTexts[ type ].value( context );
242  }
243 
244  AgentActionManager *q;
245  KActionCollection *mActionCollection;
246  QWidget *mParentWidget;
247  QItemSelectionModel *mSelectionModel;
248  QVector<KAction*> mActions;
249  QStringList mMimeTypeFilter;
250  QStringList mCapabilityFilter;
251 
252  typedef QHash<AgentActionManager::TextContext, QString> ContextTexts;
253  QHash<AgentActionManager::Type, ContextTexts> mContextTexts;
254 };
255 
256 //@endcond
257 
258 AgentActionManager::AgentActionManager( KActionCollection *actionCollection, QWidget *parent )
259  : QObject( parent ),
260  d( new Private( this ) )
261 {
262  d->mParentWidget = parent;
263  d->mActionCollection = actionCollection;
264 }
265 
266 AgentActionManager::~AgentActionManager()
267 {
268  delete d;
269 }
270 
271 void AgentActionManager::setSelectionModel( QItemSelectionModel *selectionModel )
272 {
273  d->mSelectionModel = selectionModel;
274  connect( selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
275  SLOT(updateActions()) );
276 }
277 
278 void AgentActionManager::setMimeTypeFilter( const QStringList &mimeTypes )
279 {
280  d->mMimeTypeFilter = mimeTypes;
281 }
282 
283 void AgentActionManager::setCapabilityFilter( const QStringList &capabilities )
284 {
285  d->mCapabilityFilter = capabilities;
286 }
287 
288 KAction *AgentActionManager::createAction( Type type )
289 {
290  Q_ASSERT( type >= 0 && type < LastType );
291  Q_ASSERT( agentActionData[ type ].name );
292  if ( d->mActions[ type ] ) {
293  return d->mActions[ type ];
294  }
295 
296  KAction *action = new KAction( d->mParentWidget );
297  action->setText( i18n( agentActionData[ type ].label ) );
298 
299  if ( agentActionData[ type ].icon ) {
300  action->setIcon( KIcon( QString::fromLatin1( agentActionData[ type ].icon ) ) );
301  }
302 
303  action->setShortcut( agentActionData[ type ].shortcut );
304 
305  if ( agentActionData[ type ].slot ) {
306  connect( action, SIGNAL(triggered()), agentActionData[ type ].slot );
307  }
308 
309  d->mActionCollection->addAction( QString::fromLatin1( agentActionData[ type ].name ), action );
310  d->mActions[ type ] = action;
311  d->updateActions();
312 
313  return action;
314 }
315 
316 void AgentActionManager::createAllActions()
317 {
318  for ( int type = 0; type < LastType; ++type ) {
319  createAction( (Type)type );
320  }
321 }
322 
323 KAction * AgentActionManager::action( Type type ) const
324 {
325  Q_ASSERT( type >= 0 && type < LastType );
326  return d->mActions[ type ];
327 }
328 
329 void AgentActionManager::interceptAction( Type type, bool intercept )
330 {
331  Q_ASSERT( type >= 0 && type < LastType );
332 
333  const KAction *action = d->mActions[ type ];
334 
335  if ( !action ) {
336  return;
337  }
338 
339  if ( intercept ) {
340  disconnect( action, SIGNAL(triggered()), this, agentActionData[ type ].slot );
341  } else {
342  connect( action, SIGNAL(triggered()), agentActionData[ type ].slot );
343  }
344 }
345 
346 AgentInstance::List AgentActionManager::selectedAgentInstances() const
347 {
348  return d->selectedAgentInstances();
349 }
350 
351 void AgentActionManager::setContextText( Type type, TextContext context, const QString &text )
352 {
353  d->setContextText( type, context, text );
354 }
355 
356 void AgentActionManager::setContextText( Type type, TextContext context,
357  const KLocalizedString &text )
358 {
359  d->setContextText( type, context, text );
360 }
361 
362 #include "moc_agentactionmanager.cpp"
Akonadi::AgentInstanceModel::InstanceRole
The agent instance itself.
Definition: agentinstancemodel.h:64
Akonadi::AgentActionManager::ConfigureAgentInstance
Configures the selected agent instance.
Definition: agentactionmanager.h:53
Akonadi::AgentInstance::List
QList< AgentInstance > List
Describes a list of agent instances.
Definition: agentinstance.h:71
Akonadi::AgentActionManager
Manages generic actions for agent and agent instance views.
Definition: agentactionmanager.h:43
Akonadi::AgentActionManager::createAllActions
void createAllActions()
Convenience method to create all standard actions.
Akonadi::AgentActionManager::ErrorMessageText
The text of an error message.
Definition: agentactionmanager.h:67
Akonadi::AgentInstanceCreateJob::configure
void configure(QWidget *parent=0)
Setup the job to show agent configuration dialog once the agent instance has been successfully starte...
Definition: agentinstancecreatejob.cpp:164
Akonadi::AgentInstance::type
AgentType type() const
Returns the agent type of this instance.
Definition: agentinstance.cpp:50
Akonadi::AgentActionManager::action
KAction * action(Type type) const
Returns the action of the given type, 0 if it has not been created (yet).
Akonadi::AgentActionManager::DialogTitle
The window title of a dialog.
Definition: agentactionmanager.h:61
Akonadi::AgentType::isValid
bool isValid() const
Returns whether the agent type is valid.
Definition: agenttype.cpp:41
Akonadi::AgentActionManager::Type
Type
Describes the supported actions.
Definition: agentactionmanager.h:50
Akonadi::AgentManager::removeInstance
void removeInstance(const AgentInstance &instance)
Removes the given agent instance.
Definition: agentmanager.cpp:409
Akonadi::AgentActionManager::ErrorMessageTitle
The window title of an error message.
Definition: agentactionmanager.h:66
Akonadi::AgentActionManager::MessageBoxTitle
The window title of a message box.
Definition: agentactionmanager.h:63
Akonadi::AgentActionManager::createAction
KAction * createAction(Type type)
Creates the action of the given type and adds it to the action collection specified in the constructo...
Akonadi::AgentActionManager::CreateAgentInstance
Creates an agent instance.
Definition: agentactionmanager.h:51
Akonadi::AgentType
A representation of an agent type.
Definition: agenttype.h:58
Akonadi::AgentActionManager::TextContext
TextContext
Describes the text context that can be customized.
Definition: agentactionmanager.h:60
Akonadi::AgentInstance::isValid
bool isValid() const
Returns whether the agent instance object is valid.
Definition: agentinstance.cpp:45
Akonadi::AgentType::capabilities
QStringList capabilities() const
Returns the list of supported capabilities of the agent type.
Definition: agenttype.cpp:76
Akonadi::AgentActionManager::setCapabilityFilter
void setCapabilityFilter(const QStringList &capabilities)
Sets the capability filter that will be used when creating new agent instances.
Akonadi::AgentActionManager::selectedAgentInstances
Akonadi::AgentInstance::List selectedAgentInstances() const
Returns the list of agent instances that are currently selected.
Akonadi::AgentActionManager::setMimeTypeFilter
void setMimeTypeFilter(const QStringList &mimeTypes)
Sets the mime type filter that will be used when creating new agent instances.
Akonadi::AgentActionManager::interceptAction
void interceptAction(Type type, bool intercept=true)
Sets whether the default implementation for the given action type shall be executed when the action i...
Akonadi::AgentInstanceCreateJob
Job for creating new agent instances.
Definition: agentinstancecreatejob.h:71
Akonadi::AgentActionManager::setSelectionModel
void setSelectionModel(QItemSelectionModel *model)
Sets the agent selection model based on which the actions should operate.
Akonadi::AgentActionManager::DeleteAgentInstance
Deletes the selected agent instance.
Definition: agentactionmanager.h:52
Akonadi::AgentActionManager::setContextText
void setContextText(Type type, TextContext context, const QString &text)
Sets the text of the action type for the given context.
Akonadi::AgentTypeDialog
A dialog to select an available agent type.
Definition: agenttypedialog.h:53
Akonadi::AgentManager::self
static AgentManager * self()
Returns the global instance of the agent manager.
Definition: agentmanager.cpp:380
Akonadi::AgentActionManager::LastType
Marks last action.
Definition: agentactionmanager.h:54
Akonadi::AgentInstance
A representation of an agent instance.
Definition: agentinstance.h:62
Akonadi::AgentActionManager::AgentActionManager
AgentActionManager(KActionCollection *actionCollection, QWidget *parent=0)
Creates a new agent action manager.
Akonadi::AgentActionManager::MessageBoxText
The text of a message box.
Definition: agentactionmanager.h:64
Akonadi::AgentInstanceCreateJob::start
void start()
Starts the instance creation.
Definition: agentinstancecreatejob.cpp:175
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:26 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal