7559fb332c47ccd783a3dd130ce3e6b123c7e988
[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 "glibintl.h"
26
27 /**
28  * SECTION:gactiongroup
29  * @title: GActionGroup
30  * @short_description: A group of actions
31  *
32  * #GActionGroup represents a group of actions.
33  *
34  * Each action in the group has a unique name (which is a string).  All
35  * method calls, except g_action_group_list_actions() take the name of
36  * an action as an argument.
37  *
38  * Action groups are often used together with a #GMenuModel that provides
39  * additional representation data for the actions.
40  *
41  * The #GActionGroup API is meant to be the 'public' API to the action
42  * group.  The calls here are exactly the interaction that 'external
43  * forces' (eg: UI, incoming D-Bus messages, etc.) are supposed to have
44  * with actions.  'Internal' APIs (ie: ones meant only to be accessed by
45  * the action group implementation) are found on subclasses.  This is
46  * why you will find - for example - g_action_group_get_action_enabled()
47  * but not an equivalent <function>set()</function> call.
48  *
49  * Signals are emitted on the action group in response to state changes
50  * on individual actions.
51  *
52  * Implementations of #GActionGroup should provide implementations for
53  * the virtual functions g_action_group_list_actions() and
54  * g_action_group_query_action().  The other virtual functions should
55  * not be implemented - their "wrappers" are actually implemented with
56  * calls to g_action_group_query_action().
57  **/
58
59 G_DEFINE_INTERFACE (GActionGroup, g_action_group, G_TYPE_OBJECT)
60
61 enum
62 {
63   SIGNAL_ACTION_ADDED,
64   SIGNAL_ACTION_REMOVED,
65   SIGNAL_ACTION_ENABLED_CHANGED,
66   SIGNAL_ACTION_STATE_CHANGED,
67   NR_SIGNALS
68 };
69
70 static guint g_action_group_signals[NR_SIGNALS];
71
72 static gboolean
73 g_action_group_real_has_action (GActionGroup *action_group,
74                                 const gchar  *action_name)
75 {
76   return g_action_group_query_action (action_group, action_name, NULL, NULL, NULL, NULL, NULL);
77 }
78
79 static gboolean
80 g_action_group_real_get_action_enabled (GActionGroup *action_group,
81                                         const gchar  *action_name)
82 {
83   gboolean enabled = FALSE;
84
85   g_action_group_query_action (action_group, action_name, &enabled, NULL, NULL, NULL, NULL);
86
87   return enabled;
88 }
89
90 static const GVariantType *
91 g_action_group_real_get_action_parameter_type (GActionGroup *action_group,
92                                                const gchar  *action_name)
93 {
94   const GVariantType *type = NULL;
95
96   g_action_group_query_action (action_group, action_name, NULL, &type, NULL, NULL, NULL);
97
98   return type;
99 }
100
101 static const GVariantType *
102 g_action_group_real_get_action_state_type (GActionGroup *action_group,
103                                            const gchar  *action_name)
104 {
105   const GVariantType *type = NULL;
106
107   g_action_group_query_action (action_group, action_name, NULL, NULL, &type, NULL, NULL);
108
109   return type;
110 }
111
112 static GVariant *
113 g_action_group_real_get_action_state_hint (GActionGroup *action_group,
114                                            const gchar  *action_name)
115 {
116   GVariant *hint = NULL;
117
118   g_action_group_query_action (action_group, action_name, NULL, NULL, NULL, &hint, NULL);
119
120   return hint;
121 }
122
123 static GVariant *
124 g_action_group_real_get_action_state (GActionGroup *action_group,
125                                       const gchar  *action_name)
126 {
127   GVariant *state = NULL;
128
129   g_action_group_query_action (action_group, action_name, NULL, NULL, NULL, NULL, &state);
130
131   return state;
132 }
133
134 static gboolean
135 g_action_group_real_query_action (GActionGroup        *action_group,
136                                   const gchar         *action_name,
137                                   gboolean            *enabled,
138                                   const GVariantType **parameter_type,
139                                   const GVariantType **state_type,
140                                   GVariant           **state_hint,
141                                   GVariant           **state)
142 {
143   GActionGroupInterface *iface = G_ACTION_GROUP_GET_IFACE (action_group);
144
145   /* we expect implementations to override this method, but we also
146    * allow for implementations that existed before this method was
147    * introduced to override the individual accessors instead.
148    *
149    * detect the case that neither has happened and report it.
150    */
151   if G_UNLIKELY (iface->has_action == g_action_group_real_has_action ||
152                  iface->get_action_enabled == g_action_group_real_get_action_enabled ||
153                  iface->get_action_parameter_type == g_action_group_real_get_action_parameter_type ||
154                  iface->get_action_state_type == g_action_group_real_get_action_state_type ||
155                  iface->get_action_state_hint == g_action_group_real_get_action_state_hint ||
156                  iface->get_action_state == g_action_group_real_get_action_state)
157     {
158       g_critical ("Class '%s' implements GActionGroup interface without overriding "
159                   "query_action() method -- bailing out to avoid infinite recursion.",
160                   G_OBJECT_TYPE_NAME (action_group));
161       return FALSE;
162     }
163
164   if (!(* iface->has_action) (action_group, action_name))
165     return FALSE;
166
167   if (enabled != NULL)
168     *enabled = (* iface->get_action_enabled) (action_group, action_name);
169
170   if (parameter_type != NULL)
171     *parameter_type = (* iface->get_action_parameter_type) (action_group, action_name);
172
173   if (state_type != NULL)
174     *state_type = (* iface->get_action_state_type) (action_group, action_name);
175
176   if (state_hint != NULL)
177     *state_hint = (* iface->get_action_state_hint) (action_group, action_name);
178
179   if (state != NULL)
180     *state = (* iface->get_action_state) (action_group, action_name);
181
182   return TRUE;
183 }
184
185 static void
186 g_action_group_default_init (GActionGroupInterface *iface)
187 {
188   iface->has_action = g_action_group_real_has_action;
189   iface->get_action_enabled = g_action_group_real_get_action_enabled;
190   iface->get_action_parameter_type = g_action_group_real_get_action_parameter_type;
191   iface->get_action_state_type = g_action_group_real_get_action_state_type;
192   iface->get_action_state_hint = g_action_group_real_get_action_state_hint;
193   iface->get_action_state = g_action_group_real_get_action_state;
194   iface->query_action = g_action_group_real_query_action;
195
196   /**
197    * GActionGroup::action-added:
198    * @action_group: the #GActionGroup that changed
199    * @action_name: the name of the action in @action_group
200    *
201    * Signals that a new action was just added to the group.
202    * This signal is emitted after the action has been added
203    * and is now visible.
204    *
205    * Since: 2.28
206    **/
207   g_action_group_signals[SIGNAL_ACTION_ADDED] =
208     g_signal_new (I_("action-added"),
209                   G_TYPE_ACTION_GROUP,
210                   G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
211                   G_STRUCT_OFFSET (GActionGroupInterface, action_added),
212                   NULL, NULL,
213                   g_cclosure_marshal_VOID__STRING,
214                   G_TYPE_NONE, 1,
215                   G_TYPE_STRING);
216
217   /**
218    * GActionGroup::action-removed:
219    * @action_group: the #GActionGroup that changed
220    * @action_name: the name of the action in @action_group
221    *
222    * Signals that an action is just about to be removed from the group.
223    * This signal is emitted before the action is removed, so the action
224    * is still visible and can be queried from the signal handler.
225    *
226    * Since: 2.28
227    **/
228   g_action_group_signals[SIGNAL_ACTION_REMOVED] =
229     g_signal_new (I_("action-removed"),
230                   G_TYPE_ACTION_GROUP,
231                   G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
232                   G_STRUCT_OFFSET (GActionGroupInterface, action_removed),
233                   NULL, NULL,
234                   g_cclosure_marshal_VOID__STRING,
235                   G_TYPE_NONE, 1,
236                   G_TYPE_STRING);
237
238
239   /**
240    * GActionGroup::action-enabled-changed:
241    * @action_group: the #GActionGroup that changed
242    * @action_name: the name of the action in @action_group
243    * @enabled: whether the action is enabled or not
244    *
245    * Signals that the enabled status of the named action has changed.
246    *
247    * Since: 2.28
248    **/
249   g_action_group_signals[SIGNAL_ACTION_ENABLED_CHANGED] =
250     g_signal_new (I_("action-enabled-changed"),
251                   G_TYPE_ACTION_GROUP,
252                   G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
253                   G_STRUCT_OFFSET (GActionGroupInterface,
254                                    action_enabled_changed),
255                   NULL, NULL,
256                   NULL,
257                   G_TYPE_NONE, 2,
258                   G_TYPE_STRING,
259                   G_TYPE_BOOLEAN);
260
261   /**
262    * GActionGroup::action-state-changed:
263    * @action_group: the #GActionGroup that changed
264    * @action_name: the name of the action in @action_group
265    * @value: the new value of the state
266    *
267    * Signals that the state of the named action has changed.
268    *
269    * Since: 2.28
270    **/
271   g_action_group_signals[SIGNAL_ACTION_STATE_CHANGED] =
272     g_signal_new (I_("action-state-changed"),
273                   G_TYPE_ACTION_GROUP,
274                   G_SIGNAL_RUN_LAST |
275                   G_SIGNAL_DETAILED |
276                   G_SIGNAL_MUST_COLLECT,
277                   G_STRUCT_OFFSET (GActionGroupInterface,
278                                    action_state_changed),
279                   NULL, NULL,
280                   NULL,
281                   G_TYPE_NONE, 2,
282                   G_TYPE_STRING,
283                   G_TYPE_VARIANT);
284 }
285
286 /**
287  * g_action_group_list_actions:
288  * @action_group: a #GActionGroup
289  *
290  * Lists the actions contained within @action_group.
291  *
292  * The caller is responsible for freeing the list with g_strfreev() when
293  * it is no longer required.
294  *
295  * Returns: (transfer full): a %NULL-terminated array of the names of the
296  * actions in the groupb
297  *
298  * Since: 2.28
299  **/
300 gchar **
301 g_action_group_list_actions (GActionGroup *action_group)
302 {
303   g_return_val_if_fail (G_IS_ACTION_GROUP (action_group), NULL);
304
305   return G_ACTION_GROUP_GET_IFACE (action_group)
306     ->list_actions (action_group);
307 }
308
309 /**
310  * g_action_group_has_action:
311  * @action_group: a #GActionGroup
312  * @action_name: the name of the action to check for
313  *
314  * Checks if the named action exists within @action_group.
315  *
316  * Returns: whether the named action exists
317  *
318  * Since: 2.28
319  **/
320 gboolean
321 g_action_group_has_action (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     ->has_action (action_group, action_name);
328 }
329
330 /**
331  * g_action_group_get_action_parameter_type:
332  * @action_group: a #GActionGroup
333  * @action_name: the name of the action to query
334  *
335  * Queries the type of the parameter that must be given when activating
336  * the named action within @action_group.
337  *
338  * When activating the action using g_action_group_activate_action(),
339  * the #GVariant given to that function must be of the type returned
340  * by this function.
341  *
342  * In the case that this function returns %NULL, you must not give any
343  * #GVariant, but %NULL instead.
344  *
345  * The parameter type of a particular action will never change but it is
346  * possible for an action to be removed and for a new action to be added
347  * with the same name but a different parameter type.
348  *
349  * Return value: the parameter type
350  *
351  * Since: 2.28
352  **/
353 const GVariantType *
354 g_action_group_get_action_parameter_type (GActionGroup *action_group,
355                                           const gchar  *action_name)
356 {
357   g_return_val_if_fail (G_IS_ACTION_GROUP (action_group), NULL);
358
359   return G_ACTION_GROUP_GET_IFACE (action_group)
360     ->get_action_parameter_type (action_group, action_name);
361 }
362
363 /**
364  * g_action_group_get_action_state_type:
365  * @action_group: a #GActionGroup
366  * @action_name: the name of the action to query
367  *
368  * Queries the type of the state of the named action within
369  * @action_group.
370  *
371  * If the action is stateful then this function returns the
372  * #GVariantType of the state.  All calls to
373  * g_action_group_change_action_state() must give a #GVariant of this
374  * type and g_action_group_get_action_state() will return a #GVariant
375  * of the same type.
376  *
377  * If the action is not stateful then this function will return %NULL.
378  * In that case, g_action_group_get_action_state() will return %NULL
379  * and you must not call g_action_group_change_action_state().
380  *
381  * The state type of a particular action will never change but it is
382  * possible for an action to be removed and for a new action to be added
383  * with the same name but a different state type.
384  *
385  * Returns: (transfer full): the state type, if the action is stateful
386  *
387  * Since: 2.28
388  **/
389 const GVariantType *
390 g_action_group_get_action_state_type (GActionGroup *action_group,
391                                       const gchar  *action_name)
392 {
393   g_return_val_if_fail (G_IS_ACTION_GROUP (action_group), NULL);
394
395   return G_ACTION_GROUP_GET_IFACE (action_group)
396     ->get_action_state_type (action_group, action_name);
397 }
398
399 /**
400  * g_action_group_get_action_state_hint:
401  * @action_group: a #GActionGroup
402  * @action_name: the name of the action to query
403  *
404  * Requests a hint about the valid range of values for the state of the
405  * named action within @action_group.
406  *
407  * If %NULL is returned it either means that the action is not stateful
408  * or that there is no hint about the valid range of values for the
409  * state of the action.
410  *
411  * If a #GVariant array is returned then each item in the array is a
412  * possible value for the state.  If a #GVariant pair (ie: two-tuple) is
413  * returned then the tuple specifies the inclusive lower and upper bound
414  * of valid values for the state.
415  *
416  * In any case, the information is merely a hint.  It may be possible to
417  * have a state value outside of the hinted range and setting a value
418  * within the range may fail.
419  *
420  * The return value (if non-%NULL) should be freed with
421  * g_variant_unref() when it is no longer required.
422  *
423  * Return value: (transfer full): the state range hint
424  *
425  * Since: 2.28
426  **/
427 GVariant *
428 g_action_group_get_action_state_hint (GActionGroup *action_group,
429                                       const gchar  *action_name)
430 {
431   g_return_val_if_fail (G_IS_ACTION_GROUP (action_group), NULL);
432
433   return G_ACTION_GROUP_GET_IFACE (action_group)
434     ->get_action_state_hint (action_group, action_name);
435 }
436
437 /**
438  * g_action_group_get_action_enabled:
439  * @action_group: a #GActionGroup
440  * @action_name: the name of the action to query
441  *
442  * Checks if the named action within @action_group is currently enabled.
443  *
444  * An action must be enabled in order to be activated or in order to
445  * have its state changed from outside callers.
446  *
447  * Return value: whether or not the action is currently enabled
448  *
449  * Since: 2.28
450  **/
451 gboolean
452 g_action_group_get_action_enabled (GActionGroup *action_group,
453                                    const gchar  *action_name)
454 {
455   g_return_val_if_fail (G_IS_ACTION_GROUP (action_group), FALSE);
456
457   return G_ACTION_GROUP_GET_IFACE (action_group)
458     ->get_action_enabled (action_group, action_name);
459 }
460
461 /**
462  * g_action_group_get_action_state:
463  * @action_group: a #GActionGroup
464  * @action_name: the name of the action to query
465  *
466  * Queries the current state of the named action within @action_group.
467  *
468  * If the action is not stateful then %NULL will be returned.  If the
469  * action is stateful then the type of the return value is the type
470  * given by g_action_group_get_action_state_type().
471  *
472  * The return value (if non-%NULL) should be freed with
473  * g_variant_unref() when it is no longer required.
474  *
475  * Return value: (allow-none): the current state of the action
476  *
477  * Since: 2.28
478  **/
479 GVariant *
480 g_action_group_get_action_state (GActionGroup *action_group,
481                                  const gchar  *action_name)
482 {
483   g_return_val_if_fail (G_IS_ACTION_GROUP (action_group), NULL);
484
485   return G_ACTION_GROUP_GET_IFACE (action_group)
486     ->get_action_state (action_group, action_name);
487 }
488
489 /**
490  * g_action_group_change_action_state:
491  * @action_group: a #GActionGroup
492  * @action_name: the name of the action to request the change on
493  * @value: the new state
494  *
495  * Request for the state of the named action within @action_group to be
496  * changed to @value.
497  *
498  * The action must be stateful and @value must be of the correct type.
499  * See g_action_group_get_action_state_type().
500  *
501  * This call merely requests a change.  The action may refuse to change
502  * its state or may change its state to something other than @value.
503  * See g_action_group_get_action_state_hint().
504  *
505  * If the @value GVariant is floating, it is consumed.
506  *
507  * Since: 2.28
508  **/
509 void
510 g_action_group_change_action_state (GActionGroup *action_group,
511                                     const gchar  *action_name,
512                                     GVariant     *value)
513 {
514   g_return_if_fail (G_IS_ACTION_GROUP (action_group));
515   g_return_if_fail (action_name != NULL);
516   g_return_if_fail (value != NULL);
517
518   G_ACTION_GROUP_GET_IFACE (action_group)
519     ->change_action_state (action_group, action_name, value);
520 }
521
522 /**
523  * g_action_group_activate_action:
524  * @action_group: a #GActionGroup
525  * @action_name: the name of the action to activate
526  * @parameter: (allow-none): parameters to the activation
527  *
528  * Activate the named action within @action_group.
529  *
530  * If the action is expecting a parameter, then the correct type of
531  * parameter must be given as @parameter.  If the action is expecting no
532  * parameters then @parameter must be %NULL.  See
533  * g_action_group_get_action_parameter_type().
534  *
535  * Since: 2.28
536  **/
537 void
538 g_action_group_activate_action (GActionGroup *action_group,
539                                 const gchar  *action_name,
540                                 GVariant     *parameter)
541 {
542   g_return_if_fail (G_IS_ACTION_GROUP (action_group));
543   g_return_if_fail (action_name != NULL);
544
545   G_ACTION_GROUP_GET_IFACE (action_group)
546     ->activate_action (action_group, action_name, parameter);
547 }
548
549 /**
550  * g_action_group_action_added:
551  * @action_group: a #GActionGroup
552  * @action_name: the name of an action in the group
553  *
554  * Emits the #GActionGroup::action-added signal on @action_group.
555  *
556  * This function should only be called by #GActionGroup implementations.
557  *
558  * Since: 2.28
559  **/
560 void
561 g_action_group_action_added (GActionGroup *action_group,
562                              const gchar  *action_name)
563 {
564   g_return_if_fail (G_IS_ACTION_GROUP (action_group));
565   g_return_if_fail (action_name != NULL);
566
567   g_signal_emit (action_group,
568                  g_action_group_signals[SIGNAL_ACTION_ADDED],
569                  g_quark_try_string (action_name),
570                  action_name);
571 }
572
573 /**
574  * g_action_group_action_removed:
575  * @action_group: a #GActionGroup
576  * @action_name: the name of an action in the group
577  *
578  * Emits the #GActionGroup::action-removed signal on @action_group.
579  *
580  * This function should only be called by #GActionGroup implementations.
581  *
582  * Since: 2.28
583  **/
584 void
585 g_action_group_action_removed (GActionGroup *action_group,
586                                const gchar  *action_name)
587 {
588   g_return_if_fail (G_IS_ACTION_GROUP (action_group));
589   g_return_if_fail (action_name != NULL);
590
591   g_signal_emit (action_group,
592                  g_action_group_signals[SIGNAL_ACTION_REMOVED],
593                  g_quark_try_string (action_name),
594                  action_name);
595 }
596
597 /**
598  * g_action_group_action_enabled_changed:
599  * @action_group: a #GActionGroup
600  * @action_name: the name of an action in the group
601  * @enabled: whether or not the action is now enabled
602  *
603  * Emits the #GActionGroup::action-enabled-changed signal on @action_group.
604  *
605  * This function should only be called by #GActionGroup implementations.
606  *
607  * Since: 2.28
608  **/
609 void
610 g_action_group_action_enabled_changed (GActionGroup *action_group,
611                                        const gchar  *action_name,
612                                        gboolean      enabled)
613 {
614   g_return_if_fail (G_IS_ACTION_GROUP (action_group));
615   g_return_if_fail (action_name != NULL);
616
617   enabled = !!enabled;
618
619   g_signal_emit (action_group,
620                  g_action_group_signals[SIGNAL_ACTION_ENABLED_CHANGED],
621                  g_quark_try_string (action_name),
622                  action_name,
623                  enabled);
624 }
625
626 /**
627  * g_action_group_action_state_changed:
628  * @action_group: a #GActionGroup
629  * @action_name: the name of an action in the group
630  * @state: the new state of the named action
631  *
632  * Emits the #GActionGroup::action-state-changed signal on @action_group.
633  *
634  * This function should only be called by #GActionGroup implementations.
635  *
636  * Since: 2.28
637  **/
638 void
639 g_action_group_action_state_changed (GActionGroup *action_group,
640                                      const gchar  *action_name,
641                                      GVariant     *state)
642 {
643   g_return_if_fail (G_IS_ACTION_GROUP (action_group));
644   g_return_if_fail (action_name != NULL);
645
646   g_signal_emit (action_group,
647                  g_action_group_signals[SIGNAL_ACTION_STATE_CHANGED],
648                  g_quark_try_string (action_name),
649                  action_name,
650                  state);
651 }
652
653 /**
654  * g_action_group_query_action:
655  * @action_group: a #GActionGroup
656  * @action_name: the name of an action in the group
657  * @enabled: (out): if the action is presently enabled
658  * @parameter_type: (out): the parameter type, or %NULL if none needed
659  * @state_type: (out): the state type, or %NULL if stateless
660  * @state_hint: (out): the state hint, or %NULL if none
661  * @state: (out): the current state, or %NULL if stateless
662  *
663  * Queries all aspects of the named action within an @action_group.
664  *
665  * This function acquires the information available from
666  * g_action_group_has_action(), g_action_group_get_action_enabled(),
667  * g_action_group_get_action_parameter_type(),
668  * g_action_group_get_action_state_type(),
669  * g_action_group_get_action_state_hint() and
670  * g_action_group_get_state() with a single function call.
671  *
672  * This provides two main benefits.
673  *
674  * The first is the improvement in efficiency that comes with not having
675  * to perform repeated lookups of the action in order to discover
676  * different things about it.  The second is that implementing
677  * #GActionGroup can now be done by only overriding this one virtual
678  * function.
679  *
680  * The interface provides a default implementation of this function that
681  * calls the individual functions, as required, to fetch the
682  * information.  The interface also provides default implementations of
683  * those functions that call this function.  All implementations,
684  * therefore, must override either this function or all of the others.
685  *
686  * If the action exists, %TRUE is returned and any of the requested
687  * fields (as indicated by having a non-%NULL reference passed in) are
688  * filled.  If the action doesn't exist, %FALSE is returned and the
689  * fields may or may not have been modified.
690  *
691  * Returns: %TRUE if the action exists, else %FALSE
692  *
693  * Since: 2.32
694  **/
695 gboolean
696 g_action_group_query_action (GActionGroup        *action_group,
697                              const gchar         *action_name,
698                              gboolean            *enabled,
699                              const GVariantType **parameter_type,
700                              const GVariantType **state_type,
701                              GVariant           **state_hint,
702                              GVariant           **state)
703 {
704   return G_ACTION_GROUP_GET_IFACE (action_group)
705     ->query_action (action_group, action_name, enabled, parameter_type, state_type, state_hint, state);
706 }