diff options
author | TingPing <tingping@tingping.se> | 2013-10-01 12:09:17 -0400 |
---|---|---|
committer | TingPing <tingping@tingping.se> | 2013-10-03 16:23:46 -0400 |
commit | f6dd61cb024d34df477db55edf8fc2f560a49480 (patch) | |
tree | 1a60d6efe3d19bee3d68a5ac1cdc0dc437a9856a /src/fe-gtk | |
parent | eb93130cd3a846650dfbb972194190e91564df02 (diff) |
Update to gtkspell3's iso code lookup
This no longer requires libxml so now there is no reason for this to be optional
Diffstat (limited to 'src/fe-gtk')
-rw-r--r-- | src/fe-gtk/setup.c | 6 | ||||
-rw-r--r-- | src/fe-gtk/sexy-iso-codes.c | 411 | ||||
-rw-r--r-- | src/fe-gtk/sexy-iso-codes.h | 54 | ||||
-rw-r--r-- | src/fe-gtk/sexy-spell-entry.c | 47 |
4 files changed, 232 insertions, 286 deletions
diff --git a/src/fe-gtk/setup.c b/src/fe-gtk/setup.c index 3dd09568..99fb9434 100644 --- a/src/fe-gtk/setup.c +++ b/src/fe-gtk/setup.c @@ -189,15 +189,13 @@ static const setting inputbox_settings[] = {ST_TOGGLE, N_("Use the Text box font and colors"), P_OFFINTNL(hex_gui_input_style),0,0,0}, {ST_TOGGLE, N_("Show nick box"), P_OFFINTNL(hex_gui_input_nick),0,0,1}, {ST_TOGGLE, N_("Show user mode icon in nick box"), P_OFFINTNL(hex_gui_input_icon),0,0,0}, -#ifdef USE_SPELL {ST_TOGGLE, N_("Spell checking"), P_OFFINTNL(hex_gui_input_spell),0,0,1}, {ST_ENTRY, N_("Dictionaries to use:"), P_OFFSETNL(hex_text_spell_langs),0,0,sizeof prefs.hex_text_spell_langs}, #ifdef WIN32 - {ST_LABEL, N_("Use language codes (as in \"share\\myspell\\dicts\").\nSeparate multiple entries with commas.")}, + {ST_LABEL, N_("Use language codes (as in \"%LOCALAPPDATA%\\enchant\\myspell\\dicts\").\nSeparate multiple entries with commas.")}, #else {ST_LABEL, N_("Use language codes. Separate multiple entries with commas.")}, #endif -#endif {ST_HEADER, N_("Nick Completion"),0,0,0}, {ST_ENTRY, N_("Nick completion suffix:"), P_OFFSETNL(hex_completion_suffix),0,0,sizeof prefs.hex_completion_suffix}, @@ -2021,13 +2019,11 @@ setup_apply_to_sess (session_gui *gui) else gtk_widget_hide (gui->button_box); -#ifdef USE_SPELL /* update active languages */ sexy_spell_entry_deactivate_language((SexySpellEntry *)gui->input_box,NULL); sexy_spell_entry_activate_default_languages((SexySpellEntry *)gui->input_box); sexy_spell_entry_set_checked ((SexySpellEntry *)gui->input_box, prefs.hex_gui_input_spell); -#endif } static void diff --git a/src/fe-gtk/sexy-iso-codes.c b/src/fe-gtk/sexy-iso-codes.c index 9dc20105..b93ffc2b 100644 --- a/src/fe-gtk/sexy-iso-codes.c +++ b/src/fe-gtk/sexy-iso-codes.c @@ -1,307 +1,230 @@ -/* - * Copyright (C) 2005 Nathan Fredrickson - * Borrowed from Galeon, renamed, and simplified to only use iso-codes with no - * fallback method. - * - * Copyright (C) 2004 Christian Persch - * Copyright (C) 2004 Crispin Flowerday - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA - */ - -#ifdef WIN32 -#include "../../config-win32.h" -#else -#include "../../config.h" -#endif +/* gtkspell - a spell-checking addon for GTK's TextView widget +* Copyright (c) 2013 Sandro Mani +* +* Based on gtkhtml-editor-spell-language.c code which is +* Copyright (C) 2008 Novell, Inc. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* 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., +* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ #include "sexy-iso-codes.h" - -#include <glib/gi18n.h> - +#include <libintl.h> #include <string.h> -#include <libxml/xmlreader.h> - -static GHashTable *iso_639_table = NULL; -static GHashTable *iso_3166_table = NULL; +#ifdef G_OS_WIN32 +#include "../../config-win32.h" +#else +#include "../../config.h" +#endif #define ISO_639_DOMAIN "iso_639" #define ISO_3166_DOMAIN "iso_3166" -#ifdef HAVE_ISO_CODES - -#define ISOCODESLOCALEDIR "/share/locale" +static GHashTable *iso_639_table = NULL; +static GHashTable *iso_3166_table = NULL; static void -read_iso_639_entry (xmlTextReaderPtr reader, - GHashTable *table) +iso_639_start_element (GMarkupParseContext *context, +const gchar *element_name, +const gchar **attribute_names, +const gchar **attribute_values, +gpointer data, +GError **error) { - xmlChar *code, *name; + GHashTable *hash_table = data; + const gchar *name = NULL; + const gchar *code = NULL; + int i; - code = xmlTextReaderGetAttribute (reader, (const xmlChar *) "iso_639_1_code"); - name = xmlTextReaderGetAttribute (reader, (const xmlChar *) "name"); + if (strcmp (element_name, "iso_639_entry") != 0) + return; - /* Get iso-639-2 code */ - if (code == NULL || code[0] == '\0') + for (i = 0; attribute_names[i] != NULL; i++) { - xmlFree (code); - /* FIXME: use the 2T or 2B code? */ - code = xmlTextReaderGetAttribute (reader, (const xmlChar *) "iso_639_2T_code"); + if (strcmp (attribute_names[i], "name") == 0) + name = attribute_values[i]; + else if (strcmp (attribute_names[i], "iso_639_1_code") == 0) + code = attribute_values[i]; } - if (code != NULL && code[0] != '\0' && name != NULL && name[0] != '\0') - { - g_hash_table_insert (table, code, name); - } - else - { - xmlFree (code); - xmlFree (name); - } + if (code != NULL && *code != '\0' && name != NULL && *name != '\0') + g_hash_table_insert (hash_table, g_strdup (code), + g_strdup (dgettext (ISO_639_DOMAIN, name))); } static void -read_iso_3166_entry (xmlTextReaderPtr reader, - GHashTable *table) +iso_3166_start_element (GMarkupParseContext *context, +const gchar *element_name, +const gchar **attribute_names, +const gchar **attribute_values, +gpointer data, +GError **error) { - xmlChar *code, *name; + GHashTable *hash_table = data; + const gchar *name = NULL; + const gchar *code = NULL; + int i; - code = xmlTextReaderGetAttribute (reader, (const xmlChar *) "alpha_2_code"); - name = xmlTextReaderGetAttribute (reader, (const xmlChar *) "name"); - - if (code != NULL && code[0] != '\0' && name != NULL && name[0] != '\0') - { - char *lcode; - - lcode = g_ascii_strdown ((char *) code, -1); - xmlFree (code); + if (strcmp (element_name, "iso_3166_entry") != 0) + return; - g_hash_table_insert (table, lcode, name); - } - else + for (i = 0; attribute_names[i] != NULL; i++) { - xmlFree (code); - xmlFree (name); + if (strcmp (attribute_names[i], "name") == 0) + name = attribute_values[i]; + else if (strcmp (attribute_names[i], "alpha_2_code") == 0) + code = attribute_values[i]; } + if (code != NULL && *code != '\0' && name != NULL && *name != '\0') + g_hash_table_insert (hash_table, g_strdup (code), + g_strdup (dgettext (ISO_3166_DOMAIN, name))); } -typedef enum -{ - STATE_START, - STATE_STOP, - STATE_ENTRIES, -} ParserState; - -static gboolean -load_iso_entries (int iso, - GFunc read_entry_func, - gpointer user_data) +static void +iso_codes_parse (const GMarkupParser *parser, +const gchar *basename, +GHashTable *hash_table) { - xmlTextReaderPtr reader; - ParserState state = STATE_START; - xmlChar iso_entries[32], iso_entry[32]; - char *filename; - int ret = -1; - -#ifdef WIN32 - filename = g_strdup_printf (".\\share\\xml\\iso-codes\\iso_%d.xml", iso); -#else - filename = g_strdup_printf ("/usr/share/xml/iso-codes/iso_%d.xml", iso); -#endif - reader = xmlNewTextReaderFilename (filename); - if (reader == NULL) goto out; + GMappedFile *mapped_file; + gchar *filename; + GError *error = NULL; - xmlStrPrintf (iso_entries, sizeof (iso_entries), - (xmlChar *)"iso_%d_entries", iso); - xmlStrPrintf (iso_entry, sizeof (iso_entry), - (xmlChar *)"iso_%d_entry", iso); - - ret = xmlTextReaderRead (reader); + filename = g_build_filename (ISO_CODES_PREFIX, "share", "xml", "iso-codes", + basename, NULL); + mapped_file = g_mapped_file_new (filename, FALSE, &error); + g_free (filename); - while (ret == 1) + if (mapped_file != NULL) { - const xmlChar *tag; - xmlReaderTypes type; - - tag = xmlTextReaderConstName (reader); - type = xmlTextReaderNodeType (reader); - - if (state == STATE_ENTRIES && - type == XML_READER_TYPE_ELEMENT && - xmlStrEqual (tag, iso_entry)) - { - read_entry_func (reader, user_data); - } - else if (state == STATE_START && - type == XML_READER_TYPE_ELEMENT && - xmlStrEqual (tag, iso_entries)) - { - state = STATE_ENTRIES; - } - else if (state == STATE_ENTRIES && - type == XML_READER_TYPE_END_ELEMENT && - xmlStrEqual (tag, iso_entries)) - { - state = STATE_STOP; - } - else if (type == XML_READER_TYPE_SIGNIFICANT_WHITESPACE || - type == XML_READER_TYPE_WHITESPACE || - type == XML_READER_TYPE_TEXT || - type == XML_READER_TYPE_COMMENT) - { - /* eat it */ - } - else - { - /* ignore it */ - } - - ret = xmlTextReaderRead (reader); + GMarkupParseContext *context; + const gchar *contents; + gsize length; + + context = g_markup_parse_context_new (parser, 0, hash_table, NULL); + contents = g_mapped_file_get_contents (mapped_file); + length = g_mapped_file_get_length (mapped_file); + g_markup_parse_context_parse (context, contents, length, &error); + g_markup_parse_context_free (context); + g_mapped_file_unref (mapped_file); } - xmlFreeTextReader (reader); - -out: - if (ret < 0 || state != STATE_STOP) + if (error != NULL) { - /* This is not critical, we will fallback to our own code */ - g_free (filename); - return FALSE; + g_warning ("%s: %s", basename, error->message); + g_error_free (error); } - - g_free (filename); - - return TRUE; } -#endif /* HAVE_ISO_CODES */ - - -static void -ensure_iso_codes_initialised (void) +/** +* codetable_init: +* +* Initializes the code table. +*/ +void +codetable_init (void) { - static gboolean initialised = FALSE; + GMarkupParser iso_639_parser = { + iso_639_start_element, NULL, NULL, NULL, NULL + }; - if (initialised == TRUE) - { - return; - } - initialised = TRUE; + GMarkupParser iso_3166_parser = { + iso_3166_start_element, NULL, NULL, NULL, NULL + }; -#if defined (ENABLE_NLS) && defined (HAVE_ISO_CODES) - bindtextdomain (ISO_639_DOMAIN, ISOCODESLOCALEDIR); + g_return_if_fail (iso_639_table == NULL); + g_return_if_fail (iso_3166_table == NULL); + +#ifdef ENABLE_NLS + bindtextdomain (ISO_639_DOMAIN, ISO_CODES_LOCALEDIR); bind_textdomain_codeset (ISO_639_DOMAIN, "UTF-8"); - bindtextdomain(ISO_3166_DOMAIN, ISOCODESLOCALEDIR); + bindtextdomain (ISO_3166_DOMAIN, ISO_CODES_LOCALEDIR); bind_textdomain_codeset (ISO_3166_DOMAIN, "UTF-8"); #endif iso_639_table = g_hash_table_new_full (g_str_hash, g_str_equal, - (GDestroyNotify) xmlFree, - (GDestroyNotify) xmlFree); - + (GDestroyNotify)g_free, (GDestroyNotify)g_free); iso_3166_table = g_hash_table_new_full (g_str_hash, g_str_equal, - (GDestroyNotify) g_free, - (GDestroyNotify) xmlFree); - -#ifdef HAVE_ISO_CODES - load_iso_entries (639, (GFunc) read_iso_639_entry, iso_639_table); - load_iso_entries (3166, (GFunc) read_iso_3166_entry, iso_3166_table); -#endif -} + (GDestroyNotify)g_free, (GDestroyNotify)g_free); + iso_codes_parse (&iso_639_parser, "iso_639.xml", iso_639_table); + iso_codes_parse (&iso_3166_parser, "iso_3166.xml", iso_3166_table); +} -static char * -get_iso_name_for_lang_code (const char *code) +/** +* codetable_free: +* +* Frees the code table. +*/ +void +codetable_free (void) { - char **str; - char *name = NULL; - const char *langname, *localename; - int len; - - str = g_strsplit (code, "_", -1); + g_return_if_fail (iso_639_table != NULL); + g_return_if_fail (iso_3166_table != NULL); - /* count the entries */ - for (len = 0; str[len]; len++ ) /* empty */; + g_hash_table_unref (iso_639_table); + g_hash_table_unref (iso_3166_table); - g_return_val_if_fail (len != 0, NULL); - - langname = (const char *) g_hash_table_lookup (iso_639_table, str[0]); - - if (len == 1 && langname != NULL) - { - name = g_strdup (dgettext (ISO_639_DOMAIN, langname)); - } - else if (len == 2 && langname != NULL) - { - localename = (const char *) g_hash_table_lookup (iso_3166_table, str[1]); - - if (localename != NULL) - { - /* translators: the first %s is the language name, and the - * second %s is the locale name. Example: - * "French (France) - * - * Also: The text before the "|" is context to help you decide on - * the correct translation. You MUST OMIT it in the translated string. - */ - name = g_strdup_printf (Q_("language|%s (%s)"), - dgettext (ISO_639_DOMAIN, langname), - dgettext (ISO_3166_DOMAIN, localename)); - } - else - { - name = g_strdup_printf (Q_("language|%s (%s)"), - dgettext (ISO_639_DOMAIN, langname), str[1]); - } - } - - g_strfreev (str); - - return name; + iso_639_table = NULL; + iso_3166_table = NULL; } /** - * gtkspell_iso_codes_lookup_name_for_code: - * @code: A language code, e.g. en_CA - * - * Looks up a name to display to the user for a language code, - * this might use the iso-codes package if support was compiled - * in, and it is available - * - * Returns: the UTF-8 string to display to the user, or NULL if - * a name for the code could not be found - */ -char * -gtkspell_iso_codes_lookup_name_for_code (const char *code) +* codetable_lookup: +* @language_code: A language code (i.e. "en_US") +* @language_name: (out) (transfer none): Pointer to the name of the language. +* This pointer is owned by the code table and must not be freed. +* @country_name: (out) (transfer none): Pointer to the name of the country. +* This pointer is owned by the code table and must not be freed. +* +* Looks up the language and country name for the specified language code. +* If no matching entries are found, language_name and country_name will +* simply contain the parts of the language code (i.e. "en" and "US"). +*/ +void +codetable_lookup (const gchar *language_code, const gchar **language_name, const gchar** country_name) { - char * lcode; - char * ret; + gchar **parts; - g_return_val_if_fail (code != NULL, NULL); + g_return_if_fail (iso_639_table != NULL); + g_return_if_fail (iso_3166_table != NULL); - ensure_iso_codes_initialised (); + /* Split language code into parts. */ + parts = g_strsplit (language_code, "_", 2); - lcode = g_ascii_strdown (code, -1); + g_return_if_fail (*parts != NULL); - ret = get_iso_name_for_lang_code (lcode); + *language_name = g_hash_table_lookup (iso_639_table, parts[0]); + if (*language_name == NULL) + { + g_hash_table_insert (iso_639_table, g_strdup (parts[0]), + g_strdup (parts[0])); + *language_name = g_hash_table_lookup (iso_639_table, parts[0]); + } - g_free (lcode); + if (g_strv_length (parts) == 2) + { + *country_name = g_hash_table_lookup (iso_3166_table, parts[1]); + if (*country_name == NULL) + { + g_hash_table_insert (iso_3166_table, g_strdup (parts[1]), + g_strdup (parts[1])); + *country_name = g_hash_table_lookup (iso_3166_table, parts[1]); + } + } - return ret; + g_strfreev (parts); } - diff --git a/src/fe-gtk/sexy-iso-codes.h b/src/fe-gtk/sexy-iso-codes.h index 771c8eb0..52949c72 100644 --- a/src/fe-gtk/sexy-iso-codes.h +++ b/src/fe-gtk/sexy-iso-codes.h @@ -1,35 +1,37 @@ -/* - * Copyright (C) 2005 Nathan Fredrickson - * Borrowed from Galeon, renamed, and simplified to only use iso-codes with no - * fallback method. - * - * Copyright (C) 2004 Christian Persch - * Copyright (C) 2004 Crispin Flowerday - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA - */ +/* gtkspell - a spell-checking addon for GTK's TextView widget +* Copyright (c) 2013 Sandro Mani +* +* Based on gtkhtml-editor-spell-language.c code which is +* Copyright (C) 2008 Novell, Inc. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* 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., +* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ -#ifndef GTKSPELL_ISO_CODES_H -#define GTKSPELL_ISO_CODES_H +#ifndef GTK_SPELL_CODETABLE_H +#define GTK_SPELL_CODETABLE_H #include <glib.h> G_BEGIN_DECLS -char * gtkspell_iso_codes_lookup_name_for_code (const char *code); +void codetable_init (void); +void codetable_free (void); +void codetable_lookup (const gchar *language_code, + const gchar **language_name, + const gchar **country_name); G_END_DECLS -#endif +#endif /* GTK_SPELL_CODETABLE_H */ \ No newline at end of file diff --git a/src/fe-gtk/sexy-spell-entry.c b/src/fe-gtk/sexy-spell-entry.c index 13705812..044a6610 100644 --- a/src/fe-gtk/sexy-spell-entry.c +++ b/src/fe-gtk/sexy-spell-entry.c @@ -577,11 +577,14 @@ build_spelling_menu(SexySpellEntry *entry, const gchar *word) for (li = entry->priv->dict_list; li; li = g_slist_next (li)) { dict = (struct EnchantDict *) li->data; lang = get_lang_from_dict(dict); - lang_name = gtkspell_iso_codes_lookup_name_for_code(lang); - if (lang_name) { + lang_name = sexy_spell_entry_get_language_name (entry, lang); + if (lang_name) + { mi = gtk_menu_item_new_with_label(lang_name); - g_free(lang_name); - } else { + g_free (lang_name); + } + else + { mi = gtk_menu_item_new_with_label(lang); } g_free(lang); @@ -621,11 +624,14 @@ build_spelling_menu(SexySpellEntry *entry, const gchar *word) for (li = entry->priv->dict_list; li; li = g_slist_next(li)) { dict = (struct EnchantDict *)li->data; lang = get_lang_from_dict(dict); - lang_name = gtkspell_iso_codes_lookup_name_for_code(lang); - if (lang_name) { + lang_name = sexy_spell_entry_get_language_name (entry, lang); + if (lang_name) + { submi = gtk_menu_item_new_with_label(lang_name); - g_free(lang_name); - } else { + g_free (lang_name); + } + else + { submi = gtk_menu_item_new_with_label(lang); } g_free(lang); @@ -699,6 +705,10 @@ sexy_spell_entry_init(SexySpellEntry *entry) if (have_enchant) sexy_spell_entry_activate_default_languages(entry); +#ifdef HAVE_ISO_CODES + codetable_init (); +#endif + entry->priv->attr_list = pango_attr_list_new(); entry->priv->checked = TRUE; @@ -743,6 +753,9 @@ sexy_spell_entry_finalize(GObject *obj) } g_free(entry->priv); +#ifdef HAVE_ISO_CODES + codetable_free (); +#endif if (G_OBJECT_CLASS(parent_class)->finalize) G_OBJECT_CLASS(parent_class)->finalize(obj); @@ -1273,9 +1286,21 @@ gchar * sexy_spell_entry_get_language_name(const SexySpellEntry *entry, const gchar *lang) { - if (have_enchant) - return gtkspell_iso_codes_lookup_name_for_code(lang); - return NULL; +#ifdef HAVE_ISO_CODES + const gchar *lang_name = ""; + const gchar *country_name = ""; + + g_return_val_if_fail (have_enchant, NULL); + + codetable_lookup (lang, &lang_name, &country_name); + + if (strlen (country_name) != 0) + return g_strdup_printf ("%s (%s)", lang_name, country_name); + else + return g_strdup_printf ("%s", lang_name); +#else + return g_strdup (lang); +#endif } /** |