[evolution/wip/webkit2] Bug 748246 - Load only used spell-checker dictionaries



commit b21d6d08be2b939718e34e8ee78966c440e158f0
Author: Milan Crha <mcrha redhat com>
Date:   Thu Apr 23 14:11:39 2015 +0200

    Bug 748246 - Load only used spell-checker dictionaries

 e-util/e-spell-checker.c    |   56 +++++++++++++++++++++++++++++-------------
 e-util/e-spell-dictionary.c |   26 ++++++++++++++++++++
 e-util/e-spell-dictionary.h |    3 ++
 3 files changed, 67 insertions(+), 18 deletions(-)
---
diff --git a/e-util/e-spell-checker.c b/e-util/e-spell-checker.c
index 3655248..c8b3a9e 100644
--- a/e-util/e-spell-checker.c
+++ b/e-util/e-spell-checker.c
@@ -73,6 +73,7 @@ G_DEFINE_TYPE_EXTENDED (
  * have to be freed through enchant_broker_free_dict()
  * and we also own the EnchantBroker. */
 static GHashTable *global_enchant_dicts;
+static GHashTable *global_language_tags; /* gchar * ~> NULL */
 static EnchantBroker *global_broker;
 G_LOCK_DEFINE_STATIC (global_memory);
 
@@ -84,7 +85,8 @@ spell_checker_enchant_dicts_foreach_cb (gpointer key,
        EnchantDict *enchant_dict = value;
        EnchantBroker *enchant_broker = user_data;
 
-       enchant_broker_free_dict (enchant_broker, enchant_dict);
+       if (enchant_dict)
+               enchant_broker_free_dict (enchant_broker, enchant_dict);
 
        return TRUE;
 }
@@ -450,39 +452,32 @@ e_spell_checker_new (void)
 }
 
 static void
