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

akonadi

  • akonadi
agentbase.cpp
1 /*
2  Copyright (c) 2006 Till Adam <adam@kde.org>
3  Copyright (c) 2007 Volker Krause <vkrause@kde.org>
4  Copyright (c) 2007 Bruno Virlet <bruno.virlet@gmail.com>
5  Copyright (c) 2008 Kevin Krammer <kevin.krammer@gmx.at>
6 
7  This library is free software; you can redistribute it and/or modify it
8  under the terms of the GNU Library General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version.
11 
12  This library is distributed in the hope that it will be useful, but WITHOUT
13  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
15  License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to the
19  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  02110-1301, USA.
21 */
22 
23 #include "agentbase.h"
24 #include "agentbase_p.h"
25 
26 #include "agentmanager.h"
27 #include "changerecorder.h"
28 #include "controladaptor.h"
29 #include "dbusconnectionpool.h"
30 #include "itemfetchjob.h"
31 #include "kdepimlibs-version.h"
32 #include "monitor_p.h"
33 #include "servermanager_p.h"
34 #include "session.h"
35 #include "session_p.h"
36 #include "statusadaptor.h"
37 #include "xdgbasedirs_p.h"
38 
39 #include <kaboutdata.h>
40 #include <kcmdlineargs.h>
41 #include <kdebug.h>
42 #include <klocale.h>
43 #include <kstandarddirs.h>
44 
45 #include <Solid/PowerManagement>
46 
47 #include <QtCore/QDir>
48 #include <QtCore/QSettings>
49 #include <QtCore/QTimer>
50 #include <QtDBus/QtDBus>
51 #include <QtGui/QApplication>
52 
53 #include <signal.h>
54 #include <stdlib.h>
55 
56 
57 //#define EXPERIMENTAL_INPROCESS_AGENTS 1
58 
59 using namespace Akonadi;
60 
61 static AgentBase *sAgentBase = 0;
62 
63 AgentBase::Observer::Observer()
64 {
65 }
66 
67 AgentBase::Observer::~Observer()
68 {
69 }
70 
71 void AgentBase::Observer::itemAdded( const Item &item, const Collection &collection )
72 {
73  Q_UNUSED( item );
74  Q_UNUSED( collection );
75  if ( sAgentBase != 0 )
76  sAgentBase->d_ptr->changeProcessed();
77 }
78 
79 void AgentBase::Observer::itemChanged( const Item &item, const QSet<QByteArray> &partIdentifiers )
80 {
81  Q_UNUSED( item );
82  Q_UNUSED( partIdentifiers );
83  if ( sAgentBase != 0 )
84  sAgentBase->d_ptr->changeProcessed();
85 }
86 
87 void AgentBase::Observer::itemRemoved( const Item &item )
88 {
89  Q_UNUSED( item );
90  if ( sAgentBase != 0 )
91  sAgentBase->d_ptr->changeProcessed();
92 }
93 
94 void AgentBase::Observer::collectionAdded( const Akonadi::Collection &collection, const Akonadi::Collection &parent )
95 {
96  Q_UNUSED( collection );
97  Q_UNUSED( parent );
98  if ( sAgentBase != 0 )
99  sAgentBase->d_ptr->changeProcessed();
100 }
101 
102 void AgentBase::Observer::collectionChanged( const Collection &collection )
103 {
104  Q_UNUSED( collection );
105  if ( sAgentBase != 0 )
106  sAgentBase->d_ptr->changeProcessed();
107 }
108 
109 void AgentBase::Observer::collectionRemoved( const Collection &collection )
110 {
111  Q_UNUSED( collection );
112  if ( sAgentBase != 0 )
113  sAgentBase->d_ptr->changeProcessed();
114 }
115 
116 void AgentBase::ObserverV2::itemMoved( const Akonadi::Item &item, const Akonadi::Collection &source, const Akonadi::Collection &dest )
117 {
118  Q_UNUSED( item );
119  Q_UNUSED( source );
120  Q_UNUSED( dest );
121  if ( sAgentBase != 0 )
122  sAgentBase->d_ptr->changeProcessed();
123 }
124 
125 void AgentBase::ObserverV2::itemLinked( const Akonadi::Item& item, const Akonadi::Collection& collection )
126 {
127  Q_UNUSED( item );
128  Q_UNUSED( collection );
129  if ( sAgentBase != 0 ) {
130  // not implementation, let's disconnect the signal to enable optimizations in Monitor
131  QObject::disconnect( sAgentBase->changeRecorder(), SIGNAL(itemLinked(Akonadi::Item,Akonadi::Collection)),
132  sAgentBase->d_ptr, SLOT(itemLinked(Akonadi::Item,Akonadi::Collection)) );
133  sAgentBase->d_ptr->changeProcessed();
134  }
135 }
136 
137 void AgentBase::ObserverV2::itemUnlinked( const Akonadi::Item& item, const Akonadi::Collection& collection )
138 {
139  Q_UNUSED( item );
140  Q_UNUSED( collection );
141  if ( sAgentBase != 0 ) {
142  // not implementation, let's disconnect the signal to enable optimizations in Monitor
143  QObject::disconnect( sAgentBase->changeRecorder(), SIGNAL(itemUnlinked(Akonadi::Item,Akonadi::Collection)),
144  sAgentBase->d_ptr, SLOT(itemUnlinked(Akonadi::Item,Akonadi::Collection)) );
145  sAgentBase->d_ptr->changeProcessed();
146  }
147 }
148 
149 void AgentBase::ObserverV2::collectionMoved( const Akonadi::Collection &collection, const Akonadi::Collection &source, const Akonadi::Collection &dest )
150 {
151  Q_UNUSED( collection );
152  Q_UNUSED( source );
153  Q_UNUSED( dest );
154  if ( sAgentBase != 0 )
155  sAgentBase->d_ptr->changeProcessed();
156 }
157 
158 void AgentBase::ObserverV2::collectionChanged( const Akonadi::Collection &collection, const QSet<QByteArray> &changedAttributes )
159 {
160  Q_UNUSED( changedAttributes );
161  collectionChanged( collection );
162 }
163 
164 //@cond PRIVATE
165 
166 AgentBasePrivate::AgentBasePrivate( AgentBase *parent )
167  : q_ptr( parent ),
168  mDBusConnection( QString() ),
169  mStatusCode( AgentBase::Idle ),
170  mProgress( 0 ),
171  mNeedsNetwork( false ),
172  mOnline( false ),
173  mSettings( 0 ),
174  mObserver( 0 )
175 {
176 #ifdef Q_OS_WINCE
177  QThread::currentThread()->setPriority(QThread::LowPriority);
178 #endif
179  Internal::setClientType( Internal::Agent );
180 }
181 
182 AgentBasePrivate::~AgentBasePrivate()
183 {
184  mChangeRecorder->setConfig( 0 );
185  delete mSettings;
186 }
187 
188 void AgentBasePrivate::init()
189 {
190  Q_Q( AgentBase );
191 
195  SessionPrivate::createDefaultSession( mId.toLatin1() );
196 
197  if ( QThread::currentThread() != QCoreApplication::instance()->thread() ) {
198  mDBusConnection = QDBusConnection::connectToBus( QDBusConnection::SessionBus, q->identifier() );
199  Q_ASSERT( mDBusConnection.isConnected() );
200  }
201 
202  mTracer = new org::freedesktop::Akonadi::Tracer( QLatin1String( "org.freedesktop.Akonadi" ),
203  QLatin1String( "/tracing" ),
204  DBusConnectionPool::threadConnection(), q );
205 
206  new Akonadi__ControlAdaptor( q );
207  new Akonadi__StatusAdaptor( q );
208  if ( !DBusConnectionPool::threadConnection().registerObject( QLatin1String( "/" ), q, QDBusConnection::ExportAdaptors ) )
209  q->error( QString::fromLatin1( "Unable to register object at dbus: %1" ).arg( DBusConnectionPool::threadConnection().lastError().message() ) );
210 
211  mSettings = new QSettings( QString::fromLatin1( "%1/agent_config_%2" ).arg( XdgBaseDirs::saveDir( "config", QLatin1String( "akonadi" ) ), mId ), QSettings::IniFormat );
212 
213  mChangeRecorder = new ChangeRecorder( q );
214  mChangeRecorder->ignoreSession( Session::defaultSession() );
215  mChangeRecorder->itemFetchScope().setCacheOnly( true );
216  mChangeRecorder->setConfig( mSettings );
217 
218  mOnline = mSettings->value( QLatin1String( "Agent/Online" ), true ).toBool();
219 
220  // reinitialize the status message now that online state is available
221  mStatusMessage = defaultReadyMessage();
222 
223  mName = mSettings->value( QLatin1String( "Agent/Name" ) ).toString();
224  if ( mName.isEmpty() ) {
225  mName = mSettings->value( QLatin1String( "Resource/Name" ) ).toString();
226  if ( !mName.isEmpty() ) {
227  mSettings->remove( QLatin1String( "Resource/Name" ) );
228  mSettings->setValue( QLatin1String( "Agent/Name" ), mName );
229  }
230  }
231 
232  connect( mChangeRecorder, SIGNAL(itemAdded(Akonadi::Item,Akonadi::Collection)),
233  SLOT(itemAdded(Akonadi::Item,Akonadi::Collection)) );
234  connect( mChangeRecorder, SIGNAL(itemChanged(Akonadi::Item,QSet<QByteArray>)),
235  SLOT(itemChanged(Akonadi::Item,QSet<QByteArray>)) );
236  connect( mChangeRecorder, SIGNAL(itemMoved(Akonadi::Item,Akonadi::Collection,Akonadi::Collection)),
237  SLOT(itemMoved(Akonadi::Item,Akonadi::Collection,Akonadi::Collection)) );
238  connect( mChangeRecorder, SIGNAL(itemRemoved(Akonadi::Item)),
239  SLOT(itemRemoved(Akonadi::Item)) );
240  connect( mChangeRecorder, SIGNAL(collectionAdded(Akonadi::Collection,Akonadi::Collection)),
241  SLOT(collectionAdded(Akonadi::Collection,Akonadi::Collection)) );
242  connect( mChangeRecorder, SIGNAL(itemLinked(Akonadi::Item,Akonadi::Collection)),
243  SLOT(itemLinked(Akonadi::Item,Akonadi::Collection)) );
244  connect( mChangeRecorder, SIGNAL(itemUnlinked(Akonadi::Item,Akonadi::Collection)),
245  SLOT(itemUnlinked(Akonadi::Item,Akonadi::Collection)) );
246  connect( mChangeRecorder, SIGNAL(collectionChanged(Akonadi::Collection)),
247  SLOT(collectionChanged(Akonadi::Collection)) );
248  connect( mChangeRecorder, SIGNAL(collectionChanged(Akonadi::Collection,QSet<QByteArray>)),
249  SLOT(collectionChanged(Akonadi::Collection,QSet<QByteArray>)) );
250  connect( mChangeRecorder, SIGNAL(collectionMoved(Akonadi::Collection,Akonadi::Collection,Akonadi::Collection)),
251  SLOT(collectionMoved(Akonadi::Collection,Akonadi::Collection,Akonadi::Collection)) );
252  connect( mChangeRecorder, SIGNAL(collectionRemoved(Akonadi::Collection)),
253  SLOT(collectionRemoved(Akonadi::Collection)) );
254  connect( mChangeRecorder, SIGNAL(collectionSubscribed(Akonadi::Collection,Akonadi::Collection)),
255  SLOT(collectionSubscribed(Akonadi::Collection,Akonadi::Collection)) );
256  connect( mChangeRecorder, SIGNAL(collectionUnsubscribed(Akonadi::Collection)),
257  SLOT(collectionUnsubscribed(Akonadi::Collection)) );
258 
259  connect( q, SIGNAL(status(int,QString)), q, SLOT(slotStatus(int,QString)) );
260  connect( q, SIGNAL(percent(int)), q, SLOT(slotPercent(int)) );
261  connect( q, SIGNAL(warning(QString)), q, SLOT(slotWarning(QString)) );
262  connect( q, SIGNAL(error(QString)), q, SLOT(slotError(QString)) );
263 
264  connect( Solid::PowerManagement::notifier(), SIGNAL(resumingFromSuspend()), q, SLOT(slotResumedFromSuspend()) );
265 
266  // Use reference counting to allow agents to finish internal jobs when the
267  // agent is stopped.
268  KGlobal::ref();
269  if ( QThread::currentThread() == QCoreApplication::instance()->thread() )
270  KGlobal::setAllowQuit( true );
271 
272 #ifndef Q_OS_WINCE
273  // disable session management
274  if ( KApplication::kApplication() )
275  KApplication::kApplication()->disableSessionManagement();
276 #endif
277 
278  mResourceTypeName = AgentManager::self()->instance(mId).type().name();
279  setProgramName();
280 
281  QTimer::singleShot( 0, q, SLOT(delayedInit()) );
282 }
283 
284 void AgentBasePrivate::delayedInit()
285 {
286  Q_Q( AgentBase );
287  const QString serviceId = QLatin1String( "org.freedesktop.Akonadi.Agent." ) + mId;
288  if ( !DBusConnectionPool::threadConnection().registerService( serviceId ) )
289  kFatal() << "Unable to register service" << serviceId << "at dbus:" << DBusConnectionPool::threadConnection().lastError().message();
290  q->setOnline( mOnline );
291 }
292 
293 void AgentBasePrivate::setProgramName()
294 {
295  // ugly, really ugly, if you find another solution, change it and blame me for this code (Andras)
296  QString programName = mResourceTypeName;
297  if ( !mName.isEmpty() ) {
298  programName = i18nc( "Name and type of Akonadi resource", "%1 of type %2", mName, mResourceTypeName ) ;
299  }
300  const_cast<KAboutData*>( KGlobal::mainComponent().aboutData() )->setProgramName( ki18n( programName.toUtf8() ) );
301 }
302 
303 void AgentBasePrivate::itemAdded( const Akonadi::Item &item, const Akonadi::Collection &collection )
304 {
305  if ( mObserver != 0 )
306  mObserver->itemAdded( item, collection );
307 }
308 
309 void AgentBasePrivate::itemChanged( const Akonadi::Item &item, const QSet<QByteArray> &partIdentifiers )
310 {
311  if ( mObserver != 0 )
312  mObserver->itemChanged( item, partIdentifiers );
313 }
314 
315 void AgentBasePrivate::itemMoved( const Akonadi::Item &item, const Akonadi::Collection &source, const Akonadi::Collection &dest )
316 {
317  AgentBase::ObserverV2 *observer2 = dynamic_cast<AgentBase::ObserverV2*>( mObserver );
318  if ( mObserver ) {
319  // inter-resource moves, requires we know which resources the source and destination are in though
320  if ( !source.resource().isEmpty() && !dest.resource().isEmpty() ) {
321  if ( source.resource() != dest.resource() ) {
322  if ( source.resource() == q_ptr->identifier() ) { // moved away from us
323  Akonadi::Item i( item );
324  i.setParentCollection( source );
325  mObserver->itemRemoved( i );
326  }
327  else if ( dest.resource() == q_ptr->identifier() ) // moved to us
328  mObserver->itemAdded( item, dest );
329  else if ( observer2 )
330  observer2->itemMoved( item, source, dest );
331  else
332  // not for us, not sure if we should get here at all
333  changeProcessed();
334  return;
335  }
336  }
337  // intra-resource move
338  if ( observer2 ) {
339  observer2->itemMoved( item, source, dest );
340  } else {
341  // ### we cannot just call itemRemoved here as this will already trigger changeProcessed()
342  // so, just itemAdded() is good enough as no resource can have implemented intra-resource moves anyway
343  // without using ObserverV2
344  mObserver->itemAdded( item, dest );
345  // mObserver->itemRemoved( item );
346  }
347  }
348 }
349 
350 void AgentBasePrivate::itemRemoved( const Akonadi::Item &item )
351 {
352  if ( mObserver != 0 )
353  mObserver->itemRemoved( item );
354 }
355 
356 void AgentBasePrivate::itemLinked( const Akonadi::Item &item, const Akonadi::Collection &collection )
357 {
358  AgentBase::ObserverV2 *observer2 = dynamic_cast<AgentBase::ObserverV2*>( mObserver );
359  if ( observer2 )
360  observer2->itemLinked( item, collection );
361  else
362  changeProcessed();
363 }
364 
365 void AgentBasePrivate::itemUnlinked( const Akonadi::Item &item, const Akonadi::Collection &collection )
366 {
367  AgentBase::ObserverV2 *observer2 = dynamic_cast<AgentBase::ObserverV2*>( mObserver );
368  if ( observer2 )
369  observer2->itemUnlinked( item, collection );
370  else
371  changeProcessed();
372 }
373 
374 void AgentBasePrivate::collectionAdded( const Akonadi::Collection &collection, const Akonadi::Collection &parent )
375 {
376  if ( mObserver != 0 )
377  mObserver->collectionAdded( collection, parent );
378 }
379 
380 void AgentBasePrivate::collectionChanged( const Akonadi::Collection &collection )
381 {
382  AgentBase::ObserverV2 *observer2 = dynamic_cast<AgentBase::ObserverV2*>( mObserver );
383  if ( mObserver != 0 && observer2 == 0 ) // For ObserverV2 we use the variant with the part identifiers
384  mObserver->collectionChanged( collection );
385 }
386 
387 void AgentBasePrivate::collectionChanged( const Akonadi::Collection &collection, const QSet<QByteArray> &changedAttributes )
388 {
389  AgentBase::ObserverV2 *observer2 = dynamic_cast<AgentBase::ObserverV2*>( mObserver );
390  if ( observer2 != 0 )
391  observer2->collectionChanged( collection, changedAttributes );
392 }
393 
394 void AgentBasePrivate::collectionMoved( const Akonadi::Collection &collection, const Akonadi::Collection &source, const Akonadi::Collection &dest )
395 {
396  AgentBase::ObserverV2 *observer2 = dynamic_cast<AgentBase::ObserverV2*>( mObserver );
397  if ( observer2 ) {
398  observer2->collectionMoved( collection, source, dest );
399  } else if ( mObserver ) {
400  // ### we cannot just call collectionRemoved here as this will already trigger changeProcessed()
401  // so, just collectionAdded() is good enough as no resource can have implemented intra-resource moves anyway
402  // without using ObserverV2
403  mObserver->collectionAdded( collection, dest );
404  } else {
405  changeProcessed();
406  }
407 }
408 
409 void AgentBasePrivate::collectionRemoved( const Akonadi::Collection &collection )
410 {
411  if ( mObserver != 0 )
412  mObserver->collectionRemoved( collection );
413 }
414 
415 void AgentBasePrivate::collectionSubscribed( const Akonadi::Collection &collection, const Akonadi::Collection &parent )
416 {
417  Q_UNUSED( collection );
418  Q_UNUSED( parent );
419  changeProcessed();
420 }
421 
422 void AgentBasePrivate::collectionUnsubscribed( const Akonadi::Collection &collection )
423 {
424  Q_UNUSED( collection );
425  changeProcessed();
426 }
427 
428 void AgentBasePrivate::changeProcessed()
429 {
430  mChangeRecorder->changeProcessed();
431  QTimer::singleShot( 0, mChangeRecorder, SLOT(replayNext()) );
432 }
433 
434 void AgentBasePrivate::slotStatus( int status, const QString &message )
435 {
436  mStatusMessage = message;
437  mStatusCode = 0;
438 
439  switch ( status ) {
440  case AgentBase::Idle:
441  if ( mStatusMessage.isEmpty() )
442  mStatusMessage = defaultReadyMessage();
443 
444  mStatusCode = 0;
445  break;
446  case AgentBase::Running:
447  if ( mStatusMessage.isEmpty() )
448  mStatusMessage = defaultSyncingMessage();
449 
450  mStatusCode = 1;
451  break;
452  case AgentBase::Broken:
453  if ( mStatusMessage.isEmpty() )
454  mStatusMessage = defaultErrorMessage();
455 
456  mStatusCode = 2;
457  break;
458  default:
459  Q_ASSERT( !"Unknown status passed" );
460  break;
461  }
462 }
463 
464 void AgentBasePrivate::slotPercent( int progress )
465 {
466  mProgress = progress;
467 }
468 
469 void AgentBasePrivate::slotWarning( const QString& message )
470 {
471  mTracer->warning( QString::fromLatin1( "AgentBase(%1)" ).arg( mId ), message );
472 }
473 
474 void AgentBasePrivate::slotError( const QString& message )
475 {
476  mTracer->error( QString::fromLatin1( "AgentBase(%1)" ).arg( mId ), message );
477 }
478 
479 void AgentBasePrivate::slotNetworkStatusChange( Solid::Networking::Status stat )
480 {
481  Q_Q( AgentBase );
482  q->setOnline( stat == Solid::Networking::Unknown || stat == Solid::Networking::Connected );
483 }
484 
485 void AgentBasePrivate::slotResumedFromSuspend()
486 {
487  if ( mNeedsNetwork )
488  slotNetworkStatusChange( Solid::Networking::status() );
489 }
490 
491 AgentBase::AgentBase( const QString & id )
492  : d_ptr( new AgentBasePrivate( this ) )
493 {
494  sAgentBase = this;
495  d_ptr->mId = id;
496  d_ptr->init();
497 }
498 
499 AgentBase::AgentBase( AgentBasePrivate* d, const QString &id ) :
500  d_ptr( d )
501 {
502  sAgentBase = this;
503  d_ptr->mId = id;
504  d_ptr->init();
505 }
506 
507 AgentBase::~AgentBase()
508 {
509  delete d_ptr;
510 }
511 
512 QString AgentBase::parseArguments( int argc, char **argv )
513 {
514  QString identifier;
515  if ( argc < 3 ) {
516  kDebug() << "Not enough arguments passed...";
517  exit( 1 );
518  }
519 
520  for ( int i = 1; i < argc - 1; ++i ) {
521  if ( QLatin1String( argv[ i ] ) == QLatin1String( "--identifier" ) )
522  identifier = QLatin1String( argv[ i + 1 ] );
523  }
524 
525  if ( identifier.isEmpty() ) {
526  kDebug() << "Identifier argument missing";
527  exit( 1 );
528  }
529 
530  const QFileInfo fi( QString::fromLocal8Bit( argv[0] ) );
531  // strip off full path and possible .exe suffix
532  const QByteArray catalog = fi.baseName().toLatin1();
533 
534  KCmdLineArgs::init( argc, argv, identifier.toLatin1(), catalog, ki18n( "Akonadi Agent" ), KDEPIMLIBS_VERSION,
535  ki18n( "Akonadi Agent" ) );
536 
537  KCmdLineOptions options;
538  options.add( "identifier <argument>", ki18n( "Agent identifier" ) );
539  KCmdLineArgs::addCmdLineOptions( options );
540 
541  return identifier;
542 }
543 
544 // @endcond
545 
546 int AgentBase::init( AgentBase *r )
547 {
548  QApplication::setQuitOnLastWindowClosed( false );
549  KGlobal::locale()->insertCatalog( QLatin1String( "libakonadi" ) );
550  int rv = kapp->exec();
551  delete r;
552  return rv;
553 }
554 
555 int AgentBase::status() const
556 {
557  Q_D( const AgentBase );
558 
559  return d->mStatusCode;
560 }
561 
562 QString AgentBase::statusMessage() const
563 {
564  Q_D( const AgentBase );
565 
566  return d->mStatusMessage;
567 }
568 
569 int AgentBase::progress() const
570 {
571  Q_D( const AgentBase );
572 
573  return d->mProgress;
574 }
575 
576 QString AgentBase::progressMessage() const
577 {
578  Q_D( const AgentBase );
579 
580  return d->mProgressMessage;
581 }
582 
583 bool AgentBase::isOnline() const
584 {
585  Q_D( const AgentBase );
586 
587  return d->mOnline;
588 }
589 
590 void AgentBase::setNeedsNetwork( bool needsNetwork )
591 {
592  Q_D( AgentBase );
593  d->mNeedsNetwork = needsNetwork;
594 
595  if ( d->mNeedsNetwork ) {
596  connect( Solid::Networking::notifier()
597  , SIGNAL(statusChanged(Solid::Networking::Status))
598  , this, SLOT(slotNetworkStatusChange(Solid::Networking::Status))
599  , Qt::UniqueConnection );
600 
601  } else {
602  disconnect( Solid::Networking::notifier(), 0, 0, 0 );
603  setOnline( true );
604  }
605 }
606 
607 void AgentBase::setOnline( bool state )
608 {
609  Q_D( AgentBase );
610  d->mOnline = state;
611 
612  const QString newMessage = d->defaultReadyMessage();
613  if ( d->mStatusMessage != newMessage && d->mStatusCode != AgentBase::Broken )
614  emit status( d->mStatusCode, newMessage );
615 
616  d->mSettings->setValue( QLatin1String( "Agent/Online" ), state );
617  doSetOnline( state );
618  emit onlineChanged( state );
619 }
620 
621 void AgentBase::doSetOnline( bool online )
622 {
623  Q_UNUSED( online );
624 }
625 
626 void AgentBase::configure( WId windowId )
627 {
628  Q_UNUSED( windowId );
629  emit configurationDialogAccepted();
630 }
631 
632 #ifdef Q_OS_WIN //krazy:exclude=cpp
633 void AgentBase::configure( qlonglong windowId )
634 {
635  configure( reinterpret_cast<WId>( windowId ) );
636 }
637 #endif
638 
639 WId AgentBase::winIdForDialogs() const
640 {
641  const bool registered = DBusConnectionPool::threadConnection().interface()->isServiceRegistered( QLatin1String( "org.freedesktop.akonaditray" ) );
642  if ( !registered )
643  return 0;
644 
645  QDBusInterface dbus( QLatin1String( "org.freedesktop.akonaditray" ), QLatin1String( "/Actions" ),
646  QLatin1String( "org.freedesktop.Akonadi.Tray" ) );
647  const QDBusMessage reply = dbus.call( QLatin1String( "getWinId" ) );
648 
649  if ( reply.type() == QDBusMessage::ErrorMessage )
650  return 0;
651 
652  const WId winid = (WId)reply.arguments().at( 0 ).toLongLong();
653 
654  return winid;
655 }
656 
657 void AgentBase::quit()
658 {
659  Q_D( AgentBase );
660  aboutToQuit();
661 
662  if ( d->mSettings ) {
663  d->mChangeRecorder->setConfig( 0 );
664  d->mSettings->sync();
665  }
666 
667  KGlobal::deref();
668 }
669 
670 void AgentBase::aboutToQuit()
671 {
672 }
673 
674 void AgentBase::cleanup()
675 {
676  Q_D( AgentBase );
677  // prevent the monitor from picking up deletion signals for our own data if we are a resource
678  // and thus avoid that we kill our own data as last act before our own death
679  d->mChangeRecorder->blockSignals( true );
680 
681  aboutToQuit();
682 
683  const QString fileName = d->mSettings->fileName();
684 
685  /*
686  * First destroy the settings object...
687  */
688  d->mChangeRecorder->setConfig( 0 );
689  delete d->mSettings;
690  d->mSettings = 0;
691 
692  /*
693  * ... then remove the file from hd.
694  */
695  QFile::remove( fileName );
696 
697  /*
698  * ... and remove the changes file from hd.
699  */
700  QFile::remove( fileName + QLatin1String( "_changes.dat" ) );
701 
702  /*
703  * ... and also remove the agent configuration file if there is one.
704  */
705  QString configFile = KStandardDirs::locateLocal( "config", config()->name() );
706  QFile::remove( configFile );
707 
708  KGlobal::deref();
709 }
710 
711 void AgentBase::registerObserver( Observer *observer )
712 {
713  // TODO in theory we should re-connect change recorder signals here that we disconnected previously
714  d_ptr->mObserver = observer;
715 }
716 
717 QString AgentBase::identifier() const
718 {
719  return d_ptr->mId;
720 }
721 
722 void AgentBase::setAgentName( const QString &name )
723 {
724  Q_D( AgentBase );
725  if ( name == d->mName )
726  return;
727 
728  // TODO: rename collection
729  d->mName = name;
730 
731  if ( d->mName.isEmpty() || d->mName == d->mId ) {
732  d->mSettings->remove( QLatin1String( "Resource/Name" ) );
733  d->mSettings->remove( QLatin1String( "Agent/Name" ) );
734  } else
735  d->mSettings->setValue( QLatin1String( "Agent/Name" ), d->mName );
736 
737  d->mSettings->sync();
738 
739  d->setProgramName();
740 
741  emit agentNameChanged( d->mName );
742 }
743 
744 QString AgentBase::agentName() const
745 {
746  Q_D( const AgentBase );
747  if ( d->mName.isEmpty() )
748  return d->mId;
749  else
750  return d->mName;
751 }
752 
753 void AgentBase::changeProcessed()
754 {
755  Q_D( AgentBase );
756  d->changeProcessed();
757 }
758 
759 ChangeRecorder * AgentBase::changeRecorder() const
760 {
761  return d_ptr->mChangeRecorder;
762 }
763 
764 KSharedConfigPtr AgentBase::config()
765 {
766  if ( QCoreApplication::instance()->thread() == QThread::currentThread() )
767  return KGlobal::config();
768  else
769  return componentData().config();
770 }
771 
772 void AgentBase::abort()
773 {
774  emit abortRequested();
775 }
776 
777 void AgentBase::reconfigure()
778 {
779  emit reloadConfiguration();
780 }
781 
782 extern QThreadStorage<KComponentData*> s_agentComponentDatas;
783 
784 KComponentData AgentBase::componentData()
785 {
786  if ( QThread::currentThread() == QCoreApplication::instance()->thread() ) {
787  if ( s_agentComponentDatas.hasLocalData() )
788  return *(s_agentComponentDatas.localData());
789  else
790  return KGlobal::mainComponent();
791  }
792 
793  Q_ASSERT( s_agentComponentDatas.hasLocalData() );
794  return *(s_agentComponentDatas.localData());
795 }
796 
797 #include "agentbase.moc"
798 #include "agentbase_p.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Sep 24 2012 09:06:24 by doxygen 1.8.1.1 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.1 API Reference

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