Blame gio/gaction.c

Packit Service d3d246
/*
Packit Service d3d246
 * Copyright © 2010 Codethink Limited
Packit Service d3d246
 *
Packit Service d3d246
 * This library is free software; you can redistribute it and/or
Packit Service d3d246
 * modify it under the terms of the GNU Lesser General Public
Packit Service d3d246
 * License as published by the Free Software Foundation; either
Packit Service d3d246
 * version 2.1 of the License, or (at your option) any later version.
Packit Service d3d246
 *
Packit Service d3d246
 * This library is distributed in the hope that it will be useful,
Packit Service d3d246
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service d3d246
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service d3d246
 * Lesser General Public License for more details.
Packit Service d3d246
 *
Packit Service d3d246
 * You should have received a copy of the GNU Lesser General
Packit Service d3d246
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit Service d3d246
 *
Packit Service d3d246
 * Authors: Ryan Lortie <desrt@desrt.ca>
Packit Service d3d246
 */
Packit Service d3d246
Packit Service d3d246
#include "config.h"
Packit Service d3d246
#include "gaction.h"
Packit Service d3d246
#include "glibintl.h"
Packit Service d3d246
Packit Service d3d246
#include <string.h>
Packit Service d3d246
Packit Service d3d246
G_DEFINE_INTERFACE (GAction, g_action, G_TYPE_OBJECT)
Packit Service d3d246
Packit Service d3d246
/**
Packit Service d3d246
 * SECTION:gaction
Packit Service d3d246
 * @title: GAction
Packit Service d3d246
 * @short_description: An action interface
Packit Service d3d246
 * @include: gio/gio.h
Packit Service d3d246
 *
Packit Service d3d246
 * #GAction represents a single named action.
Packit Service d3d246
 *
Packit Service d3d246
 * The main interface to an action is that it can be activated with
Packit Service d3d246
 * g_action_activate().  This results in the 'activate' signal being
Packit Service d3d246
 * emitted.  An activation has a #GVariant parameter (which may be
Packit Service d3d246
 * %NULL).  The correct type for the parameter is determined by a static
Packit Service d3d246
 * parameter type (which is given at construction time).
Packit Service d3d246
 *
Packit Service d3d246
 * An action may optionally have a state, in which case the state may be
Packit Service d3d246
 * set with g_action_change_state().  This call takes a #GVariant.  The
Packit Service d3d246
 * correct type for the state is determined by a static state type
Packit Service d3d246
 * (which is given at construction time).
Packit Service d3d246
 *
Packit Service d3d246
 * The state may have a hint associated with it, specifying its valid
Packit Service d3d246
 * range.
Packit Service d3d246
 *
Packit Service d3d246
 * #GAction is merely the interface to the concept of an action, as
Packit Service d3d246
 * described above.  Various implementations of actions exist, including
Packit Service d3d246
 * #GSimpleAction.
Packit Service d3d246
 *
Packit Service d3d246
 * In all cases, the implementing class is responsible for storing the
Packit Service d3d246
 * name of the action, the parameter type, the enabled state, the
Packit Service d3d246
 * optional state type and the state and emitting the appropriate
Packit Service d3d246
 * signals when these change.  The implementor is responsible for filtering
Packit Service d3d246
 * calls to g_action_activate() and g_action_change_state() for type
Packit Service d3d246
 * safety and for the state being enabled.
Packit Service d3d246
 *
Packit Service d3d246
 * Probably the only useful thing to do with a #GAction is to put it
Packit Service d3d246
 * inside of a #GSimpleActionGroup.
Packit Service d3d246
 **/
Packit Service d3d246
Packit Service d3d246
/**
Packit Service d3d246
 * GAction:
Packit Service d3d246
 *
Packit Service d3d246
 * #GAction is an opaque data structure and can only be accessed
Packit Service d3d246
 * using the following functions.
Packit Service d3d246
 **/
