63b857
From 7f81445b3e6613d14f64253506a309095a37c8d7 Mon Sep 17 00:00:00 2001
8a795b
From: fujiwarat <takao.fujiwara1@gmail.com>
63b857
Date: Mon, 20 Jun 2011 19:04:42 +0900
8a795b
Subject: [PATCH] Reload preload engines until users customize the list.
8a795b
8a795b
The idea is, if users don't customize the preload_engines with ibus-setup,
8a795b
users would prefer to load the system default engines again by login.
8a795b
The gconf value 'preload_engine_mode' is
63b857
IBUS_PRELOAD_ENGINE_MODE_USER by default but set
63b857
IBUS_PRELOAD_ENGINE_MODE_LANG_RELATIVE for the initial login.
8a795b
If preload_engine_mode is IBUS_PRELOAD_ENGINE_MODE_LANG_RELATIVE,
8a795b
ibus-daemon loads the system preload engines by langs.
8a795b
If preload_engine_mode is IBUS_PRELOAD_ENGINE_MODE_USER,
8a795b
ibus-daemon do not update the gconf value preload_engines.
8a795b
On the other hand, if users enable the customized engine checkbutton
8a795b
on ibus-setup, ibus-setup sets 'preload_engine_mode' as
8a795b
IBUS_PRELOAD_ENGINE_MODE_USER and users can customize the value
8a795b
'preload_engines'.
8a795b
---
63b857
 bus/ibusimpl.c       |  402 +++++++++++++++++++++++++++++++++++++++-----------
63b857
 data/ibus.schemas.in |   13 ++
9d6bb4
 ibus/common.py       |    6 +
63b857
 setup/main.py        |   70 ++++++++-
9d6bb4
 setup/setup.ui       |   21 +++-
3b5789
 src/ibustypes.h      |   10 ++
63b857
 6 files changed, 427 insertions(+), 95 deletions(-)
8a795b
8a795b
diff --git a/bus/ibusimpl.c b/bus/ibusimpl.c
63b857
index a7ae52b..bb4b8ae 100644
8a795b
--- a/bus/ibusimpl.c
8a795b
+++ b/bus/ibusimpl.c
63b857
@@ -144,6 +144,9 @@ static void     bus_ibus_impl_set_previous_engine
8a795b
 static void     bus_ibus_impl_set_preload_engines
8a795b
                                                 (BusIBusImpl        *ibus,
8a795b
                                                  GVariant           *value);
8a795b
+static void     bus_ibus_impl_set_preload_engine_mode
8a795b
+                                                (BusIBusImpl        *ibus,
8a795b
+                                                 GVariant           *value);
8a795b
 static void     bus_ibus_impl_set_use_sys_layout
8a795b
                                                 (BusIBusImpl        *ibus,
8a795b
                                                  GVariant           *value);
63b857
@@ -285,6 +288,259 @@ _panel_destroy_cb (BusPanelProxy *panel,
8a795b
 }
8a795b
 
63b857
 static void
