• 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
resourcesynchronizationjob.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 "resourcesynchronizationjob.h"
19 #include "dbusconnectionpool.h"
20 #include "kjobprivatebase_p.h"
21 #include "servermanager.h"
22 
23 #include <akonadi/agentinstance.h>
24 #include <akonadi/agentmanager.h>
25 
26 #include <KDebug>
27 #include <KLocalizedString>
28 
29 #include <QDBusConnection>
30 #include <QDBusInterface>
31 #include <QTimer>
32 
33 namespace Akonadi
34 {
35 
36 class ResourceSynchronizationJobPrivate : public KJobPrivateBase
37 {
38  public:
39  ResourceSynchronizationJobPrivate( ResourceSynchronizationJob* parent ) :
40  q( parent ),
41  interface( 0 ),
42  safetyTimer( 0 ),
43  timeoutCount( 0 ),
44  collectionTreeOnly( false )
45  {}
46 
47  void doStart();
48 
49  ResourceSynchronizationJob *q;
50  AgentInstance instance;
51  QDBusInterface* interface;
52  QTimer* safetyTimer;
53  int timeoutCount;
54  bool collectionTreeOnly;
55  static int timeoutCountLimit;
56 
57  void slotSynchronized();
58  void slotTimeout();
59 };
60 
61 int ResourceSynchronizationJobPrivate::timeoutCountLimit = 60;
62 
63 ResourceSynchronizationJob::ResourceSynchronizationJob(const AgentInstance& instance, QObject* parent) :
64  KJob( parent ),
65  d( new ResourceSynchronizationJobPrivate( this ) )
66 {
67  d->instance = instance;
68  d->safetyTimer = new QTimer( this );
69  connect( d->safetyTimer, SIGNAL(timeout()), SLOT(slotTimeout()) );
70  d->safetyTimer->setInterval( 10 * 1000 );
71  d->safetyTimer->setSingleShot( false );
72 }
73 
74 ResourceSynchronizationJob::~ResourceSynchronizationJob()
75 {
76  delete d;
77 }
78 
79 void ResourceSynchronizationJob::start()
80 {
81  d->start();
82 }
83 
84 bool ResourceSynchronizationJob::collectionTreeOnly() const
85 {
86  return d->collectionTreeOnly;
87 }
88 
89 void ResourceSynchronizationJob::setCollectionTreeOnly( bool b )
90 {
91  d->collectionTreeOnly = b;
92 }
93 
94 void ResourceSynchronizationJobPrivate::doStart()
95 {
96  if ( !instance.isValid() ) {
97  q->setError( KJob::UserDefinedError );
98  q->setErrorText( i18n( "Invalid resource instance." ) );
99  q->emitResult();
100  return;
101  }
102 
103  interface = new QDBusInterface( ServerManager::agentServiceName( ServerManager::Resource, instance.identifier() ),
104  QString::fromLatin1( "/" ),
105  QString::fromLatin1( "org.freedesktop.Akonadi.Resource" ),
106  DBusConnectionPool::threadConnection(), this );
107  if ( collectionTreeOnly )
108  connect( interface, SIGNAL(collectionTreeSynchronized()), q, SLOT(slotSynchronized()) );
109  else
110  connect( interface, SIGNAL(synchronized()), q, SLOT(slotSynchronized()) );
111 
112  if ( interface->isValid() ) {
113  if ( collectionTreeOnly )
114  instance.synchronizeCollectionTree();
115  else
116  instance.synchronize();
117 
118  safetyTimer->start();
119  } else {
120  q->setError( KJob::UserDefinedError );
121  q->setErrorText( i18n( "Unable to obtain D-Bus interface for resource '%1'", instance.identifier() ) );
122  q->emitResult();
123  return;
124  }
125 }
126 
127 void ResourceSynchronizationJobPrivate::slotSynchronized()
128 {
129  if ( collectionTreeOnly )
130  q->disconnect( interface, SIGNAL(collectionTreeSynchronized()), q, SLOT(slotSynchronized()) );
131  else
132  q->disconnect( interface, SIGNAL(synchronized()), q, SLOT(slotSynchronized()) );
133  safetyTimer->stop();
134  q->emitResult();
135 }
136 
137 void ResourceSynchronizationJobPrivate::slotTimeout()
138 {
139  instance = AgentManager::self()->instance( instance.identifier() );
140  timeoutCount++;
141 
142  if ( timeoutCount > timeoutCountLimit ) {
143  safetyTimer->stop();
144  q->setError( KJob::UserDefinedError );
145  q->setErrorText( i18n( "Resource synchronization timed out." ) );
146  q->emitResult();
147  return;
148  }
149 
150  if ( instance.status() == AgentInstance::Idle ) {
151  // try again, we might have lost the synchronized()/synchronizedCollectionTree() signal
152  kDebug() << "trying again to sync resource" << instance.identifier();
153  if ( collectionTreeOnly )
154  instance.synchronizeCollectionTree();
155  else
156  instance.synchronize();
157  }
158 }
159 
160 AgentInstance ResourceSynchronizationJob::resource() const
161 {
162  return d->instance;
163 }
164 
165 }
166 
167 #include "moc_resourcesynchronizationjob.cpp"
Akonadi::AgentInstance::Idle
The agent instance does currently nothing.
Definition: agentinstance.h:77
Akonadi::ResourceSynchronizationJob::collectionTreeOnly
bool collectionTreeOnly() const
Returns whether a full synchronization will be done, or just the collection tree (without items)...
Definition: resourcesynchronizationjob.cpp:84
Akonadi::AgentInstance::identifier
QString identifier() const
Returns the unique identifier of the agent instance.
Definition: agentinstance.cpp:55
Akonadi::ResourceSynchronizationJob::ResourceSynchronizationJob
ResourceSynchronizationJob(const AgentInstance &instance, QObject *parent=0)
Creates a new synchronization job for the given resource.
Definition: resourcesynchronizationjob.cpp:63
Akonadi::AgentManager::instance
AgentInstance instance(const QString &identifier) const
Returns the agent instance with the given identifier or an invalid agent instance if the identifier d...
Definition: agentmanager.cpp:404
Akonadi::ResourceSynchronizationJob::~ResourceSynchronizationJob
~ResourceSynchronizationJob()
Destroys the synchronization job.
Definition: resourcesynchronizationjob.cpp:74
Akonadi::ResourceSynchronizationJob::resource
AgentInstance resource() const
Returns the resource that has been synchronized.
Definition: resourcesynchronizationjob.cpp:160
Akonadi::AgentManager::self
static AgentManager * self()
Returns the global instance of the agent manager.
Definition: agentmanager.cpp:380
Akonadi::AgentInstance
A representation of an agent instance.
Definition: agentinstance.h:62
Akonadi::ResourceSynchronizationJob::setCollectionTreeOnly
void setCollectionTreeOnly(bool collectionTreeOnly)
Sets the collectionTreeOnly property.
Definition: resourcesynchronizationjob.cpp:89
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:27 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