• 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
job.cpp
1 /*
2  Copyright (c) 2006 Tobias Koenig <tokoe@kde.org>
3  2006 Marc Mutz <mutz@kde.org>
4  2006 - 2007 Volker Krause <vkrause@kde.org>
5 
6  This library is free software; you can redistribute it and/or modify it
7  under the terms of the GNU Library General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or (at your
9  option) any later version.
10 
11  This library is distributed in the hope that it will be useful, but WITHOUT
12  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14  License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301, USA.
20 */
21 
22 #include "job.h"
23 #include "job_p.h"
24 #include "dbusconnectionpool.h"
25 #include <QTime>
26 #include "imapparser_p.h"
27 #include "session.h"
28 #include "session_p.h"
29 
30 #include <kdebug.h>
31 #include <klocalizedstring.h>
32 
33 #include <QtCore/QEventLoop>
34 #include <QtCore/QTimer>
35 #include <QtCore/QTextStream>
36 #include <QtNetwork/QHostAddress>
37 #include <QtNetwork/QTcpSocket>
38 #include <QtDBus/QDBusInterface>
39 #include <QtDBus/QDBusConnectionInterface>
40 
41 using namespace Akonadi;
42 
43 static QDBusAbstractInterface *s_jobtracker = 0;
44 
45 //@cond PRIVATE
46 void JobPrivate::handleResponse( const QByteArray & tag, const QByteArray & data )
47 {
48  Q_Q( Job );
49 
50  if ( mCurrentSubJob ) {
51  mCurrentSubJob->d_ptr->handleResponse( tag, data );
52  return;
53  }
54 
55  if ( tag == mTag ) {
56  if ( data.startsWith( "NO " ) || data.startsWith( "BAD " ) ) { //krazy:exclude=strings
57  QString msg = QString::fromUtf8( data );
58 
59  msg.remove( 0, msg.startsWith( QLatin1String( "NO " ) ) ? 3 : 4 );
60 
61  if ( msg.endsWith( QLatin1String( "\r\n" ) ) )
62  msg.chop( 2 );
63 
64  q->setError( Job::Unknown );
65  q->setErrorText( msg );
66  q->emitResult();
67  return;
68  } else if ( data.startsWith( "OK" ) ) { //krazy:exclude=strings
69 
70  // We can't use emitResult() here: The slot connected to the result signal might exec()
71  // another job, and therefore this method would never return. That causes the session
72  // to deadlock, since it calls this method and does not continue starting new jobs until
73  // this method finishes. Which would also mean the exec()'d job is never started,, and there-
74  // fore everything deadlocks.
75  QTimer::singleShot( 0, q, SLOT(delayedEmitResult()) );
76  return;
77  }
78  }
79 
80  q->doHandleResponse( tag, data );
81 }
82 
83 void JobPrivate::init( QObject *parent )
84 {
85  Q_Q( Job );
86 
87  mParentJob = dynamic_cast<Job*>( parent );
88  mSession = dynamic_cast<Session*>( parent );
89 
90  if ( !mSession ) {
91  if ( !mParentJob )
92  mSession = Session::defaultSession();
93  else
94  mSession = mParentJob->d_ptr->mSession;
95  }
96 
97  if ( !mParentJob )
98  mSession->d->addJob( q );
99  else
100  mParentJob->addSubjob( q );
101 
102  // if there's a job tracker running, tell it about the new job
103  if ( !s_jobtracker ) {
104  // Let's only check for the debugging console every 3 seconds, otherwise every single job
105  // makes a dbus call to the dbus daemon, doesn't help performance.
106  static QTime s_lastTime;
107  if ( s_lastTime.isNull() || s_lastTime.elapsed() > 3000 ) {
108  if ( s_lastTime.isNull() )
109  s_lastTime.start();
110  if ( DBusConnectionPool::threadConnection().interface()->isServiceRegistered(QLatin1String( "org.kde.akonadiconsole" ) ) ) {
111  s_jobtracker = new QDBusInterface( QLatin1String( "org.kde.akonadiconsole" ),
112  QLatin1String( "/jobtracker" ),
113  QLatin1String( "org.freedesktop.Akonadi.JobTracker" ),
114  DBusConnectionPool::threadConnection(), 0 );
115  } else {
116  s_lastTime.restart();
117  }
118  }
119  // Note: we never reset s_jobtracker to 0 when a call fails; but if we did
120  // then we should restart s_lastTime.
121  }
122  QMetaObject::invokeMethod( q, "signalCreationToJobTracker", Qt::QueuedConnection );
123 }
124 
125 void JobPrivate::signalCreationToJobTracker()
126 {
127  Q_Q( Job );
128  if ( s_jobtracker ) {
129  // We do these dbus calls manually, so as to avoid having to install (or copy) the console's
130  // xml interface document. Since this is purely a debugging aid, that seems preferable to
131  // publishing something not intended for public consumption.
132  // WARNING: for any signature change here, apply it to resourcescheduler.cpp too
133  QList<QVariant> argumentList;
134  argumentList << QLatin1String( mSession->sessionId() )
135  << QString::number(reinterpret_cast<quintptr>( q ), 16)
136  << ( mParentJob ? QString::number( reinterpret_cast<quintptr>( mParentJob ), 16) : QString() )
137  << QString::fromLatin1( q->metaObject()->className() )
138  << jobDebuggingString();
139  s_jobtracker->callWithArgumentList(QDBus::NoBlock, QLatin1String( "jobCreated" ), argumentList);
140  }
141 }
142 
143 void JobPrivate::signalStartedToJobTracker()
144 {
145  Q_Q( Job );
146  if ( s_jobtracker ) {
147  // if there's a job tracker running, tell it a job started
148  QList<QVariant> argumentList;
149  argumentList << QString::number(reinterpret_cast<quintptr>( q ), 16);
150  s_jobtracker->callWithArgumentList(QDBus::NoBlock, QLatin1String( "jobStarted" ), argumentList);
151  }
152 }
153 
154 void JobPrivate::delayedEmitResult()
155 {
156  Q_Q( Job );
157  q->emitResult();
158 }
159 
160 void JobPrivate::startQueued()
161 {
162  Q_Q( Job );
163  mStarted = true;
164 
165  emit q->aboutToStart( q );
166  q->doStart();
167  QTimer::singleShot( 0, q, SLOT(startNext()) );
168  QMetaObject::invokeMethod( q, "signalStartedToJobTracker", Qt::QueuedConnection );
169 }
170 
171 void JobPrivate::lostConnection()
172 {
173  Q_Q( Job );
174 
175  if ( mCurrentSubJob ) {
176  mCurrentSubJob->d_ptr->lostConnection();
177  } else {
178  q->setError( Job::ConnectionFailed );
179  q->emitResult();
180  }
181 }
182 
183 void JobPrivate::slotSubJobAboutToStart( Job * job )
184 {
185  Q_ASSERT( mCurrentSubJob == 0 );
186  mCurrentSubJob = job;
187 }
188 
189 void JobPrivate::startNext()
190 {
191  Q_Q( Job );
192 
193  if ( mStarted && !mCurrentSubJob && q->hasSubjobs() ) {
194  Job *job = dynamic_cast<Akonadi::Job*>( q->subjobs().first() );
195  Q_ASSERT( job );
196  job->d_ptr->startQueued();
197  }
198 }
199 
200 QByteArray JobPrivate::newTag( )
201 {
202  if ( mParentJob )
203  mTag = mParentJob->d_ptr->newTag();
204  else
205  mTag = QByteArray::number( mSession->d->nextTag() );
206  return mTag;
207 }
208 
209 QByteArray JobPrivate::tag() const
210 {
211  return mTag;
212 }
213 
214 void JobPrivate::writeData( const QByteArray & data )
215 {
216  Q_ASSERT_X( !mWriteFinished, "Job::writeData()", "Calling writeData() after emitting writeFinished()" );
217  mSession->d->writeData( data );
218 }
219 
220 void JobPrivate::itemRevisionChanged( Akonadi::Item::Id itemId, int oldRevision, int newRevision )
221 {
222  mSession->d->itemRevisionChanged( itemId, oldRevision, newRevision );
223 }
224 
225 void JobPrivate::updateItemRevision( Akonadi::Item::Id itemId, int oldRevision, int newRevision )
226 {
227  Q_Q( Job );
228  foreach ( KJob *j, q->subjobs() ) {
229  Akonadi::Job *job = qobject_cast<Akonadi::Job*>( j );
230  if ( job )
231  job->d_ptr->updateItemRevision( itemId, oldRevision, newRevision );
232  }
233  doUpdateItemRevision( itemId, oldRevision, newRevision );
234 }
235 
236 void JobPrivate::doUpdateItemRevision( Akonadi::Item::Id itemId, int oldRevision, int newRevision )
237 {
238  Q_UNUSED( itemId );
239  Q_UNUSED( oldRevision );
240  Q_UNUSED( newRevision );
241 }
242 
243 int JobPrivate::protocolVersion() const
244 {
245  return mSession->d->protocolVersion;
246 }
247 //@endcond
248 
249 Job::Job( QObject *parent )
250  : KCompositeJob( parent ),
251  d_ptr( new JobPrivate( this ) )
252 {
253  d_ptr->init( parent );
254 }
255 
256 Job::Job( JobPrivate *dd, QObject *parent )
257  : KCompositeJob( parent ),
258  d_ptr( dd )
259 {
260  d_ptr->init( parent );
261 }
262 
263 Job::~Job()
264 {
265  delete d_ptr;
266 
267  // if there is a job tracer listening, tell it the job is done now
268  if ( s_jobtracker ) {
269  QList<QVariant> argumentList;
270  argumentList << QString::number(reinterpret_cast<quintptr>( this ), 16)
271  << errorString();
272  s_jobtracker->callWithArgumentList(QDBus::NoBlock, QLatin1String( "jobEnded" ), argumentList);
273  }
274 }
275 
276 void Job::start()
277 {
278 }
279 
280 bool Job::doKill()
281 {
282  Q_D( Job );
283  if ( d->mStarted ) {
284  // the only way to cancel an already started job is reconnecting to the server
285  d->mSession->d->forceReconnect();
286  }
287  d->mStarted = false;
288  return true;
289 }
290 
291 QString Job::errorString() const
292 {
293  QString str;
294  switch ( error() ) {
295  case NoError:
296  break;
297  case ConnectionFailed:
298  str = i18n( "Cannot connect to the Akonadi service." );
299  break;
300  case ProtocolVersionMismatch:
301  str = i18n( "The protocol version of the Akonadi server is incompatible. Make sure you have a compatible version installed." );
302  break;
303  case UserCanceled:
304  str = i18n( "User canceled operation." );
305  break;
306  case Unknown:
307  return errorText();
308  default:
309  str = i18n( "Unknown error." );
310  break;
311  }
312  if ( !errorText().isEmpty() ) {
313  str += QString::fromLatin1( " (%1)" ).arg( errorText() );
314  }
315  return str;
316 }
317 
318 bool Job::addSubjob( KJob * job )
319 {
320  bool rv = KCompositeJob::addSubjob( job );
321  if ( rv ) {
322  connect( job, SIGNAL(aboutToStart(Akonadi::Job*)), SLOT(slotSubJobAboutToStart(Akonadi::Job*)) );
323  QTimer::singleShot( 0, this, SLOT(startNext()) );
324  }
325  return rv;
326 }
327 
328 bool Job::removeSubjob(KJob * job)
329 {
330  bool rv = KCompositeJob::removeSubjob( job );
331  if ( job == d_ptr->mCurrentSubJob ) {
332  d_ptr->mCurrentSubJob = 0;
333  QTimer::singleShot( 0, this, SLOT(startNext()) );
334  }
335  return rv;
336 }
337 
338 void Job::doHandleResponse(const QByteArray & tag, const QByteArray & data)
339 {
340  kDebug() << "Unhandled response: " << tag << data;
341 }
342 
343 void Job::slotResult(KJob * job)
344 {
345  if ( d_ptr->mCurrentSubJob == job ) {
346  // current job finished, start the next one
347  d_ptr->mCurrentSubJob = 0;
348  KCompositeJob::slotResult( job );
349  if ( !job->error() )
350  QTimer::singleShot( 0, this, SLOT(startNext()) );
351  } else {
352  // job that was still waiting for execution finished, probably canceled,
353  // so just remove it from the queue and move on without caring about
354  // its error code
355  KCompositeJob::removeSubjob( job );
356  }
357 }
358 
359 void Job::emitWriteFinished()
360 {
361  d_ptr->mWriteFinished = true;
362  emit writeFinished( this );
363 }
364 
365 #include "moc_job.cpp"
Akonadi::JobPrivate::doUpdateItemRevision
virtual void doUpdateItemRevision(Akonadi::Item::Id, int oldRevision, int newRevision)
Overwrite this if your job does operations with conflict detection and update the item revisions if y...
Akonadi::Job::ProtocolVersionMismatch
The server protocol version is too old or too new.
Definition: job.h:107
Akonadi::Job::Unknown
Unknown error.
Definition: job.h:109
Akonadi::Job
Base class for all actions in the Akonadi storage.
Definition: job.h:86
Akonadi::JobPrivate::updateItemRevision
void updateItemRevision(Akonadi::Item::Id itemId, int oldRevision, int newRevision)
Propagate item revision changes to this job and its sub-jobs.
Akonadi::JobPrivate::itemRevisionChanged
void itemRevisionChanged(Akonadi::Item::Id itemId, int oldRevision, int newRevision)
Notify following jobs about item revision changes.
Akonadi::Job::ConnectionFailed
The connection to the Akonadi server failed.
Definition: job.h:106
Akonadi::Session::defaultSession
static Session * defaultSession()
Returns the default session for this thread.
Definition: session.cpp:444
Akonadi::Job::start
void start()
Jobs are started automatically once entering the event loop again, no need to explicitly call this...
Definition: job.cpp:276
Akonadi::Job::writeFinished
void writeFinished(Akonadi::Job *job)
This signal is emitted if the job has finished all write operations, ie.
Akonadi::Session::sessionId
QByteArray sessionId() const
Returns the session identifier.
Definition: session.cpp:422
Akonadi::JobPrivate::tag
QByteArray tag() const
Return the tag used for the request.
Akonadi::Job::addSubjob
virtual bool addSubjob(KJob *job)
Adds the given job as a subjob to this job.
Definition: job.cpp:318
Akonadi::Job::doKill
virtual bool doKill()
Kills the execution of the job.
Definition: job.cpp:280
Akonadi::Job::emitWriteFinished
void emitWriteFinished()
Call this method to indicate that this job will not call writeData() again.
Definition: job.cpp:359
Akonadi::Session
A communication session with the Akonadi storage.
Definition: session.h:59
Akonadi::JobPrivate::newTag
QByteArray newTag()
Returns a new unique command tag for communication with the backend.
Akonadi::Job::doHandleResponse
virtual void doHandleResponse(const QByteArray &tag, const QByteArray &data)
This method should be reimplemented in the concrete jobs in case you want to handle incoming data...
Definition: job.cpp:338
Akonadi::Job::~Job
virtual ~Job()
Destroys the job.
Definition: job.cpp:263
Akonadi::JobPrivate::writeData
void writeData(const QByteArray &data)
Sends raw data to the backend.
Akonadi::JobPrivate
Definition: job_p.h:31
Akonadi::Job::removeSubjob
virtual bool removeSubjob(KJob *job)
Removes the given subjob of this job.
Definition: job.cpp:328
Akonadi::Job::aboutToStart
void aboutToStart(Akonadi::Job *job)
This signal is emitted directly before the job will be started.
Akonadi::Job::UserCanceled
The user canceld this job.
Definition: job.h:108
Akonadi::Job::errorString
virtual QString errorString() const
Returns the error string, if there has been an error, an empty string otherwise.
Definition: job.cpp:291
Akonadi::Job::Job
Job(QObject *parent=0)
Creates a new job.
Definition: job.cpp:249
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