63b857
+_config_set_value_done (GObject      *object,
63b857
+                        GAsyncResult *res,
63b857
+                        gpointer      user_data)
63b857
+{
63b857
+    IBusConfig *config = (IBusConfig *) object;
63b857
+    GVariant *value = (GVariant *) user_data;
63b857
+    GError *error = NULL;
63b857
+
63b857
+    g_assert (IBUS_IS_CONFIG (config));
63b857
+
63b857
+    if (!ibus_config_set_value_async_finish (config, res, &error)) {
63b857
+        if (error) {
63b857
+            g_error_free (error);
63b857
+        }
63b857
+    }
63b857
+    g_variant_unref (value);
63b857
+}
63b857
+
9d6bb4
+#ifndef OS_CHROMEOS
3b5789
+static gint
3b5789
+_engine_desc_cmp (IBusEngineDesc *desc1,
3b5789
+                  IBusEngineDesc *desc2)
8a795b
+{
3b5789
+    return - ((gint) ibus_engine_desc_get_rank (desc1)) +
3b5789
+              ((gint) ibus_engine_desc_get_rank (desc2));
3b5789
+}
9d6bb4
+#endif
8a795b
+
9d6bb4
+#ifndef OS_CHROMEOS
3b5789
+static gint
3b5789
+_get_config_preload_engine_mode (BusIBusImpl *ibus)
3b5789
+{
3b5789
+    GVariant *variant = NULL;
63b857
+    gint preload_engine_mode = IBUS_PRELOAD_ENGINE_MODE_USER;
3b5789
+
3b5789
+    g_assert (BUS_IS_IBUS_IMPL (ibus));
3b5789
+
3b5789
+    if (ibus->config == NULL) {
3b5789
+        return preload_engine_mode;
8a795b
+    }
8a795b
+
3b5789
+    variant = ibus_config_get_value (ibus->config, "general",
63b857
+                                     "preload_engines");
63b857
+    if (variant == NULL) {
63b857
+        /* Set LANG_RELATIVE mode for the initial login */
63b857
+        preload_engine_mode = IBUS_PRELOAD_ENGINE_MODE_LANG_RELATIVE;
63b857
+        variant = g_variant_ref_sink (g_variant_new ("i", preload_engine_mode));
63b857
+        ibus_config_set_value_async (ibus->config, "general",
63b857
+                                     "preload_engine_mode", variant,
63b857
+                                     -1,
63b857
+                                     NULL,
63b857
+                                     _config_set_value_done,
63b857
+                                     variant);
63b857
+        return preload_engine_mode;
63b857
+    } else {
63b857
+        g_variant_unref (variant);
63b857
+    }
63b857
+
63b857
+    variant = ibus_config_get_value (ibus->config, "general",
3b5789
+                                     "preload_engine_mode");
3b5789
+    if (variant != NULL) {
3b5789
+        if (g_variant_classify (variant) == G_VARIANT_CLASS_INT32) {
3b5789
+            preload_engine_mode = g_variant_get_int32 (variant);
3b5789
+        }
3b5789
+        g_variant_unref (variant);
8a795b
+    }
8a795b
+
3b5789
+    return preload_engine_mode;
8a795b
+}
9d6bb4
+#endif
8a795b
+
63b857
+static int
63b857
+_compare_engine_list_value (GVariant *value_a, GVariant *value_b)
63b857
+{
63b857
+    GVariant *value;
63b857
+    GVariantIter iter;
63b857
+    const gchar *engine_name = NULL;
63b857
+    gchar *concat_engine_names;
63b857
+    gchar *concat_engine_names_a = NULL;
63b857
+    gchar *concat_engine_names_b = NULL;
63b857
+    int retval = 0;
63b857
+
63b857
+    value = value_a;
63b857
+    concat_engine_names = NULL;
63b857
+    if (value != NULL && g_variant_classify (value) == G_VARIANT_CLASS_ARRAY) {
63b857
+        g_variant_iter_init (&iter, value);
63b857
+        while (g_variant_iter_loop (&iter, "&s", &engine_name)) {
63b857
+            gchar *tmp = g_strdup_printf ("%s::%s",
63b857
+                                          concat_engine_names ? concat_engine_names : "",
63b857
+                                          engine_name ? engine_name : "");
63b857
+            g_free (concat_engine_names);
63b857
+            concat_engine_names = tmp;
63b857
+        }
63b857
+    }
63b857
+    concat_engine_names_a = concat_engine_names;
63b857
+
63b857
+    value = value_b;
63b857
+    concat_engine_names = NULL;
63b857
+    if (value != NULL && g_variant_classify (value) == G_VARIANT_CLASS_ARRAY) {
63b857
+        g_variant_iter_init (&iter, value);
63b857
+        while (g_variant_iter_loop (&iter, "&s", &engine_name)) {
63b857
+            gchar *tmp = g_strdup_printf ("%s::%s",
63b857
+                                          concat_engine_names ? concat_engine_names : "",
63b857
+                                          engine_name ? engine_name : "");
63b857
+            g_free (concat_engine_names);
63b857
+            concat_engine_names = tmp;
63b857
+        }
63b857
+    }
63b857
+    concat_engine_names_b = concat_engine_names;
63b857
+
63b857
+    retval = g_strcmp0 (concat_engine_names_a, concat_engine_names_b);
63b857
+    g_free (concat_engine_names_a);
63b857
+    g_free (concat_engine_names_b);
63b857
+    return retval;
63b857
+}
63b857
+
63b857
+static void
63b857
+_preload_engines_config_get_value_done (GObject      *object,
63b857
+                                        GAsyncResult *res,
63b857
+                                        gpointer      user_data)
63b857
+{
63b857
+    IBusConfig *config = (IBusConfig *) object;
63b857
+    GVariant *new_value = (GVariant *) user_data;
63b857
+    GVariant *value = NULL;
63b857
+    GError *error = NULL;
63b857
+
63b857
+    g_assert (IBUS_IS_CONFIG (config));
63b857
+
63b857
+    value = ibus_config_get_value_async_finish (config, res, &error);
63b857
+    if (error) {
63b857
+        g_error_free (error);
63b857
+    }
63b857
+    if (_compare_engine_list_value (value, new_value) != 0) {
63b857
+        ibus_config_set_value_async (config, "general",
63b857
+                                     "preload_engines", new_value,
63b857
+                                     -1,
63b857
+                                     NULL,
63b857
+                                     _config_set_value_done,
63b857
+                                     new_value);
63b857
+    } else if (new_value) {
63b857
+        g_variant_unref (new_value);
63b857
+    }
63b857
+    if (value) {
63b857
+        g_variant_unref (value);
63b857
+    }
63b857
+}
63b857
+
8a795b
+static void
3b5789
+_set_preload_engines (BusIBusImpl *ibus,
3b5789
+                      GVariant    *value)
3b5789
+{
3b5789
+    GList *engine_list = NULL;
3b5789
+
3b5789
+    g_assert (BUS_IS_IBUS_IMPL (ibus));
3b5789
+
3b5789
+    g_list_foreach (ibus->engine_list, (GFunc) g_object_unref, NULL);
3b5789
+    g_list_free (ibus->engine_list);
3b5789
+
3b5789
+    if (value != NULL && g_variant_classify (value) == G_VARIANT_CLASS_ARRAY) {
3b5789
+        GVariantIter iter;
3b5789
+        g_variant_iter_init (&iter, value);
3b5789
+        const gchar *engine_name = NULL;
3b5789
+        while (g_variant_iter_loop (&iter, "&s", &engine_name)) {
3b5789
+            IBusEngineDesc *engine = bus_registry_find_engine_by_name (ibus->registry, engine_name);
3b5789
+            if (engine == NULL || g_list_find (engine_list, engine) != NULL)
3b5789
+                continue;
3b5789
+            engine_list = g_list_append (engine_list, engine);
3b5789
+        }
3b5789
+
3b5789
+        if (engine_list != NULL &&
63b857
+            ibus->config != NULL) {
63b857
+            /* sync function will effects the startup performance.
63b857
+             * We'd always like to save the value so that ibus-setup
63b857
+             * get the updated engine list. */
63b857
+            ibus_config_get_value_async (ibus->config, "general",
63b857
+                                         "preload_engines",
63b857
+                                         -1,
63b857
+                                         NULL,
63b857
+                                         _preload_engines_config_get_value_done,
63b857
+                                         g_variant_ref_sink (value));
9d6bb4
+        } else {
9d6bb4
+            /* We don't update preload_engines with an empty string for safety.
9d6bb4
+             * Just unref the floating value. */
9d6bb4
+            g_variant_unref (value);
3b5789
+        }
9d6bb4
+    } else if (value != NULL) {
9d6bb4
+        g_variant_unref (value);
3b5789
+    }
3b5789
+
3b5789
+    g_list_foreach (engine_list, (GFunc) g_object_ref, NULL);
3b5789
+    ibus->engine_list = engine_list;
3b5789
+
3b5789
+    if (ibus->engine_list) {
3b5789
+        BusComponent *component = bus_component_from_engine_desc ((IBusEngineDesc *) ibus->engine_list->data);
3b5789
+        if (component && !bus_component_is_running (component)) {
3b5789
+            bus_component_start (component, g_verbose);
3b5789
+        }
3b5789
+    }
3b5789
+
48fb14
+    bus_ibus_impl_check_global_engine (ibus);
3b5789
+    bus_ibus_impl_update_engines_hotkey_profile (ibus);
3b5789
+}
3b5789
+
9d6bb4
+#ifndef OS_CHROMEOS
3b5789
+static void
3b5789
+_set_language_relative_preload_engines (BusIBusImpl *ibus)
3b5789
+{
3b5789
+    gchar *lang = NULL;
3b5789
+    gchar *p = NULL;
3b5789
+    GList *engines = NULL;
3b5789
+    GList *list;
3b5789
+    GVariantBuilder builder;
3b5789
+
3b5789
+    g_assert (BUS_IS_IBUS_IMPL (ibus));
3b5789
+
9d6bb4
+    /* The setlocale call first checks LC_ALL. If it's not available, checks
9d6bb4
+     * LC_CTYPE. If it's also not available, checks LANG. */
38194c
+    lang = g_strdup (setlocale (LC_CTYPE, NULL));
38194c
+    if (lang == NULL) {
38194c
+        return;
38194c
+    }
9d6bb4
+
3b5789
+    p = index (lang, '.');
3b5789
+    if (p) {
3b5789
+        *p = '\0';
3b5789
+    }
3b5789
+
3b5789
+    engines = bus_registry_get_engines_by_language (ibus->registry, lang);
3b5789
+    if (engines == NULL) {
3b5789
+        p = index (lang, '_');
3b5789
+        if (p) {
3b5789
+            *p = '\0';
3b5789
+            engines = bus_registry_get_engines_by_language (ibus->registry, lang);
3b5789
+        }
3b5789
+    }
3b5789
+    g_free (lang);
3b5789
+
3b5789
+    /* sort engines by rank */
3b5789
+    engines = g_list_sort (engines, (GCompareFunc) _engine_desc_cmp);
3b5789
+
3b5789
+    g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
3b5789
+    for (list = engines; list != NULL; list = list->next) {
3b5789
+        IBusEngineDesc *desc = (IBusEngineDesc *)list->data;
3b5789
+        /* ignore engines with rank <== 0 */
3b5789
+        if (ibus_engine_desc_get_rank (desc) > 0)
3b5789
+            g_variant_builder_add (&builder, "s", ibus_engine_desc_get_name (desc));
3b5789
+    }
3b5789
+    _set_preload_engines (ibus, g_variant_builder_end (&builder));
3b5789
+    g_list_free (engines);
3b5789
+}
9d6bb4
+#endif
3b5789
+
63b857
+static void
3b5789
 bus_ibus_impl_set_hotkey (BusIBusImpl *ibus,
3b5789
                           GQuark       hotkey,
63b857
                           GVariant    *value)
