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