[evolution] Add "show-headers" settings key.



commit 3e8b6ddf8d9e090e0769faede1d68337e240e58d
Author: Matthew Barnes <mbarnes redhat com>
Date:   Wed Jun 5 09:24:02 2013 -0400

    Add "show-headers" settings key.
    
    Replaces the "headers" key.  Whereas "headers" is an array of XML
    blobs, the "show-headers" key is an array of string/boolean pairs.
    
    Also update the appropriate places to get/set the new key.

 data/org.gnome.evolution.mail.gschema.xml.in |   11 +-
 modules/mail/em-mailer-prefs.c               |  187 ++++++++++++++------------
 modules/settings/e-settings-deprecated.c     |  118 ++++++++++++++++
 modules/settings/e-settings-mail-formatter.c |   27 ++--
 4 files changed, 243 insertions(+), 100 deletions(-)
---
diff --git a/data/org.gnome.evolution.mail.gschema.xml.in b/data/org.gnome.evolution.mail.gschema.xml.in
index 71fff62..249f2d8 100644
--- a/data/org.gnome.evolution.mail.gschema.xml.in
+++ b/data/org.gnome.evolution.mail.gschema.xml.in
@@ -231,10 +231,10 @@
       <_summary>Show all message headers</_summary>
       <_description>Show all the headers when viewing a messages.</_description>
     </key>
-    <key name="headers" type="as">
+    <key name="show-headers" type="a(sb)">
       <default>[]</default>
-      <_summary>List of custom headers and whether they are enabled.</_summary>
-      <_description>This key should contain a list of XML structures specifying custom headers, and whether 
they are to be displayed. The format of the XML structure is &lt;header enabled&gt; - set enabled if the 
header is to be displayed in the mail view.</_description>
+      <_summary>List of headers to show when viewing a message.</_summary>
+      <_description>Each header is represented as a pair: the header name, and a boolean indicating whether 
the header is enabled.  Disabled headers are not shown when viewing a message, but are still listed in 
Preferences.</_description>
     </key>
     <key name="show-sender-photo" type="b">
       <default>false</default>
@@ -566,6 +566,11 @@
       <_summary>(Deprecated) Default reply style</_summary>
       <_description>This key was deprecated in version 3.10 and should no longer be used.  Use 
"reply-style-name" instead.</_description>
     </key>
+    <key name="headers" type="as">
+      <default>[]</default>
+      <_summary>(Deprecated) List of custom headers and whether they are enabled.</_summary>
+      <_description>This key was deprecated in version 3.10 and should no longer be used.  Use 
"show-headers" instead.</_description>
+    </key>
     <key name="load-http-images" type="i">
       <default>0</default>
       <_summary>(Deprecated) Load images for HTML messages over HTTP</_summary>
diff --git a/modules/mail/em-mailer-prefs.c b/modules/mail/em-mailer-prefs.c
index a5f6e25..38e66b0 100644
--- a/modules/mail/em-mailer-prefs.c
+++ b/modules/mail/em-mailer-prefs.c
@@ -56,6 +56,8 @@ static GType col_types[] = {
        G_TYPE_STRING
 };
 
