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

akonadi

  • 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 <klocale.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  QList<QVariant> argumentList;
133  argumentList << QLatin1String( mSession->sessionId() )
134  << QString::number(reinterpret_cast<quintptr>( q ), 16)
135  << ( mParentJob ? QString::number( reinterpret_cast<quintptr>( mParentJob ), 16) : QString() )
136  << QString::fromLatin1( q->metaObject()->className() );
137  s_jobtracker->callWithArgumentList(QDBus::NoBlock, QLatin1String( "jobCreated" ), argumentList);
138  }
139 }
140 
141 void JobPrivate::delayedEmitResult()
142 {
143  Q_Q( Job );
144  q->emitResult();
145 }
146 
147 void JobPrivate::startQueued()
148 {
149  Q_Q( Job );
150  mStarted = true;
151 
152  emit q->aboutToStart( q );
153  q->doStart();
154  QTimer::singleShot( 0, q, SLOT(startNext()) );
155 
156  // if there's a job tracker running, tell it a job started
157  if ( s_jobtracker ) {
158  QList<QVariant> argumentList;
159  argumentList << QString::number(reinterpret_cast<quintptr>( q ), 16);
160  s_jobtracker->callWithArgumentList(QDBus::NoBlock, QLatin1String( "jobStarted" ), argumentList);
161  }
162 }
163 
164 void JobPrivate::lostConnection()
165 {
166  Q_Q( Job );
167 
168  if ( mCurrentSubJob ) {
169  mCurrentSubJob->d_ptr->lostConnection();
170  } else {
171  q->setError( Job::ConnectionFailed );
172  q->emitResult();
173  }
174 }
175 
176 void JobPrivate::slotSubJobAboutToStart( Job * job )
177 {
178  Q_ASSERT( mCurrentSubJob == 0 );
179  mCurrentSubJob = job;
180 }
181 
182 void JobPrivate::startNext()
183 {
184  Q_Q( Job );
185 
186  if ( mStarted && !mCurrentSubJob && q->hasSubjobs() ) {
187  Job *job = dynamic_cast<Akonadi::Job*>( q->subjobs().first() );
188  Q_ASSERT( job );
189  job->d_ptr->startQueued();
190  }
191 }
192 
193 QByteArray JobPrivate::newTag( )
194 {
195  if ( mParentJob )
196  mTag = mParentJob->d_ptr->newTag();
197  else
198  mTag = QByteArray::number( mSession->d->nextTag() );
199  return mTag;
200 }
201 
202 QByteArray JobPrivate::tag() const
203 {
204  return mTag;
205 }
206 
207 void JobPrivate::writeData( const QByteArray & data )
208 {
209  Q_ASSERT_X( !mWriteFinished, "Job::writeData()", "Calling writeData() after emitting writeFinished()" );
210  mSession->d->writeData( data );
211 }
212 
213 void JobPrivate::itemRevisionChanged( Akonadi::Item::Id itemId, int oldRevision, int newRevision )
214 {
215  mSession->d->itemRevisionChanged( itemId, oldRevision, newRevision );
216 }
217 
218 void JobPrivate::updateItemRevision( Akonadi::Item::Id itemId, int oldRevision, int newRevision )
219 {
220  Q_Q( Job );
221  foreach ( KJob *j, q->subjobs() ) {
222  Akonadi::Job *job = qobject_cast<Akonadi::Job*>( j );
223  if ( job )
224  job->d_ptr->updateItemRevision( itemId, oldRevision, newRevision );
225  }
226  doUpdateItemRevision( itemId, oldRevision, newRevision );
227 }
228 
229 void JobPrivate::doUpdateItemRevision( Akonadi::Item::Id itemId, int oldRevision, int newRevision )
230 {
231  Q_UNUSED( itemId );
232  Q_UNUSED( oldRevision );
233  Q_UNUSED( newRevision );
234 }
235 //@endcond
236 
237 
238 Job::Job( QObject *parent )
239  : KCompositeJob( parent ),
240  d_ptr( new JobPrivate( this ) )
241 {
242  d_ptr->init( parent );
243 }
244 
245 Job::Job( JobPrivate *dd, QObject *parent )
246  : KCompositeJob( parent ),
247  d_ptr( dd )
248 {
249  d_ptr->init( parent );
250 }
251 
252 Job::~Job()
253 {
254  delete d_ptr;
255 
256  // if there is a job tracer listening, tell it the job is done now
257  if ( s_jobtracker ) {
258  QList<QVariant> argumentList;
259  argumentList << QString::number(reinterpret_cast<quintptr>( this ), 16)
260  << errorString();
261  s_jobtracker->callWithArgumentList(QDBus::NoBlock, QLatin1String( "jobEnded" ), argumentList);
262  }
263 }
264 
265 void Job::start()
266 {
267 }
268 
269 bool Job::doKill()
270 {
271  Q_D( Job );
272  if ( d->mStarted ) {
273  // the only way to cancel an already started job is reconnecting to the server
274  d->mSession->d->forceReconnect();
275  }
276  d->mStarted = false;
277  return true;
278 }
279 
280 QString Job::errorString() const
281 {
282  QString str;
283  switch ( error() ) {
284  case NoError:
285  break;
286  case ConnectionFailed:
287  str = i18n( "Cannot connect to the Akonadi service." );
288  break;
289  case ProtocolVersionMismatch:
290  str = i18n( "The protocol version of the Akonadi server is incompatible. Make sure you have a compatible version installed." );
291  break;
292  case UserCanceled:
293  str = i18n( "User canceled operation." );
294  break;
295  case Unknown:
296  return errorText();
297  default:
298  str = i18n( "Unknown error." );
299  break;
300  }
301  if ( !errorText().isEmpty() ) {
302  str += QString::fromLatin1( " (%1)" ).arg( errorText() );
303  }
304  return str;
305 }
306 
307 bool Job::addSubjob( KJob * job )
308 {
309  bool rv = KCompositeJob::addSubjob( job );
310  if ( rv ) {
311  connect( job, SIGNAL(aboutToStart(Akonadi::Job*)), SLOT(slotSubJobAboutToStart(Akonadi::Job*)) );
312  QTimer::singleShot( 0, this, SLOT(startNext()) );
313  }
314  return rv;
315 }
316 
317 bool Job::removeSubjob(KJob * job)
318 {
319  bool rv = KCompositeJob::removeSubjob( job );
320  if ( job == d_ptr->mCurrentSubJob ) {
321  d_ptr->mCurrentSubJob = 0;
322  QTimer::singleShot( 0, this, SLOT(startNext()) );
323  }
324  return rv;
325 }
326 
327 void Job::doHandleResponse(const QByteArray & tag, const QByteArray & data)
328 {
329  kDebug() << "Unhandled response: " << tag << data;
330 }
331 
332 void Job::slotResult(KJob * job)
333 {
334  if ( d_ptr->mCurrentSubJob == job ) {
335  // current job finished, start the next one
336  d_ptr->mCurrentSubJob = 0;
337  KCompositeJob::slotResult( job );
338  if ( !job->error() )
339  QTimer::singleShot( 0, this, SLOT(startNext()) );
340  } else {
341  // job that was still waiting for execution finished, probably canceled,
342  // so just remove it from the queue and move on without caring about
343  // its error code
344  KCompositeJob::removeSubjob( job );
345  }
346 }
347 
348 void Job::emitWriteFinished()
349 {
350  d_ptr->mWriteFinished = true;
351  emit writeFinished( this );
352 }
353 
354 #include "moc_job.cpp"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Wed Mar 27 2013 08:57:24 by doxygen 1.8.3 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.10.1 API Reference

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