File: kaccessiblebridge.cpp

package info (click to toggle)
kaccessible 4%3A17.08.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 172 kB
  • sloc: cpp: 1,013; makefile: 3; sh: 1
file content (245 lines) | stat: -rw-r--r-- 10,182 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* This file is part of the KDE project
 * Copyright (C) 2010 Sebastian Sauer <sebsauer@kdab.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "kaccessiblebridge.h"
#include "kaccessibleinterface.h"

#include <QAccessibleInterface>
#include <QWidget>
#include <QFile>
#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QDBusInterface>
#include <QDBusPendingCall>
#include <QDBusArgument>
#include <QDBusMetaType>
#include <kdebug.h>

Q_EXPORT_PLUGIN(BridgePlugin)

class Bridge::Private
{
    public:
        BridgePlugin *m_plugin;
        const QString m_key;
        QAccessibleInterface *m_root;
        QList<QObject*> m_popupMenus;
        QRect m_lastFocusRect;
        QString m_lastFocusName;

        Private(BridgePlugin *plugin, const QString& key)
            : m_plugin(plugin)
            , m_key(key)
            , m_root(0)
            , m_lastFocusRect(QRect(0,0,0,0))
            , m_app(0)
        {
        }

        ~Private()
        {
            delete m_app;
        }

        QDBusInterface* app()
        {
           Q_ASSERT(m_root);
           if(!m_app) {
                m_app = new QDBusInterface(QLatin1String("org.kde.kaccessibleapp"), QLatin1String("/Adaptor"));
                if(m_app->isValid()) {
                   kDebug() << "Connected with the org.kde.kaccessibleapp dbus-service";
                   KAccessibleInterface dbusIface;
                   dbusIface.set(m_root, 0);
                   m_app->asyncCall(QLatin1String("setRootObject"), qVariantFromValue(dbusIface));
                }
            }
            if(m_app->lastError().isValid()) {
                kDebug() << "DBus error:" << m_app->lastError().name() << m_app->lastError().message();
                delete m_app; m_app = 0;
            } else if(!m_app->isValid()) {
                kDebug() << "Failed to connect with the org.kde.kaccessibleapp dbus-service";
                delete m_app; m_app = 0;
            }
            return m_app;
       }
    private:
        QDBusInterface *m_app;
};
Bridge::Bridge(BridgePlugin *plugin, const QString& key)
    : QObject(plugin)
    , QAccessibleBridge()
    , d(new Private(plugin, key))
{
}

Bridge::~Bridge()
{
    delete d;
}

