Bug 601535 - Categories not translated in menus
authorMatthew Barnes <mbarnes@redhat.com>
Sat, 19 Jun 2010 00:17:53 +0000 (20:17 -0400)
committerMatthew Barnes <mbarnes@redhat.com>
Sat, 19 Jun 2010 00:19:13 +0000 (20:19 -0400)
libedataserver/e-categories.c

index 8a9bd68..4d8ee06 100644 (file)
  * Boston, MA 02110-1301, USA.
  */
 
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
+#include <config.h>
 #include <string.h>
 #include <libxml/parser.h>
-#include <glib.h>
 #include <glib/gi18n-lib.h>
 #include <gconf/gconf-client.h>
 #include "e-categories.h"
 #include "libedataserver-private.h"
 
 typedef struct {
-       gchar *category;
+       gchar *display_name;  /* localized category name */
+       gchar *clocale_name;  /* only for default categories */
        gchar *icon_file;
-       gboolean searchable;
+       gboolean is_default;
 } CategoryInfo;
 
 typedef struct {
@@ -129,9 +126,11 @@ build_categories_filename (void)
 static void
 free_category_info (CategoryInfo *cat_info)
 {
-       g_free (cat_info->category);
+       g_free (cat_info->display_name);
+       g_free (cat_info->clocale_name);
        g_free (cat_info->icon_file);
-       g_free (cat_info);
+
+       g_slice_free (CategoryInfo, cat_info);
 }
 
 static gchar *
@@ -172,9 +171,12 @@ hash_to_xml_string (gpointer key, gpointer value, gpointer user_data)
        GString *string = user_data;
        gchar *category;
 
-       g_string_append_len (string, "\t<category", 10);
+       g_string_append_len (string, "  <category", 10);
 
-       category = escape_string (cat_info->category);
+       if (cat_info->is_default)
+               category = escape_string (cat_info->clocale_name);
+       else
+               category = escape_string (cat_info->display_name);
        g_string_append_printf (string, " a=\"%s\"", category);
        g_free (category);
 
@@ -183,7 +185,7 @@ hash_to_xml_string (gpointer key, gpointer value, gpointer user_data)
                        string, " icon=\"%s\"", cat_info->icon_file);
 
        g_string_append_printf (
-               string, " searchable=\"%d\"", cat_info->searchable);
+               string, " default=\"%d\"", cat_info->is_default);
 
        g_string_append_len (string, "/>\n", 3);
 }
@@ -204,7 +206,7 @@ idle_saver_cb (gpointer user_data)
 
        g_debug ("Saving categories to \"%s\"", filename);
 
-       /* build the file contents */
+       /* Build the file contents. */
        buffer = g_string_new ("<categories>\n");
        g_hash_table_foreach (categories_table, hash_to_xml_string, buffer);
        g_string_append_len (buffer, "</categories>\n", 14);
@@ -241,6 +243,47 @@ save_categories (void)
                idle_id = g_idle_add (idle_saver_cb, NULL);
 }
 
