Blame src/ibusconfig.c

Packit 3ff832
/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
Packit 3ff832
/* vim:set et sts=4: */
Packit 3ff832
/* ibus - The Input Bus
Packit 3ff832
 * Copyright (C) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
Packit 3ff832
 * Copyright (C) 2008-2010 Red Hat, Inc.
Packit 3ff832
 *
Packit 3ff832
 * This library is free software; you can redistribute it and/or
Packit 3ff832
 * modify it under the terms of the GNU Lesser General Public
Packit 3ff832
 * License as published by the Free Software Foundation; either
Packit 3ff832
 * version 2.1 of the License, or (at your option) any later version.
Packit 3ff832
 *
Packit 3ff832
 * This library is distributed in the hope that it will be useful,
Packit 3ff832
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 3ff832
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 3ff832
 * Lesser General Public License for more details.
Packit 3ff832
 *
Packit 3ff832
 * You should have received a copy of the GNU Lesser General Public
Packit 3ff832
 * License along with this library; if not, write to the Free Software
Packit 3ff832
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
Packit 3ff832
 * USA
Packit 3ff832
 */
Packit 3ff832
#include "ibusinternal.h"
Packit 3ff832
#include "ibusmarshalers.h"
Packit 3ff832
#include "ibusshare.h"
Packit 3ff832
#include "ibusconfig.h"
Packit 3ff832
#include "ibusbus.h"
Packit 3ff832
#include "ibuserror.h"
Packit 3ff832
Packit 3ff832
#define IBUS_CONFIG_GET_PRIVATE(o)  \
Packit 3ff832
   (G_TYPE_INSTANCE_GET_PRIVATE ((o), IBUS_TYPE_CONFIG, IBusConfigPrivate))
Packit 3ff832
Packit 3ff832
enum {
Packit 3ff832
    VALUE_CHANGED,
Packit 3ff832
    LAST_SIGNAL,
Packit 3ff832
};
Packit 3ff832
Packit 3ff832
Packit 3ff832
/* IBusConfigPriv */
Packit 3ff832
struct _IBusConfigPrivate {
Packit 3ff832
    GArray *watch_rules;
Packit 3ff832
    guint watch_config_signal_id;
Packit 3ff832
};
Packit 3ff832
Packit 3ff832
static guint    config_signals[LAST_SIGNAL] = { 0 };
Packit 3ff832
Packit 3ff832
static void      ibus_config_class_init     (IBusConfigClass    *class);
Packit 3ff832
static void      ibus_config_init           (IBusConfig         *config);
Packit 3ff832
static void      ibus_config_real_destroy   (IBusProxy          *proxy);
Packit 3ff832
Packit 3ff832
static void      ibus_config_g_signal       (GDBusProxy         *proxy,
Packit 3ff832
                                             const gchar        *sender_name,
Packit 3ff832
                                             const gchar        *signal_name,
Packit 3ff832
                                             GVariant           *parameters);
Packit 3ff832
Packit 3ff832
static void      initable_iface_init        (GInitableIface     *initable_iface);
Packit 3ff832
static void      async_initable_iface_init  (GAsyncInitableIface
Packit 3ff832
                                                                *async_initable_iface);
Packit 3ff832
Packit 3ff832
static gchar    *_make_match_rule           (const gchar        *section,
Packit 3ff832
                                             const gchar        *name);
Packit 3ff832
static guint     _signal_subscribe          (GDBusProxy         *proxy);
Packit 3ff832
static void      _signal_unsubscribe        (GDBusProxy         *proxy,
Packit 3ff832
                                             guint               signal_id);
Packit 3ff832
Packit 3ff832
static void      _remove_all_match_rules    (IBusConfig         *config);
Packit 3ff832
Packit 3ff832
G_DEFINE_TYPE_WITH_CODE (IBusConfig, ibus_config, IBUS_TYPE_PROXY,
Packit 3ff832
                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
Packit 3ff832
                         G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init)
Packit 3ff832
                         );
