1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
/*
* Copyright © 2014 Red Hat, Inc. All rights reserved.
* Copyright © 2014 Ding-Yi Chen <dchen@redhat.com>
*
* This file is part of the ibus-chewing Project.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <ibus.h>
#include <glib.h>
#include <gconf/gconf.h>
#include <gconf/gconf-client.h>
#include "MakerDialogUtil.h"
#include "GConf2Backend.h"
static GHashTable *camalCasedKeyTable = NULL;
gchar *gconf2_backend_get_key(MkdgBackend * backend,
const gchar * section, const gchar * key,
gpointer userData);
static GValue *gconf_value_to_g_value(GConfValue * confValue, GValue * value)
{
GType gType = G_VALUE_TYPE(value);
switch (confValue->type) {
case GCONF_VALUE_BOOL:
if (gType != G_TYPE_BOOLEAN) {
return NULL;
}
g_value_set_boolean(value, gconf_value_get_bool(confValue));
break;
case GCONF_VALUE_INT:
if (gType != G_TYPE_INT && gType != G_TYPE_UINT) {
return NULL;
} else if (gType == G_TYPE_INT) {
g_value_set_int(value, gconf_value_get_int(confValue));
} else {
g_value_set_uint(value, gconf_value_get_int(confValue));
}
break;
case GCONF_VALUE_STRING:
if (gType != G_TYPE_STRING) {
return NULL;
}
g_value_set_string(value, gconf_value_get_string(confValue));
break;
default:
return NULL;
}
return value;
}
static GConfValue *gconf_value_new_g_value(GValue * value)
{
GConfValue *confValue;
GType gType = G_VALUE_TYPE(value);
switch (gType) {
case G_TYPE_BOOLEAN:
confValue = gconf_value_new(GCONF_VALUE_BOOL);
gconf_value_set_bool(confValue, g_value_get_boolean(value));
break;
case G_TYPE_INT:
confValue = gconf_value_new(GCONF_VALUE_INT);
gconf_value_set_int(confValue, g_value_get_int(value));
break;
case G_TYPE_UINT:
confValue = gconf_value_new(GCONF_VALUE_INT);
gconf_value_set_int(confValue, g_value_get_uint(value));
break;
case G_TYPE_STRING:
confValue = gconf_value_new(GCONF_VALUE_STRING);
gconf_value_set_string(confValue, g_value_get_string(value));
break;
default:
return NULL;
}
return confValue;
}
#define KEY_BUFFER_SIZE 100
static gchar *to_real_key(gchar * confKey, MkdgBackend * backend,
const gchar * section, const gchar * key)
{
gchar *camalCasedKey = gconf2_backend_get_key(backend, section, key, NULL);
if (!STRING_IS_EMPTY(backend->basePath)) {
g_strlcpy(confKey, backend->basePath, KEY_BUFFER_SIZE);
g_strlcat(confKey, "/", KEY_BUFFER_SIZE);
} else {
g_strlcpy(confKey, "/", KEY_BUFFER_SIZE);
}
if (!STRING_IS_EMPTY(section)) {
g_strlcat(confKey, section, KEY_BUFFER_SIZE);
g_strlcat(confKey, "/", KEY_BUFFER_SIZE);
}
g_strlcat(confKey, camalCasedKey, KEY_BUFFER_SIZE);
return confKey;
}
/*============================================
* Interface routines
*/
gchar *gconf2_backend_get_key(MkdgBackend * backend,
const gchar * section, const gchar * key,
gpointer userData)
{
gchar *camalCasedKey =
(gchar *) g_hash_table_lookup(camalCasedKeyTable, key);
if (camalCasedKey == NULL) {
camalCasedKey = mkdg_str_dash_to_camel(key);
g_hash_table_insert(camalCasedKeyTable, (gpointer) key, camalCasedKey);
}
return camalCasedKey;
}
GValue *gconf2_backend_read_value(MkdgBackend * backend,
GValue * value,
const gchar * section,
const gchar * key, gpointer userData)
{
mkdg_log(DEBUG, "gconf2_backend_read_value(-,-,%s,%s,-):", section, key);
GConfClient *config = (GConfClient *) backend->config;
GError *err = NULL;
gchar confKey[KEY_BUFFER_SIZE];
to_real_key(confKey, backend, section, key);
GConfValue *confValue = gconf_client_get(config, confKey, &err);
if (confValue == NULL) {
mkdg_log(ERROR,
"gconf2_backend_read_value(-,-,%s,%s,-): Failed to retrieve confValue",
section, key);
return NULL;
}
if (err != NULL) {
mkdg_log(ERROR, "gconf2_backend_read_value(-,-,%s,%s,-): %s",
section, key, err->message);
return NULL;
}
return gconf_value_to_g_value(confValue, value);
}
gboolean gconf2_backend_write_value(MkdgBackend * backend,
GValue * value,
const gchar * section,
const gchar * key, gpointer userData)
{
GConfClient *config = (GConfClient *) backend->config;
GError *err = NULL;
gchar confKey[KEY_BUFFER_SIZE];
to_real_key(confKey, backend, section, key);
GConfValue *confValue = gconf_value_new_g_value(value);
gconf_client_set(config, confKey, confValue, &err);
gconf_value_free(confValue);
if (err != NULL) {
mkdg_log(ERROR, "gconf2_backend_write_value(-,-,%s,%s,-): %s",
section, key, err->message);
return FALSE;
}
return TRUE;
}
MkdgBackend *gconf2_backend_new(const gchar * basePath, gpointer auxData)
{
GConfClient *client = gconf_client_get_default();
MkdgBackend *result = mkdg_backend_new(GCONF2_BACKEND_ID,
(gpointer) client, basePath,
auxData);
camalCasedKeyTable = g_hash_table_new(g_str_hash, g_str_equal);
result->getKeyFunc = gconf2_backend_get_key;
result->readFunc = gconf2_backend_read_value;
result->writeFunc = gconf2_backend_write_value;
return result;
}
|