1
/* ibus - The Input Bus
3
* Copyright (C) 2008-2010 Red Hat, Inc.
4
* Copyright (c) 2010, Google Inc. All rights reserved.
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License as published by the Free Software Foundation; either
9
* version 2 of the License, or (at your option) any later version.
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this library; if not, write to the
18
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19
* Boston, MA 02111-1307, USA.
24
struct IBusConfigMemConfClass {
25
IBusConfigServiceClass parent;
28
static void ibus_config_memconf_class_init(IBusConfigMemConfClass* klass);
29
static void ibus_config_memconf_init(IBusConfigMemConf* config);
30
static void ibus_config_memconf_destroy(IBusConfigMemConf* config);
31
static gboolean ibus_config_memconf_set_value(IBusConfigService* config,
36
static gboolean ibus_config_memconf_get_value(IBusConfigService* config,
41
static gboolean ibus_config_memconf_unset(IBusConfigService* config,
46
// Copied from gconf/config.c.
47
static IBusConfigServiceClass* parent_class = NULL;
49
// Copied from gconf/config.c.
50
GType ibus_config_memconf_get_type() {
51
static GType type = 0;
53
static const GTypeInfo type_info = {
54
sizeof(IBusConfigMemConfClass),
57
reinterpret_cast<GClassInitFunc>(ibus_config_memconf_class_init),
60
sizeof(IBusConfigMemConf),
62
reinterpret_cast<GInstanceInitFunc>(ibus_config_memconf_init),
66
type = g_type_register_static(IBUS_TYPE_CONFIG_SERVICE,
69
static_cast<GTypeFlags>(0));
75
// Copied from gconf/config.c.
76
static void ibus_config_memconf_class_init(IBusConfigMemConfClass* klass) {
77
GObjectClass* object_class = G_OBJECT_CLASS(klass);
79
parent_class = reinterpret_cast<IBusConfigServiceClass*>(
80
g_type_class_peek_parent(klass));
82
IBUS_OBJECT_CLASS(object_class)->destroy
83
= reinterpret_cast<IBusObjectDestroyFunc>(ibus_config_memconf_destroy);
84
IBUS_CONFIG_SERVICE_CLASS(object_class)->set_value
85
= ibus_config_memconf_set_value;
86
IBUS_CONFIG_SERVICE_CLASS(object_class)->get_value
87
= ibus_config_memconf_get_value;
88
IBUS_CONFIG_SERVICE_CLASS(object_class)->unset = ibus_config_memconf_unset;
91
static void ibus_config_memconf_init(IBusConfigMemConf* config) {
92
config->entries = new std::map<std::string, GValue*>;
95
static void ibus_config_memconf_destroy(IBusConfigMemConf* config) {
97
std::map<std::string, GValue*>::iterator iter;
98
for (iter = config->entries->begin();
99
iter != config->entries->end();
101
g_value_unset(iter->second);
102
g_free(iter->second);
104
delete config->entries;
106
IBUS_OBJECT_CLASS(parent_class)->destroy(
107
reinterpret_cast<IBusObject*>(config));
110
// Remove an entry associated with |key| from the on-memory config database.
111
static gboolean do_unset(IBusConfigMemConf* memconf, const std::string& key) {
112
std::map<std::string, GValue*>::iterator iter = memconf->entries->find(key);
113
if (iter != memconf->entries->end()) {
114
g_value_unset(iter->second);
115
g_free(iter->second);
116
memconf->entries->erase(iter);
123
// Server side implementation of ibus_config_set_value.
124
static gboolean ibus_config_memconf_set_value(IBusConfigService* config,
125
const gchar* section,
129
g_return_val_if_fail(config, FALSE);
130
g_return_val_if_fail(section, FALSE);
131
g_return_val_if_fail(name, FALSE);
132
g_return_val_if_fail(value, FALSE);
133
g_return_val_if_fail(error, FALSE);
135
const std::string key = std::string(section) + name;
136
IBusConfigMemConf* memconf = reinterpret_cast<IBusConfigMemConf*>(config);
138
GValue* new_entry = g_new0(GValue, 1);
139
g_value_init(new_entry, G_VALUE_TYPE(value));
140
g_value_copy(value, new_entry);
142
do_unset(memconf, key); // remove an existing entry (if any) first.
143
bool result = memconf->entries->insert(std::make_pair(key, new_entry)).second;
145
g_value_unset(new_entry);
147
*error = ibus_error_new_from_printf(
148
"org.freedesktop.DBus.Error.Failed", "Can not set value [%s->%s]", section, name);
151
// Let ibus-daemon know that a new value is set to ibus-memconf. Note that
152
// _config_value_changed_cb() function in bus/ibusimpl.c will handle this RPC.
153
ibus_config_service_value_changed(config, section, name, value);
155
return result ? TRUE : FALSE;
158
// Server side implementation of ibus_config_get_value.
159
static gboolean ibus_config_memconf_get_value(IBusConfigService* config,
160
const gchar* section,
164
g_return_val_if_fail(config, FALSE);
165
g_return_val_if_fail(section, FALSE);
166
g_return_val_if_fail(name, FALSE);
167
g_return_val_if_fail(value, FALSE);
168
g_return_val_if_fail(error, FALSE);
170
const std::string key = std::string(section) + name;
171
IBusConfigMemConf* memconf = reinterpret_cast<IBusConfigMemConf*>(config);
173
std::map<std::string, GValue*>::const_iterator iter
174
= memconf->entries->find(key);
175
if (iter == memconf->entries->end()) {
176
*error = ibus_error_new_from_printf(
177
"org.freedesktop.DBus.Error.Failed", "Can not get value [%s->%s]", section, name);
181
const GValue* entry = iter->second;
182
g_value_init(value, G_VALUE_TYPE(entry));
183
g_value_copy(entry, value);
185
// |value| will be g_value_unset() in the super class after the value is sent
186
// to ibus-daemon. See src/ibusconfigservice.c for details.
190
// Server side implementation of ibus_config_unset_value.
191
static gboolean ibus_config_memconf_unset(IBusConfigService* config,
192
const gchar* section,
195
g_return_val_if_fail(config, FALSE);
196
g_return_val_if_fail(section, FALSE);
197
g_return_val_if_fail(name, FALSE);
198
g_return_val_if_fail(error, FALSE);
200
const std::string key = std::string(section) + name;
201
IBusConfigMemConf* memconf = reinterpret_cast<IBusConfigMemConf*>(config);
203
if (!do_unset(memconf, key)) {
204
*error = ibus_error_new_from_printf(
205
"org.freedesktop.DBus.Error.Failed", "Can not unset value [%s->%s]", section, name);
209
// Note: It is not allowed to call ibus_config_service_value_changed function
210
// with zero-cleared GValue, so we don't call the function here.
211
// See src/ibusconfigservice.c for details.
215
// Copied from gconf/config.c.
216
IBusConfigMemConf* ibus_config_memconf_new(IBusConnection* connection) {
217
IBusConfigMemConf* config = reinterpret_cast<IBusConfigMemConf*>(
218
g_object_new(ibus_config_memconf_get_type(),
219
"path", IBUS_PATH_CONFIG,
220
"connection", connection,
224
// TODO(yusukes): Upstream memconf/ code if possible. We might have to rewrite
225
// the code to C and have to change the coding style though.