2009-27-09 Mark Doffman <mark.doffman@codethink.co.uk>
[platform/core/uifw/at-spi2-atk.git] / tests / dummyatk / my-atk-action.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <atk/atk.h>
4
5 #include "my-atk-object.h"
6 #include "my-atk-action.h"
7
8 static GObjectClass *parent_class = NULL;
9 //implementaion of the interface
10 static gboolean my_atk_action_do_action(AtkAction *action, gint i)
11 {
12     MyAtkAction *self = (MyAtkAction*)action;
13     gboolean result = (i>=0) && (i < self->n);
14     self->last_performed_action = result? i : -1;
15     return result;
16 }
17 static gint my_atk_action_get_n_actions(AtkAction *action)
18 {
19     MyAtkAction *self = (MyAtkAction*)action;
20     return self->n;
21 }
22 static const gchar* my_atk_action_get_description(AtkAction *action, gint i)
23 {
24     MyAtkAction *self = (MyAtkAction*)action;
25     if((i>=0) && (i<self->n))
26     {
27          return self->actions[i].description;
28     }
29     else
30     {
31         printf("get_description: Wrong index.\n");
32         return NULL;
33     }
34 }
35 static const gchar* my_atk_action_get_name(AtkAction *action, gint i)
36 {
37     MyAtkAction *self = (MyAtkAction*)action;
38     if((i >= 0) && (i < self->n))
39     {
40          return self->actions[i].name;
41     }
42     else
43     {
44         printf("get_name: Wrong index.\n");
45         return NULL;
46     }
47 }
48 static const gchar* my_atk_action_get_localized_name(AtkAction *action, gint i)
49 {
50     return my_atk_action_get_name(action,i);
51 }
52
53 static const gchar* my_atk_action_get_keybinding(AtkAction *action, gint i)
54 {
55     MyAtkAction *self = (MyAtkAction*)action;
56     if((i >= 0) && (i < self->n))
57     {
58         gchar* keyb = self->actions[i].keybinding;
59         if(keyb == NULL || keybinding_note_define == NULL)
60         {
61             //anywhere(if action has keybinding or not) NULL will return
62             return NULL;
63         }
64         else
65         {
66             //verify, if string mean "no keybinding"
67             return strcmp(keyb, keybinding_note_define) != 0 ? keyb : NULL;
68         }
69     }
70     else
71     {
72         printf("get_keybinding: Wrong index.\n");
73         return NULL;
74     }
75 }
76 static gboolean my_atk_action_set_description(AtkAction *action, gint i, const gchar *desc)
77 {
78     MyAtkAction *self = (MyAtkAction*)action;
79
80     if(!((i >= 0) && (i < self->n)) )
81     {
82         //index out of range, but this is not application error according documentation
83         return FALSE;
84     }
85     //index in correct range
86     if(self->actions[i].description == desc)
87     {
88         //self assignment - return immediately
89         return TRUE;
90     }
91     if(self->actions[i].description != NULL)
92     {
93         //free old value of description if it is not NULL
94         free(self->actions[i].description);
95     }
96     if(desc != NULL)
97     {
98         //dump new value of description if it is not NULL
99         self->actions[i].description = (gchar*)strdup((const char*)desc);
100     }
101     return TRUE;
102 }
103 //////////
104 static void my_atk_action_instance_init(GTypeInstance *instance, gpointer g_class)
105 {
106   int i;
107     MyAtkAction *self = (MyAtkAction*)instance;
108     self->n = DEFAULT_NUMBER_ACTIONS;
109     self->actions = g_new(struct OneAction, self->n);
110     if(self->actions == NULL)
111     {
112         self->n = 0;
113         return;
114     }
115     //init fields of action 0 with values which differ from others actions
116     self->actions[0].name = (gchar*)strdup(FIRST_ACTION_NAME);
117     self->actions[0].description = (gchar*)strdup(FIRST_ACTION_DESCRIPTION);
118     self->actions[0].keybinding = (gchar*)strdup(FIRST_ACTION_KEYBINDING);
119
120     for(i = 1; i < self->n; i++)
121     {
122         self->actions[i].name = (gchar*)strdup(DEFAULT_ACTION_NAME);
123         self->actions[i].description = (gchar*)strdup(DEFAULT_ACTION_DESCRIPTION);
124         self->actions[i].keybinding = g_strdup_printf("%d", i);
125     }
126     self->disposed = FALSE;
127     self->last_performed_action = -1;
128 }
129
130 static void
131 my_atk_action_interface_init(gpointer g_iface, gpointer iface_data)
132 {
133     AtkActionIface *klass = (AtkActionIface *)g_iface;
134
135     klass->do_action = my_atk_action_do_action;
136     klass->get_n_actions = my_atk_action_get_n_actions;
137     klass->get_description = my_atk_action_get_description;
138     klass->get_name = my_atk_action_get_name;
139     klass->get_localized_name = my_atk_action_get_localized_name;
140     klass->get_keybinding = my_atk_action_get_keybinding;
141     klass->set_description = my_atk_action_set_description;
142 }
143
144 static void
145 my_atk_action_dispose(GObject *obj)
146 {
147     MyAtkAction *self = (MyAtkAction*)obj;
148
149     if(self->disposed)
150     {
151         return;
152     }
153     self->disposed = TRUE;
154
155     G_OBJECT_CLASS(parent_class)->dispose(obj);
156 }
157
158 static void 
159 my_atk_action_finalize(GObject *obj)
160 {
161     MyAtkAction *self = (MyAtkAction*)obj;
162   int i;
163
164     for(i = 0; i < self->n; i++)
165     {
166         struct OneAction oneAction = self->actions[i];
167         if(oneAction.name != NULL)
168             free(oneAction.name);
169         if(oneAction.description != NULL)
170             free(oneAction.description);
171         if(oneAction.keybinding != NULL)
172             free(oneAction.keybinding);
173     }
174     if(self->actions != NULL)
175         g_free(self->actions);
176
177     G_OBJECT_CLASS(parent_class)->finalize(obj);
178 }
179 static void
180 my_atk_action_class_init (gpointer g_class, gpointer g_class_data)
181 {
182     GObjectClass *gobject_class = G_OBJECT_CLASS(g_class);
183     MyAtkActionClass *klass = MY_ATK_ACTION_CLASS (g_class);
184
185     gobject_class->dispose = my_atk_action_dispose;
186     gobject_class->finalize = my_atk_action_finalize;
187
188     parent_class = g_type_class_peek_parent(klass);
189 }
190 GType my_atk_action_get_type(void)
191 {
192     static GType type = 0;
193     if(type == 0)
194     {
195         static const GTypeInfo info =
196         {
197             sizeof (MyAtkActionClass),
198             NULL,   /* base_init */
199             NULL,   /* base_finalize */
200             my_atk_action_class_init, /* class_init */
201             NULL,   /* class_finalize */
202             NULL,   /* class_data */
203             sizeof (MyAtkAction),
204             0,      /* n_preallocs */
205             my_atk_action_instance_init    /* instance_init */
206         };
207
208         static const GInterfaceInfo iface_info = 
209         {
210             (GInterfaceInitFunc) my_atk_action_interface_init,    /* interface_init */
211             NULL,                                       /* interface_finalize */
212             NULL                                        /* interface_data */
213         };
214         type = g_type_register_static (MY_TYPE_ATK_OBJECT,
215             "MyAtkAction",
216             &info, 0);
217         g_type_add_interface_static (type,
218             ATK_TYPE_ACTION,
219             &iface_info);
220     }
221     return type;
222 }