* src/modest-text-utils.[ch]:
authorJose Dapena Paz <jdapena@igalia.com>
Thu, 14 Jun 2007 16:44:47 +0000 (16:44 +0000)
committerJose Dapena Paz <jdapena@igalia.com>
Thu, 14 Jun 2007 16:44:47 +0000 (16:44 +0000)
        * Reworked the quote code to add attachmetns list.
          This should make attachments be shown on replying
          messages (fixes NB#58194).
        * Reworked the quote methods, to be more compliant
          to specs.
* src/modest-tny-msg.c, src/modest-formatter.[ch]:
        * Proper changes to pass the attachments list to the
          code that creates the quoted messages. It's used
          for showing the attachments names in replies including
          the original message.

pmo-trunk-r2241

src/modest-formatter.c
src/modest-formatter.h
src/modest-text-utils.c
src/modest-text-utils.h
src/modest-tny-msg.c

index 7bcd5fe..3fac885 100644 (file)
@@ -50,14 +50,14 @@ struct _ModestFormatterPrivate {
 
 static GObjectClass *parent_class = NULL;
 
-typedef gchar* FormatterFunc (ModestFormatter *self, const gchar *text, TnyHeader *header);
+typedef gchar* FormatterFunc (ModestFormatter *self, const gchar *text, TnyHeader *header, GList *attachments);
 
 static TnyMsg *modest_formatter_do (ModestFormatter *self, TnyMimePart *body,  TnyHeader *header, 
-                                   FormatterFunc func, gboolean has_attachments);
+                                   FormatterFunc func, GList *attachments);
 
-static gchar*  modest_formatter_wrapper_cite   (ModestFormatter *self, const gchar *text, TnyHeader *header);
-static gchar*  modest_formatter_wrapper_quote  (ModestFormatter *self, const gchar *text, TnyHeader *header);
-static gchar*  modest_formatter_wrapper_inline (ModestFormatter *self, const gchar *text, TnyHeader *header);
+static gchar*  modest_formatter_wrapper_cite   (ModestFormatter *self, const gchar *text, TnyHeader *header, GList *attachments);
+static gchar*  modest_formatter_wrapper_quote  (ModestFormatter *self, const gchar *text, TnyHeader *header, GList *attachments);
+static gchar*  modest_formatter_wrapper_inline (ModestFormatter *self, const gchar *text, TnyHeader *header, GList *attachments);
 
 static gchar *
 extract_text (ModestFormatter *self, TnyMimePart *body)
@@ -117,7 +117,7 @@ construct_from_text (TnyMimePart *part,
 }
 
 static TnyMsg *
-modest_formatter_do (ModestFormatter *self, TnyMimePart *body, TnyHeader *header, FormatterFunc func, gboolean has_attachments)
+modest_formatter_do (ModestFormatter *self, TnyMimePart *body, TnyHeader *header, FormatterFunc func, GList *attachments)
 {
        TnyMsg *new_msg = NULL;
        gchar *body_text = NULL, *txt = NULL;
@@ -129,7 +129,7 @@ modest_formatter_do (ModestFormatter *self, TnyMimePart *body, TnyHeader *header
        g_return_val_if_fail (func, NULL);
 
        /* Build new part */
-       new_msg = modest_formatter_create_message (self, TRUE, has_attachments);
+       new_msg = modest_formatter_create_message (self, TRUE, attachments != NULL);
        body_part = modest_formatter_create_body_part (self, new_msg);
 
        if (body)
@@ -137,7 +137,7 @@ modest_formatter_do (ModestFormatter *self, TnyMimePart *body, TnyHeader *header
        else
                body_text = g_strdup ("");
 
-       txt = (gchar *) func (self, (const gchar*) body_text, header);
+       txt = (gchar *) func (self, (const gchar*) body_text, header, attachments);
        priv = MODEST_FORMATTER_GET_PRIVATE (self);
        construct_from_text (TNY_MIME_PART (body_part), (const gchar*) txt, priv->content_type);
        g_object_unref (body_part);
@@ -152,19 +152,19 @@ modest_formatter_do (ModestFormatter *self, TnyMimePart *body, TnyHeader *header
 TnyMsg *
 modest_formatter_cite (ModestFormatter *self, TnyMimePart *body, TnyHeader *header)
 {
-       return modest_formatter_do (self, body, header, modest_formatter_wrapper_cite, FALSE);
+       return modest_formatter_do (self, body, header, modest_formatter_wrapper_cite, NULL);
 }
 
 TnyMsg *
-modest_formatter_quote (ModestFormatter *self, TnyMimePart *body, TnyHeader *header)
+modest_formatter_quote (ModestFormatter *self, TnyMimePart *body, TnyHeader *header, GList *attachments)
 {
-       return modest_formatter_do (self, body, header, modest_formatter_wrapper_quote, FALSE);
+       return modest_formatter_do (self, body, header, modest_formatter_wrapper_quote, attachments);
 }
 
 TnyMsg *
-modest_formatter_inline (ModestFormatter *self, TnyMimePart *body, TnyHeader *header, gboolean has_attachments)
+modest_formatter_inline (ModestFormatter *self, TnyMimePart *body, TnyHeader *header, GList *attachments)
 {
-       return modest_formatter_do (self, body, header, modest_formatter_wrapper_inline, has_attachments);
+       return modest_formatter_do (self, body, header, modest_formatter_wrapper_inline, attachments);
 }
 
 TnyMsg *
@@ -282,7 +282,7 @@ modest_formatter_get_type (void)
 
 /****************/
 static gchar *
-modest_formatter_wrapper_cite (ModestFormatter *self, const gchar *text, TnyHeader *header) 
+modest_formatter_wrapper_cite (ModestFormatter *self, const gchar *text, TnyHeader *header, GList *attachments) 
 {
        ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
 
@@ -294,7 +294,7 @@ modest_formatter_wrapper_cite (ModestFormatter *self, const gchar *text, TnyHead
 }
 
 static gchar *
-modest_formatter_wrapper_inline (ModestFormatter *self, const gchar *text, TnyHeader *header) 
+modest_formatter_wrapper_inline (ModestFormatter *self, const gchar *text, TnyHeader *header, GList *attachments) 
 {
        ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
 
@@ -308,17 +308,46 @@ modest_formatter_wrapper_inline (ModestFormatter *self, const gchar *text, TnyHe
 }
 
 static gchar *
-modest_formatter_wrapper_quote (ModestFormatter *self, const gchar *text, TnyHeader *header) 
+modest_formatter_wrapper_quote (ModestFormatter *self, const gchar *text, TnyHeader *header, GList *attachments) 
 {
        ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
+       GList *filenames = NULL;
+       GList *node = NULL;
+       gchar *result = NULL;
+       
+       /* First we need a GList of attachments filenames */
+       for (node = attachments; node != NULL; node = g_list_next (node)) {
+               TnyMimePart *part = (TnyMimePart *) node->data;
+               gchar *filename = NULL;
+               if (TNY_IS_MSG (part)) {
+                       TnyHeader *header = tny_msg_get_header (TNY_MSG (part));
+                       filename = g_strdup (tny_header_get_subject (header));
+                       if ((filename == NULL)||(filename[0] == '\0')) {
+                               g_free (filename);
+                               filename = g_strdup (_("mail_va_no_subject"));
+                       }
+                       g_object_unref (header);
+               } else {
+                       filename = g_strdup (tny_mime_part_get_filename (part));
+                       if ((filename == NULL)||(filename[0] == '\0'))
+                               filename = g_strdup ("");
+               }
+               filenames = g_list_append (filenames, filename);
+       }
+       filenames = g_list_reverse (filenames);
 
        /* TODO: get 80 from the configuration */
-       return modest_text_utils_quote (text, 
-                                       priv->content_type, 
-                                       priv->signature,
-                                       tny_header_get_from (header), 
-                                       tny_header_get_date_sent (header),
-                                       80);
+       result = modest_text_utils_quote (text, 
+                                         priv->content_type, 
+                                         priv->signature,
+                                         tny_header_get_from (header), 
+                                         tny_header_get_date_sent (header),
+                                         filenames,
+                                         80);
+
+       g_list_foreach (filenames, (GFunc) g_free, NULL);
+       g_list_free (filenames);
+       return result;
 }
 
 TnyMsg * 
index 73fa4f7..6ddbdd6 100644 (file)
@@ -94,6 +94,7 @@ TnyMsg * modest_formatter_cite   (ModestFormatter *self, TnyMimePart *part, TnyH
  * @self: a #ModestFormatter
  * @part: a non-NULL #TnyMimePart with the body of the original message
  * @header: a non-NULL #TnyHeader of the original message
+ * @attachments: a #GList of attachments in original message
  * 
  * Creates a new message with a text body made from the body of the
  * original message quoted. This function is locale-sensitive.
@@ -109,13 +110,14 @@ TnyMsg * modest_formatter_cite   (ModestFormatter *self, TnyMimePart *part, TnyH
  * Quoted message
  * -------------
  * Body:
- * On 1970/01/01 somemailATmodest.org wrote:
+ * ------ Original message -----
  * > This is the body of the text
+ * > Attachment: file1.txt
  * </para></programlisting>
  *
  * Returns: a newly formatted #TnyMsg or NULL in case of error
  **/
-TnyMsg * modest_formatter_quote  (ModestFormatter *self, TnyMimePart *part, TnyHeader *header);
+TnyMsg * modest_formatter_quote  (ModestFormatter *self, TnyMimePart *part, TnyHeader *header, GList *attachments);
 
 
 /**
@@ -123,6 +125,7 @@ TnyMsg * modest_formatter_quote  (ModestFormatter *self, TnyMimePart *part, TnyH
  * @self: a #ModestFormatter
  * @part: a non-NULL #TnyMimePart with the body of the original message
  * @header: a non-NULL #TnyHeader of the original message
+ * @attachments: a #GList of attachments
  * 
  * Creates a new message with a text body made from the body of the
  * original message inlined ready to be forwarded. This function is
@@ -152,7 +155,7 @@ TnyMsg * modest_formatter_quote  (ModestFormatter *self, TnyMimePart *part, TnyH
  *
  * Returns: a newly formatted #TnyMsg or NULL in case of error
  **/
-TnyMsg * modest_formatter_inline (ModestFormatter *self, TnyMimePart *part, TnyHeader *header, gboolean has_attachments);
+TnyMsg * modest_formatter_inline (ModestFormatter *self, TnyMimePart *part, TnyHeader *header, GList *attachments);
 
 /**
  * modest_formatter_attach:
index 874c0f9..f35a127 100644 (file)
@@ -101,11 +101,13 @@ static int      get_breakpoint          (const gchar * s, const gint indent, con
 static gchar*   modest_text_utils_quote_plain_text (const gchar *text, 
                                                    const gchar *cite, 
                                                    const gchar *signature,
+                                                   GList *attachments, 
                                                    int limit);
 
 static gchar*   modest_text_utils_quote_html       (const gchar *text, 
                                                    const gchar *cite,
                                                    const gchar *signature,
+                                                   GList *attachments,
                                                    int limit);
 static gchar*   get_email_from_address (const gchar *address);
 
@@ -120,6 +122,7 @@ modest_text_utils_quote (const gchar *text,
                         const gchar *signature,
                         const gchar *from,
                         const time_t sent_date, 
+                        GList *attachments,
                         int limit)
 {
        gchar *retval, *cited;
@@ -132,9 +135,9 @@ modest_text_utils_quote (const gchar *text,
        if (content_type && strcmp (content_type, "text/html") == 0)
                /* TODO: extract the <body> of the HTML and pass it to
                   the function */
-               retval = modest_text_utils_quote_html (text, cited, signature, limit);
+               retval = modest_text_utils_quote_html (text, cited, signature, attachments, limit);
        else
-               retval = modest_text_utils_quote_plain_text (text, cited, signature, limit);
+               retval = modest_text_utils_quote_plain_text (text, cited, signature, attachments, limit);
        
        g_free (cited);
 
@@ -331,7 +334,7 @@ modest_text_utils_convert_buffer_to_html (GString *html, const gchar *data)
                case '\t' : g_string_append (html, "&nbsp;&nbsp;&nbsp; "); break; /* note the space at the end*/
                case ' ':
                        if (space_seen) { /* second space in a row */
-                               g_string_append (html, "&nbsp ");
+                               g_string_append (html, "&nbsp; ");
                                space_seen = FALSE;
                        } else
                                space_seen = TRUE;
@@ -676,29 +679,40 @@ get_breakpoint (const gchar * s, const gint indent, const gint limit)
 static gchar *
 cite (const time_t sent_date, const gchar *from)
 {
-       gchar sent_str[101];
-
-       /* format sent_date */
-       modest_text_utils_strftime (sent_str, 100, "%c", sent_date);
-       return g_strdup_printf (N_("On %s, %s wrote:\n"), 
-                               sent_str, 
-                               (from) ? from : EMPTY_STRING);
+       return g_strdup (_("mcen_ia_editor_original_message"));
 }
 
+static gchar *
+quoted_attachments (GList *attachments)
+{
+       GList *node = NULL;
+       GString *result = g_string_new ("");
+       for (node = attachments; node != NULL; node = g_list_next (node)) {
+               gchar *filename = (gchar *) node->data;
+               g_string_append_printf ( result, "%s %s\n", _("mcen_ia_editor_attach_filename"), filename);
+       }
+
+       return g_string_free (result, FALSE);
+
+}
 
 static gchar *
 modest_text_utils_quote_plain_text (const gchar *text, 
                                    const gchar *cite, 
                                    const gchar *signature,
+                                   GList *attachments,
                                    int limit)
 {
        const gchar *iter;
        gint indent, breakpoint, rem_indent = 0;
        GString *q, *l, *remaining;
        gsize len;
+       gchar *attachments_string = NULL;
 
        /* remaining will store the rest of the line if we have to break it */
-       q = g_string_new (cite);
+       q = g_string_new ("\n");
+       q = g_string_append (q, cite);
+       q = g_string_append_c (q, '\n');
        remaining = g_string_new ("");
 
        iter = text;
@@ -741,6 +755,10 @@ modest_text_utils_quote_plain_text (const gchar *text,
                g_string_free (l, TRUE);
        } while ((iter < text + len) || (remaining->str[0]));
 
+       attachments_string = quoted_attachments (attachments);
+       q = g_string_append (q, attachments_string);
+       g_free (attachments_string);
+
        if (signature != NULL) {
                q = g_string_append_c (q, '\n');
                q = g_string_append (q, signature);
@@ -753,6 +771,7 @@ static gchar*
 modest_text_utils_quote_html (const gchar *text, 
                              const gchar *cite, 
                              const gchar *signature,
+                             GList *attachments,
                              int limit)
 {
        gchar *result = NULL;
@@ -761,22 +780,26 @@ modest_text_utils_quote_html (const gchar *text,
                "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n" \
                "<html>\n" \
                "<body>\n" \
-               "%s" \
-               "<blockquote type=\"cite\">\n%s\n</blockquote>\n" \
-               "%s" \
+               "<br/>%s<br/>" \
+               "<pre>%s<br/>%s<br/>%s</pre>\n" \
                "</body>\n" \
                "</html>\n";
-       const gchar *signature_format =         \
-               "<pre>\n" \
-               "%s\n" \
-               "</pre>";
+       gchar *attachments_string = NULL;
+       gchar *q_attachments_string = NULL;
+       gchar *html_text = NULL;
 
        if (signature == NULL)
                signature_result = g_strdup ("");
        else
-               signature_result = g_strdup_printf (signature_format, signature);
-
-       result = g_strdup_printf (format, cite, text, signature_result);
+               signature_result = modest_text_utils_convert_to_html_body (signature);
+
+       attachments_string = quoted_attachments (attachments);
+       q_attachments_string = modest_text_utils_convert_to_html_body (attachments_string);
+       html_text = modest_text_utils_convert_to_html_body (text);
+       result = g_strdup_printf (format, signature_result, cite, html_text, q_attachments_string);
+       g_free (html_text);
+       g_free (attachments_string);
+       g_free (q_attachments_string);
        g_free (signature_result);
        return result;
 }
index d896346..2248d90 100644 (file)
@@ -60,6 +60,7 @@ gchar* modest_text_utils_derived_subject (const gchar *subject,
  * @content_type: the non-NULL content type for the quoting, e.g. "text/html"
  * @signature: NULL or the signature to add
  * @sent_date: sent date/time of the original message
+ * @attachments: a #GList of the attachments
  * @limit: specifies the maximum characters per line in the quoted text
  * 
  * quote an existing message
@@ -71,6 +72,7 @@ gchar* modest_text_utils_quote (const gchar *text,
                                const gchar *signature,
                                const gchar *from,
                                const time_t sent_date, 
+                               GList *attachments,
                                int limit);
 
 
index 6cf08c1..d88bcf3 100644 (file)
@@ -347,7 +347,7 @@ modest_tny_msg_find_body_part (TnyMsg *msg, gboolean want_html)
 
 static TnyMsg *
 create_reply_forward_mail (TnyMsg *msg, const gchar *from, const gchar *signature, 
-                          gboolean is_reply, guint type, gboolean has_attachments)
+                          gboolean is_reply, guint type, GList *attachments)
 {
        TnyMsg *new_msg;
        TnyHeader *new_header, *header;
@@ -375,14 +375,14 @@ create_reply_forward_mail (TnyMsg *msg, const gchar *from, const gchar *signatur
                        new_msg = modest_formatter_cite  (formatter, body, header);
                        break;
                case MODEST_TNY_MSG_REPLY_TYPE_QUOTE:
-                       new_msg = modest_formatter_quote (formatter, body, header);
+                       new_msg = modest_formatter_quote (formatter, body, header, attachments);
                        break;
                }
        } else {
                switch (type) {
                case MODEST_TNY_MSG_FORWARD_TYPE_INLINE:
                default:
-                       new_msg = modest_formatter_inline  (formatter, body, header, has_attachments);
+                       new_msg = modest_formatter_inline  (formatter, body, header, attachments);
                        break;
                case MODEST_TNY_MSG_FORWARD_TYPE_ATTACHMENT:
                        new_msg = modest_formatter_attach (formatter, body, header);
@@ -439,13 +439,12 @@ modest_tny_msg_create_forward_msg (TnyMsg *msg,
        TnyList *parts = NULL;
        GList *attachments_list = NULL;
 
-
        /* Add attachments */
        parts = TNY_LIST (tny_simple_list_new());
        tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
        tny_list_foreach (parts, add_if_attachment, &attachments_list);
 
-       new_msg = create_reply_forward_mail (msg, from, signature, FALSE, forward_type, (attachments_list != NULL));
+       new_msg = create_reply_forward_mail (msg, from, signature, FALSE, forward_type, attachments_list);
        add_attachments (new_msg, attachments_list);
 
        /* Clean */
@@ -469,8 +468,20 @@ modest_tny_msg_create_reply_msg (TnyMsg *msg,
        gchar *new_cc = NULL;
        const gchar *cc = NULL, *bcc = NULL;
        GString *tmp = NULL;
+       TnyList *parts = NULL;
+       GList *attachments_list = NULL;
 
-       new_msg = create_reply_forward_mail (msg, from, signature, TRUE, reply_type, FALSE);
+       /* Add attachments */
+       parts = TNY_LIST (tny_simple_list_new());
+       tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
+       tny_list_foreach (parts, add_if_attachment, &attachments_list);
+
+       new_msg = create_reply_forward_mail (msg, from, signature, TRUE, reply_type, attachments_list);
+       if (attachments_list) {
+               g_list_foreach (attachments_list, (GFunc) g_object_unref, NULL);
+               g_list_free (attachments_list);
+       }
+       g_object_unref (G_OBJECT (parts));
 
        /* Fill the header */
        header = tny_msg_get_header (msg);