File indexing completed on 2023-11-26 11:26:12

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     , m_status(Disconnected)
0020     , m_initWatcher(nullptr)
0021 {
0022     init();
0023 }
0024 
0025 void DBusConnection::init()
0026 {
0027     QDBusConnection c = QDBusConnection::sessionBus();
0028     if (!c.isConnected()) {
0029         qWarning("Could not connect to DBus session bus.");
0030         return;
0031     }
0032 
0033     QDBusMessage m = QDBusMessage::createMethodCall(QStringLiteral("org.a11y.Bus"),
0034                                                     QStringLiteral("/org/a11y/bus"),
0035                                                     QStringLiteral("org.a11y.Bus"), QStringLiteral("GetAddress"));
0036 
0037     QDBusPendingCall async = c.asyncCall(m);
0038     m_initWatcher = new QDBusPendingCallWatcher(async, this);
0039     connect(m_initWatcher, SIGNAL(finished(QDBusPendingCallWatcher*)), this, SLOT(initFinished()));
0040 }
0041 
0042 void DBusConnection::initFinished()
0043 {
0044     if (!m_initWatcher)
0045         return;
0046     m_status = ConnectionError;
0047     QDBusPendingReply<QString> reply = *m_initWatcher;
0048     if (reply.isError() || reply.value().isEmpty()) {
0049         qWarning() << "Accessibility DBus not found. Falling back to session bus.";
0050     } else {
0051         QString busAddress = reply.value();
0052         qDebug() << "Got Accessibility DBus address:" << busAddress;
0053         QDBusConnection c = QDBusConnection::connectToBus(busAddress, QStringLiteral("a11y"));
0054         if (c.isConnected()) {
0055             qDebug() << "Connected to Accessibility DBus at address=" << busAddress;
0056             m_connection = c;
0057             m_status = Connected;
0058         } else {
0059             qWarning() << "Found Accessibility DBus address=" << busAddress << "but cannot connect. Falling back to session bus.";
0060         }
0061     }
0062     m_initWatcher->deleteLater();
0063     m_initWatcher = nullptr;
0064     Q_EMIT connectionFetched();
0065 }
0066 
0067 bool DBusConnection::isFetchingConnection() const
0068 {
0069     return m_initWatcher;
0070 }
0071 
0072 QDBusConnection DBusConnection::connection() const
0073 {
0074     if (m_initWatcher) {
0075         m_initWatcher->waitForFinished();
0076         const_cast<DBusConnection*>(this)->initFinished();
0077     }
0078     return m_connection;
0079 }
0080 
0081 DBusConnection::Status DBusConnection::status() const
0082 {
0083     return m_status;
0084 }
0085 
0086 #include "moc_dbusconnection.cpp"