Packit Service d3d246
Packit Service d3d246
/**
Packit Service d3d246
 * GActionInterface:
Packit Service d3d246
 * @get_name: the virtual function pointer for g_action_get_name()
Packit Service d3d246
 * @get_parameter_type: the virtual function pointer for g_action_get_parameter_type()
Packit Service d3d246
 * @get_state_type: the virtual function pointer for g_action_get_state_type()
Packit Service d3d246
 * @get_state_hint: the virtual function pointer for g_action_get_state_hint()
Packit Service d3d246
 * @get_enabled: the virtual function pointer for g_action_get_enabled()
Packit Service d3d246
 * @get_state: the virtual function pointer for g_action_get_state()
Packit Service d3d246
 * @change_state: the virtual function pointer for g_action_change_state()
Packit Service d3d246
 * @activate: the virtual function pointer for g_action_activate().  Note that #GAction does not have an
Packit Service d3d246
 *            'activate' signal but that implementations of it may have one.
Packit Service d3d246
 *
Packit Service d3d246
 * The virtual function table for #GAction.
Packit Service d3d246
 *
Packit Service d3d246
 * Since: 2.28
Packit Service d3d246
 */
Packit Service d3d246
Packit Service d3d246
void
Packit Service d3d246
g_action_default_init (GActionInterface *iface)
Packit Service d3d246
{
Packit Service d3d246
  /**
Packit Service d3d246
   * GAction:name:
Packit Service d3d246
   *
Packit Service d3d246
   * The name of the action.  This is mostly meaningful for identifying
Packit Service d3d246
   * the action once it has been added to a #GActionGroup. It is immutable.
Packit Service d3d246
   *
Packit Service d3d246
   * Since: 2.28
Packit Service d3d246
   **/
Packit Service d3d246
  g_object_interface_install_property (iface,
Packit Service d3d246
                                       g_param_spec_string ("name",
Packit Service d3d246
                                                            P_("Action Name"),
Packit Service d3d246
                                                            P_("The name used to invoke the action"),
Packit Service d3d246
                                                            NULL,
Packit Service d3d246
                                                            G_PARAM_READABLE |
Packit Service d3d246
                                                            G_PARAM_STATIC_STRINGS));
Packit Service d3d246
Packit Service d3d246
  /**
Packit Service d3d246
   * GAction:parameter-type:
Packit Service d3d246
   *
Packit Service d3d246
   * The type of the parameter that must be given when activating the
Packit Service d3d246
   * action. This is immutable, and may be %NULL if no parameter is needed when
Packit Service d3d246
   * activating the action.
Packit Service d3d246
   *
Packit Service d3d246
   * Since: 2.28
Packit Service d3d246
   **/
Packit Service d3d246
  g_object_interface_install_property (iface,
Packit Service d3d246
                                       g_param_spec_boxed ("parameter-type",
Packit Service d3d246
                                                           P_("Parameter Type"),
Packit Service d3d246
                                                           P_("The type of GVariant passed to activate()"),
Packit Service d3d246
                                                           G_TYPE_VARIANT_TYPE,
Packit Service d3d246
                                                           G_PARAM_READABLE |
Packit Service d3d246
                                                           G_PARAM_STATIC_STRINGS));
Packit Service d3d246
Packit Service d3d246
  /**
Packit Service d3d246
   * GAction:enabled:
Packit Service d3d246
   *
Packit Service d3d246
   * If @action is currently enabled.
Packit Service d3d246
   *
Packit Service d3d246
   * If the action is disabled then calls to g_action_activate() and
Packit Service d3d246
   * g_action_change_state() have no effect.
Packit Service d3d246
   *
Packit Service d3d246
   * Since: 2.28
Packit Service d3d246
   **/
Packit Service d3d246
  g_object_interface_install_property (iface,
Packit Service d3d246
                                       g_param_spec_boolean ("enabled",
Packit Service d3d246
                                                             P_("Enabled"),
Packit Service d3d246
                                                             P_("If the action can be activated"),
Packit Service d3d246
                                                             TRUE,
Packit Service d3d246
                                                             G_PARAM_READABLE |
Packit Service d3d246
                                                             G_PARAM_STATIC_STRINGS));
Packit Service d3d246
Packit Service d3d246
  /**
Packit Service d3d246
   * GAction:state-type:
Packit Service d3d246
   *
Packit Service d3d246
   * The #GVariantType of the state that the action has, or %NULL if the
Packit Service d3d246
   * action is stateless. This is immutable.
Packit Service d3d246
   *
Packit Service d3d246
   * Since: 2.28
Packit Service d3d246
   **/
Packit Service d3d246
  g_object_interface_install_property (iface,
Packit Service d3d246
                                       g_param_spec_boxed ("state-type",
Packit Service d3d246
                                                           P_("State Type"),
Packit Service d3d246
                                                           P_("The type of the state kept by the action"),
Packit Service d3d246
                                                           G_TYPE_VARIANT_TYPE,
Packit Service d3d246
                                                           G_PARAM_READABLE |
Packit Service d3d246
                                                           G_PARAM_STATIC_STRINGS));
Packit Service d3d246
Packit Service d3d246
  /**
Packit Service d3d246
   * GAction:state:
Packit Service d3d246
   *
Packit Service d3d246
   * The state of the action, or %NULL if the action is stateless.
Packit Service d3d246
   *
Packit Service d3d246
   * Since: 2.28
Packit Service d3d246
   **/
Packit Service d3d246
  g_object_interface_install_property (iface,
Packit Service d3d246
                                       g_param_spec_variant ("state",
Packit Service d3d246
                                                             P_("State"),
Packit Service d3d246
                                                             P_("The state the action is in"),
Packit Service d3d246
                                                             G_VARIANT_TYPE_ANY,
Packit Service d3d246
                                                             NULL,
Packit Service d3d246
                                                             G_PARAM_READABLE |
Packit Service d3d246
                                                             G_PARAM_STATIC_STRINGS));
Packit Service d3d246
}
Packit Service d3d246
Packit Service d3d246
/**
Packit Service d3d246
 * g_action_change_state:
Packit Service d3d246
 * @action: a #GAction
Packit Service d3d246
 * @value: the new state
Packit Service d3d246
 *
Packit Service d3d246
 * Request for the state of @action to be changed to @value.
Packit Service d3d246
 *
Packit Service d3d246
 * The action must be stateful and @value must be of the correct type.
Packit Service d3d246
 * See g_action_get_state_type().
Packit Service d3d246
 *
Packit Service d3d246
 * This call merely requests a change.  The action may refuse to change
Packit Service d3d246
 * its state or may change its state to something other than @value.
Packit Service d3d246
 * See g_action_get_state_hint().
Packit Service d3d246
 *
Packit Service d3d246
 * If the @value GVariant is floating, it is consumed.
Packit Service d3d246
 *
Packit Service d3d246
 * Since: 2.30
Packit Service d3d246
 **/
