[evolution] I#329 - Mail: Limit by size a text attachment inline view



commit 1bbfd143aff942472f4e9d93682a213af911e72a
Author: Milan Crha <mcrha redhat com>
Date:   Fri Jul 16 12:52:09 2021 +0200

    I#329 - Mail: Limit by size a text attachment inline view
    
    Closes https://gitlab.gnome.org/GNOME/evolution/-/issues/329

 data/org.gnome.evolution.mail.gschema.xml.in |  5 ++++
 src/em-format/e-mail-formatter-attachment.c  | 43 ++++++++++++++++++++++++++--
 2 files changed, 45 insertions(+), 3 deletions(-)
---
diff --git a/data/org.gnome.evolution.mail.gschema.xml.in b/data/org.gnome.evolution.mail.gschema.xml.in
index f5605f537b..f45f9f5bbd 100644
--- a/data/org.gnome.evolution.mail.gschema.xml.in
+++ b/data/org.gnome.evolution.mail.gschema.xml.in
@@ -867,6 +867,11 @@
       <_summary>Whether to preserve expand state of the folders when calling Copy/Move to Folder.</_summary>
       <_description>If enabled, the Copy/Move to Folder dialog will preserve the expand state of the folders 
in the dialog, otherwise all the folders will be expanded.</_description>
     </key>
+    <key name="preview-text-size-limit" type="i">
+      <default>512</default>
+      <_summary>Size limit for text attachments to show</_summary>
+      <_description>Defines the size in KB, the limit to show text attachments in the message preview. 
Anything above this limit will not be possible to show inline.</_description>
+    </key>
 
     <!-- The following keys are deprecated. -->
 
diff --git a/src/em-format/e-mail-formatter-attachment.c b/src/em-format/e-mail-formatter-attachment.c
index 863d30c2e6..81da3e8047 100644
--- a/src/em-format/e-mail-formatter-attachment.c
+++ b/src/em-format/e-mail-formatter-attachment.c
@@ -64,6 +64,8 @@ emfe_attachment_format (EMailFormatterExtension *extension,
        GString *buffer;
        gint icon_width, icon_height;
        gchar *icon_uri;
+       CamelContentType *content_type;
+       gboolean part_too_large = FALSE;
        gpointer attachment_ptr = NULL;
        const gchar *attachment_part_id;
        const gchar *part_id;
@@ -185,6 +187,40 @@ emfe_attachment_format (EMailFormatterExtension *extension,
        html = camel_text_to_html (
                text, flags & CAMEL_MIME_FILTER_TOHTML_CONVERT_URLS, 0);
        g_free (text);
+
+       content_type = camel_mime_part_get_content_type (mime_part);
+
+       if (camel_content_type_is (content_type, "text", "*") ||
+           camel_content_type_is (content_type, "application", "xml") ||
+           (empa->snoop_mime_type && (
+            g_ascii_strncasecmp (empa->snoop_mime_type, "text/", 5) == 0 ||
+            g_ascii_strcasecmp (empa->snoop_mime_type, "application/xml") == 0))) {
+               GSettings *settings;
+               gsize size_limit;
+
+               settings = e_util_ref_settings ("org.gnome.evolution.mail");
+               size_limit = g_settings_get_int (settings, "preview-text-size-limit");
+               g_object_unref (settings);
+
+               if (size_limit > 0) {
+                       gsize part_size;
+
+                       part_size = camel_data_wrapper_calculate_decoded_size_sync (CAMEL_DATA_WRAPPER 
(mime_part), cancellable, NULL);
+                       part_too_large = part_size > 1024 * size_limit;
+
+                       if (part_too_large) {
+                               EAttachment *attachment;
+
+                               e_mail_part_attachment_set_expandable (empa, FALSE);
+
+                               attachment = e_mail_part_attachment_ref_attachment (E_MAIL_PART_ATTACHMENT 
(part));
+                               e_attachment_set_can_show (attachment, FALSE);
+                               e_attachment_set_initially_shown (attachment, FALSE);
+                               g_object_unref (attachment);
+                       }
+               }
+       }
+
        g_object_unref (mime_part);
 
        if (empa->part_id_with_attachment)
@@ -269,9 +305,10 @@ emfe_attachment_format (EMailFormatterExtension *extension,
                "</button>"
                "</td><td align=\"left\">%s</td></tr>",
                part_id, attachment_ptr,
-               e_mail_part_should_show_inline (part) || e_mail_part_attachment_get_expandable (empa) ? 
_("Toggle View Inline") : _("Open in default application"),
+               !part_too_large && (e_mail_part_should_show_inline (part) || 
e_mail_part_attachment_get_expandable (empa)) ?
+               _("Toggle View Inline") : _("Open in default application"),
                attachment_ptr,
-               e_mail_part_should_show_inline (part) ? "go-down" : e_mail_part_attachment_get_expandable 
(empa) ? "go-next" : "go-top",
+               part_too_large ? "go-top" : e_mail_part_should_show_inline (part) ? "go-down" : 
e_mail_part_attachment_get_expandable (empa) ? "go-next" : "go-top",
                GTK_ICON_SIZE_BUTTON, icon_width, icon_height,
                icon_uri, icon_width, icon_height,
                part_id, attachment_ptr, _("Options"), GTK_ICON_SIZE_BUTTON, icon_width, icon_height,
@@ -281,7 +318,7 @@ emfe_attachment_format (EMailFormatterExtension *extension,
        g_free (button_id);
        g_free (html);
 
-       if (content_stream && e_mail_part_attachment_get_expandable (empa)) {
+       if (!part_too_large && content_stream && e_mail_part_attachment_get_expandable (empa)) {
                gchar *wrapper_element_id;
                gconstpointer data;
                gsize size;


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