File indexing completed on 2024-10-06 12:47:42
0001 /* 0002 SPDX-FileCopyrightText: 2012 Frederik Gladhorn <gladhorn@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 0008 #include "dbusconnection.h" 0009 0010 #include <QDBusMessage> 0011 #include <QDBusPendingReply> 0012 #include <QDebug> 0013 0014 using namespace QAccessibleClient; 0015 0016 DBusConnection::DBusConnection() 0017 : QObject() 0018 , m_connection(QDBusConnection::sessionBus()) 0019 { 0020 init(); 0021 } 0022 0023 void DBusConnection::init() 0024 { 0025 QDBusConnection c = QDBusConnection::sessionBus(); 0026 if (!c.isConnected()) { 0027 qWarning("Could not connect to DBus session bus."); 0028 return; 0029 } 0030 0031 QDBusMessage m = QDBusMessage::createMethodCall(QStringLiteral("org.a11y.Bus"), 0032 QStringLiteral("/org/a11y/bus"), 0033 QStringLiteral("org.a11y.Bus"), QStringLiteral("GetAddress")); 0034 0035 QDBusPendingCall async = c.asyncCall(m); 0036 m_initWatcher = new QDBusPendingCallWatcher(async, this); 0037 connect(m_initWatcher, SIGNAL(finished(QDBusPendingCallWatcher*)), this, SLOT(initFinished())); 0038 } 0039 0040 void DBusConnection::initFinished() 0041 { 0042 if (!m_initWatcher) 0043 return; 0044 m_status = ConnectionError; 0045 QDBusPendingReply<QString> reply = *m_initWatcher; 0046 if (reply.isError() || reply.value().isEmpty()) { 0047 qWarning() << "Accessibility DBus not found. Falling back to session bus."; 0048 } else { 0049 QString busAddress = reply.value(); 0050 qDebug() << "Got Accessibility DBus address:" << busAddress; 0051 QDBusConnection c = QDBusConnection::connectToBus(busAddress, QStringLiteral("a11y")); 0052 if (c.isConnected()) { 0053 qDebug() << "Connected to Accessibility DBus at address=" << busAddress; 0054 m_connection = c; 0055 m_status = Connected; 0056 } else { 0057 qWarning() << "Found Accessibility DBus address=" << busAddress << "but cannot connect. Falling back to session bus."; 0058 } 0059 } 0060 m_initWatcher->deleteLater(); 0061 m_initWatcher = nullptr; 0062 Q_EMIT connectionFetched(); 0063 } 0064 0065 bool DBusConnection::isFetchingConnection() const 0066 { 0067 return m_initWatcher; 0068 } 0069 0070 QDBusConnection DBusConnection::connection() const 0071 { 0072 if (m_initWatcher) { 0073 m_initWatcher->waitForFinished(); 0074 const_cast<DBusConnection*>(this)->initFinished(); 0075 } 0076 return m_connection; 0077 } 0078 0079 DBusConnection::Status DBusConnection::status() const 0080 { 0081 return m_status; 0082 } 0083 0084 #include "moc_dbusconnection.cpp"