~mial/ubuntu/oneiric/unity/bug-791810

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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*
 * Copyright (C) 2011 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Alejandro PiƱeiro Iglesias <apinheiro@igalia.com>
 */

/**
 * SECTION:unity-launcher-accessible
 * @Title: UnityLauncherAccessible
 * @short_description: Implementation of the ATK interfaces for #Launcher
 * @see_also: Launcher
 *
 * #UnityLauncherAccessible implements the required ATK interfaces for
 * #Launcher, ie: exposing the different LauncherIcon on the model as
 * #child of the object.
 *
 */

#include <glib/gi18n.h>

#include "unity-launcher-accessible.h"
#include "unity-launcher-icon-accessible.h"

#include "unitya11y.h"
#include "Launcher.h"
#include "LauncherModel.h"

/* GObject */
static void unity_launcher_accessible_class_init(UnityLauncherAccessibleClass* klass);
static void unity_launcher_accessible_init(UnityLauncherAccessible* self);
static void unity_launcher_accessible_finalize(GObject* object);

/* AtkObject.h */
static void       unity_launcher_accessible_initialize(AtkObject* accessible,
                                                       gpointer   data);
static gint       unity_launcher_accessible_get_n_children(AtkObject* obj);
static AtkObject* unity_launcher_accessible_ref_child(AtkObject* obj,
                                                      gint i);
static AtkStateSet* unity_launcher_accessible_ref_state_set(AtkObject* obj);

/* AtkSelection */
static void       atk_selection_interface_init(AtkSelectionIface* iface);
static AtkObject* unity_launcher_accessible_ref_selection(AtkSelection* selection,
                                                          gint i);
static gint       unity_launcher_accessible_get_selection_count(AtkSelection* selection);
static gboolean   unity_launcher_accessible_is_child_selected(AtkSelection* selection,
                                                              gint i);

/* private */
static void on_selection_change_cb(UnityLauncherAccessible* launcher_accessible);
static void on_icon_added_cb(LauncherIcon* icon, UnityLauncherAccessible* self);
static void on_icon_removed_cb(LauncherIcon* icon, UnityLauncherAccessible* self);
static void on_order_change_cb(UnityLauncherAccessible* self);
static void update_children_index(UnityLauncherAccessible* self);

G_DEFINE_TYPE_WITH_CODE(UnityLauncherAccessible, unity_launcher_accessible,  NUX_TYPE_VIEW_ACCESSIBLE,
                        G_IMPLEMENT_INTERFACE(ATK_TYPE_SELECTION, atk_selection_interface_init))

#define UNITY_LAUNCHER_ACCESSIBLE_GET_PRIVATE(obj)                      \
  (G_TYPE_INSTANCE_GET_PRIVATE ((obj), UNITY_TYPE_LAUNCHER_ACCESSIBLE,  \
                                UnityLauncherAccessiblePrivate))

struct _UnityLauncherAccessiblePrivate
{
  sigc::connection on_selection_change_connection;
  sigc::connection on_icon_added_connection;
  sigc::connection on_icon_removed_connection;
  sigc::connection on_order_changed_connection;
};


static void
unity_launcher_accessible_class_init(UnityLauncherAccessibleClass* klass)
{
  GObjectClass* gobject_class = G_OBJECT_CLASS(klass);
  AtkObjectClass* atk_class = ATK_OBJECT_CLASS(klass);

  gobject_class->finalize = unity_launcher_accessible_finalize;

  /* AtkObject */
  atk_class->get_n_children = unity_launcher_accessible_get_n_children;
  atk_class->ref_child = unity_launcher_accessible_ref_child;
  atk_class->initialize = unity_launcher_accessible_initialize;
  atk_class->ref_state_set = unity_launcher_accessible_ref_state_set;

  g_type_class_add_private(gobject_class, sizeof(UnityLauncherAccessiblePrivate));
}

