]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-spell.c
Updated Swedish translation
[empathy.git] / libempathy-gtk / empathy-spell.c
index 345dd79eef4077f33751e1ae7900933ff5bedec7..27f27f902f034f0b05a4a27db803a7b8db63f2c9 100644 (file)
  *
  * You should have received a copy of the GNU General Public
  * License along with this program; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA  02110-1301  USA
  *
  * Authors: Martyn Russell <martyn@imendio.com>
  *          Richard Hult <richard@imendio.com>
  */
 
 #include "config.h"
+#include "empathy-spell.h"
 
-#include <string.h>
-#include <stdlib.h>
-
-#include <glib/gi18n.h>
+#include <glib/gi18n-lib.h>
 
 #ifdef HAVE_ENCHANT
 #include <enchant.h>
 #endif
 
-#include <libempathy/empathy-debug.h>
-
-#include "empathy-spell.h"
-#include "empathy-conf.h"
+#include "empathy-gsettings.h"
 
-#define DEBUG_DOMAIN "Spell"
+#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
+#include "empathy-debug.h"
 
 #ifdef HAVE_ENCHANT
 
@@ -49,9 +45,11 @@ typedef struct {
 #define ISO_CODES_DATADIR    ISO_CODES_PREFIX "/share/xml/iso-codes"
 #define ISO_CODES_LOCALESDIR ISO_CODES_PREFIX "/share/locale"
 
+/* Language code (gchar *) -> language name (gchar *) */
 static GHashTable  *iso_code_names = NULL;
-static GList       *languages = NULL;
-static gboolean     empathy_conf_notify_inited = FALSE;
+/* Contains only _enabled_ languages
+ * Language code (gchar *) -> language (SpellLanguage *) */
+static GHashTable  *languages = NULL;
 
 static void
 spell_iso_codes_parse_start_tag (GMarkupParseContext  *ctx,
@@ -160,50 +158,54 @@ spell_iso_code_names_init (void)
 }
 
 static void
-spell_notify_languages_cb (EmpathyConf  *conf,
+spell_notify_languages_cb (GSettings   *gsettings,
                           const gchar *key,
                           gpointer     user_data)
 {
-       GList *l;
-
-       empathy_debug (DEBUG_DOMAIN, "Resetting languages due to config change");
+       DEBUG ("Resetting languages due to config change");
 
        /* We just reset the languages list. */
-       for (l = languages; l; l = l->next) {
-               SpellLanguage *lang;
-
-               lang = l->data;
-
-               enchant_broker_free_dict (lang->config, lang->speller);
-               enchant_broker_free (lang->config);
-               
-               g_slice_free (SpellLanguage, lang);
+       if (languages != NULL) {
+               g_hash_table_unref (languages);
+               languages = NULL;
        }
+}
+
+static void
+empathy_spell_free_language (SpellLanguage *lang)
+{
+       enchant_broker_free_dict (lang->config, lang->speller);
+       enchant_broker_free (lang->config);
 
-       g_list_free (languages);
-       languages = NULL;
+       g_slice_free (SpellLanguage, lang);
 }
 
 static void
 spell_setup_languages (void)
 {
+       static GSettings *gsettings = NULL;
        gchar  *str;
 
-       if (!empathy_conf_notify_inited) {
-               empathy_conf_notify_add (empathy_conf_get (),
-                                        EMPATHY_PREFS_CHAT_SPELL_CHECKER_LANGUAGES,
-                                        spell_notify_languages_cb, NULL);
+       if (gsettings == NULL) {
+               /* FIXME: this is never uninitialised */
+               gsettings = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
 
-               empathy_conf_notify_inited = TRUE;
+               g_signal_connect (gsettings,
+                       "changed::" EMPATHY_PREFS_CHAT_SPELL_CHECKER_LANGUAGES,
+                       G_CALLBACK (spell_notify_languages_cb), NULL);
        }
 
        if (languages) {
                return;
        }
 
-       if (empathy_conf_get_string (empathy_conf_get (),
-                                    EMPATHY_PREFS_CHAT_SPELL_CHECKER_LANGUAGES,
-                                    &str) && str) {
+       languages = g_hash_table_new_full (g_str_hash, g_str_equal,
+                       g_free, (GDestroyNotify) empathy_spell_free_language);
+
+       str = g_settings_get_string (gsettings,
+                       EMPATHY_PREFS_CHAT_SPELL_CHECKER_LANGUAGES);
+
+       if (str != NULL) {
                gchar **strv;
                gint    i;
 
@@ -213,14 +215,21 @@ spell_setup_languages (void)
                while (strv && strv[i]) {
                        SpellLanguage *lang;
 
-                       empathy_debug (DEBUG_DOMAIN, "Setting up language:'%s'", strv[i]);
+                       DEBUG ("Setting up language:'%s'", strv[i]);
 
                        lang = g_slice_new0 (SpellLanguage);
 
                        lang->config = enchant_broker_init ();
                        lang->speller = enchant_broker_request_dict (lang->config, strv[i]);
 
-                       languages = g_list_append (languages, lang);
+                       if (lang->speller == NULL) {
+                               DEBUG ("language '%s' has no valid dict", strv[i]);
+                       } else {
+                               g_hash_table_insert (languages,
+                                                    g_strdup (strv[i]),
+                                                    lang);
+                       }
+
                        i++;
                }
 
@@ -232,8 +241,8 @@ spell_setup_languages (void)
        }
 }
 
-const char *
-empathy_spell_get_language_name (const char *code)
+const gchar *
+empathy_spell_get_language_name (const gchar *code)
 {
        const gchar *name;
 
@@ -252,14 +261,14 @@ empathy_spell_get_language_name (const char *code)
 }
 
 static void
-enumerate_dicts (const char * const lang_tag,
-                 const char * const provider_name,
-                 const char * const provider_desc,
-                 const char * const provider_file,
-                void * user_data)
+enumerate_dicts (const gchar * const lang_tag,
+                 const gchar * const provider_name,
+                 const gchar * const provider_desc,
+                 const gchar * const provider_file,
+                gpointer            user_data)
 {
        GList **list = user_data;
-       char *lang = g_strdup(lang_tag);
+       gchar  *lang = g_strdup (lang_tag);
 
        if (strchr (lang, '_')) {
                /* cut country part out of language */
@@ -272,10 +281,9 @@ enumerate_dicts (const char * const lang_tag,
                return;
        }
 
-       *list = g_list_append (*list, g_strdup(lang));
+       *list = g_list_append (*list, lang);
 }
 
-
 GList *
 empathy_spell_get_language_codes (void)
 {
@@ -289,6 +297,13 @@ empathy_spell_get_language_codes (void)
        return list_langs;
 }
 
+GList *
+empathy_spell_get_enabled_language_codes (void)
+{
+       spell_setup_languages ();
+       return g_hash_table_get_keys (languages);
+}
+
 void
 empathy_spell_free_language_codes (GList *codes)
 {
@@ -299,19 +314,19 @@ empathy_spell_free_language_codes (GList *codes)
 gboolean
 empathy_spell_check (const gchar *word)
 {
-       int          enchant_result = 1;
+       gint         enchant_result = 1;
        const gchar *p;
        gboolean     digit;
        gunichar     c;
        gint         len;
-       GList       *l;
+       GHashTableIter iter;
+       SpellLanguage  *lang;
 
        g_return_val_if_fail (word != NULL, FALSE);
 
        spell_setup_languages ();
 
        if (!languages) {
-               empathy_debug (DEBUG_DOMAIN, "No languages to check against");
                return TRUE;
        }
 
@@ -323,16 +338,13 @@ empathy_spell_check (const gchar *word)
 
        if (digit) {
                /* We don't spell check digits. */
-               empathy_debug (DEBUG_DOMAIN, "Not spell checking word:'%s', it is all digits", word);
+               DEBUG ("Not spell checking word:'%s', it is all digits", word);
                return TRUE;
        }
 
        len = strlen (word);
-       for (l = languages; l; l = l->next) {
-               SpellLanguage  *lang;
-
-               lang = l->data;
-
+       g_hash_table_iter_init (&iter, languages);
+       while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &lang)) {
                enchant_result = enchant_dict_check (lang->speller, word, len);
 
                if (enchant_result == 0) {
@@ -344,33 +356,40 @@ empathy_spell_check (const gchar *word)
 }
 
 GList *
-empathy_spell_get_suggestions (const gchar *word)
+empathy_spell_get_suggestions (const gchar *code,
+                              const gchar *word)
 {
-       gint                     len;
-       GList *l1;
+       gint   len;
        GList *suggestion_list = NULL;
+       SpellLanguage *lang;
+       gchar **suggestions;
+       gsize   i, number_of_suggestions;
 
+       g_return_val_if_fail (code != NULL, NULL);
        g_return_val_if_fail (word != NULL, NULL);
 
        spell_setup_languages ();
 
+       if (!languages) {
+               return NULL;
+       }
+
        len = strlen (word);
 
-       for (l1 = languages; l1; l1 = l1->next) {
-               SpellLanguage *lang;
-               char    **suggestions;
-               size_t    i, number_of_suggestions;
+       lang = g_hash_table_lookup (languages, code);
+       if (!lang) {
+               return NULL;
+       }
 
-               lang = l1->data;
+       suggestions = enchant_dict_suggest (lang->speller, word, len,
+                                           &number_of_suggestions);
 
-               suggestions = enchant_dict_suggest (lang->speller, word, len,
-                               &number_of_suggestions);
-               
-               for (i = 0; i < number_of_suggestions; i++) {
-                       suggestion_list = g_list_append (suggestion_list,
-                                                        g_strdup(suggestions[i]));
-               }
+       for (i = 0; i < number_of_suggestions; i++) {
+               suggestion_list = g_list_append (suggestion_list,
+                                                g_strdup (suggestions[i]));
+       }
 
+       if (suggestions) {
                enchant_dict_free_string_list (lang->speller, suggestions);
        }
 
@@ -381,13 +400,33 @@ gboolean
 empathy_spell_supported (void)
 {
        if (g_getenv ("EMPATHY_SPELL_DISABLED")) {
-               empathy_debug (DEBUG_DOMAIN, "EMPATHY_SPELL_DISABLE env variable defined");
+               DEBUG ("EMPATHY_SPELL_DISABLE env variable defined");
                return FALSE;
        }
 
        return TRUE;
 }
 
+void
+empathy_spell_add_to_dictionary (const gchar *code,
+                                const gchar *word)
+{
+       SpellLanguage *lang;
+
+       g_return_if_fail (code != NULL);
+       g_return_if_fail (word != NULL);
+
+       spell_setup_languages ();
+       if (languages == NULL)
+               return;
+
+       lang = g_hash_table_lookup (languages, code);
+       if (lang == NULL)
+               return;
+
+       enchant_dict_add_to_pwl (lang->speller, word, strlen (word));
+}
+
 #else /* not HAVE_ENCHANT */
 
 gboolean
@@ -397,9 +436,10 @@ empathy_spell_supported (void)
 }
 
 GList *
-empathy_spell_get_suggestions (const gchar *word)
+empathy_spell_get_suggestions (const gchar *code,
+                               const gchar *word)
 {
-       empathy_debug (DEBUG_DOMAIN, "Support disabled, could not get suggestions");
+       DEBUG ("Support disabled, could not get suggestions");
 
        return NULL;
 }
@@ -407,15 +447,15 @@ empathy_spell_get_suggestions (const gchar *word)
 gboolean
 empathy_spell_check (const gchar *word)
 {
-       empathy_debug (DEBUG_DOMAIN, "Support disabled, could not check spelling");
+       DEBUG ("Support disabled, could not check spelling");
 
        return TRUE;
 }
 
-const char *
-empathy_spell_get_language_name (const char *lang)
+const gchar *
+empathy_spell_get_language_name (const gchar *lang)
 {
-       empathy_debug (DEBUG_DOMAIN, "Support disabled, could not get language name");
+       DEBUG ("Support disabled, could not get language name");
 
        return NULL;
 }
@@ -423,7 +463,7 @@ empathy_spell_get_language_name (const char *lang)
 GList *
 empathy_spell_get_language_codes (void)
 {
-       empathy_debug (DEBUG_DOMAIN, "Support disabled, could not get language codes");
+       DEBUG ("Support disabled, could not get language codes");
 
        return NULL;
 }
@@ -433,6 +473,21 @@ empathy_spell_free_language_codes (GList *codes)
 {
 }
 
+void
+empathy_spell_add_to_dictionary (const gchar *code,
+                                const gchar *word)
+{
+       DEBUG ("Support disabled, could not expand the dictionary");
+}
+
+GList *
+empathy_spell_get_enabled_language_codes (void)
+{
+       DEBUG ("Support disabled, could not get enabled language codes");
+
+       return NULL;
+}
+
 #endif /* HAVE_ENCHANT */