g_unix_set_fd_nonblocking: New API to control file descriptor blocking state
[platform/upstream/glib.git] / gio / gactiongroup.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 #include "gactiongroup.h"
24 #include "gaction.h"
25 #include "gio-marshal.h"
26 #include "glibintl.h"
27
28 /**
29  * SECTION:gactiongroup
30  * @title: GActionGroup
31  * @short_description: A group of actions
32  *
33  * #GActionGroup represents a group of actions.
34  *
35  * Each action in the group has a unique name (which is a string).  All
36  * method calls, except g_action_group_list_actions() take the name of
37  * an action as an argument.
38  *
39  * The #GActionGroup API is meant to be the 'public' API to the action
40  * group.  The calls here are exactly the interaction that 'external
41  * forces' (eg: UI, incoming D-Bus messages, etc.) are supposed to have
42  * with actions.  'Internal' APIs (ie: ones meant only to be accessed by
43  * the action group implementation) are found on subclasses.  This is
44  * why you will find -- for example -- g_action_group_get_enabled() but
45  * not an equivalent <function>set()</function> call.
46  *
47  * Signals are emitted on the action group in response to state changes
48  * on individual actions.
49  **/
50
51 G_DEFINE_INTERFACE (GActionGroup, g_action_group, G_TYPE_OBJECT)
52
53 enum
54 {
55   SIGNAL_ACTION_ADDED,
56   SIGNAL_ACTION_REMOVED,
57   SIGNAL_ACTION_ENABLED_CHANGED,
58   SIGNAL_ACTION_STATE_CHANGED,
59   NR_SIGNALS
60 };
61
62 static guint g_action_group_signals[NR_SIGNALS];
63
64 static void
65 g_action_group_default_init (GActionGroupInterface *class)
66 {
67   /**
68    * GActionGroup::action-added:
69    * @action_group: the #GActionGroup that changed
70    * @action_name: the name of the action in @action_group
71    *
72    * Signals that a new action was just added to the group.  This signal
73    * is emitted after the action has been added and is now visible.
74    *
75    * Since: 2.28
76    **/
77   g_action_group_signals[SIGNAL_ACTION_ADDED] =
78     g_signal_new (I_("action-added"),
79                   G_TYPE_ACTION_GROUP,
80                   G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
81                   G_STRUCT_OFFSET (GActionGroupInterface, action_added),
82                   NULL, NULL,
83                   g_cclosure_marshal_VOID__STRING,
84                   G_TYPE_NONE, 1,
85                   G_TYPE_STRING);
86
87   /**
88    * GActionGroup::action-removed:
89    * @action_group: the #GActionGroup that changed
90    * @action_name: the name of the action in @action_group
91    *
92    * Signals that an action is just about to be removed from the group.
93    * This signal is emitted before the action is removed, so the action
94    * is still visible and can be queried from the signal handler.
95    *
96    * Since: 2.28
97    **/
98   g_action_group_signals[SIGNAL_ACTION_REMOVED] =
99     g_signal_new (I_("action-removed"),
100                   G_TYPE_ACTION_GROUP,
101                   G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
102                   G_STRUCT_OFFSET (GActionGroupInterface, action_removed),
103                   NULL, NULL,
104                   g_cclosure_marshal_VOID__STRING,
105                   G_TYPE_NONE, 1,
106                   G_TYPE_STRING);
107
108
109   /**
110    * GActionGroup::action-enabled-changed:
111    * @action_group: the #GActionGroup that changed
112    * @action_name: the name of the action in @action_group
113    * @enabled: whether the action is enabled or not
114    *
115    * Signals that the enabled status of the named action has changed.
116    *
117    * Since: 2.28
118    **/
119   g_action_group_signals[SIGNAL_ACTION_ENABLED_CHANGED] =
120     g_signal_new (I_("action-enabled-changed"),
121                   G_TYPE_ACTION_GROUP,
122                   G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
123                   G_STRUCT_OFFSET (GActionGroupInterface,
124                                    action_enabled_changed),
125                   NULL, NULL,
126                   _gio_marshal_VOID__STRING_BOOLEAN,
127                   G_TYPE_NONE, 2,
128                   G_TYPE_STRING,
129                   G_TYPE_BOOLEAN);
130
131   /**
132    * GActionGroup::action-state-changed:
133    * @action_group: the #GActionGroup that changed
134    * @action_name: the name of the action in @action_group
135    * @value: the new value of the state
136    *
137    * Signals that the state of the named action has changed.
138    *
139    * Since: 2.28
140    **/
141   g_action_group_signals[SIGNAL_ACTION_STATE_CHANGED] =
142     g_signal_new (I_("action-state-changed"),
143                   G_TYPE_ACTION_GROUP,
144                   G_SIGNAL_RUN_LAST |
145                   G_SIGNAL_DETAILED |
146                   G_SIGNAL_MUST_COLLECT,
147                   G_STRUCT_OFFSET (GActionGroupInterface,
148                                    action_state_changed),
149                   NULL, NULL,
150                   _gio_marshal_VOID__STRING_VARIANT,
151                   G_TYPE_NONE, 2,
152                   G_TYPE_STRING,
153                   G_TYPE_VARIANT);
154 }
155
156 /**
157  * g_action_group_list_actions:
158  * @action_group: a #GActionGroup
159  *
160  * Lists the actions contained within @action_group.
161  *
162  * The caller is responsible for freeing the list with g_strfreev() when
163  * it is no longer required.
164  *
165  * Returns: (transfer full): a %NULL-terminated array of the names of the
166  * actions in the groupb
167  *
168  * Since: 2.28
169  **/
170 gchar **
171 g_action_group_list_actions (GActionGroup *action_group)
172 {
173   g_return_val_if_fail (G_IS_ACTION_GROUP (action_group), NULL);
174
175   return G_ACTION_GROUP_GET_IFACE (action_group)
176     ->list_actions (action_group);
177 }
178
179 /**
180  * g_action_group_has_action:
181  * @action_group: a #GActionGroup
182  * @action_name: the name of the action to check for
183  *
184  * Checks if the named action exists within @action_group.
185  *
186  * Returns: whether the named action exists
187  *
188  * Since: 2.28
189  **/
190 gboolean
191 g_action_group_has_action (GActionGroup *action_group,
192                            const gchar  *action_name)
193 {
194   g_return_val_if_fail (G_IS_ACTION_GROUP (action_group), FALSE);
195
196   return G_ACTION_GROUP_GET_IFACE (action_group)
197     ->has_action (action_group, action_name);
198 }
199
200 /**
201  * g_action_group_get_action_parameter_type:
202  * @action_group: a #GActionGroup
203  * @action_name: the name of the action to query
204  *
205  * Queries the type of the parameter that must be given when activating
206  * the named action within @action_group.
207  *
208  * When activating the action using g_action_group_activate(), the
209  * #GVariant given to that function must be of the type returned by this
210  * function.
211  *
212  * In the case that this function returns %NULL, you must not give any
213  * #GVariant, but %NULL instead.
214  *
215  * The parameter type of a particular action will never change but it is
216  * possible for an action to be removed and for a new action to be added
217  * with the same name but a different parameter type.
218  *
219  * Return value: the parameter type
220  *
221  * Since: 2.28
222  **/
223 const GVariantType *
224 g_action_group_get_action_parameter_type (GActionGroup *action_group,
225                                           const gchar  *action_name)
226 {
227   g_return_val_if_fail (G_IS_ACTION_GROUP (action_group), NULL);
228
229   return G_ACTION_GROUP_GET_IFACE (action_group)
230     ->get_action_parameter_type (action_group, action_name);
231 }
232
233 /**
234  * g_action_group_get_action_state_type:
235  * @action_group: a #GActionGroup
236  * @action_name: the name of the action to query
237  *
238  * Queries the type of the state of the named action within
239  * @action_group.
240  *
241  * If the action is stateful then this function returns the
242  * #GVariantType of the state.  All calls to g_action_group_set_state()
243  * must give a #GVariant of this type and g_action_group_get_state()
244  * will return a #GVariant of the same type.
245  *
246  * If the action is not stateful then this function will return %NULL.
247  * In that case, g_action_group_get_state() will return %NULL and you
248  * must not call g_action_group_set_state().
249  *
250  * The state type of a particular action will never change but it is
251  * possible for an action to be removed and for a new action to be added
252  * with the same name but a different state type.
253  *
254  * Returns: (transfer full): the state type, if the action is stateful
255  *
256  * Since: 2.28
257  **/
258 const GVariantType *
259 g_action_group_get_action_state_type (GActionGroup *action_group,
260                                       const gchar  *action_name)
261 {
262   g_return_val_if_fail (G_IS_ACTION_GROUP (action_group), NULL);
263
264   return G_ACTION_GROUP_GET_IFACE (action_group)
265     ->get_action_state_type (action_group, action_name);
266 }
267
268 /**
269  * g_action_group_get_action_state_hint:
270  * @action_group: a #GActionGroup
271  * @action_name: the name of the action to query
272  *
273  * Requests a hint about the valid range of values for the state of the
274  * named action within @action_group.
275  *
276  * If %NULL is returned it either means that the action is not stateful
277  * or that there is no hint about the valid range of values for the
278  * state of the action.
279  *
280  * If a #GVariant array is returned then each item in the array is a
281  * possible value for the state.  If a #GVariant pair (ie: two-tuple) is
282  * returned then the tuple specifies the inclusive lower and upper bound
283  * of valid values for the state.
284  *
285  * In any case, the information is merely a hint.  It may be possible to
286  * have a state value outside of the hinted range and setting a value
287  * within the range may fail.
288  *
289  * The return value (if non-%NULL) should be freed with
290  * g_variant_unref() when it is no longer required.
291  *
292  * Return value: (transfer full): the state range hint
293  *
294  * Since: 2.28
295  **/
296 GVariant *
297 g_action_group_get_action_state_hint (GActionGroup *action_group,
298                                       const gchar  *action_name)
299 {
300   g_return_val_if_fail (G_IS_ACTION_GROUP (action_group), NULL);
301
302   return G_ACTION_GROUP_GET_IFACE (action_group)
303     ->get_action_state_hint (action_group, action_name);
304 }
305
306 /**
307  * g_action_group_get_action_enabled:
308  * @action_group: a #GActionGroup
309  * @action_name: the name of the action to query
310  *
311  * Checks if the named action within @action_group is currently enabled.
312  *
313  * An action must be enabled in order to be activated or in order to
314  * have its state changed from outside callers.
315  *
316  * Return value: whether or not the action is currently enabled
317  *
318  * Since: 2.28
319  **/
320 gboolean
321 g_action_group_get_action_enabled (GActionGroup *action_group,
322                                    const gchar  *action_name)
323 {
324   g_return_val_if_fail (G_IS_ACTION_GROUP (action_group), FALSE);
325
326   return G_ACTION_GROUP_GET_IFACE (action_group)
327     ->get_action_enabled (action_group, action_name);
328 }
329
330 /**
331  * g_action_group_get_action_state:
332  * @action_group: a #GActionGroup
333  * @action_name: the name of the action to query
334  *
335  * Queries the current state of the named action within @action_group.
336  *
337  * If the action is not stateful then %NULL will be returned.  If the
338  * action is stateful then the type of the return value is the type
339  * given by g_action_group_get_state_type().
340  *
341  * The return value (if non-%NULL) should be freed with
342  * g_variant_unref() when it is no longer required.
343  *
344  * Return value: (allow-none): the current state of the action
345  *
346  * Since: 2.28
347  **/
348 GVariant *
349 g_action_group_get_action_state (GActionGroup *action_group,
350                                  const gchar  *action_name)
351 {
352   g_return_val_if_fail (G_IS_ACTION_GROUP (action_group), NULL);
353
354   return G_ACTION_GROUP_GET_IFACE (action_group)
355     ->get_action_state (action_group, action_name);
356 }
357
358 /**
359  * g_action_group_change_action_state:
360  * @action_group: a #GActionGroup
361  * @action_name: the name of the action to request the change on
362  * @value: the new state
363  *
364  * Request for the state of the named action within @action_group to be
365  * changed to @value.
366  *
367  * The action must be stateful and @value must be of the correct type.
368  * See g_action_group_get_state_type().
369  *
370  * This call merely requests a change.  The action may refuse to change
371  * its state or may change its state to something other than @value.
372  * See g_action_group_get_state_hint().
373  *
374  * If the @value GVariant is floating, it is consumed.
375  *
376  * Since: 2.28
377  **/
378 void
379 g_action_group_change_action_state (GActionGroup *action_group,
380                                     const gchar  *action_name,
381                                     GVariant     *value)
382 {
383   g_return_if_fail (G_IS_ACTION_GROUP (action_group));
384   g_return_if_fail (action_name != NULL);
385   g_return_if_fail (value != NULL);
386
387   G_ACTION_GROUP_GET_IFACE (action_group)
388     ->change_action_state (action_group, action_name, value);
389 }
390
391 /**
392  * g_action_group_activate_action:
393  * @action_group: a #GActionGroup
394  * @action_name: the name of the action to activate
395  * @parameter: (allow-none): parameters to the activation
396  *
397  * Activate the named action within @action_group.
398  *
399  * If the action is expecting a parameter, then the correct type of
400  * parameter must be given as @parameter.  If the action is expecting no
401  * parameters then @parameter must be %NULL.  See
402  * g_action_group_get_parameter_type().
403  *
404  * Since: 2.28
405  **/
406 void
407 g_action_group_activate_action (GActionGroup *action_group,
408                                 const gchar  *action_name,
409                                 GVariant     *parameter)
410 {
411   g_return_if_fail (G_IS_ACTION_GROUP (action_group));
412   g_return_if_fail (action_name != NULL);
413
414   G_ACTION_GROUP_GET_IFACE (action_group)
415     ->activate_action (action_group, action_name, parameter);
416 }
417
418 /**
419  * g_action_group_action_added:
420  * @action_group: a #GActionGroup
421  * @action_name: the name of an action in the group
422  *
423  * Emits the #GActionGroup::action-added signal on @action_group.
424  *
425  * This function should only be called by #GActionGroup implementations.
426  *
427  * Since: 2.28
428  **/
429 void
430 g_action_group_action_added (GActionGroup *action_group,
431                              const gchar  *action_name)
432 {
433   g_return_if_fail (G_IS_ACTION_GROUP (action_group));
434   g_return_if_fail (action_name != NULL);
435
436   g_signal_emit (action_group,
437                  g_action_group_signals[SIGNAL_ACTION_ADDED],
438                  g_quark_try_string (action_name),
439                  action_name);
440 }
441
442 /**
443  * g_action_group_action_removed:
444  * @action_group: a #GActionGroup
445  * @action_name: the name of an action in the group
446  *
447  * Emits the #GActionGroup::action-removed signal on @action_group.
448  *
449  * This function should only be called by #GActionGroup implementations.
450  *
451  * Since: 2.28
452  **/
453 void
454 g_action_group_action_removed (GActionGroup *action_group,
455                                const gchar  *action_name)
456 {
457   g_return_if_fail (G_IS_ACTION_GROUP (action_group));
458   g_return_if_fail (action_name != NULL);
459
460   g_signal_emit (action_group,
461                  g_action_group_signals[SIGNAL_ACTION_REMOVED],
462                  g_quark_try_string (action_name),
463                  action_name);
464 }
465
466 /**
467  * g_action_group_action_enabled_changed:
468  * @action_group: a #GActionGroup
469  * @action_name: the name of an action in the group
470  * @enabled: whether or not the action is now enabled
471  *
472  * Emits the #GActionGroup::action-enabled-changed signal on @action_group.
473  *
474  * This function should only be called by #GActionGroup implementations.
475  *
476  * Since: 2.28
477  **/
478 void
479 g_action_group_action_enabled_changed (GActionGroup *action_group,
480                                        const gchar  *action_name,
481                                        gboolean      enabled)
482 {
483   g_return_if_fail (G_IS_ACTION_GROUP (action_group));
484   g_return_if_fail (action_name != NULL);
485
486   enabled = !!enabled;
487
488   g_signal_emit (action_group,
489                  g_action_group_signals[SIGNAL_ACTION_ENABLED_CHANGED],
490                  g_quark_try_string (action_name),
491                  action_name,
492                  enabled);
493 }
494
495 /**
496  * g_action_group_action_state_changed:
497  * @action_group: a #GActionGroup
498  * @action_name: the name of an action in the group
499  * @state: the new state of the named action
500  *
501  * Emits the #GActionGroup::action-state-changed signal on @action_group.
502  *
503  * This function should only be called by #GActionGroup implementations.
504  *
505  * Since: 2.28
506  **/
507 void
508 g_action_group_action_state_changed (GActionGroup *action_group,
509                                      const gchar  *action_name,
510                                      GVariant     *state)
511 {
512   g_return_if_fail (G_IS_ACTION_GROUP (action_group));
513   g_return_if_fail (action_name != NULL);
514
515   g_signal_emit (action_group,
516                  g_action_group_signals[SIGNAL_ACTION_STATE_CHANGED],
517                  g_quark_try_string (action_name),
518                  action_name,
519                  state);
520 }