static void
unity_launcher_accessible_init(UnityLauncherAccessible* self)
{
  UnityLauncherAccessiblePrivate* priv =
    UNITY_LAUNCHER_ACCESSIBLE_GET_PRIVATE(self);

  self->priv = priv;
}

static void
unity_launcher_accessible_finalize(GObject* object)
{
  UnityLauncherAccessible* self = UNITY_LAUNCHER_ACCESSIBLE(object);

  self->priv->on_selection_change_connection.disconnect();
  self->priv->on_icon_added_connection.disconnect();
  self->priv->on_icon_removed_connection.disconnect();
  self->priv->on_order_changed_connection.disconnect();

  G_OBJECT_CLASS(unity_launcher_accessible_parent_class)->finalize(object);
}

AtkObject*
unity_launcher_accessible_new(nux::Object* object)
{
  AtkObject* accessible = NULL;

  g_return_val_if_fail(dynamic_cast<Launcher*>(object), NULL);

  accessible = ATK_OBJECT(g_object_new(UNITY_TYPE_LAUNCHER_ACCESSIBLE, NULL));

  atk_object_initialize(accessible, object);
  atk_object_set_name(accessible, _("Launcher"));

  return accessible;
}

/* AtkObject.h */
static void
unity_launcher_accessible_initialize(AtkObject* accessible,
                                     gpointer data)
{
  Launcher* launcher = NULL;
  nux::Object* nux_object = NULL;
  UnityLauncherAccessible* self = NULL;
  LauncherModel* model = NULL;

  ATK_OBJECT_CLASS(unity_launcher_accessible_parent_class)->initialize(accessible, data);

  accessible->role = ATK_ROLE_TOOL_BAR;

  self = UNITY_LAUNCHER_ACCESSIBLE(accessible);
  nux_object = nux_object_accessible_get_object(NUX_OBJECT_ACCESSIBLE(accessible));

  launcher = dynamic_cast<Launcher*>(nux_object);

  self->priv->on_selection_change_connection  =
    launcher->selection_change.connect(sigc::bind(sigc::ptr_fun(on_selection_change_cb), self));

  model = launcher->GetModel();

  if (model)
  {
    self->priv->on_icon_added_connection =
      model->icon_added.connect(sigc::bind(sigc::ptr_fun(on_icon_added_cb), self));

    self->priv->on_icon_removed_connection =
      model->icon_removed.connect(sigc::bind(sigc::ptr_fun(on_icon_removed_cb), self));

    self->priv->on_order_changed_connection =
      model->order_changed.connect(sigc::bind(sigc::ptr_fun(on_order_change_cb), self));
  }
}

static gint
unity_launcher_accessible_get_n_children(AtkObject* obj)
{
  nux::Object* object = NULL;
  Launcher* launcher = NULL;
  LauncherModel* launcher_model = NULL;

  g_return_val_if_fail(UNITY_IS_LAUNCHER_ACCESSIBLE(obj), 0);

  object = nux_object_accessible_get_object(NUX_OBJECT_ACCESSIBLE(obj));
  if (!object) /* state is defunct */
    return 0;

  launcher = dynamic_cast<Launcher*>(object);

  launcher_model = launcher->GetModel();

  if (launcher_model)
    return launcher_model->Size();
  else
    return 0;
}

static AtkObject*
unity_launcher_accessible_ref_child(AtkObject* obj,
                                    gint i)
{
  gint num = 0;
  nux::Object* nux_object = NULL;
  Launcher* launcher = NULL;
  LauncherModel* launcher_model = NULL;
  LauncherModel::iterator it;
  nux::Object* child = NULL;
  AtkObject* child_accessible = NULL;

  g_return_val_if_fail(UNITY_IS_LAUNCHER_ACCESSIBLE(obj), NULL);
  num = atk_object_get_n_accessible_children(obj);
  g_return_val_if_fail((i < num) && (i >= 0), NULL);

  nux_object = nux_object_accessible_get_object(NUX_OBJECT_ACCESSIBLE(obj));
  if (!nux_object) /* state is defunct */
    return 0;

  launcher = dynamic_cast<Launcher*>(nux_object);

  launcher_model = launcher->GetModel();

  it = launcher_model->begin();
  std::advance(it, i);

  child = dynamic_cast<nux::Object*>(*it);
  child_accessible = unity_a11y_get_accessible(child);

  g_object_ref(child_accessible);

  return child_accessible;
}

