1
/*
2
 * AT-SPI - Assistive Technology Service Provider Interface
3
 * (Gnome Accessibility Project; https://wiki.gnome.org/Accessibility)
4
 *
5
 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library; if not, write to the
19
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20
 * Boston, MA 02110-1301, USA.
21
 */
22

            
23
#include <atk/atk.h>
24
#include <stdio.h>
25
#include <string.h>
26

            
27
#include "my-atk-action.h"
28
#include "my-atk-object.h"
29

            
30
typedef struct _MyAtkActionInfo MyAtkActionInfo;
31

            
32
struct _MyAtkActionInfo
33
{
34
  gchar *name;
35
  gchar *description;
36
  gchar *keybinding;
37

            
38
  MyAtkActionFunc do_action_func;
39
};
40

            
41
static void atk_action_interface_init (AtkActionIface *iface);
42

            
43
struct _MyAtkActionPrivate
44
{
45
  GQueue *action_queue;
46
  guint action_idle_handler;
47
  GList *action_list;
48
  GList *children;
49
};
50

            
51
7
G_DEFINE_TYPE_WITH_CODE (MyAtkAction,
52
                         my_atk_action,
53
                         MY_TYPE_ATK_OBJECT,
54
                         G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION,
55
                                                atk_action_interface_init)
56
                             G_ADD_PRIVATE (MyAtkAction));
