00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "dbusconnectionpool.h"
00022 #include <QCoreApplication>
00023 #include <QThread>
00024 #include <QThreadStorage>
00025
00026 namespace {
00027 QAtomicInt s_connectionCounter;
00028
00029 class DBusConnectionPoolPrivate
00030 {
00031 public:
00032 DBusConnectionPoolPrivate()
00033 : m_connection( QDBusConnection::connectToBus(
00034 QDBusConnection::SessionBus,
00035 QString::fromLatin1("AkonadiKde%1").arg(newNumber()) ) )
00036 {
00037 }
00038 ~DBusConnectionPoolPrivate() {
00039 QDBusConnection::disconnectFromBus( m_connection.name() );
00040 }
00041
00042 QDBusConnection connection() const { return m_connection; }
00043
00044 private:
00045 static int newNumber() {
00046 return s_connectionCounter.fetchAndAddAcquire(1);
00047 }
00048 QDBusConnection m_connection;
00049 };
00050 }
00051
00052 QThreadStorage<DBusConnectionPoolPrivate *> s_perThreadConnection;
00053
00054 QDBusConnection Akonadi::DBusConnectionPool::threadConnection()
00055 {
00056 if ( !QCoreApplication::instance() || QCoreApplication::instance()->thread() == QThread::currentThread() )
00057 return QDBusConnection::sessionBus();
00058 if (!s_perThreadConnection.hasLocalData()) {
00059 s_perThreadConnection.setLocalData(new DBusConnectionPoolPrivate);
00060 }
00061 return s_perThreadConnection.localData()->connection();
00062 }