+static void
+categories_add_full (const gchar *category,
+                     const gchar *icon_file,
+                     gboolean is_default)
+{
+       CategoryInfo *cat_info;
+       gchar *collation_key;
+
+       cat_info = g_slice_new (CategoryInfo);
+       if (is_default) {
+               const gchar *display_name;
+               display_name = g_dgettext (GETTEXT_PACKAGE, category);
+               cat_info->display_name = g_strdup (display_name);
+               cat_info->clocale_name = g_strdup (category);
+       } else {
+               cat_info->display_name = g_strdup (category);
+               cat_info->clocale_name = NULL;
+       }
+       cat_info->icon_file = g_strdup (icon_file);
+       cat_info->is_default = is_default;
+
+       collation_key = g_utf8_collate_key (cat_info->display_name, -1);
+       g_hash_table_insert (categories_table, collation_key, cat_info);
+
+       changed = TRUE;
+       save_categories ();
+}
+
+static CategoryInfo *
+categories_lookup (const gchar *category)
+{
+       CategoryInfo *cat_info;
+       gchar *collation_key;
+
+       collation_key = g_utf8_collate_key (category, -1);
+       cat_info = g_hash_table_lookup (categories_table, collation_key);
+       g_free (collation_key);
+
+       return cat_info;
+}
+
 static gint
 parse_categories (const gchar *contents, gsize length)
 {
@@ -262,25 +305,27 @@ parse_categories (const gchar *contents, gsize length)
        }
 
        for (node = node->xmlChildrenNode; node != NULL; node = node->next) {
-               xmlChar *category, *icon, *searchable;
+               xmlChar *category, *icon_file, *is_default;
 
                category = xmlGetProp (node, (xmlChar *) "a");
-               icon = xmlGetProp (node, (xmlChar *) "icon");
-               searchable = xmlGetProp (node, (xmlChar *) "searchable");
+               icon_file = xmlGetProp (node, (xmlChar *) "icon");
+               is_default = xmlGetProp (node, (xmlChar *) "default");
+
+               /* Default categories used to be called "searchable". */
+               if (is_default == NULL)
+                       is_default = xmlGetProp (
+                               node, (xmlChar *) "searchable");
 
                if (category != NULL) {
-                       e_categories_add (
-                               (gchar *) category,
-                               NULL,
-                               (gchar *) icon,
-                               (searchable != NULL) &&
-                               strcmp ((gchar *) searchable, "0") != 0);
+                       categories_add_full (
+                               (gchar *) category, (gchar *) icon_file,
+                               g_strcmp0 ((gchar *) is_default, "1") == 0);
                        n_added++;
                }
 
                xmlFree (category);
-               xmlFree (icon);
-               xmlFree (searchable);
+               xmlFree (icon_file);
+               xmlFree (is_default);
        }
 
        xmlFreeDoc (doc);
