akonadi
dbusconnectionpool.cpp
00001 /* 00002 * Copyright (C) 2010 Sebastian Trueg <trueg@kde.org> 00003 * Copyright (C) 2010 David Faure <faure@kde.org> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Library General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Library General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Library General Public License 00016 * along with this library; see the file COPYING.LIB. If not, write to 00017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 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(); // main thread, use the default session bus, breaks unported resources otherwise 00058 if (!s_perThreadConnection.hasLocalData()) { 00059 s_perThreadConnection.setLocalData(new DBusConnectionPoolPrivate); 00060 } 00061 return s_perThreadConnection.localData()->connection(); 00062 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 22:18:32 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 22:18:32 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.