[geary/wip/714317-hide-html-in-preview: 3/4] Workaround conversation message preview being cut off on wide screens.



commit f577e41ce8c87db69ef51d1a3936a432d4cce55b
Author: Michael James Gratton <mike vee net>
Date:   Tue Dec 20 12:07:52 2016 +1100

    Workaround conversation message preview being cut off on wide screens.
    
    Bug 772607.
    
    * src/engine/rfc822/rfc822-utils.vala (Geary.RFC822.Utils): Don't
      truncate the preview here - fetch preview will already be truncated,
      and the conversation message preview needs the full text anyway. Update
      unit tests.
    
    * src/client/conversation-viewer/conversation-message.vala
      (ConversationMessage::ConversationMessage): Since we need to know if
      the preview has been truncated so we know if we need to add an elipsis
      or not, get the full preview and tuncate it here if needed.
    
    * src/engine/rfc822/rfc822-message.vala (Message): Don't bother trying to
      set the preview when creating a Geary.Email from the message. Minor
      code & doc cleanup.
    
    * src/engine/api/geary-email.vala (Email): Double length of max fetch
      preview so HTML parts might actually pick up some content. Tidy up doc
      comments.

 .../conversation-viewer/conversation-message.vala  |   11 ++++++++---
 src/engine/api/geary-email.vala                    |   14 ++++++++++----
 src/engine/rfc822/rfc822-message.vala              |   12 +++++-------
 src/engine/rfc822/rfc822-utils.vala                |    8 ++------
 test/engine/rfc822-message-data-test.vala          |    8 ++++----
 test/engine/rfc822-utils-test.vala                 |    8 ++++----
 6 files changed, 33 insertions(+), 28 deletions(-)
