* Sets the engines to be loaded by the ibus daemon, only those set via this * method can be then activated. * Note that there is a slight delay between the configuration values being set * and the actual propagation into the ibus daemon (accessed via bus_ member), * so direct call to GetInstalledEngines just after this method could return * past results for isntance. A 1 second pause (see tes
| 190 | * past results for isntance. A 1 second pause (see tests) is generally enough. |
| 191 | */ |
| 192 | int IBusHandler::LoadEngines(const std::vector<std::string>& engine_names) { |
| 193 | int nb_loaded_engines = 0; |
| 194 | |
| 195 | if (! ibus_available_) { |
| 196 | return nb_loaded_engines; |
| 197 | } |
| 198 | |
| 199 | // We want to avoid strange states where no engines are loaded, hence we |
| 200 | // simply ignore the call if an empty vector is passed as parameter. |
| 201 | if (!engine_names.empty()) { |
| 202 | GValue gvalue = {0}; |
| 203 | g_value_init(&gvalue, G_TYPE_VALUE_ARRAY); |
| 204 | // TODO: Where is the array freed? |
| 205 | GValueArray* array = g_value_array_new(engine_names.size()); |
| 206 | std::vector<std::string> available_engines = GetAvailableEngines(); |
| 207 | for (std::vector<std::string>::const_iterator it = engine_names.begin() ; |
| 208 | it != engine_names.end() ; ++it) { |
| 209 | // We load the engines only if they are installed on the system. |
| 210 | if (std::find(available_engines.begin(), available_engines.end(), *it) != |
| 211 | available_engines.end()) { |
| 212 | GValue array_element = {0}; |
| 213 | g_value_init(&array_element, G_TYPE_STRING); |
| 214 | g_value_set_string(&array_element, it->c_str()); |
| 215 | g_value_array_append(array, &array_element); |
| 216 | ++nb_loaded_engines; |
| 217 | } |
| 218 | } |
| 219 | // If we made at least one change, then we override the current |
| 220 | // configuration. |
| 221 | if (nb_loaded_engines > 0) { |
| 222 | g_value_take_boxed(&gvalue, array); |
| 223 | IBusConfig* conf = ibus_bus_get_config(bus_); |
| 224 | ibus_config_set_value(conf, "general", "preload_engines", &gvalue); |
| 225 | } |
| 226 | g_value_unset(&gvalue); |
| 227 | } |
| 228 | return nb_loaded_engines; |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * Sets the engine designed by its name to be the global engine |