~ubuntu-branches/ubuntu/quantal/kdepimlibs/quantal-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * Copyright (c) 2009 Volker Krause <vkrause@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "resourcesynchronizationjob.h"
#include "dbusconnectionpool.h"
#include "kjobprivatebase_p.h"

#include <akonadi/agentinstance.h>
#include <akonadi/agentmanager.h>

#include <KDebug>
#include <KLocale>

#include <QDBusConnection>
#include <QDBusInterface>
#include <QTimer>

namespace Akonadi
{

class ResourceSynchronizationJobPrivate : public KJobPrivateBase
{
  public:
    ResourceSynchronizationJobPrivate( ResourceSynchronizationJob* parent ) :
      q( parent ),
      interface( 0 ),
      safetyTimer( 0 ),
      timeoutCount( 0 ),
      collectionTreeOnly( false )
    {}

    void doStart();

    ResourceSynchronizationJob *q;
    AgentInstance instance;
    QDBusInterface* interface;
    QTimer* safetyTimer;
    int timeoutCount;
    bool collectionTreeOnly;
    static int timeoutCountLimit;

    void slotSynchronized();
    void slotTimeout();
};

int ResourceSynchronizationJobPrivate::timeoutCountLimit = 60;

ResourceSynchronizationJob::ResourceSynchronizationJob(const AgentInstance& instance, QObject* parent) :
  KJob( parent ),
  d( new ResourceSynchronizationJobPrivate( this ) )
{
  d->instance = instance;
  d->safetyTimer = new QTimer( this );
  connect( d->safetyTimer, SIGNAL(timeout()), SLOT(slotTimeout()) );
  d->safetyTimer->setInterval( 10 * 1000 );
  d->safetyTimer->setSingleShot( false );
}

ResourceSynchronizationJob::~ResourceSynchronizationJob()
{
  delete d;
}

void ResourceSynchronizationJob::start()
{
  d->start();
}

bool ResourceSynchronizationJob::collectionTreeOnly() const
{
  return d->collectionTreeOnly;
}

void ResourceSynchronizationJob::setCollectionTreeOnly( bool b )
{
  d->collectionTreeOnly = b;
}

void ResourceSynchronizationJobPrivate::doStart()
{
  if ( !instance.isValid() ) {
    q->setError( KJob::UserDefinedError );
    q->setErrorText( i18n( "Invalid resource instance." ) );
    q->emitResult();
    return;
  }

  interface = new QDBusInterface( QString::fromLatin1( "org.freedesktop.Akonadi.Resource.%1" ).arg( instance.identifier() ),
                                  QString::fromLatin1( "/" ),
                                  QString::fromLatin1( "org.freedesktop.Akonadi.Resource" ),
                                  DBusConnectionPool::threadConnection(), this );
  if ( collectionTreeOnly )
    connect( interface, SIGNAL(collectionTreeSynchronized()), q, SLOT(slotSynchronized()) );
  else
    connect( interface, SIGNAL(synchronized()), q, SLOT(slotSynchronized()) );

  if ( interface->isValid() ) {
    if ( collectionTreeOnly )
      instance.synchronizeCollectionTree();
    else
      instance.synchronize();

    safetyTimer->start();
  } else {
    q->setError( KJob::UserDefinedError );
    q->setErrorText( i18n( "Unable to obtain D-Bus interface for resource '%1'", instance.identifier() ) );
    q->emitResult();
    return;
  }
}

void ResourceSynchronizationJobPrivate::slotSynchronized()
{
  if ( collectionTreeOnly )
    q->disconnect( interface, SIGNAL(collectionTreeSynchronized()), q, SLOT(slotSynchronized()) );
  else
    q->disconnect( interface, SIGNAL(synchronized()), q, SLOT(slotSynchronized()) );
  safetyTimer->stop();
  q->emitResult();
}

void ResourceSynchronizationJobPrivate::slotTimeout()
{
  instance = AgentManager::self()->instance( instance.identifier() );
  timeoutCount++;

  if ( timeoutCount > timeoutCountLimit ) {
    safetyTimer->stop();
    q->setError( KJob::UserDefinedError );
    q->setErrorText( i18n( "Resource synchronization timed out." ) );
    q->emitResult();
    return;
  }

  if ( instance.status() == AgentInstance::Idle ) {
    // try again, we might have lost the synchronized()/synchronizedCollectionTree() signal
    kDebug() << "trying again to sync resource" << instance.identifier();
    if ( collectionTreeOnly )
      instance.synchronizeCollectionTree();
    else
      instance.synchronize();
  }
}

AgentInstance ResourceSynchronizationJob::resource() const
{
  return d->instance;
}

}

#include "resourcesynchronizationjob.moc"