void Bridge::notifyAccessibilityUpdate(int reason, QAccessibleInterface *interface, int child)
{
    if(reason == QAccessible::ObjectShow || reason == QAccessible::ObjectHide) {
        return;
    }

    if(!d->m_root) {
        return;
    }

    QObject *obj = interface->object();
    if(!obj) {
         return;
    }

    QDBusInterface* app = d->app();
    if(!app) {
        return;
    }

    const QString name = interface->text(QAccessible::Name, child);
    const QString description = interface->text(QAccessible::Description, child);

    KAccessibleInterface dbusIface;
    dbusIface.set(interface, child);

    QAccessibleInterface *childInterface = 0;
    //if(child > 0) interface->navigate(QAccessible::Child, child, &childInterface);

    switch(reason) {
        case QAccessible::PopupMenuStart: {
            d->m_popupMenus.append(obj);
        } break;
        case QAccessible::PopupMenuEnd: {
            const int index = d->m_popupMenus.lastIndexOf(obj);
            if(index >= 0) d->m_popupMenus.removeAt(index);
        } break;

        case QAccessible::Alert: {
            //kDebug() << reasonToString(reason) << "object=" << (obj ? QString("%1 (%2)").arg(obj->objectName()).arg(obj->metaObject()->className()) : "NULL") << "name=" << name;
            app->asyncCall(QLatin1String( "setAlert" ), qVariantFromValue(dbusIface));
        } break;

        case QAccessible::DialogStart: {
            kDebug() << reasonToString(reason) << QLatin1String( "object=" ) << (obj ? QString(QLatin1String( "%1 (%2)" )).arg(obj->objectName()).arg(QLatin1String( obj->metaObject()->className() )) : QLatin1String( "NULL" )) << QLatin1String( "name=" ) << name;
            //app->asyncCall("sayText", name);
        } break;
        case QAccessible::DialogEnd: {
            kDebug() << reasonToString(reason) << QLatin1String( "object=" ) << (obj ? QString(QLatin1String( "%1 (%2)" )).arg(obj->objectName()).arg(QLatin1String( obj->metaObject()->className() )) : QLatin1String( "NULL" )) << QLatin1String( "name=" ) << name;
            //app->asyncCall("sayText", name);
        } break;

        case QAccessible::NameChanged: {
            kDebug() << reasonToString(reason) << QLatin1String( "object=" ) << (obj ? QString(QLatin1String( "%1 (%2)" )).arg(obj->objectName()).arg(QLatin1String( obj->metaObject()->className() )) : QLatin1String( "NULL" )) << QLatin1String( "name=" ) << name;
            //app->asyncCall("sayText", name);
        } break;
        case QAccessible::ValueChanged: {
            QString value = interface->text(QAccessible::Value, child);
            kDebug() << reasonToString(reason) << QLatin1String( "object=" ) << (obj ? QString(QLatin1String( "%1 (%2)" )).arg(obj->objectName() ).arg(QLatin1String( obj->metaObject()->className() )) : QLatin1String( "NULL" )) << QLatin1String( "name=" ) << name << QLatin1String( "value=" ) << value;
            app->asyncCall(QLatin1String( "setValueChanged" ), qVariantFromValue(dbusIface));
        } break;
        case QAccessible::StateChanged: {
            kDebug() << reasonToString(reason) << QLatin1String( "object=" ) << (obj ? QString(QLatin1String( "%1 (%2)" ) ).arg(obj->objectName()).arg(QLatin1String( obj->metaObject()->className() )) : QLatin1String( "NULL" )) << QLatin1String( "name=" )<< name << QLatin1String( "state=" ) << stateToString(dbusIface.state);
        } break;

        case QAccessible::Focus: {
            // abort if the focus would interrupt a popupmenu
            if(!d->m_popupMenus.isEmpty()) {
                bool ok = false;
                QObject* lastPopupMenu = d->m_popupMenus.last();
                for(QObject* o = obj; o; o = o->parent())
                    if(o == lastPopupMenu) { ok = true; break; }
                if(!ok)
                    return;
            }

            // don't emit the focus changed signal if the focus didn't really changed since last time
            if(dbusIface.rect == d->m_lastFocusRect && dbusIface.name == d->m_lastFocusName)
                return;
            d->m_lastFocusRect = dbusIface.rect;
            d->m_lastFocusName = dbusIface.name;

            // here we could add hacks to special case applications/widgets :)
            //
            // QWidget *w = childInterface ? dynamic_cast<QWidget*>(childobj) : 0;
            // if(!w) w = dynamic_cast<QWidget*>(obj);
            // if(w) r = QRect(w->mapToGlobal(QPoint(w->x(), w->y())), w->size());

            kDebug() << reasonToString(reason) << QLatin1String( "object=" ) << (obj ? QString(QLatin1String( "%1 (%2)" )).arg(obj->objectName()).arg(QLatin1String( obj->metaObject()->className() )) : QLatin1String( "NULL" )) << QLatin1String( "name=" ) << name << QLatin1String( "rect=" ) << dbusIface.rect;
            app->asyncCall(QLatin1String( "setFocusChanged" ), qVariantFromValue(dbusIface));
        } break;
        default:
            kDebug() << reasonToString(reason) << QLatin1String( "object=" ) << (obj ? QString(QLatin1String( "%1 (%2)" )).arg(obj->objectName()).arg(QLatin1String( obj->metaObject()->className() )) : QLatin1String( "NULL" )) << QLatin1String( "name=" ) << name;
            break;
    }

    delete childInterface;
    //delete d->m_app; d->m_app = 0;
}

void Bridge::focusChanged(int px, int py, int rx, int ry, int rwidth, int rheight)
{
    kDebug()<<"KAccessibleBridge: focusChanged px=" << px << "py=" << py << "rx=" << rx << "ry=" << ry << "rwidth=" << rwidth << "rheight=" << rheight;
}

void Bridge::setRootObject(QAccessibleInterface *interface)
{
    d->m_root = interface;

    kDebug()<<QLatin1String( "KAccessibleBridge: setRootObject object=" ) << (interface->object() ? QString(QLatin1String( "%1 (%2)" )).arg(interface->object()->objectName()).arg(QLatin1String( interface->object()->metaObject()->className() )) : QLatin1String( "NULL" ));

    if( ! QDBusConnection::sessionBus().isConnected()) {
        kWarning()<<"KAccessibleBridge: Failed to connect to session bus";
        d->m_root = 0;
        return;
    }

    if( ! QDBusConnection::sessionBus().interface()->isServiceRegistered(QLatin1String( "org.kde.kaccessibleapp" ))) {
        kDebug()<<"KAccessibleBridge: Starting kaccessibleapp dbus service";
        QDBusConnection::sessionBus().interface()->startService(QLatin1String( "org.kde.kaccessibleapp" ));
        if( ! QDBusConnection::sessionBus().interface()->isServiceRegistered(QLatin1String( "org.kde.kaccessibleapp" ))) {
            kWarning()<<"KAccessibleBridge: Failed to start kaccessibleapp dbus service";
            d->m_root = 0;
            return;
        }

        //for testing;
        //QDBusConnection::sessionBus().connect("org.kde.kaccessibleapp", "/Adaptor", "org.kde.kaccessibleapp.Adaptor", "focusChanged", this, SLOT(focusChanged(int,int,int,int,int,int)));
    }
}

BridgePlugin::BridgePlugin(QObject *parent)
    : QAccessibleBridgePlugin(parent)
{
    qDBusRegisterMetaType<KAccessibleInterface>();
}

BridgePlugin::~BridgePlugin()
{
}

QAccessibleBridge* BridgePlugin::create(const QString &key)
{
    return new Bridge(this, key);
}

QStringList BridgePlugin::keys() const
{
    return QStringList() << QLatin1String( "KAccessibleBridge" );
}