Packit Service d3d246
void
Packit Service d3d246
g_action_change_state (GAction  *action,
Packit Service d3d246
                       GVariant *value)
Packit Service d3d246
{
Packit Service d3d246
  const GVariantType *state_type;
Packit Service d3d246
Packit Service d3d246
  g_return_if_fail (G_IS_ACTION (action));
Packit Service d3d246
  g_return_if_fail (value != NULL);
Packit Service d3d246
  state_type = g_action_get_state_type (action);
Packit Service d3d246
  g_return_if_fail (state_type != NULL);
Packit Service d3d246
  g_return_if_fail (g_variant_is_of_type (value, state_type));
Packit Service d3d246
Packit Service d3d246
  g_variant_ref_sink (value);
Packit Service d3d246
Packit Service d3d246
  G_ACTION_GET_IFACE (action)
Packit Service d3d246
    ->change_state (action, value);
Packit Service d3d246
Packit Service d3d246
  g_variant_unref (value);
Packit Service d3d246
}
Packit Service d3d246
Packit Service d3d246
/**
Packit Service d3d246
 * g_action_get_state:
Packit Service d3d246
 * @action: a #GAction
Packit Service d3d246
 *
Packit Service d3d246
 * Queries the current state of @action.
Packit Service d3d246
 *
Packit Service d3d246
 * If the action is not stateful then %NULL will be returned.  If the
Packit Service d3d246
 * action is stateful then the type of the return value is the type
Packit Service d3d246
 * given by g_action_get_state_type().
Packit Service d3d246
 *
Packit Service d3d246
 * The return value (if non-%NULL) should be freed with
Packit Service d3d246
 * g_variant_unref() when it is no longer required.
Packit Service d3d246
 *
Packit Service d3d246
 * Returns: (transfer full): the current state of the action
Packit Service d3d246
 *
Packit Service d3d246
 * Since: 2.28
Packit Service d3d246
 **/
