Blame conf/memconf/config.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) 2010, Google Inc. All rights reserved.
Packit 3ff832
 * Copyright (C) 2010 Peng Huang <shawn.p.huang@gmail.com>
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 <string.h>
Packit 3ff832
#include <ibus.h>
Packit 3ff832
#include "config.h"
Packit 3ff832
Packit 3ff832
Packit 3ff832
typedef struct _IBusConfigMemconfClass IBusConfigMemconfClass;
Packit 3ff832
Packit 3ff832
struct _IBusConfigMemconf {
Packit 3ff832
    IBusConfigService parent;
Packit 3ff832
    GHashTable *values;
Packit 3ff832
};
Packit 3ff832
Packit 3ff832
struct _IBusConfigMemconfClass {
Packit 3ff832
    IBusConfigServiceClass parent;
Packit 3ff832
};
Packit 3ff832
Packit 3ff832
/* functions prototype */
Packit 3ff832
static void         ibus_config_memconf_class_init  (IBusConfigMemconfClass *class);
Packit 3ff832
static void         ibus_config_memconf_init        (IBusConfigMemconf      *config);
Packit 3ff832
static void         ibus_config_memconf_destroy     (IBusConfigMemconf      *config);
Packit 3ff832
static gboolean     ibus_config_memconf_set_value   (IBusConfigService      *config,
Packit 3ff832
                                                     const gchar            *section,
Packit 3ff832
                                                     const gchar            *name,
Packit 3ff832
                                                     GVariant               *value,
Packit 3ff832
                                                     GError                **error);
Packit 3ff832
static GVariant    *ibus_config_memconf_get_value   (IBusConfigService      *config,
Packit 3ff832
                                                     const gchar            *section,
Packit 3ff832
                                                     const gchar            *name,
Packit 3ff832
                                                     GError                **error);
Packit 3ff832
static GVariant    *ibus_config_memconf_get_values  (IBusConfigService      *config,
Packit 3ff832
                                                     const gchar            *section,
Packit 3ff832
                                                     GError                **error);
Packit 3ff832
static gboolean     ibus_config_memconf_unset_value (IBusConfigService      *config,
Packit 3ff832
                                                     const gchar            *section,
Packit 3ff832
                                                     const gchar            *name,
Packit 3ff832
                                                     GError                **error);