static AtkStateSet*
unity_launcher_accessible_ref_state_set(AtkObject* obj)
{
  AtkStateSet* state_set = NULL;
  nux::Object* nux_object = NULL;

  g_return_val_if_fail(UNITY_IS_LAUNCHER_ACCESSIBLE(obj), NULL);

  state_set =
    ATK_OBJECT_CLASS(unity_launcher_accessible_parent_class)->ref_state_set(obj);

  nux_object = nux_object_accessible_get_object(NUX_OBJECT_ACCESSIBLE(obj));

  if (nux_object == NULL) /* defunct */
    return state_set;

  /* The Launcher is always focusable */
  atk_state_set_add_state(state_set, ATK_STATE_FOCUSABLE);

  return state_set;
}

/* AtkSelection */
static void
atk_selection_interface_init(AtkSelectionIface* iface)
{
  iface->ref_selection = unity_launcher_accessible_ref_selection;
  iface->get_selection_count = unity_launcher_accessible_get_selection_count;
  iface->is_child_selected = unity_launcher_accessible_is_child_selected;

  /* NOTE: for the moment we don't provide the implementation for the
     "interactable" methods, it is, the methods that allow to change
     the selected icon. The Launcher doesn't provide that API, and
     right now  we are focusing on a normal user input.*/
  /* iface->add_selection = unity_launcher_accessible_add_selection; */
  /* iface->clear_selection = unity_launcher_accessible_clear_selection; */
  /* iface->remove_selection = unity_launcher_accessible_remove_selection; */

  /* This method will never be implemented, as select all the launcher
     icons makes no sense */
  /* iface->select_all = unity_launcher_accessible_select_all_selection; */
}

static AtkObject*
unity_launcher_accessible_ref_selection(AtkSelection* selection,
                                        gint i)
{
  Launcher* launcher = NULL;
  LauncherIcon* selected_icon = NULL;
  nux::Object* nux_object = NULL;
  AtkObject* accessible_selected = NULL;

  g_return_val_if_fail(UNITY_IS_LAUNCHER_ACCESSIBLE(selection), 0);
  /* there can be only just item selected */
  g_return_val_if_fail(i == 0, NULL);

  nux_object = nux_object_accessible_get_object(NUX_OBJECT_ACCESSIBLE(selection));
  if (!nux_object) /* state is defunct */
    return 0;

  launcher = dynamic_cast<Launcher*>(nux_object);
  selected_icon = launcher->GetSelectedMenuIcon();

  if (selected_icon != 0)
  {
    accessible_selected = unity_a11y_get_accessible(selected_icon);
    g_object_ref(accessible_selected);
  }

  return accessible_selected;
}

static gint
unity_launcher_accessible_get_selection_count(AtkSelection* selection)
{
  Launcher* launcher = NULL;
  LauncherIcon* selected_icon = NULL;
  nux::Object* nux_object = NULL;

  g_return_val_if_fail(UNITY_IS_LAUNCHER_ACCESSIBLE(selection), 0);

  nux_object = nux_object_accessible_get_object(NUX_OBJECT_ACCESSIBLE(selection));
  if (!nux_object) /* state is defunct */
    return 0;

  launcher = dynamic_cast<Launcher*>(nux_object);
  selected_icon = launcher->GetSelectedMenuIcon();

  if (selected_icon == 0)
    return 0;
  else
    return 1;
}