Packit Service d3d246
GVariant *
Packit Service d3d246
g_action_get_state (GAction *action)
Packit Service d3d246
{
Packit Service d3d246
  g_return_val_if_fail (G_IS_ACTION (action), NULL);
Packit Service d3d246
Packit Service d3d246
  return G_ACTION_GET_IFACE (action)
Packit Service d3d246
    ->get_state (action);
Packit Service d3d246
}
Packit Service d3d246
Packit Service d3d246
/**
Packit Service d3d246
 * g_action_get_name:
Packit Service d3d246
 * @action: a #GAction
Packit Service d3d246
 *
Packit Service d3d246
 * Queries the name of @action.
Packit Service d3d246
 *
Packit Service d3d246
 * Returns: the name of the action
Packit Service d3d246
 *
Packit Service d3d246
 * Since: 2.28
Packit Service d3d246
 **/
Packit Service d3d246
const gchar *
Packit Service d3d246
g_action_get_name (GAction *action)
Packit Service d3d246
{
Packit Service d3d246
  g_return_val_if_fail (G_IS_ACTION (action), NULL);
Packit Service d3d246
Packit Service d3d246
  return G_ACTION_GET_IFACE (action)
Packit Service d3d246
    ->get_name (action);
Packit Service d3d246
}
Packit Service d3d246
Packit Service d3d246
/**
Packit Service d3d246
 * g_action_get_parameter_type:
Packit Service d3d246
 * @action: a #GAction
Packit Service d3d246
 *
Packit Service d3d246
 * Queries the type of the parameter that must be given when activating
Packit Service d3d246
 * @action.
Packit Service d3d246
 *
Packit Service d3d246
 * When activating the action using g_action_activate(), the #GVariant
Packit Service d3d246
 * given to that function must be of the type returned by this function.
Packit Service d3d246
 *
Packit Service d3d246
 * In the case that this function returns %NULL, you must not give any
Packit Service d3d246
 * #GVariant, but %NULL instead.
Packit Service d3d246
 *
Packit Service d3d246
 * Returns: (nullable): the parameter type
Packit Service d3d246
 *
Packit Service d3d246
 * Since: 2.28
Packit Service d3d246
 **/
Packit Service d3d246
const GVariantType *
Packit Service d3d246
g_action_get_parameter_type (GAction *action)
Packit Service d3d246
{
Packit Service d3d246
  g_return_val_if_fail (G_IS_ACTION (action), NULL);
Packit Service d3d246
Packit Service d3d246
  return G_ACTION_GET_IFACE (action)
Packit Service d3d246
    ->get_parameter_type (action);
Packit Service d3d246
}
Packit Service d3d246
Packit Service d3d246
/**
Packit Service d3d246
 * g_action_get_state_type:
Packit Service d3d246
 * @action: a #GAction
Packit Service d3d246
 *
Packit Service d3d246
 * Queries the type of the state of @action.
Packit Service d3d246
 *
Packit Service d3d246
 * If the action is stateful (e.g. created with
Packit Service d3d246
 * g_simple_action_new_stateful()) then this function returns the
Packit Service d3d246
 * #GVariantType of the state.  This is the type of the initial value
Packit Service d3d246
 * given as the state. All calls to g_action_change_state() must give a
Packit Service d3d246
 * #GVariant of this type and g_action_get_state() will return a
Packit Service d3d246
 * #GVariant of the same type.
Packit Service d3d246
 *
Packit Service d3d246
 * If the action is not stateful (e.g. created with g_simple_action_new())
Packit Service d3d246
 * then this function will return %NULL. In that case, g_action_get_state()
Packit Service d3d246
 * will return %NULL and you must not call g_action_change_state().
Packit Service d3d246
 *
Packit Service d3d246
 * Returns: (nullable): the state type, if the action is stateful
Packit Service d3d246
 *
Packit Service d3d246
 * Since: 2.28
Packit Service d3d246
 **/
