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

akonadi

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

Skip menu "kdepimlibs-4.9.4 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