+#define EM_FORMAT_HEADER_XMAILER "x-evolution-mailer"
+
 /* temporarily copied from em-format.c */
 static const gchar *default_headers[] = {
        N_("From"),
@@ -67,11 +69,9 @@ static const gchar *default_headers[] = {
        N_("Date"),
        N_("Newsgroups"),
        N_("Face"),
-       "x-evolution-mailer", /* DO NOT translate */
+       EM_FORMAT_HEADER_XMAILER /* DO NOT translate */
 };
 
-#define EM_FORMAT_HEADER_XMAILER "x-evolution-mailer"
-
 /* for empty trash on exit frequency */
 static const struct {
        const gchar *label;
@@ -443,40 +443,38 @@ emmp_header_add_sensitivity (EMMailerPrefs *prefs)
 static void
 emmp_save_headers (EMMailerPrefs *prefs)
 {
-       GPtrArray *headers;
+       GVariantBuilder builder;
+       GtkTreeModel *model;
+       GVariant *variant;
        GtkTreeIter iter;
        gboolean valid;
 
-       /* Headers */
-       headers = g_ptr_array_new_full (3, g_free);
-       valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (prefs->header_list_store), &iter);
-       while (valid) {
-               struct _EMailReaderHeader h;
-               gboolean enabled;
-               gchar *xml;
+       g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(sb)"));
 
-               h.name = NULL;
+       model = GTK_TREE_MODEL (prefs->header_list_store);
+       valid = gtk_tree_model_get_iter_first (model, &iter);
+
+       while (valid) {
+               gchar *name = NULL;
+               gboolean enabled = TRUE;
 
                gtk_tree_model_get (
-                       GTK_TREE_MODEL (prefs->header_list_store), &iter,
-                       HEADER_LIST_HEADER_COLUMN, &h.name,
+                       model, &iter,
+                       HEADER_LIST_HEADER_COLUMN, &name,
                        HEADER_LIST_ENABLED_COLUMN, &enabled,
                        -1);
-               h.enabled = enabled;
-
-               if ((xml = e_mail_reader_header_to_xml (&h)))
-                       g_ptr_array_add (headers, xml);
 
-               g_free (h.name);
+               if (name != NULL) {
+                       g_variant_builder_add (
+                               &builder, "(sb)", name, enabled);
+                       g_free (name);
+               }
 
-               valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (prefs->header_list_store), &iter);
+               valid = gtk_tree_model_iter_next (model, &iter);
        }
 
-       g_ptr_array_add (headers, NULL);
-
-       g_settings_set_strv (prefs->settings, "headers", (const gchar * const *) headers->pdata);
-
-       g_ptr_array_free (headers, TRUE);
+       variant = g_variant_builder_end (&builder);
+       g_settings_set_value (prefs->settings, "show-headers", variant);
 }
 
 static void
@@ -822,17 +820,17 @@ em_mailer_prefs_construct (EMMailerPrefs *prefs,
                            EMailSession *session,
                            EShell *shell)
 {
-       GSList *header_add_list, *p;
-       gchar **headers_config;
        GSettings *settings;
        GHashTable *default_header_hash;
        GtkWidget *toplevel;
        GtkWidget *container;
        GtkWidget *table;
        GtkWidget *widget;
+       GtkTreeModel *tree_model;
        GtkTreeSelection *selection;
        GtkCellRenderer *renderer;
-       GtkTreeIter iter;
+       GVariant *variant;
+       gsize ii, n_children;
        gboolean locked;
        gboolean writable;
        gint val, i;
@@ -1118,72 +1116,95 @@ em_mailer_prefs_construct (EMMailerPrefs *prefs,
                "text", HEADER_LIST_NAME_COLUMN,
                NULL);
 
-       /* populated the listview with entries; firstly we add all the default headers, and then
-        * we add read header configuration out of settings. If a header in settings is a default header,
-        * we update the enabled flag accordingly
-       */
-       header_add_list = NULL;
-       default_header_hash = g_hash_table_new (g_str_hash, g_str_equal);
-       for (i = 0; i < G_N_ELEMENTS (default_headers); i++) {
-               EMailReaderHeader *h;
-
-               h = g_malloc (sizeof (EMailReaderHeader));
-               h->is_default = TRUE;
-               h->name = g_strdup (default_headers[i]);
-               h->enabled = strcmp ((gchar *) default_headers[i], "x-evolution-mailer") != 0;
-               g_hash_table_insert (default_header_hash, (gpointer) default_headers[i], h);
-               header_add_list = g_slist_append (header_add_list, h);
-       }
+       /* Populate the list store with entries.  Firstly we add all the
+        * default headers, and then we add read header configuration out of
+        * settings. If a header in settings is a default header, we update
+        * the enabled flag accordingly. */
 
-       /* read stored headers from settings */
-       headers_config = g_settings_get_strv (prefs->settings, "headers");
-       if (headers_config) {
-               for (i = 0; headers_config[i]; i++) {
-                       EMailReaderHeader *h, *def;
-                       const gchar *xml = headers_config[i];
-
-                       h = e_mail_reader_header_from_xml (xml);
-                       if (h) {
-                               def = g_hash_table_lookup (default_header_hash, h->name);
-                               if (def) {
-                                       def->enabled = h->enabled;
-                                       e_mail_reader_header_free (h);
-                               } else {
-                                       h->is_default = FALSE;
-                                       header_add_list = g_slist_append (header_add_list, h);
-                               }
-                       }
-               }
+       /* FIXME Split the headers section into a separate widget to
+        *       better isolate its functionality.  There's too much
+        *       complexity to just embed it like this. */
 
-               g_strfreev (headers_config);
-       }
+       default_header_hash = g_hash_table_new_full (
+               (GHashFunc) g_str_hash,
+               (GEqualFunc) g_str_equal,
+               (GDestroyNotify) g_free,
+               (GDestroyNotify) gtk_tree_path_free);
 
-       g_hash_table_destroy (default_header_hash);
+       tree_model = GTK_TREE_MODEL (prefs->header_list_store);
+
+       for (ii = 0; ii < G_N_ELEMENTS (default_headers); ii++) {
+               GtkTreeIter iter;
+               const gchar *display_name;
+               const gchar *header_name;
+               gboolean enabled;
 
-       p = header_add_list;
-       while (p) {
-               struct _EMailReaderHeader *h = (struct _EMailReaderHeader *) p->data;
-               const gchar *name;
+               header_name = default_headers[ii];
+               if (g_strcmp0 (header_name, EM_FORMAT_HEADER_XMAILER) == 0) {
+                       display_name = _("Mailer");
+                       enabled = FALSE;
+               } else {
+                       display_name = _(header_name);
+                       enabled = TRUE;
+               }
 
-               if (g_ascii_strcasecmp (h->name, EM_FORMAT_HEADER_XMAILER) == 0)
-                       name = _("Mailer");
-               else
-                       name = _(h->name);
+               gtk_list_store_append (
+                       GTK_LIST_STORE (tree_model), &iter);
 
-               gtk_list_store_append (prefs->header_list_store, &iter);
                gtk_list_store_set (
-                       prefs->header_list_store, &iter,
-                       HEADER_LIST_NAME_COLUMN, name,
-                       HEADER_LIST_ENABLED_COLUMN, h->enabled,
-                       HEADER_LIST_IS_DEFAULT_COLUMN, h->is_default,
-                       HEADER_LIST_HEADER_COLUMN, h->name,
+                       GTK_LIST_STORE (tree_model), &iter,
+                       HEADER_LIST_NAME_COLUMN, display_name,
+                       HEADER_LIST_ENABLED_COLUMN, enabled,
+                       HEADER_LIST_IS_DEFAULT_COLUMN, TRUE,
+                       HEADER_LIST_HEADER_COLUMN, header_name,
                        -1);
 
-               e_mail_reader_header_free (h);
-               p = p->next;
+               g_hash_table_insert (
+                       default_header_hash, g_strdup (header_name),
+                       gtk_tree_model_get_path (tree_model, &iter));
+       }
+
+       variant = g_settings_get_value (prefs->settings, "show-headers");
+       n_children = g_variant_n_children (variant);
+
+       for (ii = 0; ii < n_children; ii++) {
+               GtkTreeIter iter;
+               GtkTreePath *path;
+               const gchar *header_name = NULL;
+               gboolean enabled = FALSE;
+
+               g_variant_get_child (
+                       variant, ii, "(&sb)", &header_name, &enabled);
+
+               if (header_name == NULL) {
+                       g_warn_if_reached ();
+                       continue;
+               }
+
+               path = g_hash_table_lookup (default_header_hash, header_name);
+               if (path != NULL) {
+                       gtk_tree_model_get_iter (tree_model, &iter, path);
+                       gtk_list_store_set (
+                               GTK_LIST_STORE (tree_model), &iter,
+                               HEADER_LIST_ENABLED_COLUMN, enabled,
+                               -1);
+               } else {
+                       gtk_list_store_append (
+                               GTK_LIST_STORE (tree_model), &iter);
+
+                       gtk_list_store_set (
+                               GTK_LIST_STORE (tree_model), &iter,
+                               HEADER_LIST_NAME_COLUMN, _(header_name),
+                               HEADER_LIST_ENABLED_COLUMN, enabled,
+                               HEADER_LIST_IS_DEFAULT_COLUMN, FALSE,
+                               HEADER_LIST_HEADER_COLUMN, header_name,
+                               -1);
+               }
        }
 
-       g_slist_free (header_add_list);
+       g_variant_unref (variant);
+
+       g_hash_table_destroy (default_header_hash);
 
        /* date/time format */
        table = e_builder_get_widget (prefs->builder, "datetime-format-table");
diff --git a/modules/settings/e-settings-deprecated.c b/modules/settings/e-settings-deprecated.c
index 13f0819..058c7fd 100644
--- a/modules/settings/e-settings-deprecated.c
+++ b/modules/settings/e-settings-deprecated.c
@@ -46,6 +46,7 @@ struct _ESettingsDeprecatedPrivate {
        gulong forward_style_name_handler_id;
        gulong reply_style_name_handler_id;
        gulong image_loading_policy_handler_id;
+       gulong show_headers_handler_id;
 };
 
 /* Flag values used in the "working-days" key. */
@@ -65,6 +66,69 @@ G_DEFINE_DYNAMIC_TYPE (
        E_TYPE_EXTENSION)
 
 static void
+settings_deprecated_header_start_element (GMarkupParseContext *context,
+                                          const gchar *element_name,
+                                          const gchar **attribute_names,
+                                          const gchar **attribute_values,
+                                          gpointer user_data,
+                                          GError **error)
+{
+       GVariantBuilder *builder = user_data;
+       const gchar *name = NULL;
+       const gchar *enabled = NULL;
+
+       /* The enabled flag is determined by the presence of an "enabled"
+        * attribute, but the actual value of the "enabled" attribute, if
+        * present, is just an empty string.  It's pretty convoluted. */
+       g_markup_collect_attributes (
+               element_name,
+               attribute_names,
+               attribute_values,
+               error,
+               G_MARKUP_COLLECT_STRING,
+               "name", &name,
+               G_MARKUP_COLLECT_STRING |
+               G_MARKUP_COLLECT_OPTIONAL,
+               "enabled", &enabled,
+               G_MARKUP_COLLECT_INVALID);
+
+       if (name != NULL)
+               g_variant_builder_add (
+                       builder, "(sb)", name, (enabled != NULL));
+}
+
+static void
+settings_deprecated_header_parse_xml (const gchar *xml,
+                                      GVariantBuilder *builder)
+{
+       static GMarkupParser parser = {
+               settings_deprecated_header_start_element, };
+
+       GMarkupParseContext *context;
+
+       context = g_markup_parse_context_new (&parser, 0, builder, NULL);
+       g_markup_parse_context_parse (context, xml, -1, NULL);
+       g_markup_parse_context_end_parse (context, NULL);
+       g_markup_parse_context_free (context);
+}
+
+static GVariant *
+settings_deprecated_header_strv_to_variant (gchar **strv)
+{
+       GVariantBuilder builder;
+       guint ii, length;
+
+       length = g_strv_length (strv);
+
+       g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(sb)"));
+
+       for (ii = 0; ii < length; ii++)
+               settings_deprecated_header_parse_xml (strv[ii], &builder);
+
+       return g_variant_builder_end (&builder);
+}
+
+static void
 settings_deprecated_week_start_day_name_cb (GSettings *settings,
                                             const gchar *key)
 {
@@ -241,6 +305,41 @@ settings_deprecated_image_loading_policy_cb (GSettings *settings,
 }
 
 static void
+settings_deprecated_show_headers_cb (GSettings *settings,
+                                     const gchar *key)
+{
+       GVariant *variant;
+       gsize ii, n_children;
+       gchar **strv = NULL;
+
+       variant = g_settings_get_value (settings, key);
+       n_children = g_variant_n_children (variant);
+
+       strv = g_new0 (gchar *, n_children + 1);
+
+       for (ii = 0; ii < n_children; ii++) {
+               const gchar *name = NULL;
+               gboolean enabled = FALSE;
+
+               g_variant_get_child (
+                       variant, ii, "(&sb)", &name, &enabled);
+
+               strv[ii] = g_strdup_printf (
+                       "<?xml version=\"1.0\"?>\n"
+                       "<header name=\"%s\"%s/>\n",
+                       name, enabled ? " enabled=\"\"" : "");
+       }
+
+       g_settings_set_strv (
+               settings, "headers",
+               (const gchar * const *) strv);
+
+       g_strfreev (strv);
+
+       g_variant_unref (variant);
+}
+
+static void
 settings_deprecated_dispose (GObject *object)
 {
        ESettingsDeprecatedPrivate *priv;
@@ -331,6 +430,13 @@ settings_deprecated_dispose (GObject *object)
                priv->image_loading_policy_handler_id = 0;
        }
 
+       if (priv->show_headers_handler_id > 0) {
+               g_signal_handler_disconnect (
+                       priv->mail_settings,
+                       priv->show_headers_handler_id);
+               priv->show_headers_handler_id = 0;
+       }
+
        g_clear_object (&priv->calendar_settings);
        g_clear_object (&priv->mail_settings);
 
@@ -342,8 +448,10 @@ static void
 settings_deprecated_constructed (GObject *object)
 {
        ESettingsDeprecatedPrivate *priv;
+       GVariant *variant;
        gulong handler_id;
        gchar *string_value;
+       gchar **strv_value;
        gint int_value;
 
        priv = E_SETTINGS_DEPRECATED_GET_PRIVATE (object);
@@ -409,6 +517,11 @@ settings_deprecated_constructed (GObject *object)
        g_settings_set_enum (
                priv->mail_settings, "forward-style-name", int_value);
 
+       strv_value = g_settings_get_strv (priv->mail_settings, "headers");
+       variant = settings_deprecated_header_strv_to_variant (strv_value);
+       g_settings_set_value (priv->mail_settings, "show-headers", variant);
+       g_strfreev (strv_value);
+
        /* XXX The "reply-style" key uses a completely different
         *     numbering than the EMailReplyStyle enum.  *sigh* */
        switch (g_settings_get_int (priv->mail_settings, "reply-style")) {
@@ -507,6 +620,11 @@ settings_deprecated_constructed (GObject *object)
                priv->mail_settings, "changed::image-loading-policy",
                G_CALLBACK (settings_deprecated_image_loading_policy_cb), NULL);
        priv->image_loading_policy_handler_id = handler_id;
+
+       handler_id = g_signal_connect (
+               priv->mail_settings, "changed::show-headers",
+               G_CALLBACK (settings_deprecated_show_headers_cb), NULL);
+       priv->show_headers_handler_id = handler_id;
 }
 
 static void
diff --git a/modules/settings/e-settings-mail-formatter.c b/modules/settings/e-settings-mail-formatter.c
index ae53291..2c3b469 100644
--- a/modules/settings/e-settings-mail-formatter.c
+++ b/modules/settings/e-settings-mail-formatter.c
@@ -74,31 +74,30 @@ settings_mail_formatter_headers_changed_cb (GSettings *settings,
                                             ESettingsMailFormatter *extension)
 {
        EMailFormatter *formatter;
-       gchar **headers;
-       gint ii;
+       GVariant *variant;
+       gsize ii, n_children;
 
        formatter = settings_mail_formatter_get_extensible (extension);
+       e_mail_formatter_clear_headers (formatter);
 
-       headers = g_settings_get_strv (settings, "headers");
+       variant = g_settings_get_value (settings, "show-headers");
+       n_children = g_variant_n_children (variant);
 
-       e_mail_formatter_clear_headers (formatter);
-       for (ii = 0; headers && headers[ii]; ii++) {
-               EMailReaderHeader *h;
-               const gchar *xml = headers[ii];
+       for (ii = 0; ii < n_children; ii++) {
+               const gchar *name = NULL;
+               gboolean enabled = FALSE;
 
-               h = e_mail_reader_header_from_xml (xml);
-               if (h && h->enabled)
+               g_variant_get_child (variant, ii, "(&sb)", &name, &enabled);
+               if (name != NULL && enabled)
                        e_mail_formatter_add_header (
-                               formatter, h->name, NULL,
+                               formatter, name, NULL,
                                E_MAIL_FORMATTER_HEADER_FLAG_BOLD);
-
-               e_mail_reader_header_free (h);
        }
 
-       if (!headers || !headers[0])
+       if (n_children == 0)
                e_mail_formatter_set_default_headers (formatter);
 
-       g_strfreev (headers);
+       g_variant_unref (variant);
 }
 
 static void


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