Packit Service d3d246
const GVariantType *
Packit Service d3d246
g_action_get_state_type (GAction *action)
Packit Service d3d246
{
Packit Service d3d246
  g_return_val_if_fail (G_IS_ACTION (action), NULL);
Packit Service d3d246
Packit Service d3d246
  return G_ACTION_GET_IFACE (action)
Packit Service d3d246
    ->get_state_type (action);
Packit Service d3d246
}
Packit Service d3d246
Packit Service d3d246
/**
Packit Service d3d246
 * g_action_get_state_hint:
Packit Service d3d246
 * @action: a #GAction
Packit Service d3d246
 *
Packit Service d3d246
 * Requests a hint about the valid range of values for the state of
Packit Service d3d246
 * @action.
Packit Service d3d246
 *
Packit Service d3d246
 * If %NULL is returned it either means that the action is not stateful
Packit Service d3d246
 * or that there is no hint about the valid range of values for the
Packit Service d3d246
 * state of the action.
Packit Service d3d246
 *
Packit Service d3d246
 * If a #GVariant array is returned then each item in the array is a
Packit Service d3d246
 * possible value for the state.  If a #GVariant pair (ie: two-tuple) is
Packit Service d3d246
 * returned then the tuple specifies the inclusive lower and upper bound
Packit Service d3d246
 * of valid values for the state.
Packit Service d3d246
 *
Packit Service d3d246
 * In any case, the information is merely a hint.  It may be possible to
Packit Service d3d246
 * have a state value outside of the hinted range and setting a value
Packit Service d3d246
 * within the range may fail.
Packit Service d3d246
 *
Packit Service d3d246
 * The return value (if non-%NULL) should be freed with
Packit Service d3d246
 * g_variant_unref() when it is no longer required.
Packit Service d3d246
 *
Packit Service d3d246
 * Returns: (nullable) (transfer full): the state range hint
Packit Service d3d246
 *
Packit Service d3d246
 * Since: 2.28
Packit Service d3d246
 **/
Packit Service d3d246
GVariant *
Packit Service d3d246
g_action_get_state_hint (GAction *action)
Packit Service d3d246
{
Packit Service d3d246
  g_return_val_if_fail (G_IS_ACTION (action), NULL);
Packit Service d3d246
Packit Service d3d246
  return G_ACTION_GET_IFACE (action)
Packit Service d3d246
    ->get_state_hint (action);
Packit Service d3d246
}
Packit Service d3d246
Packit Service d3d246
/**
Packit Service d3d246
 * g_action_get_enabled:
Packit Service d3d246
 * @action: a #GAction
Packit Service d3d246
 *
Packit Service d3d246
 * Checks if @action is currently enabled.
Packit Service d3d246
 *
Packit Service d3d246
 * An action must be enabled in order to be activated or in order to
Packit Service d3d246
 * have its state changed from outside callers.
Packit Service d3d246
 *
Packit Service d3d246
 * Returns: whether the action is enabled
Packit Service d3d246
 *
Packit Service d3d246
 * Since: 2.28
Packit Service d3d246
 **/
