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

akonadi

  • 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 <KActionCollection>
31 #include <KDebug>
32 #include <KInputDialog>
33 #include <KLocale>
34 #include <KMessageBox>
35 
36 #include <QItemSelectionModel>
37 
38 #include <boost/static_assert.hpp>
39 
40 using namespace Akonadi;
41 
42 //@cond PRIVATE
43 
44 static const struct {
45  const char *name;
46  const char *label;
47  const char *icon;
48  int shortcut;
49  const char *slot;
50 } agentActionData[] = {
51  { "akonadi_agentinstance_create", I18N_NOOP( "&New Agent Instance..." ),
52  "folder-new", 0, SLOT(slotCreateAgentInstance())
53  },
54  { "akonadi_agentinstance_delete", I18N_NOOP( "&Delete Agent Instance" ),
55  "edit-delete", 0, SLOT(slotDeleteAgentInstance())
56  },
57  { "akonadi_agentinstance_configure", I18N_NOOP( "&Configure Agent Instance" ),
58  "configure", 0, SLOT(slotConfigureAgentInstance())
59  }
60 };
61 static const int numAgentActionData = sizeof agentActionData / sizeof *agentActionData;
62 
63 BOOST_STATIC_ASSERT( numAgentActionData == AgentActionManager::LastType );
64 
68 class AgentActionManager::Private
69 {
70  public:
71  Private( AgentActionManager *parent ) :
72  q( parent ),
73  mSelectionModel( 0 )
74  {
75  mActions.fill( 0, AgentActionManager::LastType );
76 
77  setContextText( AgentActionManager::CreateAgentInstance,
78  AgentActionManager::DialogTitle,
79  i18nc( "@title:window", "New Agent Instance" ) );
80 
81  setContextText( AgentActionManager::CreateAgentInstance,
82  AgentActionManager::ErrorMessageText,
83  ki18n( "Could not create agent instance: %1" ) );
84 
85  setContextText( AgentActionManager::CreateAgentInstance,
86  AgentActionManager::ErrorMessageTitle,
87  i18n( "Agent instance creation failed" ) );
88 
89  setContextText( AgentActionManager::DeleteAgentInstance,
90  AgentActionManager::MessageBoxTitle,
91  i18nc( "@title:window", "Delete Agent Instance?" ) );
92 
93  setContextText( AgentActionManager::DeleteAgentInstance,
94  AgentActionManager::MessageBoxText,
95  i18n( "Do you really want to delete the selected agent instance?" ) );
96  }
97 
98  void enableAction( AgentActionManager::Type type, bool enable )
99  {
100  Q_ASSERT( type >= 0 && type < AgentActionManager::LastType );
101  if ( mActions[ type ] ) {
102  mActions[ type ]->setEnabled( enable );
103  }
104  }
105 
106  void updateActions()
107  {
108  const AgentInstance::List instances = selectedAgentInstances();
109 
110  const bool createActionEnabled = true;
111  bool deleteActionEnabled = true;
112  bool configureActionEnabled = true;
113 
114  if ( instances.isEmpty() ) {
115  deleteActionEnabled = false;
116  configureActionEnabled = false;
117  }
118 
119  if ( instances.count() == 1 ) {
120  const AgentInstance instance = instances.first();
121  if ( instance.type().capabilities().contains( QLatin1String( "NoConfig" ) ) ) {
122  configureActionEnabled = false;
123  }
124  }
125 
126  enableAction( CreateAgentInstance, createActionEnabled );
127  enableAction( DeleteAgentInstance, deleteActionEnabled );
128  enableAction( ConfigureAgentInstance, configureActionEnabled );
129 
130  emit q->actionStateUpdated();
131  }
132 
133  AgentInstance::List selectedAgentInstances() const
134  {
135  AgentInstance::List instances;
136 
137  if ( !mSelectionModel ) {
138  return instances;
139  }
140 
141  foreach ( const QModelIndex &index, mSelectionModel->selectedRows() ) {
142  const AgentInstance instance =
143  index.data( AgentInstanceModel::InstanceRole ).value<AgentInstance>();
144  if ( instance.isValid() ) {
145  instances << instance;
146  }
147  }
148 
149  return instances;
150  }
151 
152  void slotCreateAgentInstance()
153  {
154  Akonadi::AgentTypeDialog dlg( mParentWidget );
155  dlg.setCaption( contextText( AgentActionManager::CreateAgentInstance,
156  AgentActionManager::DialogTitle ) );
157 
158  foreach ( const QString &mimeType, mMimeTypeFilter ) {
159  dlg.agentFilterProxyModel()->addMimeTypeFilter( mimeType );
160  }
161 
162  foreach ( const QString &capability, mCapabilityFilter ) {
163  dlg.agentFilterProxyModel()->addCapabilityFilter( capability );
164  }
165 
166  if ( dlg.exec() ) {
167  const AgentType agentType = dlg.agentType();
168 
169  if ( agentType.isValid() ) {
170  AgentInstanceCreateJob *job = new AgentInstanceCreateJob( agentType, q );
171  q->connect( job, SIGNAL(result(KJob*)), SLOT(slotAgentInstanceCreationResult(KJob*)) );
172  job->configure( mParentWidget );
173  job->start();
174  }
175  }
176  }
177 
178  void slotDeleteAgentInstance()
179  {
180  const AgentInstance::List instances = selectedAgentInstances();
181  if ( !instances.isEmpty() ) {
182  if ( KMessageBox::questionYesNo(
183  mParentWidget,
184  contextText( AgentActionManager::DeleteAgentInstance,
185  AgentActionManager::MessageBoxText ),
186  contextText( AgentActionManager::DeleteAgentInstance,
187  AgentActionManager::MessageBoxTitle ),
188  KStandardGuiItem::del(),
189  KStandardGuiItem::cancel(),
190  QString(),
191  KMessageBox::Dangerous ) == KMessageBox::Yes ) {
192 
193  foreach ( const AgentInstance &instance, instances ) {
194  AgentManager::self()->removeInstance( instance );
195  }
196  }
197  }
198  }
199 
200  void slotConfigureAgentInstance()
201  {
202  AgentInstance::List instances = selectedAgentInstances();
203  if ( instances.isEmpty() ) {
204  return;
205  }
206 
207  instances.first().configure( mParentWidget );
208  }
209 
210  void slotAgentInstanceCreationResult( KJob *job )
211  {
212  if ( job->error() ) {
213  KMessageBox::error(
214  mParentWidget,
215  contextText( AgentActionManager::CreateAgentInstance,
216  AgentActionManager::ErrorMessageText ).arg( job->errorString() ),
217  contextText( AgentActionManager::CreateAgentInstance,
218  AgentActionManager::ErrorMessageTitle ) );
219  }
220  }
221 
222  void setContextText( AgentActionManager::Type type,
223  AgentActionManager::TextContext context, const QString &data )
224  {
225  mContextTexts[ type ].insert( context, data );
226  }
227 
228  void setContextText( AgentActionManager::Type type,
229  AgentActionManager::TextContext context, const KLocalizedString &data )
230  {
231 
232  mContextTexts[ type ].insert( context, data.toString() );
233  }
234 
235  QString contextText( AgentActionManager::Type type,
236  AgentActionManager::TextContext context ) const
237  {
238  return mContextTexts[ type ].value( context );
239  }
240 
241  AgentActionManager *q;
242  KActionCollection *mActionCollection;
243  QWidget *mParentWidget;
244  QItemSelectionModel *mSelectionModel;
245  QVector<KAction*> mActions;
246  QStringList mMimeTypeFilter;
247  QStringList mCapabilityFilter;
248 
249  typedef QHash<AgentActionManager::TextContext, QString> ContextTexts;
250  QHash<AgentActionManager::Type, ContextTexts> mContextTexts;
251 };
252 
253 //@endcond
254 
255 AgentActionManager::AgentActionManager( KActionCollection *actionCollection, QWidget *parent )
256  : QObject( parent ),
257  d( new Private( this ) )
258 {
259  d->mParentWidget = parent;
260  d->mActionCollection = actionCollection;
261 }
262 
263 AgentActionManager::~AgentActionManager()
264 {
265  delete d;
266 }
267 
268 void AgentActionManager::setSelectionModel( QItemSelectionModel *selectionModel )
269 {
270  d->mSelectionModel = selectionModel;
271  connect( selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
272  SLOT(updateActions()) );
273 }
274 
275 void AgentActionManager::setMimeTypeFilter( const QStringList &mimeTypes )
276 {
277  d->mMimeTypeFilter = mimeTypes;
278 }
279 
280 void AgentActionManager::setCapabilityFilter( const QStringList &capabilities )
281 {
282  d->mCapabilityFilter = capabilities;
283 }
284 
285 KAction *AgentActionManager::createAction( Type type )
286 {
287  Q_ASSERT( type >= 0 && type < LastType );
288  Q_ASSERT( agentActionData[ type ].name );
289  if ( d->mActions[ type ] ) {
290  return d->mActions[ type ];
291  }
292 
293  KAction *action = new KAction( d->mParentWidget );
294  action->setText( i18n( agentActionData[ type ].label ) );
295 
296  if ( agentActionData[ type ].icon ) {
297  action->setIcon( KIcon( QString::fromLatin1( agentActionData[ type ].icon ) ) );
298  }
299 
300  action->setShortcut( agentActionData[ type ].shortcut );
301 
302  if ( agentActionData[ type ].slot ) {
303  connect( action, SIGNAL(triggered()), agentActionData[ type ].slot );
304  }
305 
306  d->mActionCollection->addAction( QString::fromLatin1( agentActionData[ type ].name ), action );
307  d->mActions[ type ] = action;
308  d->updateActions();
309 
310  return action;
311 }
312 
313 void AgentActionManager::createAllActions()
314 {
315  for ( int type = 0; type < LastType; ++type ) {
316  createAction( (Type)type );
317  }
318 }
319 
320 KAction * AgentActionManager::action( Type type ) const
321 {
322  Q_ASSERT( type >= 0 && type < LastType );
323  return d->mActions[ type ];
324 }
325 
326 void AgentActionManager::interceptAction( Type type, bool intercept )
327 {
328  Q_ASSERT( type >= 0 && type < LastType );
329 
330  const KAction *action = d->mActions[ type ];
331 
332  if ( !action ) {
333  return;
334  }
335 
336  if ( intercept ) {
337  disconnect( action, SIGNAL(triggered()), this, agentActionData[ type ].slot );
338  } else {
339  connect( action, SIGNAL(triggered()), agentActionData[ type ].slot );
340  }
341 }
342 
343 AgentInstance::List AgentActionManager::selectedAgentInstances() const
344 {
345  return d->selectedAgentInstances();
346 }
347 
348 void AgentActionManager::setContextText( Type type, TextContext context, const QString &text )
349 {
350  d->setContextText( type, context, text );
351 }
352 
353 void AgentActionManager::setContextText( Type type, TextContext context,
354  const KLocalizedString &text )
355 {
356  d->setContextText( type, context, text );
357 }
358 
359 #include "moc_agentactionmanager.cpp"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:27:31 by doxygen 1.8.3.1 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-4.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
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