gtestdbus: Fix non-const use of const variables
[platform/upstream/glib.git] / gio / gactionmap.c
1 /*
2  * Copyright © 2010 Codethink Limited
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; either version 2 of the licence or (at
7  * your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General
15  * Public License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17  * Boston, MA 02111-1307, USA.
18  *
19  * Authors: Ryan Lortie <desrt@desrt.ca>
20  */
21
22 #include "config.h"
23
24 #include "gsimpleaction.h"
25 #include "gactionmap.h"
26 #include "gaction.h"
27
28 /**
29  * SECTION:gactionmap
30  * @title: GActionMap
31  * @short_description: Interface for action containers
32  *
33  * The GActionMap interface is implemented by #GActionGroup
34  * implementations that operate by containing a number of
35  * named #GAction instances, such as #GSimpleActionGroup.
36  *
37  * One useful application of this interface is to map the
38  * names of actions from various action groups to unique,
39  * prefixed names (e.g. by prepending "app." or "win.").
40  * This is the motivation for the 'Map' part of the interface
41  * name.
42  *
43  * Since: 2.32
44  **/
45
46 /**
47  * GActionMapInterface:
48  * @lookup_action: the virtual function pointer for g_action_map_lookup_action()
49  * @add_action: the virtual function pointer for g_action_map_add_action()
50  * @remove_action: the virtual function pointer for g_action_map_remove_action()
51  *
52  * The virtual function table for #GActionMap.
53  *
54  * Since: 2.32
55  **/
56
57 G_DEFINE_INTERFACE (GActionMap, g_action_map, G_TYPE_OBJECT)
58
59 static void
60 g_action_map_default_init (GActionMapInterface *iface)
61 {
62 }
63
64 /**
65  * g_action_map_lookup_action:
66  * @action_map: a #GActionMap
67  * @action_name: the name of an action
68  *
69  * Looks up the action with the name @action_name in @action_map.
70  *
71  * If no such action exists, returns %NULL.
72  *
73  * Returns: (transfer none): a #GAction, or %NULL
74  *
75  * Since: 2.32
76  */
77 GAction *
78 g_action_map_lookup_action (GActionMap  *action_map,
79                             const gchar *action_name)
80 {
81   return G_ACTION_MAP_GET_IFACE (action_map)
82     ->lookup_action (action_map, action_name);
83 }
84
85 /**
86  * g_action_map_add_action:
87  * @action_map: a #GActionMap
88  * @action: a #GAction
89  *
90  * Adds an action to the @action_map.
91  *
92  * If the action map already contains an action with the same name
93  * as @action then the old action is dropped from the action map.
94  *
95  * The action map takes its own reference on @action.
96  *
97  * Since: 2.32
98  */
99 void
100 g_action_map_add_action (GActionMap *action_map,
101                          GAction    *action)
102 {
103   G_ACTION_MAP_GET_IFACE (action_map)->add_action (action_map, action);
104 }
105
106 /**
107  * g_action_map_remove_action:
108  * @action_map: a #GActionMap
109  * @action_name: the name of the action
110  *
111  * Removes the named action from the action map.
112  *
113  * If no action of this name is in the map then nothing happens.
114  *
115  * Since: 2.32
116  */
117 void
118 g_action_map_remove_action (GActionMap  *action_map,
119                             const gchar *action_name)
120 {
121   G_ACTION_MAP_GET_IFACE (action_map)->remove_action (action_map, action_name);
122 }
123
124 /**
125  * GActionEntry:
126  * @name: the name of the action
127  * @activate: the callback to connect to the "activate" signal of the
128  *            action
129  * @parameter_type: the type of the parameter that must be passed to the
130  *                  activate function for this action, given as a single
131  *                  GVariant type string (or %NULL for no parameter)
132  * @state: the initial state for this action, given in GVariant text
133  *         format.  The state is parsed with no extra type information,
134  *         so type tags must be added to the string if they are
135  *         necessary.
136  * @change_state: the callback to connect to the "change-state" signal
137  *                of the action
138  *
139  * This struct defines a single action.  It is for use with
140  * g_action_map_add_action_entries().
141  *
142  * The order of the items in the structure are intended to reflect
143  * frequency of use.  It is permissible to use an incomplete initialiser
144  * in order to leave some of the later values as %NULL.  All values
145  * after @name are optional.  Additional optional fields may be added in
146  * the future.
147  *
148  * See g_action_map_add_action_entries() for an example.
149  **/
150
151 /**
152  * g_action_map_add_action_entries:
153  * @action_map: a #GActionMap
154  * @entries: (array length=n_entries) (element-type GActionEntry): a pointer to
155  *           the first item in an array of #GActionEntry structs
156  * @n_entries: the length of @entries, or -1 if @entries is %NULL-terminated
157  * @user_data: the user data for signal connections
158  *
159  * A convenience function for creating multiple #GSimpleAction instances
160  * and adding them to a #GActionMap.
161  *
162  * Each action is constructed as per one #GActionEntry.
163  *
164  * <example>
165  * <title>Using g_action_map_add_action_entries()</title>
166  * <programlisting>
167  * static void
168  * activate_quit (GSimpleAction *simple,
169  *                GVariant      *parameter,
170  *                gpointer       user_data)
171  * {
172  *   exit (0);
173  * }
174  *
175  * static void
176  * activate_print_string (GSimpleAction *simple,
177  *                        GVariant      *parameter,
178  *                        gpointer       user_data)
179  * {
180  *   g_print ("%s\n", g_variant_get_string (parameter, NULL));
181  * }
182  *
183  * static GActionGroup *
184  * create_action_group (void)
185  * {
186  *   const GActionEntry entries[] = {
187  *     { "quit",         activate_quit              },
188  *     { "print-string", activate_print_string, "s" }
189  *   };
190  *   GSimpleActionGroup *group;
191  *
192  *   group = g_simple_action_group_new ();
193  *   g_action_map_add_action_entries (G_ACTION_MAP (group), entries, G_N_ELEMENTS (entries), NULL);
194  *
195  *   return G_ACTION_GROUP (group);
196  * }
197  * </programlisting>
198  * </example>
199  *
200  * Since: 2.32
201  */
202 void
203 g_action_map_add_action_entries (GActionMap         *action_map,
204                                  const GActionEntry *entries,
205                                  gint                n_entries,
206                                  gpointer            user_data)
207 {
208   gint i;
209
210   g_return_if_fail (G_IS_ACTION_MAP (action_map));
211   g_return_if_fail (entries != NULL || n_entries == 0);
212
213   for (i = 0; n_entries == -1 ? entries[i].name != NULL : i < n_entries; i++)
214     {
215       const GActionEntry *entry = &entries[i];
216       const GVariantType *parameter_type;
217       GSimpleAction *action;
218
219       if (entry->parameter_type)
220         {
221           if (!g_variant_type_string_is_valid (entry->parameter_type))
222             {
223               g_critical ("g_action_map_add_entries: the type "
224                           "string '%s' given as the parameter type for "
225                           "action '%s' is not a valid GVariant type "
226                           "string.  This action will not be added.",
227                           entry->parameter_type, entry->name);
228               return;
229             }
230
231           parameter_type = G_VARIANT_TYPE (entry->parameter_type);
232         }
233       else
234         parameter_type = NULL;
235
236       if (entry->state)
237         {
238           GError *error = NULL;
239           GVariant *state;
240
241           state = g_variant_parse (NULL, entry->state, NULL, NULL, &error);
242           if (state == NULL)
243             {
244               g_critical ("g_action_map_add_entries: GVariant could "
245                           "not parse the state value given for action '%s' "
246                           "('%s'): %s.  This action will not be added.",
247                           entry->name, entry->state, error->message);
248               g_error_free (error);
249               continue;
250             }
251
252           action = g_simple_action_new_stateful (entry->name,
253                                                  parameter_type,
254                                                  state);
255
256           g_variant_unref (state);
257         }
258       else
259         {
260           action = g_simple_action_new (entry->name,
261                                         parameter_type);
262         }
263
264       if (entry->activate != NULL)
265         g_signal_connect (action, "activate",
266                           G_CALLBACK (entry->activate), user_data);
267
268       if (entry->change_state != NULL)
269         g_signal_connect (action, "change-state",
270                           G_CALLBACK (entry->change_state), user_data);
271
272       g_action_map_add_action (action_map, G_ACTION (action));
273       g_object_unref (action);
274     }
275 }