Packit Service d3d246
gboolean
Packit Service d3d246
g_action_get_enabled (GAction *action)
Packit Service d3d246
{
Packit Service d3d246
  g_return_val_if_fail (G_IS_ACTION (action), FALSE);
Packit Service d3d246
Packit Service d3d246
  return G_ACTION_GET_IFACE (action)
Packit Service d3d246
    ->get_enabled (action);
Packit Service d3d246
}
Packit Service d3d246
Packit Service d3d246
/**
Packit Service d3d246
 * g_action_activate:
Packit Service d3d246
 * @action: a #GAction
Packit Service d3d246
 * @parameter: (nullable): the parameter to the activation
Packit Service d3d246
 *
Packit Service d3d246
 * Activates the action.
Packit Service d3d246
 *
Packit Service d3d246
 * @parameter must be the correct type of parameter for the action (ie:
Packit Service d3d246
 * the parameter type given at construction time).  If the parameter
Packit Service d3d246
 * type was %NULL then @parameter must also be %NULL.
Packit Service d3d246
 *
Packit Service d3d246
 * If the @parameter GVariant is floating, it is consumed.
Packit Service d3d246
 *
Packit Service d3d246
 * Since: 2.28
Packit Service d3d246
 **/
Packit Service d3d246
void
Packit Service d3d246
g_action_activate (GAction  *action,
Packit Service d3d246
                   GVariant *parameter)
Packit Service d3d246
{
Packit Service d3d246
  g_return_if_fail (G_IS_ACTION (action));
Packit Service d3d246
Packit Service d3d246
  if (parameter != NULL)
Packit Service d3d246
    g_variant_ref_sink (parameter);
Packit Service d3d246
Packit Service d3d246
  G_ACTION_GET_IFACE (action)
Packit Service d3d246
    ->activate (action, parameter);
Packit Service d3d246
Packit Service d3d246
  if (parameter != NULL)
Packit Service d3d246
    g_variant_unref (parameter);
Packit Service d3d246
}
Packit Service d3d246
Packit Service d3d246
/**
Packit Service d3d246
 * g_action_name_is_valid:
Packit Service d3d246
 * @action_name: an potential action name
Packit Service d3d246
 *
Packit Service d3d246
 * Checks if @action_name is valid.
Packit Service d3d246
 *
Packit Service d3d246
 * @action_name is valid if it consists only of alphanumeric characters,
Packit Service d3d246
 * plus '-' and '.'.  The empty string is not a valid action name.
Packit Service d3d246
 *
Packit Service d3d246
 * It is an error to call this function with a non-utf8 @action_name.
Packit Service d3d246
 * @action_name must not be %NULL.
Packit Service d3d246
 *
Packit Service d3d246
 * Returns: %TRUE if @action_name is valid
Packit Service d3d246
 *
Packit Service d3d246
 * Since: 2.38
Packit Service d3d246
 **/