---
diff --git a/src/client/conversation-viewer/conversation-message.vala 
b/src/client/conversation-viewer/conversation-message.vala
index d60cbf6..96fc7ff 100644
--- a/src/client/conversation-viewer/conversation-message.vala
+++ b/src/client/conversation-viewer/conversation-message.vala
@@ -19,6 +19,7 @@ public class ConversationMessage : Gtk.Grid {
 
 
     private const string FROM_CLASS = "geary-from";
+    private const int MAX_PREVIEW_BYTES = Geary.Email.MAX_PREVIEW_BYTES;
 
 
     internal static inline bool has_distinct_name(
@@ -377,9 +378,13 @@ public class ConversationMessage : Gtk.Grid {
         this.preview_date.set_text(date_text);
         this.preview_date.set_tooltip_text(date_tooltip);
 
-        this.preview_body.set_text(
-            Geary.String.reduce_whitespace(this.message.get_preview()) + "…"
-        );
+        string preview = this.message.get_preview();
+        if (preview.length > MAX_PREVIEW_BYTES) {
+            preview = Geary.String.safe_byte_substring(preview, MAX_PREVIEW_BYTES);
+            // Add an ellipsis in case the wider is wider than the text
+            preview += "…";
+        }
+        this.preview_body.set_text(preview);
 
         // Full headers
 
diff --git a/src/engine/api/geary-email.vala b/src/engine/api/geary-email.vala
index 1c72e27..2d26c08 100644
--- a/src/engine/api/geary-email.vala
+++ b/src/engine/api/geary-email.vala
@@ -4,11 +4,17 @@
  * (version 2.1 or later).  See the COPYING file in this distribution.
  */
 
+/**
+ * Representation of a single email message in the engine.
+ *
+ * This class encapsulates an email, attachments and its related
+ * server and database state.
+ */
 public class Geary.Email : BaseObject {
-    // This value is not persisted, but it does represent the expected max size of the preview
-    // when returned.
-    public const int MAX_PREVIEW_BYTES = 128;
-    
+
+    /** The maximum expected length of message body preview text. */
+    public const int MAX_PREVIEW_BYTES = 256;
+
     /**
      * Currently only one field is mutable: FLAGS.  All others never change once stored in the
      * database.
diff --git a/src/engine/rfc822/rfc822-message.vala b/src/engine/rfc822/rfc822-message.vala
index 4d927ad..4a18b6a 100644
--- a/src/engine/rfc822/rfc822-message.vala
+++ b/src/engine/rfc822/rfc822-message.vala
@@ -371,16 +371,13 @@ public class Geary.RFC822.Message : BaseObject {
         email.set_message_subject(subject);
         email.set_message_body(new Geary.RFC822.Text(new Geary.Memory.OffsetBuffer(
             body_buffer, body_offset)));
-        email.set_message_preview(new Geary.RFC822.PreviewText.from_string(get_preview()));
-        
         return email;
     }
 
     /**
      * Generates a preview from the email's message body.
      *
-     * If there is no body or the body is the empty string, the empty
-     * string will be returned.
+     * If there is no body, the empty string will be returned.
      */
     public string get_preview() {
         TextFormat format = TextFormat.PLAIN;
@@ -392,13 +389,14 @@ public class Geary.RFC822.Message : BaseObject {
                 format = TextFormat.HTML;
                 preview = get_html_body(null);
             } catch (Error error) {
-                debug("Could not generate message preview: %s\n and: %s", e.message, error.message);
+                debug("Could not generate message preview: %s\n and: %s",
+                      e.message, error.message);
             }
         }
 
         return (preview != null)
-          ? Geary.RFC822.Utils.to_preview_text(preview, format)
-          : "";
+            ? Geary.RFC822.Utils.to_preview_text(preview, format)
+            : "";
     }
 
     /**
diff --git a/src/engine/rfc822/rfc822-utils.vala b/src/engine/rfc822/rfc822-utils.vala
index 0e503ac..64aea29 100644
--- a/src/engine/rfc822/rfc822-utils.vala
+++ b/src/engine/rfc822/rfc822-utils.vala
@@ -345,8 +345,7 @@ public bool comp_char_arr_slice(char[] array, uint start, string comp) {
 /**
  * Obtains the best preview text from a plain or HTML string.
  *
- * The string returned will be at most `Geary.Email.MAX_PREVIEW_BYTES`
- * long, and will have had its whitespace squashed.
+ * The string returned will will have had its whitespace squashed.
  */
 public string to_preview_text(string? text, TextFormat format) {
     string preview = "";
@@ -394,10 +393,7 @@ public string to_preview_text(string? text, TextFormat format) {
         preview = Geary.HTML.html_to_text(text, false);
     }
 
-    return Geary.String.safe_byte_substring(
-        Geary.String.reduce_whitespace(preview),
-        Geary.Email.MAX_PREVIEW_BYTES
-    );
+    return Geary.String.reduce_whitespace(preview);
 }
 
 /**
diff --git a/test/engine/rfc822-message-data-test.vala b/test/engine/rfc822-message-data-test.vala
index 90cce91..f36e72f 100644
--- a/test/engine/rfc822-message-data-test.vala
+++ b/test/engine/rfc822-message-data-test.vala
@@ -17,7 +17,7 @@ class Geary.RFC822.MessageDataTest : Gee.TestCase {
             new Geary.Memory.StringBuffer(PLAIN_BODY1_ENCODED),
             new Geary.Memory.StringBuffer(PLAIN_BODY1_HEADERS)
         );
-        assert(plain_preview1.buffer.to_string() == PLAIN_BODY1_EXPECTED.substring(0, 
Geary.Email.MAX_PREVIEW_BYTES));
+        assert(plain_preview1.buffer.to_string() == PLAIN_BODY1_EXPECTED);
 
         string html_part_headers = "Content-Type: text/html; charset=utf-8\r\nContent-Transfer-Encoding: 
quoted-printable\r\n\r\n";
 
@@ -25,20 +25,20 @@ class Geary.RFC822.MessageDataTest : Gee.TestCase {
             new Geary.Memory.StringBuffer(HTML_BODY1_ENCODED),
             new Geary.Memory.StringBuffer(html_part_headers)
         );
-        assert(html_preview1.buffer.to_string() == HTML_BODY1_EXPECTED.substring(0, 
Geary.Email.MAX_PREVIEW_BYTES));
+        assert(html_preview1.buffer.to_string() == HTML_BODY1_EXPECTED);
 
         PreviewText html_preview2 = new PreviewText.with_header(
             new Geary.Memory.StringBuffer(HTML_BODY2_ENCODED),
             new Geary.Memory.StringBuffer(html_part_headers)
         );
-        assert(html_preview2.buffer.to_string() == HTML_BODY2_EXPECTED.substring(0, 
Geary.Email.MAX_PREVIEW_BYTES));
+        assert(html_preview2.buffer.to_string() == HTML_BODY2_EXPECTED);
     }
 
     public static string PLAIN_BODY1_HEADERS = "Content-Type: text/plain; 
charset=\"us-ascii\"\r\nContent-Transfer-Encoding: 7bit\r\n";
 
     public static string PLAIN_BODY1_ENCODED = "-----BEGIN PGP SIGNED MESSAGE-----\r\nHash: 
SHA512\r\n\r\n=============================================================================\r\nFreeBSD-EN-16:11.vmbus
                                          Errata Notice\r\n                                                   
       The FreeBSD Project\r\n\r\nTopic:          Avoid using spin locks for channel message 
locks\r\n\r\nCategory:       core\r\nModule:         vmbus\r\nAnnounced:      2016-08-12\r\nCredits:        
Microsoft OSTC\r\nAffects:        FreeBSD 10.3\r\nCorrected:      2016-06-15 09:52:01 UTC (stable/10, 
10.3-STABLE)\r\n                2016-08-12 04:01:16 UTC (releng/10.3, 10.3-RELEASE-p7)\r\n\r\nFor general 
information regarding FreeBSD Errata Notices and Security\r\nAdvisories, including descriptions of the fields 
above, security\r\nbranches, and the following sections, please 
visit\r\n<URL:https://security.FreeBSD.org/>.\r\n";
 
-    public static string PLAIN_BODY1_EXPECTED = "FreeBSD-EN-16:11.vmbus Errata Notice The FreeBSD Project 
Topic: Avoid using spin locks for channel message locks Category: core Module: vmbus Announced: 2016-08-12 
Credits: Microsoft OSTC Affects: FreeBSD 10.3 Corrected: 2016-06-15 09:52:01 UTC (stable/10, 10.3-STABLE) 
2016-08-12 04:01:16 UTC (releng/10.3, 10.3-RELEASE-p7)";
+    public static string PLAIN_BODY1_EXPECTED = "FreeBSD-EN-16:11.vmbus Errata Notice The FreeBSD Project 
Topic: Avoid using spin locks for channel message locks Category: core Module: vmbus Announced: 2016-08-12 
Credits: Microsoft OSTC Affects: FreeBSD 10.3 Corrected: 2016-06-15 09:52:01 UTC (stable/10, 10.3-STABLE) 
2016-08-12 04:01:16 UTC (releng/10.3, 10.3-RELEASE-p7) For general information regarding FreeBSD Errata 
Notices and Security Advisories, including descriptions of the fields above, security branches, and the 
following sections, please visit <URL:https://security.FreeBSD.org/>.";
 
     public static string HTML_BODY1_ENCODED = """<html><head>
 <meta http-equiv=3DContent-Type content=3D"text/html; charset=3Dutf-8">
diff --git a/test/engine/rfc822-utils-test.vala b/test/engine/rfc822-utils-test.vala
index afc0ee0..0566983 100644
--- a/test/engine/rfc822-utils-test.vala
+++ b/test/engine/rfc822-utils-test.vala
@@ -14,16 +14,16 @@ class Geary.RFC822.Utils.Test : Gee.TestCase {
 
     public void to_preview_text() {
         assert(Geary.RFC822.Utils.to_preview_text(PLAIN_BODY_ENCODED, Geary.RFC822.TextFormat.PLAIN) ==
-               PLAIN_BODY_EXPECTED.substring(0, Geary.Email.MAX_PREVIEW_BYTES));
+               PLAIN_BODY_EXPECTED);
         assert(Geary.RFC822.Utils.to_preview_text(HTML_BODY_ENCODED, Geary.RFC822.TextFormat.HTML) ==
-               HTML_BODY_EXPECTED.substring(0, Geary.Email.MAX_PREVIEW_BYTES));
+               HTML_BODY_EXPECTED);
         assert(Geary.RFC822.Utils.to_preview_text(HTML_BODY_ENCODED, Geary.RFC822.TextFormat.HTML) ==
-               HTML_BODY_EXPECTED.substring(0, Geary.Email.MAX_PREVIEW_BYTES));
+               HTML_BODY_EXPECTED);
     }
 
     public static string PLAIN_BODY_ENCODED = "-----BEGIN PGP SIGNED MESSAGE-----\r\nHash: 
SHA512\r\n\r\n=============================================================================\r\nFreeBSD-EN-16:11.vmbus
                                          Errata Notice\r\n                                                   
       The FreeBSD Project\r\n\r\nTopic:          Avoid using spin locks for channel message 
locks\r\n\r\nCategory:       core\r\nModule:         vmbus\r\nAnnounced:      2016-08-12\r\nCredits:        
Microsoft OSTC\r\nAffects:        FreeBSD 10.3\r\nCorrected:      2016-06-15 09:52:01 UTC (stable/10, 
10.3-STABLE)\r\n                2016-08-12 04:01:16 UTC (releng/10.3, 10.3-RELEASE-p7)\r\n\r\nFor general 
information regarding FreeBSD Errata Notices and Security\r\nAdvisories, including descriptions of the fields 
above, security\r\nbranches, and the following sections, please 
visit\r\n<URL:https://security.FreeBSD.org/>.\r\n";
 
-    public static string PLAIN_BODY_EXPECTED = "FreeBSD-EN-16:11.vmbus Errata Notice The FreeBSD Project 
Topic: Avoid using spin locks for channel message locks Category: core Module: vmbus Announced: 2016-08-12 
Credits: Microsoft OSTC Affects: FreeBSD 10.3 Corrected: 2016-06-15 09:52:01 UTC (stable/10, 10.3-STABLE) 
2016-08-12 04:01:16 UTC (releng/10.3, 10.3-RELEASE-p7)";
+    public static string PLAIN_BODY_EXPECTED = "FreeBSD-EN-16:11.vmbus Errata Notice The FreeBSD Project 
Topic: Avoid using spin locks for channel message locks Category: core Module: vmbus Announced: 2016-08-12 
Credits: Microsoft OSTC Affects: FreeBSD 10.3 Corrected: 2016-06-15 09:52:01 UTC (stable/10, 10.3-STABLE) 
2016-08-12 04:01:16 UTC (releng/10.3, 10.3-RELEASE-p7) For general information regarding FreeBSD Errata 
Notices and Security Advisories, including descriptions of the fields above, security branches, and the 
following sections, please visit <URL:https://security.FreeBSD.org/>.";
 
     public static string HTML_BODY_ENCODED = """<html><head>
 <meta http-equiv=Content-Type content="text/html; charset=utf-8">


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