Packit 3ff832
Packit 3ff832
static void
Packit 3ff832
ibus_config_class_init (IBusConfigClass *class)
Packit 3ff832
{
Packit 3ff832
    GDBusProxyClass *dbus_proxy_class = G_DBUS_PROXY_CLASS (class);
Packit 3ff832
    IBusProxyClass *proxy_class = IBUS_PROXY_CLASS (class);
Packit 3ff832
Packit 3ff832
    g_type_class_add_private (class, sizeof (IBusConfigPrivate));
Packit 3ff832
Packit 3ff832
    dbus_proxy_class->g_signal = ibus_config_g_signal;
Packit 3ff832
    proxy_class->destroy = ibus_config_real_destroy;
Packit 3ff832
Packit 3ff832
Packit 3ff832
    /* install signals */
Packit 3ff832
    /**
Packit 3ff832
     * IBusConfig::value-changed:
Packit 3ff832
     * @config: An IBusConfig.
Packit 3ff832
     * @section: Section name.
Packit 3ff832
     * @name: Name of the property.
Packit 3ff832
     * @value: Value.
Packit 3ff832
     *
Packit 3ff832
     * Emitted when configuration value is changed.
Packit 3ff832
     * <note><para>Argument @user_data is ignored in this function.</para></note>
Packit 3ff832
     */
Packit 3ff832
    config_signals[VALUE_CHANGED] =
Packit 3ff832
        g_signal_new (I_("value-changed"),
Packit 3ff832
            G_TYPE_FROM_CLASS (class),
Packit 3ff832
            G_SIGNAL_RUN_LAST,
Packit 3ff832
            0,
Packit 3ff832
            NULL, NULL,
Packit 3ff832
            _ibus_marshal_VOID__STRING_STRING_VARIANT,
Packit 3ff832
            G_TYPE_NONE,
Packit 3ff832
            3,
Packit 3ff832
            G_TYPE_STRING,
Packit 3ff832
            G_TYPE_STRING,
Packit 3ff832
            G_TYPE_VARIANT | G_SIGNAL_TYPE_STATIC_SCOPE);
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
static void
Packit 3ff832
ibus_config_init (IBusConfig *config)
Packit 3ff832
{
Packit 3ff832
    config->priv = IBUS_CONFIG_GET_PRIVATE (config);
Packit 3ff832
    config->priv->watch_rules = g_array_new (FALSE, FALSE, sizeof (gchar *));
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
static void
Packit 3ff832
ibus_config_real_destroy (IBusProxy *proxy)
Packit 3ff832
{
Packit 3ff832
    IBusConfigPrivate *priv = IBUS_CONFIG_GET_PRIVATE (proxy);
Packit 3ff832
Packit 3ff832
    _signal_unsubscribe (G_DBUS_PROXY (proxy), priv->watch_config_signal_id);
Packit 3ff832
    _remove_all_match_rules (IBUS_CONFIG (proxy));
Packit 3ff832
    g_array_free (priv->watch_rules, FALSE);
Packit 3ff832
Packit 3ff832
    IBUS_PROXY_CLASS(ibus_config_parent_class)->destroy (proxy);
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
Packit 3ff832
static void
Packit 3ff832
ibus_config_g_signal (GDBusProxy  *proxy,
Packit 3ff832
                      const gchar *sender_name,
Packit 3ff832
                      const gchar *signal_name,
Packit 3ff832
                      GVariant    *parameters)
Packit 3ff832
{
Packit 3ff832
    if (g_strcmp0 (signal_name, "ValueChanged") == 0) {
Packit 3ff832
        const gchar *section = NULL;
Packit 3ff832
        const gchar *name = NULL;
Packit 3ff832
        GVariant *value = NULL;
Packit 3ff832
Packit 3ff832
        g_variant_get (parameters, "(&s&sv)", &section, &name, &value);
Packit 3ff832
Packit 3ff832
        g_signal_emit (proxy,
Packit 3ff832
                       config_signals[VALUE_CHANGED],
Packit 3ff832
                       0,
Packit 3ff832
                       section,
Packit 3ff832
                       name,
Packit 3ff832
                       value);
Packit 3ff832
        g_variant_unref (value);
Packit 3ff832
        return;
Packit 3ff832
    }
Packit 3ff832
Packit 3ff832
    g_return_if_reached ();
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
static void
Packit 3ff832
_connection_signal_cb (GDBusConnection *connection,
Packit 3ff832
                       const gchar     *sender_name,
Packit 3ff832
                       const gchar     *object_path,
Packit 3ff832
                       const gchar     *interface_name,
Packit 3ff832
                       const gchar     *signal_name,
Packit 3ff832
                       GVariant        *parameters,
Packit 3ff832
                       IBusConfig      *config)
Packit 3ff832
{
Packit 3ff832
    g_return_if_fail (IBUS_IS_CONFIG (config));
Packit 3ff832
Packit 3ff832
    ibus_config_g_signal (G_DBUS_PROXY (config),
Packit 3ff832
                          sender_name,
Packit 3ff832
                          signal_name,
Packit 3ff832
                          parameters);
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
static gchar *
Packit 3ff832
_make_match_rule (const gchar *section,
Packit 3ff832
                  const gchar *name)
Packit 3ff832
{
Packit 3ff832
    GString *str = g_string_new ("type='signal',"
Packit 3ff832
                                 "interface='" IBUS_INTERFACE_CONFIG "',"
Packit 3ff832
                                 "path='" IBUS_PATH_CONFIG "',"
Packit 3ff832
                                 "member='ValueChanged'");
Packit 3ff832
    if (section != NULL) {
Packit 3ff832
        g_string_append_printf (str, ",arg0='%s'", section);
Packit 3ff832
        if (name != NULL)
Packit 3ff832
            g_string_append_printf (str, ",arg1='%s'", name);
Packit 3ff832
    }
Packit 3ff832
    return g_string_free (str, FALSE);
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
static void
Packit 3ff832
_remove_all_match_rules (IBusConfig *config)
Packit 3ff832
{
Packit 3ff832
    gint i;
Packit 3ff832
Packit 3ff832
    for (i = 0; i < config->priv->watch_rules->len; i++) {
Packit 3ff832
        IBusBus *bus = ibus_bus_new ();
Packit 3ff832
        gchar *rule = g_array_index (config->priv->watch_rules, gchar *, i);
Packit 3ff832
        ibus_bus_remove_match (bus, rule);
Packit 3ff832
        g_object_unref (bus);
Packit 3ff832
        g_free (rule);
Packit 3ff832
    }
Packit 3ff832
    g_array_set_size (config->priv->watch_rules, 0);
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
gboolean
Packit 3ff832
ibus_config_watch (IBusConfig  *config,
Packit 3ff832
                   const gchar *section,
Packit 3ff832
                   const gchar *name)
Packit 3ff832
{
Packit 3ff832
    g_return_val_if_fail (IBUS_IS_CONFIG (config), FALSE);
Packit 3ff832
    g_assert ((section != NULL) || (section == NULL && name == NULL));
Packit 3ff832
Packit 3ff832
    IBusBus *bus = ibus_bus_new ();
Packit 3ff832
    gchar *rule;
Packit 3ff832
    gboolean retval;
Packit 3ff832
Packit 3ff832
    if (section == NULL && name == NULL) {
Packit 3ff832
        _remove_all_match_rules (config);
Packit 3ff832
Packit 3ff832
        rule = _make_match_rule (NULL, NULL);
Packit 3ff832
        retval = ibus_bus_add_match (bus, rule);
Packit 3ff832
        g_object_unref (bus);
Packit 3ff832
        g_free (rule);
Packit 3ff832
Packit 3ff832
        return retval;
Packit 3ff832
    }
Packit 3ff832
Packit 3ff832
    if (config->priv->watch_rules->len == 0) {
Packit 3ff832
        rule = _make_match_rule (NULL, NULL);
Packit 3ff832
        retval = ibus_bus_remove_match (bus, rule);
Packit 3ff832
        g_free (rule);
Packit 3ff832
        if (!retval) {
Packit 3ff832
            g_object_unref (bus);
Packit 3ff832
            return FALSE;
Packit 3ff832
        }
Packit 3ff832
    }
Packit 3ff832
Packit 3ff832
    rule = _make_match_rule (section, name);
Packit 3ff832
    retval = ibus_bus_add_match (bus, rule);
Packit 3ff832
    g_object_unref (bus);
Packit 3ff832
    if (!retval) {
Packit 3ff832
        g_free (rule);
Packit 3ff832
        return FALSE;
Packit 3ff832
    }
Packit 3ff832
Packit 3ff832
    g_array_append_val (config->priv->watch_rules, rule);
Packit 3ff832
    return TRUE;
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
gboolean
Packit 3ff832
ibus_config_unwatch (IBusConfig  *config,
Packit 3ff832
                     const gchar *section,
Packit 3ff832
                     const gchar *name)
Packit 3ff832
{
Packit 3ff832
    g_return_val_if_fail (IBUS_IS_CONFIG (config), FALSE);
Packit 3ff832
    g_assert ((section != NULL) || (section == NULL && name == NULL));
Packit 3ff832
Packit 3ff832
    IBusBus *bus = ibus_bus_new ();
Packit 3ff832
    gchar *rule = _make_match_rule (section, name);
Packit 3ff832
    gboolean retval;
Packit 3ff832
Packit 3ff832
    retval = ibus_bus_remove_match (bus, rule);
Packit 3ff832
    g_object_unref (bus);
Packit 3ff832
    if (retval && (section != NULL || name != NULL)) {
Packit 3ff832
        /* Remove the previously registered match rule from
Packit 3ff832
           config->priv->watch_rules. */
Packit 3ff832
        gint i;
Packit 3ff832
        for (i = 0; i < config->priv->watch_rules->len; i++) {
Packit 3ff832
            gchar *_rule = g_array_index (config->priv->watch_rules, gchar *,
Packit 3ff832
                                          i);
Packit 3ff832
            if (g_strcmp0 (_rule, rule) == 0) {
Packit 3ff832
                config->priv->watch_rules =
Packit 3ff832
                    g_array_remove_index_fast (config->priv->watch_rules, i);
Packit 3ff832
                g_free (_rule);
Packit 3ff832
                break;
Packit 3ff832
            }
Packit 3ff832
        }
Packit 3ff832
    }
Packit 3ff832
    g_free (rule);
Packit 3ff832
Packit 3ff832
    return TRUE;
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
IBusConfig *
Packit 3ff832
ibus_config_new (GDBusConnection  *connection,
Packit 3ff832
                 GCancellable     *cancellable,
Packit 3ff832
                 GError          **error)
Packit 3ff832
{
Packit 3ff832
    g_assert (G_IS_DBUS_CONNECTION (connection));
Packit 3ff832
Packit 3ff832
    GInitable *initable;
Packit 3ff832
    char *owner;
Packit 3ff832
Packit 3ff832
    GDBusProxyFlags flags = G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START |
Packit 3ff832
                            G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
Packit 3ff832
                            G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS;
Packit 3ff832
Packit 3ff832
    initable = g_initable_new (IBUS_TYPE_CONFIG,
Packit 3ff832
                               cancellable,
Packit 3ff832
                               error,
Packit 3ff832
                               "g-connection",      connection,
Packit 3ff832
                               "g-flags",           flags,
Packit 3ff832
                               "g-name",            IBUS_SERVICE_CONFIG,
Packit 3ff832
                               "g-interface-name",  IBUS_INTERFACE_CONFIG,
Packit 3ff832
                               "g-object-path",     IBUS_PATH_CONFIG,
Packit 3ff832
                               "g-default-timeout", ibus_get_timeout (),
Packit 3ff832
                               NULL);
Packit 3ff832
    if (initable == NULL)
Packit 3ff832
        return NULL;
Packit 3ff832
Packit 3ff832
    owner = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (initable));
Packit 3ff832
    if (owner == NULL) {
Packit 3ff832
        /* The configuration daemon, which is usually ibus-gconf, is not started yet. */
Packit 3ff832
        g_set_error (error,
Packit 3ff832
                     IBUS_ERROR,
Packit 3ff832
                     IBUS_ERROR_NO_CONFIG,
Packit 3ff832
                     "Configuration daemon is not running.");
Packit 3ff832
        g_object_unref (initable);
Packit 3ff832
        return NULL;
Packit 3ff832
    }
Packit 3ff832
    g_free (owner);
Packit 3ff832
Packit 3ff832
    /* clients should not destroy the config service. */
Packit 3ff832
    IBUS_PROXY (initable)->own = FALSE;
Packit 3ff832
Packit 3ff832
    return IBUS_CONFIG (initable);
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
void
Packit 3ff832
ibus_config_new_async (GDBusConnection     *connection,
Packit 3ff832
                       GCancellable        *cancellable,
Packit 3ff832
                       GAsyncReadyCallback  callback,
Packit 3ff832
                       gpointer             user_data)
Packit 3ff832
{
Packit 3ff832
    g_assert (G_IS_DBUS_CONNECTION (connection));
Packit 3ff832
    g_assert (callback != NULL);
Packit 3ff832
Packit 3ff832
    GDBusProxyFlags flags = G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START |
Packit 3ff832
                            G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
Packit 3ff832
                            G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS;
Packit 3ff832
Packit 3ff832
    g_async_initable_new_async (IBUS_TYPE_CONFIG,
Packit 3ff832
                                G_PRIORITY_DEFAULT,
Packit 3ff832
                                cancellable,
Packit 3ff832
                                callback,
Packit 3ff832
                                user_data,
Packit 3ff832
                                "g-connection",      connection,
Packit 3ff832
                                "g-flags",           flags,
Packit 3ff832
                                "g-name",            IBUS_SERVICE_CONFIG,
Packit 3ff832
                                "g-interface-name",  IBUS_INTERFACE_CONFIG,
Packit 3ff832
                                "g-object-path",     IBUS_PATH_CONFIG,
Packit 3ff832
                                "g-default-timeout", ibus_get_timeout (),
Packit 3ff832
                                NULL);
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
IBusConfig *
Packit 3ff832
ibus_config_new_async_finish (GAsyncResult  *res,
Packit 3ff832
                              GError       **error)
Packit 3ff832
{
Packit 3ff832
    g_assert (G_IS_ASYNC_RESULT (res));
Packit 3ff832
    g_assert (error == NULL || *error == NULL);
Packit 3ff832
Packit 3ff832
    GObject *object = NULL;
Packit 3ff832
    GObject *source_object = NULL;
Packit 3ff832
Packit 3ff832
    source_object = g_async_result_get_source_object (res);
Packit 3ff832
    g_assert (source_object != NULL);
Packit 3ff832
Packit 3ff832
    object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
Packit 3ff832
                                          res,
Packit 3ff832
                                          error);
Packit 3ff832
    g_object_unref (source_object);
Packit 3ff832
Packit 3ff832
    if (object != NULL) {
Packit 3ff832
        char *owner;
Packit 3ff832
        owner = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (object));
Packit 3ff832
        if (owner == NULL) {
Packit 3ff832
            /* The configuration daemon, which is usually ibus-gconf, 
Packit 3ff832
             * is not started yet. */
Packit 3ff832
            g_set_error (error,
Packit 3ff832
                         IBUS_ERROR,
Packit 3ff832
                         IBUS_ERROR_NO_CONFIG,
Packit 3ff832
                         "Configuration daemon is not running.");
Packit 3ff832
            g_object_unref (object);
Packit 3ff832
            return NULL;
Packit 3ff832
        }
Packit 3ff832
        g_free (owner);
Packit 3ff832
        /* clients should not destroy the config service. */
Packit 3ff832
        IBUS_PROXY (object)->own = FALSE;
Packit 3ff832
        return IBUS_CONFIG (object);
Packit 3ff832
    }
Packit 3ff832
    else {
Packit 3ff832
        return NULL;
Packit 3ff832
    }
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
GVariant *
Packit 3ff832
ibus_config_get_value (IBusConfig  *config,
Packit 3ff832
                       const gchar *section,
Packit 3ff832
                       const gchar *name)
Packit 3ff832
{
Packit 3ff832
    g_assert (IBUS_IS_CONFIG (config));
Packit 3ff832
    g_assert (section != NULL);
Packit 3ff832
    g_assert (name != NULL);
Packit 3ff832
Packit 3ff832
    GError *error = NULL;
Packit 3ff832
    GVariant *result;
Packit 3ff832
    result = g_dbus_proxy_call_sync ((GDBusProxy *) config,
Packit 3ff832
                                     "GetValue",                /* method_name */
Packit 3ff832
                                     g_variant_new ("(ss)",
Packit 3ff832
                                        section, name),         /* parameters */
Packit 3ff832
                                     G_DBUS_CALL_FLAGS_NONE,    /* flags */
Packit 3ff832
                                     -1,                        /* timeout */
Packit 3ff832
                                     NULL,                      /* cancellable */
Packit 3ff832
                                     &error                     /* error */
Packit 3ff832
                                     );
Packit 3ff832
    if (result == NULL) {
Packit 3ff832
        g_warning ("%s.GetValue: %s", IBUS_INTERFACE_CONFIG, error->message);
Packit 3ff832
        g_error_free (error);
Packit 3ff832
        return NULL;
Packit 3ff832
    }
Packit 3ff832
Packit 3ff832
    GVariant *value = NULL;
Packit 3ff832
    g_variant_get (result, "(v)", &value);
Packit 3ff832
    g_variant_unref (result);
Packit 3ff832
Packit 3ff832
    return value;
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
void
Packit 3ff832
ibus_config_get_value_async (IBusConfig         *config,
Packit 3ff832
                             const gchar        *section,
Packit 3ff832
                             const gchar        *name,
Packit 3ff832
                             gint                timeout_ms,
Packit 3ff832
                             GCancellable       *cancellable,
Packit 3ff832
                             GAsyncReadyCallback callback,
Packit 3ff832
                             gpointer            user_data)
Packit 3ff832
{
Packit 3ff832
    g_assert (IBUS_IS_CONFIG (config));
Packit 3ff832
    g_assert (section != NULL);
Packit 3ff832
    g_assert (name != NULL);
Packit 3ff832
Packit 3ff832
    g_dbus_proxy_call ((GDBusProxy *)config,
Packit 3ff832
                       "GetValue",
Packit 3ff832
                       g_variant_new ("(ss)", section, name),
Packit 3ff832
                       G_DBUS_CALL_FLAGS_NONE,
Packit 3ff832
                       timeout_ms,
Packit 3ff832
                       cancellable,
Packit 3ff832
                       callback,
Packit 3ff832
                       user_data);
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
GVariant *
Packit 3ff832
ibus_config_get_value_async_finish (IBusConfig    *config,
Packit 3ff832
                                    GAsyncResult  *result,
Packit 3ff832
                                    GError       **error)
Packit 3ff832
{
Packit 3ff832
    g_assert (IBUS_IS_CONFIG (config));
Packit 3ff832
    g_assert (G_IS_ASYNC_RESULT (result));
Packit 3ff832
    g_assert (error == NULL || *error == NULL);
Packit 3ff832
Packit 3ff832
    GVariant *value = NULL;
Packit 3ff832
    GVariant *retval = g_dbus_proxy_call_finish ((GDBusProxy *)config,
Packit 3ff832
                                                 result,
Packit 3ff832
                                                 error);
Packit 3ff832
    if (retval != NULL) {
Packit 3ff832
        g_variant_get (retval, "(v)", &value);
Packit 3ff832
        g_variant_unref (retval);
Packit 3ff832
    }
Packit 3ff832
Packit 3ff832
    return value;
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
GVariant *
Packit 3ff832
ibus_config_get_values (IBusConfig  *config,
Packit 3ff832
                        const gchar *section)
Packit 3ff832
{
Packit 3ff832
    g_assert (IBUS_IS_CONFIG (config));
Packit 3ff832
    g_assert (section != NULL);
Packit 3ff832
Packit 3ff832
    GError *error = NULL;
Packit 3ff832
    GVariant *result;
Packit 3ff832
    result = g_dbus_proxy_call_sync ((GDBusProxy *) config,
Packit 3ff832
                                     "GetValues",
Packit 3ff832
                                     g_variant_new ("(s)", section),
Packit 3ff832
                                     G_DBUS_CALL_FLAGS_NONE,
Packit 3ff832
                                     -1,
Packit 3ff832
                                     NULL,
Packit 3ff832
                                     &error);
Packit 3ff832
    if (result == NULL) {
Packit 3ff832
        g_warning ("%s.GetValues: %s", IBUS_INTERFACE_CONFIG, error->message);
Packit 3ff832
        g_error_free (error);
Packit 3ff832
        return NULL;
Packit 3ff832
    }
Packit 3ff832
Packit 3ff832
    GVariant *value = NULL;
Packit 3ff832
    g_variant_get (result, "(@a{sv})", &value);
Packit 3ff832
    g_variant_unref (result);
Packit 3ff832
Packit 3ff832
    return value;
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
void
Packit 3ff832
ibus_config_get_values_async (IBusConfig         *config,
Packit 3ff832
                              const gchar        *section,
Packit 3ff832
                              gint                timeout_ms,
Packit 3ff832
                              GCancellable       *cancellable,
Packit 3ff832
                              GAsyncReadyCallback callback,
Packit 3ff832
                              gpointer            user_data)
Packit 3ff832
{
Packit 3ff832
    g_assert (IBUS_IS_CONFIG (config));
Packit 3ff832
    g_assert (section != NULL);
Packit 3ff832
Packit 3ff832
    g_dbus_proxy_call ((GDBusProxy *)config,
Packit 3ff832
                       "GetValues",
Packit 3ff832
                       g_variant_new ("(s)", section),
Packit 3ff832
                       G_DBUS_CALL_FLAGS_NONE,
Packit 3ff832
                       timeout_ms,
Packit 3ff832
                       cancellable,
Packit 3ff832
                       callback,
Packit 3ff832
                       user_data);
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
GVariant *
Packit 3ff832
ibus_config_get_values_async_finish (IBusConfig    *config,
Packit 3ff832
                                     GAsyncResult  *result,
Packit 3ff832
                                     GError       **error)
Packit 3ff832
{
Packit 3ff832
    g_assert (IBUS_IS_CONFIG (config));
Packit 3ff832
    g_assert (G_IS_ASYNC_RESULT (result));
Packit 3ff832
    g_assert (error == NULL || *error == NULL);
Packit 3ff832
Packit 3ff832
    GVariant *value = NULL;
Packit 3ff832
    GVariant *retval = g_dbus_proxy_call_finish ((GDBusProxy *)config,
Packit 3ff832
                                                 result,
Packit 3ff832
                                                 error);
Packit 3ff832
    if (retval != NULL) {
Packit 3ff832
        g_variant_get (retval, "(@a{sv})", &value);
Packit 3ff832
        g_variant_unref (retval);
Packit 3ff832
    }
Packit 3ff832
Packit 3ff832
    return value;
Packit 3ff832
}
Packit 3ff832
    
Packit 3ff832
gboolean
Packit 3ff832
ibus_config_set_value (IBusConfig   *config,
Packit 3ff832
                       const gchar  *section,
Packit 3ff832
                       const gchar  *name,
Packit 3ff832
                       GVariant     *value)
Packit 3ff832
{
Packit 3ff832
    g_assert (IBUS_IS_CONFIG (config));
Packit 3ff832
    g_assert (section != NULL);
Packit 3ff832
    g_assert (name != NULL);
Packit 3ff832
    g_assert (value != NULL);
Packit 3ff832
Packit 3ff832
    GError *error = NULL;
Packit 3ff832
    GVariant *result;
Packit 3ff832
    result = g_dbus_proxy_call_sync ((GDBusProxy *) config,
Packit 3ff832
                                     "SetValue",                /* method_name */
Packit 3ff832
                                     g_variant_new ("(ssv)",
Packit 3ff832
                                        section, name, value),  /* parameters */
Packit 3ff832
                                     G_DBUS_CALL_FLAGS_NONE,    /* flags */
Packit 3ff832
                                     -1,                        /* timeout */
Packit 3ff832
                                     NULL,                      /* cancellable */
Packit 3ff832
                                     &error                     /* error */
Packit 3ff832
                                     );
Packit 3ff832
    if (result == NULL) {
Packit 3ff832
        g_warning ("%s.SetValue: %s", IBUS_INTERFACE_CONFIG, error->message);
Packit 3ff832
        g_error_free (error);
Packit 3ff832
        return FALSE;
Packit 3ff832
    }
Packit 3ff832
    g_variant_unref (result);
Packit 3ff832
    return TRUE;
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
void
Packit 3ff832
ibus_config_set_value_async (IBusConfig         *config,
Packit 3ff832
                             const gchar        *section,
Packit 3ff832
                             const gchar        *name,
Packit 3ff832
                             GVariant           *value,
Packit 3ff832
                             gint                timeout_ms,
Packit 3ff832
                             GCancellable       *cancellable,
Packit 3ff832
                             GAsyncReadyCallback callback,
Packit 3ff832
                             gpointer            user_data)
Packit 3ff832
{
Packit 3ff832
    g_assert (IBUS_IS_CONFIG (config));
Packit 3ff832
    g_assert (section != NULL);
Packit 3ff832
    g_assert (name != NULL);
Packit 3ff832
    g_assert (value != NULL);
Packit 3ff832
Packit 3ff832
    g_dbus_proxy_call ((GDBusProxy *) config,
Packit 3ff832
                       "SetValue",                /* method_name */
Packit 3ff832
                       g_variant_new ("(ssv)",
Packit 3ff832
                                      section, name, value),  /* parameters */
Packit 3ff832
                       G_DBUS_CALL_FLAGS_NONE,    /* flags */
Packit 3ff832
                       timeout_ms,
Packit 3ff832
                       cancellable,
Packit 3ff832
                       callback,
Packit 3ff832
                       user_data);
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
gboolean
Packit 3ff832
ibus_config_set_value_async_finish (IBusConfig         *config,
Packit 3ff832
                                    GAsyncResult       *result,
Packit 3ff832
                                    GError            **error)
Packit 3ff832
{
Packit 3ff832
    g_assert (IBUS_IS_CONFIG (config));
Packit 3ff832
    g_assert (G_IS_ASYNC_RESULT (result));
Packit 3ff832
    g_assert (error == NULL || *error == NULL);
Packit 3ff832
Packit 3ff832
    GVariant *retval = g_dbus_proxy_call_finish ((GDBusProxy *)config,
Packit 3ff832
                                                 result,
Packit 3ff832
                                                 error);
Packit 3ff832
    if (retval != NULL) {
Packit 3ff832
        g_variant_unref (retval);
Packit 3ff832
        return TRUE;
Packit 3ff832
    }
Packit 3ff832
Packit 3ff832
    return FALSE;
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
gboolean
Packit 3ff832
ibus_config_unset (IBusConfig   *config,
Packit 3ff832
                   const gchar  *section,
Packit 3ff832
                   const gchar  *name)
Packit 3ff832
{
Packit 3ff832
    g_assert (IBUS_IS_CONFIG (config));
Packit 3ff832
    g_assert (section != NULL);
Packit 3ff832
    g_assert (name != NULL);
Packit 3ff832
Packit 3ff832
    GError *error = NULL;
Packit 3ff832
    GVariant *result;
Packit 3ff832
    result = g_dbus_proxy_call_sync ((GDBusProxy *) config,
Packit 3ff832
                                     "UnsetValue",              /* method_name */
Packit 3ff832
                                     g_variant_new ("(ss)",
Packit 3ff832
                                        section, name),         /* parameters */
Packit 3ff832
                                     G_DBUS_CALL_FLAGS_NONE,    /* flags */
Packit 3ff832
                                     -1,                        /* timeout */
Packit 3ff832
                                     NULL,                      /* cancellable */
Packit 3ff832
                                     &error                     /* error */
Packit 3ff832
                                     );
Packit 3ff832
    if (result == NULL) {
Packit 3ff832
        g_warning ("%s.UnsetValue: %s", IBUS_INTERFACE_CONFIG, error->message);
Packit 3ff832
        g_error_free (error);
Packit 3ff832
        return FALSE;
Packit 3ff832
    }
Packit 3ff832
    g_variant_unref (result);
Packit 3ff832
    return TRUE;
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
static guint
Packit 3ff832
_signal_subscribe (GDBusProxy *proxy)
Packit 3ff832
{
Packit 3ff832
    GDBusConnection *connection = g_dbus_proxy_get_connection (proxy);
Packit 3ff832
    return g_dbus_connection_signal_subscribe (connection,
Packit 3ff832
                                               NULL,
Packit 3ff832
                                               IBUS_INTERFACE_CONFIG,
Packit 3ff832
                                               NULL,
Packit 3ff832
                                               NULL,
Packit 3ff832
                                               NULL,
Packit 3ff832
                                               G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE,
Packit 3ff832
                                               (GDBusSignalCallback) _connection_signal_cb,
Packit 3ff832
                                               g_object_ref (proxy),
Packit 3ff832
                                               (GDestroyNotify) g_object_unref);
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
static void
Packit 3ff832
_signal_unsubscribe (GDBusProxy *proxy, guint signal_id)
Packit 3ff832
{
Packit 3ff832
    GDBusConnection *connection = g_dbus_proxy_get_connection (proxy);
Packit 3ff832
    g_dbus_connection_signal_unsubscribe (connection, signal_id);
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
static GInitableIface *initable_iface_parent = NULL;
Packit 3ff832
Packit 3ff832
static gboolean
Packit 3ff832
initable_init (GInitable     *initable,
Packit 3ff832
               GCancellable  *cancellable,
Packit 3ff832
               GError       **error)
Packit 3ff832
{
Packit 3ff832
    if (!initable_iface_parent->init (initable, cancellable, error))
Packit 3ff832
        return FALSE;
Packit 3ff832
Packit 3ff832
    IBusConfig *config = IBUS_CONFIG (initable);
Packit 3ff832
    config->priv->watch_config_signal_id =
Packit 3ff832
        _signal_subscribe (G_DBUS_PROXY (initable));
Packit 3ff832
Packit 3ff832
    gboolean retval = ibus_config_watch (config, NULL, NULL);
Packit 3ff832
    if (!retval)
Packit 3ff832
        g_set_error (error,
Packit 3ff832
                     IBUS_ERROR,
Packit 3ff832
                     IBUS_ERROR_FAILED,
Packit 3ff832
                     "Cannot watch configuration change.");
Packit 3ff832
    return retval;
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
static void
Packit 3ff832
initable_iface_init (GInitableIface *initable_iface)
Packit 3ff832
{
Packit 3ff832
    initable_iface_parent = g_type_interface_peek_parent (initable_iface);
Packit 3ff832
    initable_iface->init = initable_init;
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
static GAsyncInitableIface *async_initable_iface_parent = NULL;
Packit 3ff832
Packit 3ff832
static void
Packit 3ff832
async_initable_init_async (GAsyncInitable      *initable,
Packit 3ff832
                           gint                 io_priority,
Packit 3ff832
                           GCancellable        *cancellable,
Packit 3ff832
                           GAsyncReadyCallback  callback,
Packit 3ff832
                           gpointer             user_data)
Packit 3ff832
{
Packit 3ff832
    async_initable_iface_parent->init_async (initable,
Packit 3ff832
                                             io_priority,
Packit 3ff832
                                             cancellable,
Packit 3ff832
                                             callback,
Packit 3ff832
                                             user_data);
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
static gboolean
Packit 3ff832
async_initable_init_finish (GAsyncInitable  *initable,
Packit 3ff832
                            GAsyncResult    *res,
Packit 3ff832
                            GError         **error)
Packit 3ff832
{
Packit 3ff832
    if (!async_initable_iface_parent->init_finish (initable, res, error))
Packit 3ff832
        return FALSE;
Packit 3ff832
Packit 3ff832
    IBusConfig *config = IBUS_CONFIG (initable);
Packit 3ff832
    config->priv->watch_config_signal_id =
Packit 3ff832
        _signal_subscribe (G_DBUS_PROXY (initable));
Packit 3ff832
    return ibus_config_watch (config, NULL, NULL);
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
static void
Packit 3ff832
async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
Packit 3ff832
{
Packit 3ff832
    async_initable_iface_parent =
Packit 3ff832
        g_type_interface_peek_parent (async_initable_iface);
Packit 3ff832
    async_initable_iface->init_async = async_initable_init_async;
Packit 3ff832
    async_initable_iface->init_finish = async_initable_init_finish;
Packit 3ff832
}