Packit Service d3d246
gboolean
Packit Service d3d246
g_action_name_is_valid (const gchar *action_name)
Packit Service d3d246
{
Packit Service d3d246
  gchar c;
Packit Service d3d246
  gint i;
Packit Service d3d246
Packit Service d3d246
  g_return_val_if_fail (action_name != NULL, FALSE);
Packit Service d3d246
Packit Service d3d246
  for (i = 0; (c = action_name[i]); i++)
Packit Service d3d246
    if (!g_ascii_isalnum (c) && c != '.' && c != '-')
Packit Service d3d246
      return FALSE;
Packit Service d3d246
Packit Service d3d246
  return i > 0;
Packit Service d3d246
}
Packit Service d3d246
Packit Service d3d246
/**
Packit Service d3d246
 * g_action_parse_detailed_name:
Packit Service d3d246
 * @detailed_name: a detailed action name
Packit Service d3d246
 * @action_name: (out): the action name
Packit Service d3d246
 * @target_value: (out): the target value, or %NULL for no target
Packit Service d3d246
 * @error: a pointer to a %NULL #GError, or %NULL
Packit Service d3d246
 *
Packit Service d3d246
 * Parses a detailed action name into its separate name and target
Packit Service d3d246
 * components.
Packit Service d3d246
 *
Packit Service d3d246
 * Detailed action names can have three formats.
Packit Service d3d246
 *
Packit Service d3d246
 * The first format is used to represent an action name with no target
Packit Service d3d246
 * value and consists of just an action name containing no whitespace
Packit Service d3d246
 * nor the characters ':', '(' or ')'.  For example: "app.action".
Packit Service d3d246
 *
Packit Service d3d246
 * The second format is used to represent an action with a target value
Packit Service d3d246
 * that is a non-empty string consisting only of alphanumerics, plus '-'
Packit Service d3d246
 * and '.'.  In that case, the action name and target value are
Packit Service d3d246
 * separated by a double colon ("::").  For example:
Packit Service d3d246
 * "app.action::target".
Packit Service d3d246
 *
Packit Service d3d246
 * The third format is used to represent an action with any type of
Packit Service d3d246
 * target value, including strings.  The target value follows the action
Packit Service d3d246
 * name, surrounded in parens.  For example: "app.action(42)".  The
Packit Service d3d246
 * target value is parsed using g_variant_parse().  If a tuple-typed
Packit Service d3d246
 * value is desired, it must be specified in the same way, resulting in
Packit Service d3d246
 * two sets of parens, for example: "app.action((1,2,3))".  A string
Packit Service d3d246
 * target can be specified this way as well: "app.action('target')".
Packit Service d3d246
 * For strings, this third format must be used if * target value is
Packit Service d3d246
 * empty or contains characters other than alphanumerics, '-' and '.'.
Packit Service d3d246
 *
Packit Service d3d246
 * Returns: %TRUE if successful, else %FALSE with @error set
Packit Service d3d246
 *
Packit Service d3d246
 * Since: 2.38
Packit Service d3d246
 **/
Packit Service d3d246
gboolean
Packit Service d3d246
g_action_parse_detailed_name (const gchar  *detailed_name,
Packit Service d3d246
                              gchar       **action_name,
Packit Service d3d246
                              GVariant    **target_value,
Packit Service d3d246
                              GError      **error)
