[balsa/75-html-display-printing] body: Use a key for HTML selection storage



commit 1dfa6c67253fb7ef648d90f43242576f2133715e
Author: Peter Bloomfield <PeterBloomfield bellsouth net>
Date:   Sat May 7 21:31:37 2022 -0600

    body: Use a key for HTML selection storage
    
    Use the BalsaMessage as a key to store and retrieve the HTML vs
    text/plain selection for a multi-part-alternative body.

 libbalsa/body.c      | 40 +++++++++++++++++++++++++++-------------
 libbalsa/body.h      |  8 +++++---
 src/balsa-message.c  | 14 +++++++-------
 src/main-window.c    |  3 ++-
 src/message-window.c |  2 +-
 src/print-gtk.c      | 23 +++++++++++++++++------
 src/print.h          |  2 +-
 src/sendmsg-window.c |  2 +-
 8 files changed, 61 insertions(+), 33 deletions(-)
---
diff --git a/libbalsa/body.c b/libbalsa/body.c
index f0a56a967..9ef3b13fb 100644
--- a/libbalsa/body.c
+++ b/libbalsa/body.c
@@ -93,8 +93,11 @@ libbalsa_message_body_free(LibBalsaMessageBody * body)
     libbalsa_message_body_free(body->parts);
 
     if (body->mime_part)
-       g_object_unref(body->mime_part);        
-    
+       g_object_unref(body->mime_part);
+
+    if (body->selection_table != NULL)
+        g_hash_table_destroy(body->selection_table);
+
     g_free(body);
 }
 
@@ -997,7 +1000,7 @@ find_mp_alt_parent(const LibBalsaMessageBody *body)
  * @sa find_mp_alt_parent(), libbalsa_html_type()
  */
 void
-libbalsa_message_body_set_mp_alt_selection(LibBalsaMessageBody *body)
+libbalsa_message_body_set_mp_alt_selection(LibBalsaMessageBody *body, gpointer key)
 {
        LibBalsaMessageBody *mp_alt_body;
 
@@ -1005,18 +1008,27 @@ libbalsa_message_body_set_mp_alt_selection(LibBalsaMessageBody *body)
                mp_alt_body = find_mp_alt_parent(body);
                if (mp_alt_body != NULL) {
                        gchar *conttype;
+                        LibBalsaMpAltSelection selection;
 
                        conttype = libbalsa_message_body_get_mime_type(body);
                        if (libbalsa_html_type(conttype) != LIBBALSA_HTML_TYPE_NONE) {
-                               mp_alt_body->mp_alt_selection = LIBBALSA_MP_ALT_HTML;
+                               selection = LIBBALSA_MP_ALT_HTML;
                        } else {
-                               mp_alt_body->mp_alt_selection = LIBBALSA_MP_ALT_PLAIN;
+                               selection = LIBBALSA_MP_ALT_PLAIN;
                        }
                        g_free(conttype);
+
+                        if (mp_alt_body->selection_table == NULL)
+                            mp_alt_body->selection_table = g_hash_table_new(NULL, NULL);
+                        g_hash_table_insert(mp_alt_body->selection_table, key,
+                                            GINT_TO_POINTER(selection));
                }
        }
 }
 
+static inline gboolean body_is_type(const LibBalsaMessageBody *body,
+                                    const gchar               *type,
+                                    const gchar               *sub_type);
 
 /** @brief Check if a multipart/alternative HTML or plain part is selected
  *
@@ -1026,17 +1038,19 @@ libbalsa_message_body_set_mp_alt_selection(LibBalsaMessageBody *body)
  * @sa find_mp_alt_parent()
  */
 LibBalsaMpAltSelection
-libbalsa_message_body_get_mp_alt_selection(LibBalsaMessageBody *body)
+libbalsa_message_body_get_mp_alt_selection(LibBalsaMessageBody *body, gpointer key)
 {
-       LibBalsaMessageBody *mp_alt_body;
-       LibBalsaMpAltSelection result = LIBBALSA_MP_ALT_AUTO;
+       LibBalsaMpAltSelection selection = LIBBALSA_MP_ALT_AUTO;
 
        g_return_val_if_fail(body != NULL, FALSE);
-       mp_alt_body = find_mp_alt_parent(body);
-       if (mp_alt_body != NULL) {
-               result = mp_alt_body->mp_alt_selection;
-       }
-       return result;
+
+       if (!body_is_type(body, "multipart", "alternative"))
+            body = find_mp_alt_parent(body);
+
+       if (body != NULL && body->selection_table != NULL)
+               selection = GPOINTER_TO_INT(g_hash_table_lookup(body->selection_table, key));
+
+       return selection;
 }
 
 #endif /*HAVE_HTML_WIDGET*/
