build: get doxygen generating selective
[platform/core/uifw/aurum.git] / libaurum / src / Impl / Accessibility / AtspiAccessibleNode.cc
1 #include "AtspiAccessibleNode.h"
2
3 #include <gio/gio.h>
4
5 #include <loguru.hpp>
6
7 //std::map<AtspiAccessible *, AccessibleNode *> AccessibleNode::mNodeMap{};
8
9 AtspiAccessibleNode::AtspiAccessibleNode(AtspiAccessible *node)
10 : mNode{node}
11 {
12   GArray *ifaces = atspi_accessible_get_interfaces(mNode);
13     if (ifaces) {
14         for (unsigned int i = 0; i < ifaces->len; i++) {
15             char *iface = g_array_index(ifaces, char *, i);
16             if (!strcmp(iface, "Action"))
17                 mSupportingIfaces |=
18                     static_cast<int>(AccessibleNodeInterface::ACTION);
19             else if (!strcmp(iface, "Collection"))
20                 mSupportingIfaces |=
21                     static_cast<int>(AccessibleNodeInterface::COLLECTION);
22             else if (!strcmp(iface, "Component"))
23                 mSupportingIfaces |=
24                     static_cast<int>(AccessibleNodeInterface::COMPONENT);
25             else if (!strcmp(iface, "Document"))
26                 mSupportingIfaces |=
27                     static_cast<int>(AccessibleNodeInterface::DOCUMENT);
28             else if (!strcmp(iface, "EditableText"))
29                 mSupportingIfaces |=
30                     static_cast<int>(AccessibleNodeInterface::EDITABLETEXT);
31             else if (!strcmp(iface, "Hypertext"))
32                 mSupportingIfaces |=
33                     static_cast<int>(AccessibleNodeInterface::HYPERTEXT);
34             else if (!strcmp(iface, "Image"))
35                 mSupportingIfaces |=
36                     static_cast<int>(AccessibleNodeInterface::IMAGE);
37             else if (!strcmp(iface, "Selection"))
38                 mSupportingIfaces |=
39                     static_cast<int>(AccessibleNodeInterface::SELECTION);
40             else if (!strcmp(iface, "Text"))
41                 mSupportingIfaces |=
42                     static_cast<int>(AccessibleNodeInterface::TEXT);
43             else if (!strcmp(iface, "Value"))
44                 mSupportingIfaces |=
45                     static_cast<int>(AccessibleNodeInterface::VALUE);
46             else if (!strcmp(iface, "Accessible"))
47                 mSupportingIfaces |=
48                     static_cast<int>(AccessibleNodeInterface::ACCESSIBLE);
49             else if (!strcmp(iface, "Table"))
50                 mSupportingIfaces |=
51                     static_cast<int>(AccessibleNodeInterface::TABLE);
52             else if (!strcmp(iface, "TableCell"))
53                 mSupportingIfaces |=
54                     static_cast<int>(AccessibleNodeInterface::TABLECELL);
55             else
56                 LOG_F(WARNING, "Not Supported interface found %s", iface);
57
58             g_free(iface);
59         }
60         g_array_free(ifaces, FALSE);
61     }
62     this->refresh();
63 }
64
65 AtspiAccessibleNode::~AtspiAccessibleNode()
66 {
67     if(mNode) g_object_unref(mNode);
68 }
69
70 int AtspiAccessibleNode::getChildCount() const
71 {
72     int count = atspi_accessible_get_child_count(mNode, NULL);
73     if (count <= 0) return 0;
74     return count;
75 }
76
77 std::shared_ptr<AccessibleNode> AtspiAccessibleNode::getChildAt(int index) const
78 {
79     LOG_SCOPE_F(INFO, "getChild @ %d from node(%p)", index, mNode);
80     AtspiAccessible *rawChild = atspi_accessible_get_child_at_index(mNode, index, NULL);
81     return std::make_shared<AtspiAccessibleNode>(rawChild);
82 }
83
84 std::vector<std::shared_ptr<AccessibleNode>> AtspiAccessibleNode::getChildren() const
85 {
86     std::vector<std::shared_ptr<AccessibleNode>> ret{};
87     int nchild = this->getChildCount();
88     for (int i = 0; i < nchild; i++) {
89         auto child = getChildAt(i);
90         if (child) ret.push_back(child);
91     }
92     return ret;
93 }
94
95 std::shared_ptr<AccessibleNode> AtspiAccessibleNode::getParent() const
96 {
97     AtspiAccessible *rawParent = atspi_accessible_get_parent(mNode, NULL);
98     return std::make_shared<AtspiAccessibleNode>(rawParent);
99 /*  auto node = AccessibleNode::get(parent);
100     if (parent) g_object_unref(parent);
101     return node; */
102 }
103
104
105 void* AtspiAccessibleNode::getRawHandler(void) const
106 {
107     return static_cast<void*>(mNode);
108 }
109
110 void AtspiAccessibleNode::refresh()
111 {
112     gchar *rolename = atspi_accessible_get_role_name(mNode, NULL);
113     if (rolename) {
114         mRole = rolename;
115         g_free(rolename);
116     }
117 #ifdef TIZEN
118     gchar *uID = atspi_accessible_get_unique_id(mNode, NULL);
119     if (uID) {
120         mRes = uID;
121         g_free(uID);
122     }
123 #else
124     mRes = std::string{"N/A"};
125 #endif
126
127     gchar *name = atspi_accessible_get_name(mNode, NULL);
128     mText = name;
129     mPkg = name;
130     g_free(name);
131
132     GHashTable *attributes = atspi_accessible_get_attributes(mNode, NULL);
133     char *t = (char*)g_hash_table_lookup(attributes, "type");
134     char *s = (char*)g_hash_table_lookup(attributes, "style");
135
136     if (t) mType =  std::string(t);
137     if (s) mStyle = std::string(s);
138
139     free(t);
140     free(s);
141
142     g_hash_table_unref(attributes);
143
144     AtspiStateSet *st = atspi_accessible_get_state_set(mNode);
145     GArray *states = atspi_state_set_get_states(st);
146
147     char *state_name = NULL;
148     AtspiStateType stat;
149     for (int i = 0; states && (i < states->len); ++i) {
150         stat = g_array_index(states, AtspiStateType, i);
151         setFeatureProperty(stat);
152     }
153
154     if (states) g_array_free(states, 0);
155     g_object_unref(st);
156
157     AtspiComponent *component = atspi_accessible_get_component_iface(mNode);
158     if (component) {
159         AtspiRect *extent = atspi_component_get_extents(
160             component, ATSPI_COORD_TYPE_SCREEN, NULL);
161         if (extent) {
162             mBoundingBox =
163                 Rect<int>{extent->x, extent->y, extent->x + extent->width,
164                           extent->y + extent->height};
165             g_free(extent);
166         }
167         g_object_unref(component);
168     }
169 }
170
171 std::vector<std::string> AtspiAccessibleNode::getActions() const
172 {
173     std::vector<std::string> result{};
174     const char *name;
175     AtspiAction *action;
176
177     action = atspi_accessible_get_action_iface(mNode);
178     if (action) {
179         int a;
180         int n_actions = atspi_action_get_n_actions(action, NULL);
181
182         for (a = 0; a < n_actions; a++) {
183             char *action_name = atspi_action_get_action_name(action, a, NULL);
184             if (!action_name) continue;
185             result.push_back(std::string{action_name});
186             g_free(action_name);
187         }
188         g_object_unref(action);
189     }
190     return result;
191 }
192
193 bool AtspiAccessibleNode::doAction(std::string actionName)
194 {
195     const char *name;
196     AtspiAction *action;
197
198     action = atspi_accessible_get_action_iface(mNode);
199     if (action) {
200         int a;
201         int n_actions = atspi_action_get_n_actions(action, NULL);
202
203         for (a = 0; a < n_actions; a++) {
204             char *action_name = atspi_action_get_action_name(action, a, NULL);
205             if (!action_name) return false;
206
207             if (!strcmp(actionName.c_str(), action_name)) {
208                 atspi_action_do_action(action, a, NULL);
209                 g_free(action_name);
210                 g_object_unref(action);
211                 return true;
212             }
213             g_free(action_name);
214         }
215         g_object_unref(action);
216     }
217     return false;
218 }
219
220 void AtspiAccessibleNode::setValue(std::string text)
221 {
222     AtspiEditableText *iface = atspi_accessible_get_editable_text(mNode);
223     LOG_F(INFO,"set Value iface:%p obj:%p text:%s", iface, mNode, text.c_str() );
224     if (iface) {
225         int len = getText().length();
226         atspi_editable_text_delete_text(iface, 0, len, NULL);
227         atspi_editable_text_insert_text(iface, 0, text.c_str(), text.length(),
228                                         NULL);
229     }
230 }
231
232 void AtspiAccessibleNode::setFeatureProperty(AtspiStateType type)
233 {
234 /*
235     LONGCLICKABLE = 0X0040,
236     SCROLLABLE = 0X0080,
237 */
238     switch(type) {
239         case ATSPI_STATE_CHECKED:
240             setFeatureProperty(NodeFeatureProperties::CHECKED, true);
241             break;
242         case ATSPI_STATE_CHECKABLE:
243             setFeatureProperty(NodeFeatureProperties::CHECKABLE, true);
244             break;
245         case ATSPI_STATE_ENABLED:
246             setFeatureProperty(NodeFeatureProperties::ENABLED, true);
247             break;
248         case ATSPI_STATE_FOCUSABLE:
249             setFeatureProperty(NodeFeatureProperties::FOCUSABLE, true);
250             break;
251         case ATSPI_STATE_FOCUSED:
252             setFeatureProperty(NodeFeatureProperties::FOCUSED, true);
253             break;
254         case ATSPI_STATE_SELECTABLE:
255             setFeatureProperty(NodeFeatureProperties::SELECTABLE, true);
256             break;
257         case ATSPI_STATE_SELECTED:
258             setFeatureProperty(NodeFeatureProperties::SELECTED, true);
259             break;
260         case ATSPI_STATE_SHOWING:
261             setFeatureProperty(NodeFeatureProperties::SHOWING, true);
262             break;
263         case ATSPI_STATE_VISIBLE:
264             setFeatureProperty(NodeFeatureProperties::VISIBLE, true);
265             break;
266         case ATSPI_STATE_ACTIVE:
267             setFeatureProperty(NodeFeatureProperties::ACTIVE, true);
268             break;
269         case ATSPI_STATE_SENSITIVE:
270             setFeatureProperty(NodeFeatureProperties::CLICKABLE, true);
271             break;
272         case ATSPI_STATE_DEFUNCT:
273         case ATSPI_STATE_INVALID:
274             setFeatureProperty(NodeFeatureProperties::INVALID, true);
275             break;
276         case ATSPI_STATE_TRANSIENT:
277         case ATSPI_STATE_TRUNCATED:
278         case ATSPI_STATE_ANIMATED:
279         case ATSPI_STATE_ARMED:
280         case ATSPI_STATE_BUSY:
281         case ATSPI_STATE_COLLAPSED:
282         case ATSPI_STATE_EDITABLE:
283         case ATSPI_STATE_EXPANDABLE:
284         case ATSPI_STATE_EXPANDED:
285         case ATSPI_STATE_HAS_TOOLTIP:
286         case ATSPI_STATE_HORIZONTAL:
287         case ATSPI_STATE_ICONIFIED:
288         case ATSPI_STATE_MODAL:
289         case ATSPI_STATE_MULTI_LINE:
290         case ATSPI_STATE_MULTISELECTABLE:
291         case ATSPI_STATE_OPAQUE:
292         case ATSPI_STATE_PRESSED:
293         case ATSPI_STATE_RESIZABLE:
294         case ATSPI_STATE_SINGLE_LINE:
295         case ATSPI_STATE_STALE:
296         case ATSPI_STATE_VERTICAL:
297         case ATSPI_STATE_MANAGES_DESCENDANTS:
298         case ATSPI_STATE_INDETERMINATE:
299         case ATSPI_STATE_REQUIRED:
300         case ATSPI_STATE_INVALID_ENTRY:
301         case ATSPI_STATE_SUPPORTS_AUTOCOMPLETION:
302         case ATSPI_STATE_SELECTABLE_TEXT:
303         case ATSPI_STATE_IS_DEFAULT:
304         case ATSPI_STATE_VISITED:
305         case ATSPI_STATE_HAS_POPUP:
306         case ATSPI_STATE_READ_ONLY:
307         case ATSPI_STATE_LAST_DEFINED:
308         break;
309     }
310 }