OLD | NEW |
(Empty) | |
| 1 /* ibus - The Input Bus |
| 2 * |
| 3 * Copyright (C) 2008-2010 Red Hat, Inc. |
| 4 * Copyright (c) 2010, Google Inc. All rights reserved. |
| 5 * |
| 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. |
| 10 * |
| 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. |
| 15 * |
| 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. |
| 20 */ |
| 21 |
| 22 #include "config.h" |
| 23 |
| 24 struct IBusConfigMemConfClass { |
| 25 IBusConfigServiceClass parent; |
| 26 }; |
| 27 |
| 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, |
| 32 const gchar* section, |
| 33 const gchar* name, |
| 34 const GValue* value, |
| 35 IBusError** error); |
| 36 static gboolean ibus_config_memconf_get_value(IBusConfigService* config, |
| 37 const gchar* section, |
| 38 const gchar* name, |
| 39 GValue* value, |
| 40 IBusError** error); |
| 41 static gboolean ibus_config_memconf_unset(IBusConfigService* config, |
| 42 const gchar* section, |
| 43 const gchar* name, |
| 44 IBusError** error); |
| 45 |
| 46 // Copied from gconf/config.c. |
| 47 static IBusConfigServiceClass* parent_class = NULL; |
| 48 |
| 49 // Copied from gconf/config.c. |
| 50 GType ibus_config_memconf_get_type() { |
| 51 static GType type = 0; |
| 52 |
| 53 static const GTypeInfo type_info = { |
| 54 sizeof(IBusConfigMemConfClass), |
| 55 NULL, |
| 56 NULL, |
| 57 reinterpret_cast<GClassInitFunc>(ibus_config_memconf_class_init), |
| 58 NULL, |
| 59 NULL, |
| 60 sizeof(IBusConfigMemConf), |
| 61 0, |
| 62 reinterpret_cast<GInstanceInitFunc>(ibus_config_memconf_init), |
| 63 }; |
| 64 |
| 65 if (type == 0) { |
| 66 type = g_type_register_static(IBUS_TYPE_CONFIG_SERVICE, |
| 67 "IBusConfigMemConf", |
| 68 &type_info, |
| 69 static_cast<GTypeFlags>(0)); |
| 70 } |
| 71 |
| 72 return type; |
| 73 } |
| 74 |
| 75 // Copied from gconf/config.c. |
| 76 static void ibus_config_memconf_class_init(IBusConfigMemConfClass* klass) { |
| 77 GObjectClass* object_class = G_OBJECT_CLASS(klass); |
| 78 |
| 79 parent_class = reinterpret_cast<IBusConfigServiceClass*>( |
| 80 g_type_class_peek_parent(klass)); |
| 81 |
| 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; |
| 89 } |
| 90 |
| 91 static void ibus_config_memconf_init(IBusConfigMemConf* config) { |
| 92 config->entries = new std::map<std::string, GValue*>; |
| 93 } |
| 94 |
| 95 static void ibus_config_memconf_destroy(IBusConfigMemConf* config) { |
| 96 if (config) { |
| 97 std::map<std::string, GValue*>::iterator iter; |
| 98 for (iter = config->entries->begin(); |
| 99 iter != config->entries->end(); |
| 100 ++iter) { |
| 101 g_value_unset(iter->second); |
| 102 g_free(iter->second); |
| 103 } |
| 104 delete config->entries; |
| 105 } |
| 106 IBUS_OBJECT_CLASS(parent_class)->destroy( |
| 107 reinterpret_cast<IBusObject*>(config)); |
| 108 } |
| 109 |
| 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); |
| 117 return TRUE; |
| 118 } |
| 119 |
| 120 return FALSE; |
| 121 } |
| 122 |
| 123 // Server side implementation of ibus_config_set_value. |
| 124 static gboolean ibus_config_memconf_set_value(IBusConfigService* config, |
| 125 const gchar* section, |
| 126 const gchar* name, |
| 127 const GValue* value, |
| 128 IBusError** error) { |
| 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); |
| 134 |
| 135 const std::string key = std::string(section) + name; |
| 136 IBusConfigMemConf* memconf = reinterpret_cast<IBusConfigMemConf*>(config); |
| 137 |
| 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); |
| 141 |
| 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; |
| 144 if (!result) { |
| 145 g_value_unset(new_entry); |
| 146 g_free(new_entry); |
| 147 *error = ibus_error_new_from_printf( |
| 148 DBUS_ERROR_FAILED, "Can not set value [%s->%s]", section, name); |
| 149 } |
| 150 |
| 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); |
| 154 |
| 155 return result ? TRUE : FALSE; |
| 156 } |
| 157 |
| 158 // Server side implementation of ibus_config_get_value. |
| 159 static gboolean ibus_config_memconf_get_value(IBusConfigService* config, |
| 160 const gchar* section, |
| 161 const gchar* name, |
| 162 GValue* value, |
| 163 IBusError** error) { |
| 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); |
| 169 |
| 170 const std::string key = std::string(section) + name; |
| 171 IBusConfigMemConf* memconf = reinterpret_cast<IBusConfigMemConf*>(config); |
| 172 |
| 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 DBUS_ERROR_FAILED, "Can not get value [%s->%s]", section, name); |
| 178 return FALSE; |
| 179 } |
| 180 |
| 181 const GValue* entry = iter->second; |
| 182 g_value_init(value, G_VALUE_TYPE(entry)); |
| 183 g_value_copy(entry, value); |
| 184 |
| 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. |
| 187 return TRUE; |
| 188 } |
| 189 |
| 190 // Server side implementation of ibus_config_unset_value. |
| 191 static gboolean ibus_config_memconf_unset(IBusConfigService* config, |
| 192 const gchar* section, |
| 193 const gchar* name, |
| 194 IBusError** error) { |
| 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); |
| 199 |
| 200 const std::string key = std::string(section) + name; |
| 201 IBusConfigMemConf* memconf = reinterpret_cast<IBusConfigMemConf*>(config); |
| 202 |
| 203 if (!do_unset(memconf, key)) { |
| 204 *error = ibus_error_new_from_printf( |
| 205 DBUS_ERROR_FAILED, "Can not unset value [%s->%s]", section, name); |
| 206 return FALSE; |
| 207 } |
| 208 |
| 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. |
| 212 return TRUE; |
| 213 } |
| 214 |
| 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, |
| 221 NULL)); |
| 222 return config; |
| 223 } |
| 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. |
OLD | NEW |