Merge remote branch 'gerrit/master' into refactor
[profile/ivi/qtbase.git] / src / widgets / accessible / qaccessible_unix.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtGui module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qaccessible.h"
43 #include "qaccessiblebridge.h"
44
45 #ifndef QT_NO_ACCESSIBILITY
46
47 #include "qcoreapplication.h"
48 #include "qmutex.h"
49 #include "qvector.h"
50 #include "private/qfactoryloader_p.h"
51
52 #include <stdlib.h>
53
54 QT_BEGIN_NAMESPACE
55
56 #ifndef QT_NO_LIBRARY
57 Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
58     (QAccessibleBridgeFactoryInterface_iid, QLatin1String("/accessiblebridge")))
59 #endif
60 Q_GLOBAL_STATIC(QVector<QAccessibleBridge *>, bridges)
61 static bool isInit = false;
62
63 void QAccessible::initialize()
64 {
65     if (isInit)
66         return;
67     isInit = true;
68
69     if (qgetenv("QT_ACCESSIBILITY") != "1")
70         return;
71 #ifndef QT_NO_LIBRARY
72     const QStringList l = loader()->keys();
73     for (int i = 0; i < l.count(); ++i) {
74         if (QAccessibleBridgeFactoryInterface *factory =
75                 qobject_cast<QAccessibleBridgeFactoryInterface*>(loader()->instance(l.at(i)))) {
76             QAccessibleBridge * bridge = factory->create(l.at(i));
77             if (bridge)
78                 bridges()->append(bridge);
79         }
80     }
81 #endif
82 }
83
84 void QAccessible::cleanup()
85 {
86     qDeleteAll(*bridges());
87 }
88
89 void QAccessible::updateAccessibility(QObject *o, int who, Event reason)
90 {
91     Q_ASSERT(o);
92
93     if (updateHandler) {
94         updateHandler(o, who, reason);
95         return;
96     }
97
98     initialize();
99     if (!bridges() || bridges()->isEmpty())
100         return;
101
102     QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(o);
103     if (!iface)
104         return;
105
106     // updates for List/Table/Tree should send child
107     if (who) {
108         QAccessibleInterface *child;
109         iface->navigate(QAccessible::Child, who, &child);
110         if (child) {
111             delete iface;
112             iface = child;
113             who = 0;
114         }
115     }
116
117     for (int i = 0; i < bridges()->count(); ++i)
118         bridges()->at(i)->notifyAccessibilityUpdate(reason, iface, who);
119     delete iface;
120 }
121
122 void QAccessible::setRootObject(QObject *o)
123 {
124     if (rootObjectHandler) {
125         rootObjectHandler(o);
126         return;
127     }
128
129     initialize();
130     if (bridges()->isEmpty())
131         return;
132
133     if (!o)
134         return;
135
136     for (int i = 0; i < bridges()->count(); ++i) {
137         QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(o);
138         bridges()->at(i)->setRootObject(iface);
139     }
140 }
141
142 QT_END_NAMESPACE
143
144 #endif // QT_NO_ACCESSIBILITY
145