Packit Service d3d246
{
Packit Service d3d246
  const gchar *target;
Packit Service d3d246
  gsize target_len;
Packit Service d3d246
  gsize base_len;
Packit Service d3d246
Packit Service d3d246
  /* For historical (compatibility) reasons, this function accepts some
Packit Service d3d246
   * cases of invalid action names as long as they don't interfere with
Packit Service d3d246
   * the separation of the action from the target value.
Packit Service d3d246
   *
Packit Service d3d246
   * We decide which format we have based on which we see first between
Packit Service d3d246
   * '::' '(' and '\0'.
Packit Service d3d246
   */
Packit Service d3d246
Packit Service d3d246
  if (*detailed_name == '\0' || *detailed_name == ' ')
Packit Service d3d246
    goto bad_fmt;
Packit Service d3d246
Packit Service d3d246
  base_len = strcspn (detailed_name, ": ()");
Packit Service d3d246
  target = detailed_name + base_len;
Packit Service d3d246
  target_len = strlen (target);
Packit Service d3d246
Packit Service d3d246
  switch (target[0])
Packit Service d3d246
    {
Packit Service d3d246
    case ' ':
Packit Service d3d246
    case ')':
Packit Service d3d246
      goto bad_fmt;
Packit Service d3d246
Packit Service d3d246
    case ':':
Packit Service d3d246
      if (target[1] != ':')
Packit Service d3d246
        goto bad_fmt;
Packit Service d3d246
Packit Service d3d246
      *target_value = g_variant_ref_sink (g_variant_new_string (target + 2));
Packit Service d3d246
      break;
Packit Service d3d246
Packit Service d3d246
    case '(':
Packit Service d3d246
      {
Packit Service d3d246
        if (target[target_len - 1] != ')')
Packit Service d3d246
          goto bad_fmt;
Packit Service d3d246
Packit Service d3d246
        *target_value = g_variant_parse (NULL, target + 1, target + target_len - 1, NULL, error);
Packit Service d3d246
        if (*target_value == NULL)
Packit Service d3d246
          goto bad_fmt;
Packit Service d3d246
      }
Packit Service d3d246
      break;
Packit Service d3d246
Packit Service d3d246
    case '\0':
Packit Service d3d246
      *target_value = NULL;
Packit Service d3d246
      break;
Packit Service d3d246
    }
Packit Service d3d246
Packit Service d3d246
  *action_name = g_strndup (detailed_name, base_len);
Packit Service d3d246
Packit Service d3d246
  return TRUE;
Packit Service d3d246
Packit Service d3d246
bad_fmt:
Packit Service d3d246
  if (error)
Packit Service d3d246
    {
Packit Service d3d246
      if (*error == NULL)
Packit Service d3d246
        g_set_error (error, G_VARIANT_PARSE_ERROR, G_VARIANT_PARSE_ERROR_FAILED,
Packit Service d3d246
                     "Detailed action name '%s' has invalid format", detailed_name);
Packit Service d3d246
      else
Packit Service d3d246
        g_prefix_error (error, "Detailed action name '%s' has invalid format: ", detailed_name);
Packit Service d3d246
    }
Packit Service d3d246
Packit Service d3d246
  return FALSE;
Packit Service d3d246
}
Packit Service d3d246
Packit Service d3d246
/**
Packit Service d3d246
 * g_action_print_detailed_name:
Packit Service d3d246
 * @action_name: a valid action name
Packit Service d3d246
 * @target_value: (nullable): a #GVariant target value, or %NULL
Packit Service d3d246
 *
Packit Service d3d246
 * Formats a detailed action name from @action_name and @target_value.
Packit Service d3d246
 *
Packit Service d3d246
 * It is an error to call this function with an invalid action name.
Packit Service d3d246
 *
Packit Service d3d246
 * This function is the opposite of g_action_parse_detailed_name().
Packit Service d3d246
 * It will produce a string that can be parsed back to the @action_name
Packit Service d3d246
 * and @target_value by that function.
Packit Service d3d246
 *
Packit Service d3d246
 * See that function for the types of strings that will be printed by
Packit Service d3d246
 * this function.
Packit Service d3d246
 *
Packit Service d3d246
 * Returns: a detailed format string
Packit Service d3d246
 *
Packit Service d3d246
 * Since: 2.38
Packit Service d3d246
 **/
Packit Service d3d246
gchar *
Packit Service d3d246
g_action_print_detailed_name (const gchar *action_name,
Packit Service d3d246
                              GVariant    *target_value)
Packit Service d3d246
{
Packit Service d3d246
  g_return_val_if_fail (g_action_name_is_valid (action_name), NULL);
Packit Service d3d246
Packit Service d3d246
  if (target_value == NULL)
Packit Service d3d246
    return g_strdup (action_name);
Packit Service d3d246
Packit Service d3d246
  if (g_variant_is_of_type (target_value, G_VARIANT_TYPE_STRING))
Packit Service d3d246
    {
Packit Service d3d246
      const gchar *str = g_variant_get_string (target_value, NULL);
Packit Service d3d246
Packit Service d3d246
      if (g_action_name_is_valid (str))
Packit Service d3d246
        return g_strconcat (action_name, "::", str, NULL);
Packit Service d3d246
    }
Packit Service d3d246
Packit Service d3d246
  {
Packit Service d3d246
    GString *result = g_string_new (action_name);
Packit Service d3d246
    g_string_append_c (result, '(');
Packit Service d3d246
    g_variant_print_string (target_value, result, TRUE);
Packit Service d3d246
    g_string_append_c (result, ')');
Packit Service d3d246
Packit Service d3d246
    return g_string_free (result, FALSE);
Packit Service d3d246
  }
Packit Service d3d246
}