63b857
@@ -394,35 +650,50 @@ static void
3b5789
 bus_ibus_impl_set_preload_engines (BusIBusImpl *ibus,
3b5789
                                    GVariant    *value)
8a795b
 {
3b5789
-    GList *engine_list = NULL;
9d6bb4
-
069aa9
-    g_list_foreach (ibus->engine_list, (GFunc) g_object_unref, NULL);
069aa9
-    g_list_free (ibus->engine_list);
9d6bb4
+#ifndef OS_CHROMEOS
9d6bb4
+    gint preload_engine_mode = _get_config_preload_engine_mode (ibus);
9d6bb4
 
3b5789
-    if (value != NULL && g_variant_classify (value) == G_VARIANT_CLASS_ARRAY) {
3b5789
-        GVariantIter iter;
3b5789
-        g_variant_iter_init (&iter, value);
3b5789
-        const gchar *engine_name = NULL;
3b5789
-        while (g_variant_iter_loop (&iter, "&s", &engine_name)) {
3b5789
-            IBusEngineDesc *engine = bus_registry_find_engine_by_name (ibus->registry, engine_name);
3b5789
-            if (engine == NULL || g_list_find (engine_list, engine) != NULL)
3b5789
-                continue;
3b5789
-            engine_list = g_list_append (engine_list, engine);
3b5789
+    if (preload_engine_mode == IBUS_PRELOAD_ENGINE_MODE_USER) {
3b5789
+        if (value == NULL) {
3b5789
+            _set_language_relative_preload_engines (ibus);
3b5789
+        } else {
3b5789
+            _set_preload_engines (ibus, value);
3b5789
         }
3b5789
     }
9d6bb4
+#else
9d6bb4
+    _set_preload_engines (ibus, value);
9d6bb4
+#endif
3b5789
+}
3b5789
 
3b5789
-    g_list_foreach (engine_list, (GFunc) g_object_ref, NULL);
3b5789
-    ibus->engine_list = engine_list;
9d6bb4
+/**
9d6bb4
+ * bus_ibus_impl_set_preload_engine_mode:
9d6bb4
+ *
63b857
+ * A function to be called when "preload_engine_mode" config is updated.
9d6bb4
+ */
3b5789
+static void
3b5789
+bus_ibus_impl_set_preload_engine_mode (BusIBusImpl *ibus,
3b5789
+                                       GVariant    *value)
3b5789
+{
9d6bb4
+#ifndef OS_CHROMEOS
63b857
+    gint preload_engine_mode = IBUS_PRELOAD_ENGINE_MODE_USER;
3b5789
 
3b5789
-    if (ibus->engine_list) {
3b5789
-        BusComponent *component = bus_component_from_engine_desc ((IBusEngineDesc *) ibus->engine_list->data);
3b5789
-        if (component && !bus_component_is_running (component)) {
3b5789
-            bus_component_start (component, g_verbose);
3b5789
-        }
193527
+    /* bus_ibus_impl_reload_config() sets value = NULL.
193527
+     * bus_ibus_impl_reload_config() is always called when
193527
+     * RequestName signal is sent so it is good to get the gconf value
193527
+     * again when value == NULL.
193527
+     */
3b5789
+    if (value != NULL && g_variant_classify (value) == G_VARIANT_CLASS_INT32) {
3b5789
+        preload_engine_mode = g_variant_get_int32 (value);
193527
+    } else {
193527
+        preload_engine_mode = _get_config_preload_engine_mode (ibus);
3b5789
     }
3b5789
 
48fb14
-    bus_ibus_impl_check_global_engine (ibus);
3b5789
-    bus_ibus_impl_update_engines_hotkey_profile (ibus);
3b5789
+    if (preload_engine_mode == IBUS_PRELOAD_ENGINE_MODE_USER) {
3b5789
+        return;
3b5789
+    }
3b5789
+
3b5789
+    _set_language_relative_preload_engines (ibus);
9d6bb4
+#endif
8a795b
 }
8a795b
 
3b5789
 /**
63b857
@@ -503,89 +774,47 @@ bus_ibus_impl_set_use_global_engine (BusIBusImpl *ibus,
3b5789
     }
3b5789
 }
3b5789
 
0610dd
-#ifndef OS_CHROMEOS
3b5789
-static gint
3b5789
-_engine_desc_cmp (IBusEngineDesc *desc1,
3b5789
-                  IBusEngineDesc *desc2)
3b5789
-{
3b5789
-    return - ((gint) ibus_engine_desc_get_rank (desc1)) +
3b5789
-              ((gint) ibus_engine_desc_get_rank (desc2));
3b5789
-}
0610dd
-#endif
3b5789
-
3b5789
 /**
3b5789
  * bus_ibus_impl_set_default_preload_engines:
3b5789
  *
9d6bb4
- * If the "preload_engines" config variable is not set yet, set the default value which is determined based on a current locale.
3b5789
+ * bus_ibus_impl_set_default_preload_engines handles the gconf value
8a795b
+ * /desktop/ibus/general/preload_engines and preload_engine_mode.
8a795b
+ * The idea is, if users don't customize the preload_engines with ibus-setup,
8a795b
+ * users would prefer to load the system default engines again by login.
8a795b
+ * The gconf value 'preload_engine_mode' is
63b857
+ * IBUS_PRELOAD_ENGINE_MODE_USER by default but set
63b857
+ * IBUS_PRELOAD_ENGINE_MODE_LANG_RELATIVE for the initial login.
8a795b
+ * If preload_engine_mode is IBUS_PRELOAD_ENGINE_MODE_LANG_RELATIVE,
8a795b
+ * ibus-daemon loads the system preload engines by langs.
8a795b
+ * If preload_engine_mode is IBUS_PRELOAD_ENGINE_MODE_USER,
8a795b
+ * ibus-daemon do not update the gconf value preload_engines.
8a795b
+ * On the other hand, if users enable the customized engine checkbutton
8a795b
+ * on ibus-setup, ibus-setup sets 'preload_engine_mode' as
8a795b
+ * IBUS_PRELOAD_ENGINE_MODE_USER and users can customize the value
8a795b
+ * 'preload_engines'.
3b5789
  */
8a795b
 static void
3b5789
 bus_ibus_impl_set_default_preload_engines (BusIBusImpl *ibus)
8a795b
 {
0610dd
 #ifndef OS_CHROMEOS
3b5789
-    g_assert (BUS_IS_IBUS_IMPL (ibus));
3b5789
-
8a795b
     static gboolean done = FALSE;
63b857
+    gint preload_engine_mode = IBUS_PRELOAD_ENGINE_MODE_USER;
3b5789
+
3b5789
+    g_assert (BUS_IS_IBUS_IMPL (ibus));
8a795b
 
3b5789
     if (done || ibus->config == NULL) {
3b5789
         return;
3b5789
     }
8a795b
 
8a795b
-    GVariant *variant = ibus_config_get_value (ibus->config, "general", "preload_engines");
8a795b
-    if (variant != NULL) {
3b5789
+    preload_engine_mode = _get_config_preload_engine_mode (ibus);
8a795b
+
3b5789
+    if (preload_engine_mode == IBUS_PRELOAD_ENGINE_MODE_USER) {
3b5789
         done = TRUE;
3b5789
-        g_variant_unref (variant);
3b5789
         return;
8a795b
     }
8a795b
 
8a795b
     done = TRUE;
9d6bb4
-
9d6bb4
-    /* The setlocale call first checks LC_ALL. If it's not available, checks
9d6bb4
-     * LC_CTYPE. If it's also not available, checks LANG. */
9d6bb4
-    gchar *lang = g_strdup (setlocale (LC_CTYPE, NULL));
9d6bb4
-    if (lang == NULL) {
9d6bb4
-        return;
9d6bb4
-    }
9d6bb4
-
3b5789
-    gchar *p = index (lang, '.');
3b5789
-    if (p) {
3b5789
-        *p = '\0';
3b5789
-    }
3b5789
-
3b5789
-    GList *engines = bus_registry_get_engines_by_language (ibus->registry, lang);
3b5789
-    if (engines == NULL) {
3b5789
-        p = index (lang, '_');
3b5789
-        if (p) {
3b5789
-            *p = '\0';
3b5789
-            engines = bus_registry_get_engines_by_language (ibus->registry, lang);
3b5789
-        }
3b5789
-    }
3b5789
-    g_free (lang);
3b5789
-
3b5789
-    /* sort engines by rank */
3b5789
-    engines = g_list_sort (engines, (GCompareFunc) _engine_desc_cmp);
3b5789
-
3b5789
-    GVariantBuilder builder;
3b5789
-    g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
3b5789
-    GList *list;
3b5789
-    for (list = engines; list != NULL; list = list->next) {
3b5789
-        IBusEngineDesc *desc = (IBusEngineDesc *) list->data;
3b5789
-        /* ignore engines with rank <= 0 */
3b5789
-        if (ibus_engine_desc_get_rank (desc) > 0)
3b5789
-            g_variant_builder_add (&builder, "s", ibus_engine_desc_get_name (desc));
3b5789
-    }
069aa9
-
069aa9
-    GVariant *value = g_variant_builder_end (&builder);
069aa9
-    if (value != NULL) {
069aa9
-        if (g_variant_n_children (value) > 0) {
069aa9
-            ibus_config_set_value (ibus->config,
069aa9
-                                   "general", "preload_engines", value);
069aa9
-        } else {
069aa9
-            /* We don't update preload_engines with an empty string for safety.
069aa9
-             * Just unref the floating value. */
069aa9
-            g_variant_unref (value);
069aa9
-        }
069aa9
-    }
3b5789
-    g_list_free (engines);
3b5789
+    _set_language_relative_preload_engines (ibus);
0610dd
 #endif
3b5789
 }
3b5789
 
63b857
@@ -601,6 +830,7 @@ const static struct {
9d6bb4
     { "general/hotkey", "next_engine_in_menu",   bus_ibus_impl_set_next_engine_in_menu },
9d6bb4
     { "general/hotkey", "previous_engine",       bus_ibus_impl_set_previous_engine },
9d6bb4
     { "general", "preload_engines",              bus_ibus_impl_set_preload_engines },
63b857
+    { "general", "preload_engine_mode",          bus_ibus_impl_set_preload_engine_mode },
9d6bb4
     { "general", "use_system_keyboard_layout",   bus_ibus_impl_set_use_sys_layout },
9d6bb4
     { "general", "use_global_engine",            bus_ibus_impl_set_use_global_engine },
9d6bb4
     { "general", "embed_preedit_text",           bus_ibus_impl_set_embed_preedit_text },
8a795b
diff --git a/data/ibus.schemas.in b/data/ibus.schemas.in
63b857
index 7ca4899..d4334e1 100644
8a795b
--- a/data/ibus.schemas.in
8a795b
+++ b/data/ibus.schemas.in
8a795b
@@ -13,6 +13,19 @@
8a795b
       </locale>
8a795b
     </schema>
8a795b
     <schema>
8a795b
+      <key>/schemas/desktop/ibus/general/preload_engine_mode</key>
8a795b
+      <applyto>/desktop/ibus/general/preload_engine_mode</applyto>
8a795b
+      <owner>ibus</owner>
8a795b
+      <type>int</type>
63b857
+      <default>0</default>
8a795b
+      <locale name="C">
8a795b
+        <short>Preload engine mode</short>
8a795b
+           <long>Preload engines are loaded with this mode.
3b5789
+                 0 = user customized engines.
3b5789
+                 1 = language related engines.</long>
8a795b
+      </locale>
8a795b
+    </schema>
8a795b
+    <schema>
8a795b
       <key>/schemas/desktop/ibus/general/hotkey/trigger</key>
8a795b
       <applyto>/desktop/ibus/general/hotkey/trigger</applyto>
8a795b
       <owner>ibus</owner>
8a795b
diff --git a/ibus/common.py b/ibus/common.py
63b857
index 6483aae..127ed93 100644
8a795b
--- a/ibus/common.py
8a795b
+++ b/ibus/common.py
48fb14
@@ -40,6 +40,8 @@ __all__ = (
48fb14
         "BUS_REQUEST_NAME_REPLY_IN_QUEUE",
48fb14
         "BUS_REQUEST_NAME_REPLY_EXISTS",
48fb14
         "BUS_REQUEST_NAME_REPLY_ALREADY_OWNER",
3b5789
+        "PRELOAD_ENGINE_MODE_USER",
3b5789
+        "PRELOAD_ENGINE_MODE_LANG_RELATIVE",
3b5789
         "default_reply_handler",
3b5789
         "default_error_handler",
3b5789
         "DEFAULT_ASYNC_HANDLERS",
48fb14
@@ -150,6 +152,10 @@ BUS_REQUEST_NAME_REPLY_IN_QUEUE      = 2
48fb14
 BUS_REQUEST_NAME_REPLY_EXISTS        = 3
48fb14
 BUS_REQUEST_NAME_REPLY_ALREADY_OWNER = 4
8a795b
 
8a795b
+# define preload engine mode
3b5789
+PRELOAD_ENGINE_MODE_USER          = 0
3b5789
+PRELOAD_ENGINE_MODE_LANG_RELATIVE = 1
8a795b
+
8a795b
 def default_reply_handler( *args):
8a795b
     pass
8a795b
 
8a795b
diff --git a/setup/main.py b/setup/main.py
63b857
index 7f4a040..192fb88 100644
8a795b
--- a/setup/main.py
8a795b
+++ b/setup/main.py
63b857
@@ -92,6 +92,7 @@ class Setup(object):
63b857
         # keyboard shortcut
63b857
         # trigger
63b857
         self.__config = self.__bus.get_config()
63b857
+        self.__config.connect("value-changed", self.__config_value_changed_cb)
63b857
         shortcuts = self.__config.get_value(
63b857
                         "general/hotkey", "trigger",
63b857
                         ibus.CONFIG_GENERAL_SHORTCUT_TRIGGER_DEFAULT)
63b857
@@ -213,15 +214,22 @@ class Setup(object):
8a795b
         self.__checkbutton_use_global_engine.connect("toggled", self.__checkbutton_use_global_engine_toggled_cb)
8a795b
 
8a795b
         # init engine page
8a795b
+        preload_engine_mode = self.__config.get_value("general",
8a795b
+                                                      "preload_engine_mode",
3b5789
+                                                      ibus.common.PRELOAD_ENGINE_MODE_USER)
8a795b
+        button = self.__builder.get_object("checkbutton_preload_engine_mode")
8a795b
+        if preload_engine_mode == ibus.common.PRELOAD_ENGINE_MODE_USER:
8a795b
+            button.set_active(True)
8a795b
+            self.__builder.get_object("hbox_customize_active_input_methods").set_sensitive(True)
8a795b
+        else:
8a795b
+            button.set_active(False)
8a795b
+            self.__builder.get_object("hbox_customize_active_input_methods").set_sensitive(False)
8a795b
+        button.connect("toggled", self.__checkbutton_preload_engine_mode_toggled_cb)
8a795b
         self.__engines = self.__bus.list_engines()
8a795b
         self.__combobox = self.__builder.get_object("combobox_engines")
8a795b
         self.__combobox.set_engines(self.__engines)
8a795b
 
8a795b
-        tmp_dict = {}
8a795b
-        for e in self.__engines:
8a795b
-            tmp_dict[e.name] = e
3b5789
-        engine_names = self.__config.get_value("general", "preload_engines", [])
8a795b
-        engines = [tmp_dict[name] for name in engine_names if name in tmp_dict]
3b5789
+        engines = self.__bus.list_active_engines()
8a795b
 
8a795b
         self.__treeview = self.__builder.get_object("treeview_engines")
8a795b
         self.__treeview.set_engines(engines)
63b857
@@ -265,6 +273,26 @@ class Setup(object):
8a795b
             engine_names = map(lambda e: e.name, engines)
8a795b
             self.__config.set_list("general", "preload_engines", engine_names, "s")
8a795b
 
8a795b
+    def __get_engine_descs_from_names(self, engine_names):
8a795b
+        tmp_dict = {}
8a795b
+        for e in self.__engines:
8a795b
+            tmp_dict[e.name] = e
8a795b
+        engines = [tmp_dict[name] for name in engine_names if name in tmp_dict]
8a795b
+        return engines
8a795b
+
63b857
+    def __compare_descs(self, engines_a, engines_b):
63b857
+        engines = engines_a
63b857
+        concat_engine_names = ""
63b857
+        for engine in engines:
63b857
+            concat_engine_names = "%s::%s" % (concat_engine_names, engine.name)
63b857
+        concat_engine_names_a = concat_engine_names
63b857
+        engines = engines_b
63b857
+        concat_engine_names = ""
63b857
+        for engine in engines:
63b857
+            concat_engine_names = "%s::%s" % (concat_engine_names, engine.name)
63b857
+        concat_engine_names_b = concat_engine_names
63b857
+        return concat_engine_names_a == concat_engine_names_b
63b857
+
8a795b
     def __button_engine_add_cb(self, button):
8a795b
         engine = self.__combobox.get_active_engine()
8a795b
         self.__treeview.append_engine(engine)
63b857
@@ -276,6 +304,32 @@ class Setup(object):
8a795b
             about.run()
8a795b
             about.destroy()
8a795b
 
8a795b
+    def __checkbutton_preload_engine_mode_toggled_cb(self, button):
8a795b
+        if button.get_active():
8a795b
+            self.__config.set_value("general",
8a795b
+                                    "preload_engine_mode",
8a795b
+                                    ibus.common.PRELOAD_ENGINE_MODE_USER)
8a795b
+            self.__builder.get_object("hbox_customize_active_input_methods").set_sensitive(True)
3b5789
+            self.__treeview.notify("engines")
8a795b
+        else:
63b857
+            message = _("The list of your saved input methods will be " \
63b857
+                        "cleared immediately and the list will be " \
63b857
+                        "configured by the login language every time. " \
63b857
+                        "Do you agree with this?")
63b857
+            dlg = gtk.MessageDialog(type = gtk.MESSAGE_QUESTION,
63b857
+                    buttons = gtk.BUTTONS_YES_NO,
63b857
+                    message_format = message)
63b857
+            id = dlg.run()
63b857
+            dlg.destroy()
63b857
+            self.__flush_gtk_events()
63b857
+            if id != gtk.RESPONSE_YES:
63b857
+                button.set_active(True)
63b857
+                return
8a795b
+            self.__config.set_value("general",
8a795b
+                                    "preload_engine_mode",
8a795b
+                                    ibus.common.PRELOAD_ENGINE_MODE_LANG_RELATIVE)
8a795b
+            self.__builder.get_object("hbox_customize_active_input_methods").set_sensitive(False)
8a795b
+
8a795b
     def __init_bus(self):
8a795b
         try:
8a795b
             self.__bus = ibus.Bus()
63b857
@@ -466,7 +520,11 @@ class Setup(object):
63b857
         self.__config.set_value("general", "use_global_engine", value)
63b857
 
63b857
     def __config_value_changed_cb(self, bus, section, name, value):
63b857
-        pass
63b857
+        if section == 'general' and name == 'preload_engines':
63b857
+            engines = self.__get_engine_descs_from_names(value)
63b857
+            current_engines = self.__treeview.get_engines()
63b857
+            if self.__compare_descs(engines, current_engines) == False:
63b857
+                self.__treeview.set_engines(engines)
63b857
 
63b857
     def __config_reloaded_cb(self, bus):
63b857
         pass
8a795b
diff --git a/setup/setup.ui b/setup/setup.ui
193527
index f1e6d0b..562c091 100644
8a795b
--- a/setup/setup.ui
8a795b
+++ b/setup/setup.ui
9d6bb4
@@ -582,7 +582,22 @@
8a795b
                     <property name="visible">True</property>
8a795b
                     <property name="orientation">vertical</property>
8a795b
                     <child>
8a795b
-                      <object class="GtkHBox" id="hbox1">
8a795b
+                      <object class="GtkCheckButton" id="checkbutton_preload_engine_mode">
8a795b
+                        <property name="visible">True</property>
8a795b
+                        <property name="label" translatable="yes">Customize active input _methods</property>
8a795b
+                        <property name="use_underline">True</property>
8a795b
+                        <property name="can_focus">True</property>
8a795b
+                        <property name="receives_default">False</property>
8a795b
+                        <property name="tooltip_text" translatable="yes">Customize active input methods</property>
8a795b
+                        <property name="draw_indicator">True</property>
8a795b
+                      </object>
8a795b
+                      <packing>
8a795b
+                        <property name="expand">False</property>
8a795b
+                        <property name="position">0</property>
8a795b
+                      </packing>
8a795b
+                    </child>
8a795b
+                    <child>
8a795b
+                      <object class="GtkHBox" id="hbox_customize_active_input_methods">
8a795b
                         <property name="visible">True</property>
8a795b
                         <child>
8a795b
                           <object class="GtkAlignment" id="alignment6">
9d6bb4
@@ -733,7 +748,7 @@
8a795b
                         </child>
8a795b
                       </object>
8a795b
                       <packing>
8a795b
-                        <property name="position">0</property>
8a795b
+                        <property name="position">1</property>
8a795b
                       </packing>
8a795b
                     </child>
8a795b
                     <child>
9d6bb4
@@ -772,7 +787,7 @@ You may use up/down buttons to change it.</i></small></property>
8a795b
                       </object>
8a795b
                       <packing>
8a795b
                         <property name="expand">False</property>
8a795b
-                        <property name="position">1</property>
8a795b
+                        <property name="position">2</property>
8a795b
                       </packing>
8a795b
                     </child>
8a795b
                   </object>
8a795b
diff --git a/src/ibustypes.h b/src/ibustypes.h
63b857
index 6a31847..14b44fc 100644
8a795b
--- a/src/ibustypes.h
8a795b
+++ b/src/ibustypes.h
63b857
@@ -186,6 +186,16 @@ typedef enum {
63b857
 } IBusError;
8a795b
 
8a795b
 /**
8a795b
+ * IBusPreloadEngineMode:
8a795b
+ * @IBUS_PRELOAD_ENGINE_MODE_USER: user custimized engines
3b5789
+ * @IBUS_PRELOAD_ENGINE_MODE_LANG_RELATIVE: language related engines.
8a795b
+ */
8a795b
+typedef enum {
3b5789
+    IBUS_PRELOAD_ENGINE_MODE_USER          = 0,
3b5789
+    IBUS_PRELOAD_ENGINE_MODE_LANG_RELATIVE = 1,
8a795b
+} IBusPreloadEngineMode;
8a795b
+
8a795b
+/**
8a795b
  * IBusRectangle:
8a795b
  * @x: x coordinate.
8a795b
  * @y: y coordinate.
8a795b
-- 
63b857
1.7.4.4
8a795b