Fix leak on error variable
[claws.git] / src / plugins / litehtml_viewer / lh_viewer.c
index fe5727f2bcc3dd54d8cebf243d9ed13a64787a91..2d2fc504431ad9aa8ae417ddc702551372d6ecaf 100644 (file)
@@ -1,9 +1,7 @@
 /*
  * Claws Mail -- A GTK+ based, lightweight, and fast e-mail client
- * Copyright(C) 1999-2015 the Claws Mail Team
- * == Fancy Plugin ==
- * This file Copyright (C) 2009-2015 Salvatore De Paolis
- * <iwkse@claws-mail.org> and the Claws Mail Team
+ * Copyright(C) 2019 the Claws Mail Team
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 3 of the License, or
@@ -83,36 +81,37 @@ static gchar *get_utf8_string(const gchar *string) {
        return utf8;
 }
 
-static void lh_show_mimepart(MimeViewer *_viewer, const gchar *infole,
+static void lh_show_mimepart(MimeViewer *_viewer, const gchar *infile,
                MimeInfo *partinfo)
 {
        debug_print("LH: show_mimepart\n");
        LHViewer *viewer = (LHViewer *)_viewer;
+       gchar *string = procmime_get_part_as_string(partinfo, TRUE);
+       gchar *utf8 = NULL;
 
-       gchar *msgfile = procmime_get_tmp_file_name(partinfo);
-       debug_print("LH: msgfile '%s'\n", msgfile);
-
-       if (procmime_get_part(msgfile, partinfo) < 0) {
-               debug_print("LH: couldn't get MIME part file\n");
-               g_free(msgfile);
+       if (string == NULL) {
+               g_warning("LH: couldn't get MIME part file\n");
                return;
        }
 
-       gchar *contents, *utf8;
-       gsize length;
-       GError *error = NULL;
-       if (!g_file_get_contents(msgfile, &contents, &length, &error)) {
-               g_warning("LiteHTML viewer: couldn't read contents of file '%s': %s",
-                               msgfile, error->message);
-               g_clear_error(&error);
-               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);
+                       g_error_free(error);
+                       return;
+               }
+               debug_print("LH: successfully converted %" G_GSIZE_FORMAT " bytes\n", length);
        } else {
-               utf8 = get_utf8_string(contents);
-               g_free(contents);
+               utf8 = string;
        }
 
-       g_free(msgfile);
-
+       lh_widget_set_partinfo(viewer->widget, partinfo);
        lh_widget_open_html(viewer->widget, utf8);
        g_free(utf8);
 }
@@ -126,13 +125,10 @@ static void lh_clear_viewer(MimeViewer *_viewer)
 
 static void lh_destroy_viewer(MimeViewer *_viewer)
 {
-       debug_print("LH: destroy_viewer\n");
-
-       /* Just in case. */
-       lh_clear_viewer(_viewer);
+       LHViewer *viewer = (LHViewer *)_viewer;
 
-//     LHViewer *viewer = (LHViewer *)_viewer;
-//     lh_widget_destroy(viewer->widget);
+       debug_print("LH: destroy_viewer\n");
+       g_free(viewer);
 }
 
 static void lh_print_viewer (MimeViewer *_viewer)