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