[gnome-terminal] Fix locale encoding option



commit 85ce77f25579423182316430338cbffffe64b585
Author: Christian Persch <chpe gnome org>
Date:   Tue Aug 10 18:00:15 2010 +0200

    Fix locale encoding option
    
    Bug #624489.

 src/terminal-app.c      |   25 ++++++++++---------------
 src/terminal-encoding.c |   39 ++++++++++++++++++++++-----------------
 src/terminal-encoding.h |    2 +-
 src/terminal-window.c   |   20 +++++++++++++-------
 4 files changed, 46 insertions(+), 40 deletions(-)
---
diff --git a/src/terminal-app.c b/src/terminal-app.c
index 691be12..4eca8fa 100644
--- a/src/terminal-app.c
+++ b/src/terminal-app.c
@@ -924,18 +924,17 @@ terminal_app_encoding_list_notify_cb (GConfClient *client,
   /* Mark all as non-active, then re-enable the active ones */
   g_hash_table_foreach (app->encodings, (GHFunc) encoding_mark_active, GUINT_TO_POINTER (FALSE));
 
-  /* First add the local encoding. */
-  if (!g_get_charset (&charset))
-    {
-      encoding = g_hash_table_lookup (app->encodings, charset);
-      if (encoding)
-        encoding->is_active = TRUE;
-    }
+  /* First add the locale's charset */
+  encoding = g_hash_table_lookup (app->encodings, "current");
+  g_assert (encoding);
+  if (terminal_encoding_is_valid (encoding))
+    encoding->is_active = TRUE;
 
-  /* Always ensure that UTF-8 is available. */
+  /* Also always make UTF-8 available */
   encoding = g_hash_table_lookup (app->encodings, "UTF-8");
   g_assert (encoding);
-  encoding->is_active = TRUE;
+  if (terminal_encoding_is_valid (encoding))
+    encoding->is_active = TRUE;
 
   val = gconf_entry_get_value (entry);
   if (val != NULL &&
@@ -953,19 +952,15 @@ terminal_app_encoding_list_notify_cb (GConfClient *client,
       if (!charset)
         continue;
 
-      /* We already handled the locale charset above */
-      if (strcmp (charset, "current") == 0)
-        continue; 
-
       encoding = g_hash_table_lookup (app->encodings, charset);
-      if (!encoding)
+      if (encoding == NULL)
         {
           encoding = terminal_encoding_new (charset,
                                             _("User Defined"),
                                             TRUE,
                                             TRUE /* scary! */);
           g_hash_table_insert (app->encodings,
-                               (gpointer) terminal_encoding_get_charset (encoding),
+                               (gpointer) terminal_encoding_get_id (encoding),
                                encoding);
         }
 
diff --git a/src/terminal-encoding.c b/src/terminal-encoding.c
index f70bbc6..e7f246c 100644
--- a/src/terminal-encoding.c
+++ b/src/terminal-encoding.c
@@ -153,7 +153,7 @@ terminal_encoding_new (const char *charset,
 
   encoding = g_slice_new (TerminalEncoding);
   encoding->refcount = 1;
-  encoding->charset = g_strdup (charset);
+  encoding->id = g_strdup (charset);
   encoding->name = g_strdup (display_name);
   encoding->valid = encoding->validity_checked = force_valid;
   encoding->is_custom = is_custom;
@@ -178,7 +178,7 @@ terminal_encoding_unref (TerminalEncoding *encoding)
     return;
 
   g_free (encoding->name);
-  g_free (encoding->charset);
+  g_free (encoding->id);
   g_slice_free (TerminalEncoding, encoding);
 }
 
@@ -186,8 +186,8 @@ const char *
 terminal_encoding_get_id (TerminalEncoding *encoding)
 {
   g_return_val_if_fail (encoding != NULL, NULL);
-  
-  return encoding->charset;
+
+  return encoding->id;
 }
 
 const char *
@@ -195,7 +195,15 @@ terminal_encoding_get_charset (TerminalEncoding *encoding)
 {
   g_return_val_if_fail (encoding != NULL, NULL);
 
-  return encoding->charset;
+  if (strcmp (encoding->id, "current") == 0)
+    {
+      const char *charset;
+
+      g_get_charset (&charset);
+      return charset;
+    }
+
+  return encoding->id;
 }
 
 gboolean
@@ -575,27 +583,24 @@ terminal_encodings_get_builtins (void)
 {
   GHashTable *encodings_hashtable;
   guint i;
-  const char *locale_charset = NULL;
+  TerminalEncoding *encoding;
 
   encodings_hashtable = g_hash_table_new_full (g_str_hash, g_str_equal,
                                                NULL,
                                                (GDestroyNotify) terminal_encoding_unref);
 
-  if (!g_get_charset (&locale_charset))
-    {
-      TerminalEncoding *encoding;
 
-      encoding = terminal_encoding_new (locale_charset,
-                                        _("Current Locale"),
-                                        FALSE,
-                                        TRUE);
-      g_hash_table_insert (encodings_hashtable, (gpointer) terminal_encoding_get_id (encoding), encoding);
-    }
+  /* Placeholder entry for the current locale's charset */
+  encoding = terminal_encoding_new ("current",
+                                    _("Current Locale"),
+                                    FALSE,
+                                    TRUE);
+  g_hash_table_insert (encodings_hashtable,
+                       (gpointer) terminal_encoding_get_id (encoding),
+                       encoding);
 
   for (i = 0; i < G_N_ELEMENTS (encodings); ++i)
     {
-      TerminalEncoding *encoding;
-
       encoding = terminal_encoding_new (encodings[i].charset,
                                         _(encodings[i].name),
                                         FALSE,
diff --git a/src/terminal-encoding.h b/src/terminal-encoding.h
index 63740c9..ddfe9ed 100644
--- a/src/terminal-encoding.h
+++ b/src/terminal-encoding.h
@@ -29,7 +29,7 @@
 typedef struct
 {
   int   refcount;
-  char *charset;
+  char *id;
   char *name;
   guint valid            : 1;
   guint validity_checked : 1;
diff --git a/src/terminal-window.c b/src/terminal-window.c
index 2f73e50..b3c3561 100644
--- a/src/terminal-window.c
+++ b/src/terminal-window.c
@@ -107,6 +107,8 @@ struct _TerminalWindowPrivate
 #define STOCK_NEW_WINDOW  "window-new"
 #define STOCK_NEW_TAB     "tab-new"
 
+#define ENCODING_DATA_KEY "encoding"
+
 #if 1
 /*
  * We don't want to enable content saving until vte supports it async.
@@ -743,7 +745,7 @@ terminal_set_encoding_callback (GtkToggleAction *action,
                                 TerminalWindow *window)
 {
   TerminalWindowPrivate *priv = window->priv;
-  const char *name, *charset;
+  TerminalEncoding *encoding;
   
   if (!gtk_toggle_action_get_active (action))
     return;
@@ -751,11 +753,11 @@ terminal_set_encoding_callback (GtkToggleAction *action,
   if (priv->active_screen == NULL)
     return;
 
-  name = gtk_action_get_name (GTK_ACTION (action));
-  g_assert (g_str_has_prefix (name, SET_ENCODING_ACTION_NAME_PREFIX));
-  charset = name + strlen (SET_ENCODING_ACTION_NAME_PREFIX);
+  encoding = g_object_get_data (G_OBJECT (action), ENCODING_DATA_KEY);
+  g_assert (encoding);
 
-  vte_terminal_set_encoding (VTE_TERMINAL (priv->active_screen), charset);
+  vte_terminal_set_encoding (VTE_TERMINAL (priv->active_screen),
+                             terminal_encoding_get_charset (encoding));
 }
 
 static void
@@ -791,7 +793,7 @@ terminal_window_update_encoding_menu (TerminalWindow *window)
   if (priv->active_screen)
     charset = vte_terminal_get_encoding (VTE_TERMINAL (priv->active_screen));
   else
-    charset = NULL;
+    charset = "current";
   
   encodings = terminal_app_get_active_encodings (terminal_app_get ());
 
@@ -817,12 +819,16 @@ terminal_window_update_encoding_menu (TerminalWindow *window)
       gtk_radio_action_set_group (encoding_action, group);
       group = gtk_radio_action_get_group (encoding_action);
 
-      if (charset && strcmp (terminal_encoding_get_charset (e), charset) == 0)
+      if (charset && strcmp (terminal_encoding_get_id (e), charset) == 0)
         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (encoding_action), TRUE);
 
       g_signal_connect (encoding_action, "toggled",
                         G_CALLBACK (terminal_set_encoding_callback), window);
 
+      g_object_set_data_full (G_OBJECT (encoding_action), ENCODING_DATA_KEY,
+                              terminal_encoding_ref (e),
+                              (GDestroyNotify) terminal_encoding_unref);
+
       gtk_action_group_add_action (action_group, GTK_ACTION (encoding_action));
       g_object_unref (encoding_action);
 



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