diff --git a/libbalsa/body.h b/libbalsa/body.h
index 45193c577..5affc4c8f 100644
--- a/libbalsa/body.h
+++ b/libbalsa/body.h
@@ -93,7 +93,7 @@ struct _LibBalsaMessageBody {
 
 #ifdef HAVE_HTML_WIDGET
     gboolean html_ext_loaded;  /* if external HTML content was loaded */
-    LibBalsaMpAltSelection mp_alt_selection;   /* which part of a multipart/alternative is selected */
+    GHashTable *selection_table; /* which part of a multipart/alternative is selected for a given key*/
 #endif /* HAVE_HTML_WIDGET */
 
     LibBalsaMessageBody *parent;       /* Parent part in the message */
@@ -147,8 +147,10 @@ LibBalsaMessageBody *libbalsa_message_body_get_by_id(LibBalsaMessageBody *
 LibBalsaMessageBody *libbalsa_message_body_mp_related_root(LibBalsaMessageBody *body);
 
 #ifdef HAVE_HTML_WIDGET
-void libbalsa_message_body_set_mp_alt_selection(LibBalsaMessageBody *body);
-LibBalsaMpAltSelection libbalsa_message_body_get_mp_alt_selection(LibBalsaMessageBody *body);
+void libbalsa_message_body_set_mp_alt_selection(LibBalsaMessageBody *body,
+                                                gpointer key);
+LibBalsaMpAltSelection libbalsa_message_body_get_mp_alt_selection(LibBalsaMessageBody *body,
+                                                                  gpointer key);
 #else
 #define libbalsa_message_body_set_mp_alt_selection(x)
 #define libbalsa_message_body_get_mp_alt_selection(x)  LIBBALSA_MP_ALT_AUTO
diff --git a/src/balsa-message.c b/src/balsa-message.c
index d7bf39888..eb3ea42f7 100644
--- a/src/balsa-message.c
+++ b/src/balsa-message.c
@@ -2011,15 +2011,15 @@ balsa_message_has_previous_part(BalsaMessage * balsa_message)
 
 #ifdef HAVE_HTML_WIDGET
 static gboolean
-libbalsa_can_display(LibBalsaMessageBody *part, InternetAddressList *from)
+libbalsa_can_display(LibBalsaMessageBody *part, InternetAddressList *from, gpointer key)
 {
        gchar *content_type = libbalsa_message_body_get_mime_type(part);
        gboolean res;
 
        if (strcmp(content_type, "multipart/related") == 0) {
-               res = (part->parts != NULL) ? libbalsa_can_display(part->parts, from) : FALSE;
+               res = (part->parts != NULL) ? libbalsa_can_display(part->parts, from, key) : FALSE;
        } else {
-               switch (libbalsa_message_body_get_mp_alt_selection(part)) {
+               switch (libbalsa_message_body_get_mp_alt_selection(part, key)) {
                case LIBBALSA_MP_ALT_AUTO:
                        res = (!balsa_app.display_alt_plain || libbalsa_html_get_prefer_html(from)) &&
                                (libbalsa_html_type(content_type) != LIBBALSA_HTML_TYPE_NONE);
@@ -2054,7 +2054,7 @@ libbalsa_can_display(LibBalsaMessageBody *part, InternetAddressList *from)
    In the case as above, B & C should be displayed.
 */
 static LibBalsaMessageBody*
-preferred_part(LibBalsaMessageBody *parts, InternetAddressList *from)
+preferred_part(LibBalsaMessageBody *parts, InternetAddressList *from, gpointer key)
 {
     LibBalsaMessageBody *body, *preferred = parts;
 
@@ -2067,7 +2067,7 @@ preferred_part(LibBalsaMessageBody *parts, InternetAddressList *from)
             strcmp(content_type, "text/calendar") == 0)
             preferred = body;
 #ifdef HAVE_HTML_WIDGET
-        else if (libbalsa_can_display(body, from))
+        else if (libbalsa_can_display(body, from, key))
             preferred = body;
 #endif                          /* HAVE_HTML_WIDGET */
 
@@ -2195,7 +2195,7 @@ add_multipart(BalsaMessage *balsa_message, LibBalsaMessageBody *body,
 
         from = libbalsa_message_get_headers(balsa_message->message)->from;
         /* Add the most suitable part. */
-        body = add_body(balsa_message, preferred_part(body->parts, from), container);
+        body = add_body(balsa_message, preferred_part(body->parts, from, balsa_message), container);
     } else if (g_mime_content_type_is_type(type, "multipart", "digest")) {
        body = add_multipart_digest(balsa_message, body->parts, container);
     } else { /* default to multipart/mixed */
@@ -2291,7 +2291,7 @@ select_part(BalsaMessage * balsa_message, BalsaPartInfo *info)
     body = add_part(balsa_message, info,
                     balsa_mime_widget_get_container(balsa_message->bm_widget));
     balsa_message->current_part = part_info_from_body(balsa_message, body);
-    libbalsa_message_body_set_mp_alt_selection(body);
+    libbalsa_message_body_set_mp_alt_selection(body, balsa_message);
 
     g_signal_emit(balsa_message, balsa_message_signals[SELECT_PART], 0);
 
diff --git a/src/main-window.c b/src/main-window.c
index e013e7ea5..545c9ce28 100644
--- a/src/main-window.c
+++ b/src/main-window.c
@@ -1177,6 +1177,7 @@ print_activated(GSimpleAction * action,
                 gpointer        user_data)
 {
     BalsaWindow *window = BALSA_WINDOW(user_data);
+    BalsaWindowPrivate *priv = balsa_window_get_instance_private(window);
     GtkWidget *widget;
     BalsaIndex *bindex;
     guint current_msgno;
@@ -1195,7 +1196,7 @@ print_activated(GSimpleAction * action,
         if (message == NULL)
             return;
 
-        message_print(message, GTK_WINDOW(window));
+        message_print(message, GTK_WINDOW(window), priv->preview);
         g_object_unref(message);
     }
 }
diff --git a/src/message-window.c b/src/message-window.c
index 46c5413b3..3a523c4dd 100644
--- a/src/message-window.c
+++ b/src/message-window.c
@@ -720,7 +720,7 @@ mw_print_activated(GSimpleAction * action, GVariant * parameter,
 {
     MessageWindow *mw = (MessageWindow *) data;
 
-    message_print(mw->message, GTK_WINDOW(mw->window));
+    message_print(mw->message, GTK_WINDOW(mw->window), mw->bmessage);
 }
 
 static void
diff --git a/src/print-gtk.c b/src/print-gtk.c
index 521d0662d..f03a65d48 100644
--- a/src/print-gtk.c
+++ b/src/print-gtk.c
@@ -67,6 +67,9 @@ typedef struct {
     /* page footer related stuff */
     gchar *footer;
     gdouble c_footer_y;
+
+    /* key used to find the mp_alt_selection */
+    gpointer mp_alt_selection_key;
 } BalsaPrintData;
 
 
@@ -209,7 +212,7 @@ select_from_mp_alt(LibBalsaMessageBody *parts, gboolean html_part)
  */
 static GList *
 scan_body(GList *bpo_list, GtkPrintContext * context, BalsaPrintSetup * psetup,
-         LibBalsaMessageBody * body, gboolean no_first_sep)
+         LibBalsaMessageBody * body, gboolean no_first_sep, gpointer key)
 {
     gboolean add_signature;
     gboolean have_crypto_frame;
@@ -249,7 +252,9 @@ scan_body(GList *bpo_list, GtkPrintContext * context, BalsaPrintSetup * psetup,
 
                if (strcmp(conttype, "multipart/alternative") == 0) {
 #ifdef HAVE_HTML_WIDGET
-                       print_part = select_from_mp_alt(body->parts, body->mp_alt_selection == 
LIBBALSA_MP_ALT_HTML);
+                        LibBalsaMpAltSelection selection =
+                            libbalsa_message_body_get_mp_alt_selection(body, key);
+                       print_part = select_from_mp_alt(body->parts, selection == LIBBALSA_MP_ALT_HTML);
 #else
                        print_part = select_from_mp_alt(body->parts, FALSE);
 #endif
@@ -262,7 +267,10 @@ scan_body(GList *bpo_list, GtkPrintContext * context, BalsaPrintSetup * psetup,
                        mp_rel_root_type = libbalsa_message_body_get_mime_type(mp_rel_root);
                        if (strcmp(mp_rel_root_type, "multipart/alternative") == 0) {
 #ifdef HAVE_HTML_WIDGET
-                               print_part = select_from_mp_alt(mp_rel_root->parts, 
mp_rel_root->mp_alt_selection == LIBBALSA_MP_ALT_HTML);
+                                LibBalsaMpAltSelection selection =
+                                    libbalsa_message_body_get_mp_alt_selection(mp_rel_root, key);
+                               print_part = select_from_mp_alt(mp_rel_root->parts,
+                                                                selection == LIBBALSA_MP_ALT_HTML);
 #else
                                print_part = select_from_mp_alt(mp_rel_root->parts, FALSE);
 #endif
@@ -272,7 +280,7 @@ scan_body(GList *bpo_list, GtkPrintContext * context, BalsaPrintSetup * psetup,
                        g_free(mp_rel_root_type);
                        bpo_list = print_single_part(bpo_list, context, psetup, print_part, no_first_sep, 
add_signature);
                } else {
-                       bpo_list = scan_body(bpo_list, context, psetup, body->parts, no_first_sep);
+                       bpo_list = scan_body(bpo_list, context, psetup, body->parts, no_first_sep, key);
                }
                no_first_sep = FALSE;
        }
@@ -419,7 +427,8 @@ begin_print(GtkPrintOperation * operation, GtkPrintContext * context,
     /* add the mime bodies */
     pdata->print_parts = 
        scan_body(pdata->print_parts, context, &pdata->setup,
-                 libbalsa_message_get_body_list(pdata->message), FALSE);
+                 libbalsa_message_get_body_list(pdata->message), FALSE,
+                  pdata->mp_alt_selection_key);
 
     /* done */
     gtk_print_operation_set_n_pages(operation, pdata->setup.page_count);
@@ -729,7 +738,8 @@ message_print_page_setup(GtkWindow * parent)
 
 
 void
-message_print(LibBalsaMessage * msg, GtkWindow * parent)
+message_print(LibBalsaMessage * msg, GtkWindow * parent,
+              gpointer mp_alt_selection_key)
 {
     GtkPrintOperation *print;
     GtkPrintOperationResult res;
@@ -756,6 +766,7 @@ message_print(LibBalsaMessage * msg, GtkWindow * parent)
     /* create a print context */
     print_data = g_new0(BalsaPrintData, 1);
     print_data->message = msg;
+    print_data->mp_alt_selection_key = mp_alt_selection_key;
 
     g_signal_connect(print, "begin_print", G_CALLBACK(begin_print), print_data);
     g_signal_connect(print, "draw_page", G_CALLBACK(draw_page), print_data);
diff --git a/src/print.h b/src/print.h
index 2dcd7df8d..12dbddde2 100644
--- a/src/print.h
+++ b/src/print.h
@@ -26,7 +26,7 @@
 
 G_BEGIN_DECLS
 
-    void message_print(LibBalsaMessage * msg, GtkWindow * parent);
+    void message_print(LibBalsaMessage * msg, GtkWindow * parent, gpointer mp_alt_selection_key);
     void message_print_page_setup(GtkWindow * parent);
 
 G_END_DECLS
diff --git a/src/sendmsg-window.c b/src/sendmsg-window.c
index c9adcf805..f24d75199 100644
--- a/src/sendmsg-window.c
+++ b/src/sendmsg-window.c
@@ -5676,7 +5676,7 @@ sw_print_activated(GSimpleAction * action,
     LibBalsaMessage *message;
 
     message = bsmsg2message(bsmsg);
-    message_print(message, GTK_WINDOW(bsmsg->window));
+    message_print(message, GTK_WINDOW(bsmsg->window), NULL);
     g_object_unref(message);
 }
 


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