Packit 3ff832
Packit 3ff832
G_DEFINE_TYPE (IBusConfigMemconf, ibus_config_memconf, IBUS_TYPE_CONFIG_SERVICE)
Packit 3ff832
Packit 3ff832
static void
Packit 3ff832
ibus_config_memconf_class_init (IBusConfigMemconfClass *class)
Packit 3ff832
{
Packit 3ff832
    GObjectClass *object_class = G_OBJECT_CLASS (class);
Packit 3ff832
Packit 3ff832
    IBUS_OBJECT_CLASS (object_class)->destroy = (IBusObjectDestroyFunc) ibus_config_memconf_destroy;
Packit 3ff832
    IBUS_CONFIG_SERVICE_CLASS (object_class)->set_value   = ibus_config_memconf_set_value;
Packit 3ff832
    IBUS_CONFIG_SERVICE_CLASS (object_class)->get_value   = ibus_config_memconf_get_value;
Packit 3ff832
    IBUS_CONFIG_SERVICE_CLASS (object_class)->get_values  = ibus_config_memconf_get_values;
Packit 3ff832
    IBUS_CONFIG_SERVICE_CLASS (object_class)->unset_value = ibus_config_memconf_unset_value;
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
static void
Packit 3ff832
ibus_config_memconf_init (IBusConfigMemconf *config)
Packit 3ff832
{
Packit 3ff832
    config->values = g_hash_table_new_full (g_str_hash,
Packit 3ff832
                                            g_str_equal,
Packit 3ff832
                                            (GDestroyNotify)g_free,
Packit 3ff832
                                            (GDestroyNotify)g_variant_unref);
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
static void
Packit 3ff832
ibus_config_memconf_destroy (IBusConfigMemconf *config)
Packit 3ff832
{
Packit 3ff832
    g_hash_table_destroy (config->values);
Packit 3ff832
    IBUS_OBJECT_CLASS (ibus_config_memconf_parent_class)->destroy ((IBusObject *)config);
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
static gboolean
Packit 3ff832
ibus_config_memconf_set_value (IBusConfigService *config,
Packit 3ff832
                               const gchar       *section,
Packit 3ff832
                               const gchar       *name,
Packit 3ff832
                               GVariant          *value,
Packit 3ff832
                               GError           **error)
Packit 3ff832
{
Packit 3ff832
    g_assert (IBUS_IS_CONFIG_MEMCONF (config));
Packit 3ff832
    g_assert (section);
Packit 3ff832
    g_assert (name);
Packit 3ff832
    g_assert (value);
Packit 3ff832
    g_assert (error == NULL || *error == NULL);
Packit 3ff832
Packit 3ff832
    gchar *key = g_strdup_printf ("%s:%s", section, name);
Packit 3ff832
Packit 3ff832
    g_hash_table_insert (IBUS_CONFIG_MEMCONF (config)->values,
Packit 3ff832
                         key, g_variant_ref_sink (value));
Packit 3ff832
Packit 3ff832
    ibus_config_service_value_changed (config, section, name, value);
Packit 3ff832
Packit 3ff832
    return TRUE;
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
static GVariant *
Packit 3ff832
ibus_config_memconf_get_value (IBusConfigService *config,
Packit 3ff832
                               const gchar       *section,
Packit 3ff832
                               const gchar       *name,
Packit 3ff832
                               GError           **error)
Packit 3ff832
{
Packit 3ff832
    g_assert (IBUS_IS_CONFIG_MEMCONF (config));
Packit 3ff832
    g_assert (section);
Packit 3ff832
    g_assert (name);
Packit 3ff832
    g_assert (error == NULL || *error == NULL);
Packit 3ff832
Packit 3ff832
    gchar *key = g_strdup_printf ("%s:%s", section, name);
Packit 3ff832
    GVariant *value = (GVariant *)g_hash_table_lookup (IBUS_CONFIG_MEMCONF (config)->values, key);
Packit 3ff832
    g_free (key);
Packit 3ff832
Packit 3ff832
    if (value != NULL) {
Packit 3ff832
        g_variant_ref (value);
Packit 3ff832
    }
Packit 3ff832
    else if (error != NULL) {
Packit 3ff832
        *error = g_error_new (G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
Packit 3ff832
                              "Config value [%s:%s] does not exist.", section, name);
Packit 3ff832
    }
Packit 3ff832
    return value;
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
static GVariant *
Packit 3ff832
ibus_config_memconf_get_values (IBusConfigService *config,
Packit 3ff832
                                const gchar       *section,
Packit 3ff832
                                GError           **error)
Packit 3ff832
{
Packit 3ff832
    g_assert (IBUS_IS_CONFIG_MEMCONF (config));
Packit 3ff832
    g_assert (section);
Packit 3ff832
    g_assert (error == NULL || *error == NULL);
Packit 3ff832
Packit 3ff832
    GHashTableIter iter;
Packit 3ff832
    const gchar *key;
Packit 3ff832
    GVariant *value;
Packit 3ff832
    
Packit 3ff832
    GVariantBuilder *builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
Packit 3ff832
    g_hash_table_iter_init (&iter, IBUS_CONFIG_MEMCONF (config)->values);
Packit 3ff832
    while (g_hash_table_iter_next (&iter, (gpointer *)&key, (gpointer *)&value)) {
Packit 3ff832
        gchar **v = g_strsplit (key, ":", 2);
Packit 3ff832
        if (g_strcmp0 (v[0], section) == 0) {
Packit 3ff832
            g_variant_builder_add (builder, "{sv}", v[1], value); 
Packit 3ff832
        }
Packit 3ff832
        g_strfreev(v);
Packit 3ff832
    }
Packit 3ff832
Packit 3ff832
    return g_variant_builder_end (builder);
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
static gboolean
Packit 3ff832
ibus_config_memconf_unset_value (IBusConfigService *config,
Packit 3ff832
                                 const gchar       *section,
Packit 3ff832
                                 const gchar       *name,
Packit 3ff832
                                 GError            **error)
Packit 3ff832
{
Packit 3ff832
    g_assert (IBUS_IS_CONFIG_MEMCONF (config));
Packit 3ff832
    g_assert (section);
Packit 3ff832
    g_assert (name);
Packit 3ff832
    g_assert (error == NULL || *error == NULL);
Packit 3ff832
Packit 3ff832
    gchar *key = g_strdup_printf ("%s:%s", section, name);
Packit 3ff832
    gboolean retval = g_hash_table_remove (IBUS_CONFIG_MEMCONF (config)->values, key);
Packit 3ff832
    g_free (key);
Packit 3ff832
Packit 3ff832
    if (retval) {
Packit 3ff832
        ibus_config_service_value_changed (config,
Packit 3ff832
                                           section,
Packit 3ff832
                                           name,
Packit 3ff832
                                           g_variant_new_tuple (NULL, 0));
Packit 3ff832
    }
Packit 3ff832
    else {
Packit 3ff832
        g_set_error (error,
Packit 3ff832
                     G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
Packit 3ff832
                     "Config value [%s:%s] does not exist.",
Packit 3ff832
                     section, name);
Packit 3ff832
    }
Packit 3ff832
    return retval;
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
IBusConfigMemconf *
Packit 3ff832
ibus_config_memconf_new (GDBusConnection *connection)
Packit 3ff832
{
Packit 3ff832
    IBusConfigMemconf *config;
Packit 3ff832
    config = (IBusConfigMemconf *) g_object_new (IBUS_TYPE_CONFIG_MEMCONF,
Packit 3ff832
                                                 "object-path", IBUS_PATH_CONFIG,
Packit 3ff832
                                                 "connection", connection,
Packit 3ff832
                                                 NULL);
Packit 3ff832
    return config;
Packit 3ff832
}