-list_enchant_dicts (const gchar * const lang_tag,
+list_enchant_dicts (const gchar * const language_tag,
                     const gchar * const provider_name,
                     const gchar * const provider_desc,
                     const gchar * const provider_file,
                     gpointer user_data)
 {
-       EnchantBroker *broker = user_data;
-       EnchantDict *enchant_dict;
-
-       enchant_dict = enchant_broker_request_dict (broker, lang_tag);
-       if (enchant_dict != NULL) {
-               g_hash_table_insert (
-                       global_enchant_dicts,
-                       g_strdup (lang_tag), enchant_dict);
-       }
+       g_hash_table_insert (
+               global_language_tags,
+               g_strdup (language_tag), NULL);
 }
 
 static void
-copy_enchant_dicts (gpointer pcode,
-                   gpointer pdict,
+copy_enchant_dicts (gpointer planguage_tag,
+                   gpointer punused,
                    gpointer user_data)
 {
-       EnchantDict *enchant_dict = pdict;
        ESpellChecker *checker = user_data;
 
-       if (enchant_dict) {
+       if (planguage_tag) {
                ESpellDictionary *dictionary;
                const gchar *code;
 
                /* Note that we retain ownership of the EnchantDict.
                 * Since EnchantDict is not reference counted, we're
                 * merely loaning the pointer to ESpellDictionary. */
-               dictionary = e_spell_dictionary_new (checker, enchant_dict);
+               dictionary = e_spell_dictionary_new_bare (checker, planguage_tag);
                code = e_spell_dictionary_get_code (dictionary);
 
                g_hash_table_insert (
@@ -503,6 +498,9 @@ e_spell_checker_init_global_memory (void)
                        (GEqualFunc) g_str_equal,
                        (GDestroyNotify) g_free,
                        (GDestroyNotify) NULL);
+               global_language_tags = g_hash_table_new_full (
+                       g_str_hash, g_str_equal,
+                       g_free, NULL);
 
                enchant_broker_list_dicts (
                        global_broker,
@@ -538,6 +536,11 @@ e_spell_checker_free_global_memory (void)
                global_broker = NULL;
        }
 
+       if (global_language_tags) {
+               g_hash_table_destroy (global_language_tags);
+               global_language_tags = NULL;
+       }
+
        G_UNLOCK (global_memory);
 }
 
@@ -561,7 +564,7 @@ e_spell_checker_list_available_dicts (ESpellChecker *checker)
 
        if (g_hash_table_size (checker->priv->dictionaries_cache) == 0) {
                e_spell_checker_init_global_memory ();
-               g_hash_table_foreach (global_enchant_dicts, copy_enchant_dicts, checker);
+               g_hash_table_foreach (global_language_tags, copy_enchant_dicts, checker);
        }
 
        list = g_hash_table_get_values (checker->priv->dictionaries_cache);
@@ -622,12 +625,29 @@ EnchantDict *
 e_spell_checker_get_enchant_dict (ESpellChecker *checker,
                                   const gchar *language_code)
 {
+       EnchantDict *dict;
+
        g_return_val_if_fail (E_IS_SPELL_CHECKER (checker), NULL);
        g_return_val_if_fail (language_code != NULL, NULL);
 
        e_spell_checker_init_global_memory ();
 
-       return g_hash_table_lookup (global_enchant_dicts, language_code);
+       G_LOCK (global_memory);
+
+       dict = g_hash_table_lookup (global_enchant_dicts, language_code);
+       if (((gpointer) dict) == GINT_TO_POINTER (1)) {
+               dict = NULL;
+       } else if (!dict) {
+               dict = enchant_broker_request_dict (global_broker, language_code);
+               if (dict)
+                       g_hash_table_insert (global_enchant_dicts, g_strdup (language_code), dict);
+               else
+                       g_hash_table_insert (global_enchant_dicts, g_strdup (language_code), GINT_TO_POINTER 
(1));
+       }
+
+       G_UNLOCK (global_memory);
+
+       return dict;
 }
 
 gboolean
diff --git a/e-util/e-spell-dictionary.c b/e-util/e-spell-dictionary.c
index 12f2b6e..6b221ab 100644
--- a/e-util/e-spell-dictionary.c
+++ b/e-util/e-spell-dictionary.c
@@ -457,6 +457,32 @@ e_spell_dictionary_new (ESpellChecker *spell_checker,
        return dictionary;
 }
 
+ESpellDictionary *
+e_spell_dictionary_new_bare (ESpellChecker *spell_checker,
+                            const gchar *language_tag)
+{
+       ESpellDictionary *dictionary;
+       struct _enchant_dict_description_data descr_data;
+
+       g_return_val_if_fail (E_IS_SPELL_CHECKER (spell_checker), NULL);
+       g_return_val_if_fail (language_tag != NULL, NULL);
+
+       dictionary = g_object_new (
+               E_TYPE_SPELL_DICTIONARY,
+               "spell-checker", spell_checker, NULL);
+
+       descr_data.language_tag = NULL;
+       descr_data.dict_name = NULL;
+
+       describe_dictionary (language_tag, NULL, NULL, NULL, &descr_data);
+
+       dictionary->priv->code = descr_data.language_tag;
+       dictionary->priv->name = descr_data.dict_name;
+       dictionary->priv->collate_key = g_utf8_collate_key (descr_data.dict_name, -1);
+
+       return dictionary;
+}
+
 /**
  * e_spell_dictionary_hash:
  * @dictionary: an #ESpellDictionary
diff --git a/e-util/e-spell-dictionary.h b/e-util/e-spell-dictionary.h
index f181026..97cec2c 100644
--- a/e-util/e-spell-dictionary.h
+++ b/e-util/e-spell-dictionary.h
@@ -67,6 +67,9 @@ GType         e_spell_dictionary_get_type     (void) G_GNUC_CONST;
 ESpellDictionary *
                e_spell_dictionary_new          (struct _ESpellChecker *spell_checker,
                                                 EnchantDict *enchant_dict);
+ESpellDictionary *
+               e_spell_dictionary_new_bare     (struct _ESpellChecker *spell_checker,
+                                                const gchar *language_tag);
 guint          e_spell_dictionary_hash         (ESpellDictionary *dictionary);
 gboolean       e_spell_dictionary_equal        (ESpellDictionary *dictionary1,
                                                 ESpellDictionary *dictionary2);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]