static gboolean
unity_launcher_accessible_is_child_selected(AtkSelection* selection,
                                            gint i)
{
  Launcher* launcher = NULL;
  LauncherIcon* icon = NULL;
  LauncherIcon* selected_icon = NULL;
  LauncherModel* launcher_model = NULL;
  LauncherModel::iterator it;
  nux::Object* nux_object = NULL;

  g_return_val_if_fail(UNITY_IS_LAUNCHER_ACCESSIBLE(selection), FALSE);

  nux_object = nux_object_accessible_get_object(NUX_OBJECT_ACCESSIBLE(selection));
  if (!nux_object) /* state is defunct */
    return 0;

  launcher = dynamic_cast<Launcher*>(nux_object);
  launcher_model = launcher->GetModel();
  it = launcher_model->begin();
  std::advance(it, i);
  icon = dynamic_cast<LauncherIcon*>(*it);

  selected_icon = launcher->GetSelectedMenuIcon();

  if (selected_icon == icon)
    return TRUE;
  else
    return FALSE;
}

/* private */
static void on_selection_change_cb(UnityLauncherAccessible* launcher_accessible)
{
  g_signal_emit_by_name(ATK_OBJECT(launcher_accessible), "selection-changed");
}


static void
on_icon_added_cb(LauncherIcon* icon,
                 UnityLauncherAccessible* self)
{
  AtkObject* icon_accessible = NULL;
  nux::Object* nux_object = NULL;
  gint index = 0;

  g_return_if_fail(UNITY_IS_LAUNCHER_ACCESSIBLE(self));

  nux_object = nux_object_accessible_get_object(NUX_OBJECT_ACCESSIBLE(self));
  if (nux_object == NULL) /* state is defunct */
    return;

  icon_accessible = unity_a11y_get_accessible(icon);

  update_children_index(self);

  index = atk_object_get_index_in_parent(icon_accessible);

  g_signal_emit_by_name(self, "children-changed::add",
                        index, icon_accessible, NULL);
}

static void
on_icon_removed_cb(LauncherIcon* icon,
                   UnityLauncherAccessible* self)
{
  AtkObject* icon_accessible = NULL;
  nux::Object* nux_object = NULL;
  gint index = 0;

  g_return_if_fail(UNITY_IS_LAUNCHER_ACCESSIBLE(self));

  nux_object = nux_object_accessible_get_object(NUX_OBJECT_ACCESSIBLE(self));
  if (nux_object == NULL) /* state is defunct */
    return;

  icon_accessible = unity_a11y_get_accessible(icon);

  index = atk_object_get_index_in_parent(icon_accessible);

  g_signal_emit_by_name(self, "children-changed::remove",
                        index, icon_accessible, NULL);

  update_children_index(self);
}

static void
update_children_index(UnityLauncherAccessible* self)
{
  gint index = 0;
  nux::Object* nux_object = NULL;
  Launcher* launcher = NULL;
  LauncherModel* launcher_model = NULL;
  LauncherModel::iterator it;
  nux::Object* child = NULL;
  AtkObject* child_accessible = NULL;

  nux_object = nux_object_accessible_get_object(NUX_OBJECT_ACCESSIBLE(self));
  if (!nux_object) /* state is defunct */
    return;

  launcher = dynamic_cast<Launcher*>(nux_object);
  launcher_model = launcher->GetModel();

  if (launcher_model == NULL)
    return;

  for (it = launcher_model->begin(); it != launcher_model->end(); it++)
  {
    child =  dynamic_cast<nux::Object*>(*it);
    child_accessible = unity_a11y_get_accessible(child);

    unity_launcher_icon_accessible_set_index(UNITY_LAUNCHER_ICON_ACCESSIBLE(child_accessible),
                                             index++);
  }
}

static void
on_order_change_cb(UnityLauncherAccessible* self)
{
  g_return_if_fail(UNITY_IS_LAUNCHER_ACCESSIBLE(self));

  update_children_index(self);
}