Don't show mojibake when encoding is not UTF-8
authorRicardo Mones <ricardo@mones.org>
Fri, 26 Jul 2019 10:31:57 +0000 (12:31 +0200)
committerRicardo Mones <ricardo@mones.org>
Fri, 26 Jul 2019 10:31:57 +0000 (12:31 +0200)
Litehtml expect displayed strings in UTF-8, so convert message part
before displaying.

src/plugins/litehtml_viewer/lh_viewer.c

index a567d2c..bf6435b 100644 (file)
@@ -86,13 +86,30 @@ static void lh_show_mimepart(MimeViewer *_viewer, const gchar *infile,
 {
        debug_print("LH: show_mimepart\n");
        LHViewer *viewer = (LHViewer *)_viewer;
-       gchar *utf8 = procmime_get_part_as_string(partinfo, TRUE);
+       gchar *string = procmime_get_part_as_string(partinfo, TRUE);
+       gchar *utf8 = NULL;
 
-       if (utf8 == NULL) {
+       if (string == NULL) {
                g_warning("LH: couldn't get MIME part file\n");
                return;
        }
 
+       gchar *charset = procmime_mimeinfo_get_parameter(partinfo, "charset");
+       if (charset != NULL && g_ascii_strcasecmp("utf-8", charset) != 0) {
+               gsize length;
+               GError *error = NULL;
+               debug_print("LH: converting mimepart to UTF-8 from %s\n", charset);
+               utf8 = g_convert(string, -1, "utf-8", charset, NULL, &length, &error);
+               if (error) {
+                       g_warning("LH: failed mimepart conversion to UTF-8: %s", error->message);
+                       g_free(string);
+                       return;
+               }
+               debug_print("LH: successfully converted %" G_GSIZE_FORMAT " bytes\n", length);
+       } else {
+               utf8 = string;
+       }
+
        lh_widget_set_partinfo(viewer->widget, partinfo);
        lh_widget_open_html(viewer->widget, utf8);
        g_free(utf8);