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

akonadi

  • akonadi
collectionattributessynchronizationjob.cpp
1 /*
2  * Copyright (c) 2009 Volker Krause <vkrause@kde.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "collectionattributessynchronizationjob.h"
19 #include "dbusconnectionpool.h"
20 #include "kjobprivatebase_p.h"
21 
22 #include <akonadi/agentinstance.h>
23 #include <akonadi/agentmanager.h>
24 #include <akonadi/collection.h>
25 
26 #include <KDebug>
27 #include <KLocale>
28 
29 #include <QDBusConnection>
30 #include <QDBusInterface>
31 #include <QTimer>
32 
33 namespace Akonadi
34 {
35 
36 class CollectionAttributesSynchronizationJobPrivate : public KJobPrivateBase
37 {
38  public:
39  CollectionAttributesSynchronizationJobPrivate( CollectionAttributesSynchronizationJob* parent )
40  : q( parent ),
41  interface( 0 ),
42  safetyTimer( 0 ),
43  timeoutCount( 0 )
44  {
45  }
46 
47  void doStart();
48 
49  CollectionAttributesSynchronizationJob *q;
50  AgentInstance instance;
51  Collection collection;
52  QDBusInterface* interface;
53  QTimer* safetyTimer;
54  int timeoutCount;
55  static int timeoutCountLimit;
56 
57  void slotSynchronized( qlonglong );
58  void slotTimeout();
59 };
60 
61 int CollectionAttributesSynchronizationJobPrivate::timeoutCountLimit = 60;
62 
63 CollectionAttributesSynchronizationJob::CollectionAttributesSynchronizationJob( const Collection &collection, QObject *parent )
64  : KJob( parent ),
65  d( new CollectionAttributesSynchronizationJobPrivate( this ) )
66 {
67  d->instance = AgentManager::self()->instance( collection.resource() );
68  d->collection = collection;
69  d->safetyTimer = new QTimer( this );
70  connect( d->safetyTimer, SIGNAL(timeout()), SLOT(slotTimeout()) );
71  d->safetyTimer->setInterval( 10 * 1000 );
72  d->safetyTimer->setSingleShot( false );
73 }
74 
75 CollectionAttributesSynchronizationJob::~CollectionAttributesSynchronizationJob()
76 {
77  delete d;
78 }
79 
80 void CollectionAttributesSynchronizationJob::start()
81 {
82  d->start();
83 }
84 
85 void CollectionAttributesSynchronizationJobPrivate::doStart()
86 {
87  if ( !collection.isValid() ) {
88  q->setError( KJob::UserDefinedError );
89  q->setErrorText( i18n( "Invalid collection instance." ) );
90  q->emitResult();
91  return;
92  }
93 
94  if ( !instance.isValid() ) {
95  q->setError( KJob::UserDefinedError );
96  q->setErrorText( i18n( "Invalid resource instance." ) );
97  q->emitResult();
98  return;
99  }
100 
101  interface = new QDBusInterface( QString::fromLatin1( "org.freedesktop.Akonadi.Resource.%1" ).arg( instance.identifier() ),
102  QString::fromLatin1( "/" ),
103  QString::fromLatin1( "org.freedesktop.Akonadi.Resource" ),
104  DBusConnectionPool::threadConnection(), this );
105  connect( interface, SIGNAL(attributesSynchronized(qlonglong)),
106  q, SLOT(slotSynchronized(qlonglong)) );
107 
108  if ( interface->isValid() ) {
109  const QDBusMessage reply = interface->call( QString::fromUtf8( "synchronizeCollectionAttributes" ), collection.id() );
110  if ( reply.type() == QDBusMessage::ErrorMessage ) {
111  // This means that the resource doesn't provide a synchronizeCollectionAttributes method, so we just finish the job
112  q->emitResult();
113  return;
114  }
115  safetyTimer->start();
116  } else {
117  q->setError( KJob::UserDefinedError );
118  q->setErrorText( i18n( "Unable to obtain D-Bus interface for resource '%1'", instance.identifier() ) );
119  q->emitResult();
120  return;
121  }
122 }
123 
124 void CollectionAttributesSynchronizationJobPrivate::slotSynchronized( qlonglong id )
125 {
126  if ( id == collection.id() ) {
127  q->disconnect( interface, SIGNAL(attributesSynchronized(qlonglong)),
128  q, SLOT(slotSynchronized(qlonglong)) );
129  safetyTimer->stop();
130  q->emitResult();
131  }
132 }
133 
134 void CollectionAttributesSynchronizationJobPrivate::slotTimeout()
135 {
136  instance = AgentManager::self()->instance( instance.identifier() );
137  timeoutCount++;
138 
139  if ( timeoutCount > timeoutCountLimit ) {
140  safetyTimer->stop();
141  q->setError( KJob::UserDefinedError );
142  q->setErrorText( i18n( "Collection attributes synchronization timed out." ) );
143  q->emitResult();
144  return;
145  }
146 
147  if ( instance.status() == AgentInstance::Idle ) {
148  // try again, we might have lost the synchronized() signal
149  kDebug() << "trying again to sync collection attributes" << collection.id() << instance.identifier();
150  interface->call( QString::fromUtf8( "synchronizeCollectionAttributes" ), collection.id() );
151  }
152 }
153 
154 }
155 
156 #include "collectionattributessynchronizationjob.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jan 5 2013 19:46:02 by doxygen 1.8.1.2 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.9.5 API Reference

Skip menu "kdepimlibs-4.9.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • 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