File indexing completed on 2024-04-28 15:20:31
0001 /* 0002 This file is part of the KDE Frameworks. 0003 SPDX-FileCopyrightText: 2010 Sebastian Trueg <trueg@kde.org> 0004 SPDX-FileCopyrightText: 2010 David Faure <faure@kde.org> 0005 0006 SPDX-License-Identifier: LGPL-2.0-or-later 0007 */ 0008 0009 #include "kdbusconnectionpool.h" 0010 #include <QCoreApplication> 0011 #include <QThread> 0012 #include <QThreadStorage> 0013 0014 #if KDBUSADDONS_BUILD_DEPRECATED_SINCE(5, 68) 0015 namespace 0016 { 0017 QAtomicInt s_connectionCounter; 0018 0019 class KDBusConnectionPoolPrivate 0020 { 0021 public: 0022 KDBusConnectionPoolPrivate() 0023 : m_connection(QDBusConnection::connectToBus(QDBusConnection::SessionBus, // 0024 QStringLiteral("KDBusConnectionPoolConnection%1").arg(newNumber()))) 0025 { 0026 } 0027 0028 ~KDBusConnectionPoolPrivate() 0029 { 0030 QDBusConnection::disconnectFromBus(m_connection.name()); 0031 } 0032 0033 QDBusConnection connection() const 0034 { 0035 return m_connection; 0036 } 0037 0038 private: 0039 static int newNumber() 0040 { 0041 return s_connectionCounter.fetchAndAddRelaxed(1); 0042 } 0043 0044 QDBusConnection m_connection; 0045 }; 0046 } // namespace 0047 0048 static QThreadStorage<KDBusConnectionPoolPrivate *> s_perThreadConnection; 0049 0050 QDBusConnection KDBusConnectionPool::threadConnection() 0051 { 0052 Q_ASSERT(QCoreApplication::instance() != nullptr); 0053 if (QCoreApplication::instance()->thread() == QThread::currentThread()) { 0054 return QDBusConnection::sessionBus(); 0055 } 0056 if (!s_perThreadConnection.hasLocalData()) { 0057 s_perThreadConnection.setLocalData(new KDBusConnectionPoolPrivate); 0058 } 0059 0060 return s_perThreadConnection.localData()->connection(); 0061 } 0062 #endif