57

            
58
static void
59
my_atk_action_initialize (AtkObject *obj, gpointer data)
60
{
61
}
62

            
63
static void
64
14
my_atk_action_init (MyAtkAction *action_obj)
65
{
66
14
  MyAtkActionPrivate *priv = my_atk_action_get_instance_private (action_obj);
67
14
  action_obj->priv = priv;
68
14
  priv->action_queue = NULL;
69
14
  priv->action_idle_handler = 0;
70
14
  priv->action_list = NULL;
71
14
  priv->children = NULL;
72
14
}
73

            
74
static void
75
my_atk_action_finalize (GObject *object)
76
{
77
}
78

            
79
static void
80
7
my_atk_action_class_init (MyAtkActionClass *my_class)
81
{
82
7
  AtkObjectClass *atk_class = ATK_OBJECT_CLASS (my_class);
83
7
  GObjectClass *gobject_class = G_OBJECT_CLASS (my_class);
84

            
85
7
  gobject_class->finalize = my_atk_action_finalize;
86

            
87
7
  atk_class->initialize = my_atk_action_initialize;
88
7
}
89

            
90
static MyAtkActionInfo *
91
4
_my_atk_action_get_action_info (MyAtkAction *action, gint i)
92
{
93
4
  MyAtkActionPrivate *priv = NULL;
94
4
  MyAtkActionInfo *node_data = NULL;
95

            
96
4
  g_return_val_if_fail (MY_IS_ATK_ACTION (action), NULL);
97

            
98
4
  priv = action->priv;
99

            
100
4
  if (priv->action_list == NULL)
101
    return NULL;
102

            
103
4
  node_data = g_list_nth_data (priv->action_list, i);
104

            
105
4
  g_return_val_if_fail (node_data, NULL);
106

            
107
4
  return node_data;
108
}
109

            
110
static const gchar *
111
1
my_atk_action_description_get (AtkAction *action, gint i)
112
{
113
1
  MyAtkAction *my_action = NULL;
114
1
  MyAtkActionInfo *info = NULL;
115

            
116
1
  g_return_val_if_fail (MY_IS_ATK_ACTION (action), NULL);
117
1
  my_action = MY_ATK_ACTION (action);
118

            
119
1
  info = _my_atk_action_get_action_info (my_action, i);
120

            
121
1
  if (info == NULL)
122
    return NULL;
123

            
124
1
  return strdup (info->description);
125
}
126

            
127
static gboolean
128
my_atk_action_description_set (AtkAction *action, gint i, const char *des)
129
{
130
  MyAtkAction *my_action = NULL;
131
  MyAtkActionInfo *info = NULL;
132

            
133
  g_return_val_if_fail (MY_IS_ATK_ACTION (action), FALSE);
134
  my_action = MY_ATK_ACTION (action);
135

            
136
  info = _my_atk_action_get_action_info (my_action, i);
137

            
138
  if (info == NULL)
139
    return FALSE;
140

            
141
  g_free (info->description);
142
  info->description = g_strdup (des);
143

            
144
  return TRUE;
145
}
146

            
147
static const gchar *
148
1
my_atk_action_name_get (AtkAction *action, gint i)
149
{
150
1
  MyAtkAction *my_action = NULL;
151
1
  MyAtkActionInfo *info = NULL;
152

            
153
1
  g_return_val_if_fail (MY_IS_ATK_ACTION (action), NULL);
154
1
  my_action = MY_ATK_ACTION (action);
155

            
156
1
  info = _my_atk_action_get_action_info (my_action, i);
157

            
158
1
  if (info == NULL)
159
    return NULL;
160

            
161
1
  return strdup (info->name);
162
}
163

            
164
static const gchar *
165
1
my_atk_action_localized_name_get (AtkAction *action, gint i)
166
{
167
1
  MyAtkAction *my_action = NULL;
168
1
  MyAtkActionInfo *info = NULL;
169

            
170
1
  g_return_val_if_fail (MY_IS_ATK_ACTION (action), NULL);
171
1
  my_action = MY_ATK_ACTION (action);
172

            
173
1
  info = _my_atk_action_get_action_info (my_action, i);
174

            
175
1
  if (info == NULL)
176
    return NULL;
177

            
178
1
  return strdup (info->name);
179
}
180

            
181
static gint
182
1
my_atk_action_get_n_actions (AtkAction *action)
183
{
184
1
  MyAtkAction *action_obj = NULL;
185
1
  MyAtkActionPrivate *priv = NULL;
186

            
187
1
  action_obj = MY_ATK_ACTION (action);
188
1
  priv = action_obj->priv;
189

            
190
1
  return g_list_length (priv->action_list);
191
}
192

            
193
static const gchar *
194
1
my_atk_action_get_keybinding (AtkAction *action, gint i)
195
{
196
1
  MyAtkAction *my_action = NULL;
197
1
  MyAtkActionInfo *info = NULL;
198

            
199
1
  g_return_val_if_fail (MY_IS_ATK_ACTION (action), NULL);
200
1
  my_action = MY_ATK_ACTION (action);
201

            
202
1
  info = _my_atk_action_get_action_info (my_action, i);
203

            
204
1
  if (info == NULL)
205
    return NULL;
206

            
207
1
  return strdup (info->keybinding);
208
}
209

            
210
void
211
1
perform_action (AtkObject *obj)
212
{
213
1
  AtkStateSet *state_set1 = atk_object_ref_state_set (obj);
214
1
  atk_state_set_add_state (state_set1, ATK_STATE_ACTIVE);
215
1
}
216

            
217
static gboolean
218
1
my_atk_action_do_action (AtkAction *action, gint i)
219
{
220
1
  g_return_val_if_fail (MY_IS_ATK_ACTION (action), FALSE);
221

            
222
1
  perform_action (ATK_OBJECT (action));
223

            
224
1
  return FALSE;
225
}
226

            
227
guint
228
21
my_atk_action_add_action (MyAtkAction *action,
229
                          const gchar *action_name,
230
                          const gchar *action_description,
231
                          const gchar *action_keybinding)
232
{
233
21
  MyAtkActionInfo *info = NULL;
234
21
  MyAtkActionPrivate *priv = NULL;
235

            
236
21
  g_return_val_if_fail (MY_IS_ATK_ACTION (action), -1);
237

            
238
21
  priv = action->priv;
239

            
240
21
  info = g_slice_new (MyAtkActionInfo);
241
21
  info->name = g_strdup (action_name);
242
21
  info->description = g_strdup (action_description);
243
21
  info->keybinding = g_strdup (action_keybinding);
244

            
245
21
  priv->action_list = g_list_append (priv->action_list, info);
246

            
247
21
  return g_list_length (priv->action_list);
248
}
249

            
250
static void
251
7
atk_action_interface_init (AtkActionIface *iface)
252
{
253
7
  g_return_if_fail (iface);
254

            
255
7
  iface->do_action = my_atk_action_do_action;
256

            
257
7
  iface->get_n_actions = my_atk_action_get_n_actions;
258
7
  iface->get_description = my_atk_action_description_get;
259
7
  iface->get_keybinding = my_atk_action_get_keybinding;
260
7
  iface->get_name = my_atk_action_name_get;
261
7
  iface->set_description = my_atk_action_description_set;
262
7
  iface->get_localized_name = my_atk_action_localized_name_get;
263
}