@@ -330,8 +375,8 @@ migrate_old_icon_file (gpointer key, gpointer value, gpointer user_data)
                return;
 
        /* We can't be sure where the old icon files were stored, but
-         * a good guess is (E_DATA_SERVER_IMAGESDIR "-2.x").  Convert
-         * any such paths to just E_DATA_SERVER_IMAGESDIR. */
+        * a good guess is (E_DATA_SERVER_IMAGESDIR "-2.x").  Convert
+        * any such paths to just E_DATA_SERVER_IMAGESDIR. */
        if (g_str_has_prefix (info->icon_file, E_DATA_SERVER_IMAGESDIR)) {
                basename = g_path_get_basename (info->icon_file);
                g_free (info->icon_file);
@@ -345,8 +390,8 @@ static gboolean
 migrate_old_categories (void)
 {
        /* Try migrating old category settings from GConf to the new
-         * category XML file.  If successful, unset the old GConf key
-         * so that this is a one-time-only operation. */
+        * category XML file.  If successful, unset the old GConf key
+        * so that this is a one-time-only operation. */
 
        const gchar *key = "/apps/evolution/general/category_master_list";
 
@@ -365,7 +410,7 @@ migrate_old_categories (void)
        if (n_added == 0)
                goto exit;
 
-       /* default icon files are now in an unversioned directory */
+       /* Default icon files are now in an unversioned directory. */
        g_hash_table_foreach (categories_table, migrate_old_icon_file, NULL);
 
        gconf_client_unset (client, key, NULL);
@@ -381,17 +426,18 @@ static void
 load_default_categories (void)
 {
        DefaultCategory *cat_info = default_categories;
-       gchar *icon_file = NULL;
 
-       /* Note: All default categories are searchable. */
        while (cat_info->category != NULL) {
+               gchar *icon_file = NULL;
+
                if (cat_info->icon_file != NULL)
-                       icon_file = g_build_filename (E_DATA_SERVER_IMAGESDIR, cat_info->icon_file, NULL);
-               e_categories_add (
-                       _ (cat_info->category),
-                       NULL, icon_file, TRUE);
+                       icon_file = g_build_filename (
+                               E_DATA_SERVER_IMAGESDIR,
+                               cat_info->icon_file, NULL);
+
+               categories_add_full (cat_info->category, icon_file, TRUE);
+
                g_free (icon_file);
-               icon_file = NULL;
                cat_info++;
        }
 }
@@ -430,8 +476,11 @@ initialize_categories (void)
 
        initialized = TRUE;
 
+       bindtextdomain (GETTEXT_PACKAGE, E_DATA_SERVER_LOCALEDIR);
+
        categories_table = g_hash_table_new_full (
-               g_str_hash, g_str_equal, g_free,
+               g_str_hash, g_str_equal,
+               (GDestroyNotify) g_free,
                (GDestroyNotify) free_category_info);
 
        listeners = g_object_new (e_changed_listener_get_type (), NULL);
@@ -457,14 +506,6 @@ initialize_categories (void)
        save_categories ();
 }
 
-static void
-add_hash_to_list (gpointer key, gpointer value, gpointer user_data)
-{
-       GList **list = user_data;
-
-       *list = g_list_prepend (*list, key);
-}
-
 /**
  * e_categories_get_list:
  *
@@ -477,12 +518,19 @@ add_hash_to_list (gpointer key, gpointer value, gpointer user_data)
 GList *
 e_categories_get_list (void)
 {
+       GHashTableIter iter;
        GList *list = NULL;
+       gpointer key, value;
 
        if (!initialized)
                initialize_categories ();
 
-       g_hash_table_foreach (categories_table, add_hash_to_list, &list);
+       g_hash_table_iter_init (&iter, categories_table);
+
+       while (g_hash_table_iter_next (&iter, &key, &value)) {
+               CategoryInfo *cat_info = value;
+               list = g_list_prepend (list, cat_info->display_name);
+       }
 
        return g_list_sort (list, (GCompareFunc) g_utf8_collate);
 }
@@ -498,25 +546,17 @@ e_categories_get_list (void)
  * configuration database.
  */
 void
-e_categories_add (const gchar *category, const gchar *unused, const gchar *icon_file, gboolean searchable)
+e_categories_add (const gchar *category,
+                  const gchar *unused,
+                  const gchar *icon_file,
+                  gboolean searchable)
 {
-       CategoryInfo *cat_info;
-
        g_return_if_fail (category != NULL);
 
        if (!initialized)
                initialize_categories ();
 
-       /* add the new category */
-       cat_info = g_new0 (CategoryInfo, 1);
-       cat_info->category = g_strdup (category);
-       cat_info->icon_file = g_strdup (icon_file);
-       cat_info->searchable = searchable;
-
-       g_hash_table_insert (categories_table, g_strdup (category), cat_info);
-
-       changed = TRUE;
-       save_categories ();
+       categories_add_full (category, icon_file, FALSE);
 }
 
 /**
@@ -555,7 +595,7 @@ e_categories_exist (const gchar *category)
        if (!initialized)
                initialize_categories ();
 
-       return (g_hash_table_lookup (categories_table, category) != NULL);
+       return (categories_lookup (category) != NULL);
 }
 
 /**
@@ -576,7 +616,7 @@ e_categories_get_icon_file_for (const gchar *category)
        if (!initialized)
                initialize_categories ();
 
-       cat_info = g_hash_table_lookup (categories_table, category);
+       cat_info = categories_lookup (category);
        if (cat_info == NULL)
                return NULL;
 
@@ -591,7 +631,8 @@ e_categories_get_icon_file_for (const gchar *category)
  * Sets the icon file associated with the given category.
  */
 void
-e_categories_set_icon_file_for (const gchar *category, const gchar *icon_file)
+e_categories_set_icon_file_for (const gchar *category,
+                                const gchar *icon_file)
 {
        CategoryInfo *cat_info;
 
@@ -600,11 +641,12 @@ e_categories_set_icon_file_for (const gchar *category, const gchar *icon_file)
        if (!initialized)
                initialize_categories ();
 
-       cat_info = g_hash_table_lookup (categories_table, category);
+       cat_info = categories_lookup (category);
        g_return_if_fail (cat_info != NULL);
 
        g_free (cat_info->icon_file);
        cat_info->icon_file = g_strdup (icon_file);
+
        changed = TRUE;
        save_categories ();
 }
@@ -627,11 +669,11 @@ e_categories_is_searchable (const gchar *category)
        if (!initialized)
                initialize_categories ();
 
-       cat_info = g_hash_table_lookup (categories_table, category);
+       cat_info = categories_lookup (category);
        if (cat_info == NULL)
                return FALSE;
 
-       return cat_info->searchable;
+       return cat_info->is_default;
 }
 
 /**