Queue a draw event in open_html
[claws.git] / src / plugins / litehtml_viewer / lh_viewer.c
index c8912e2219fa9f6a73c2b35a37e4f6cee3f29969..7fee13e4799dfcc9ca705090099abf30742dae20 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
@@ -24,6 +22,8 @@
 
 #include <codeconv.h>
 #include "common/utils.h"
+#include "mainwindow.h"
+#include "statusbar.h"
 #include "lh_viewer.h"
 
 static gchar *content_types[] = { "text/html", NULL };
@@ -44,20 +44,32 @@ static GtkWidget *lh_get_widget(MimeViewer *_viewer)
 }
 
 static gchar *get_utf8_string(const gchar *string) {
-        gchar *utf8;
+        gchar *utf8 = NULL;
         gsize length;
         GError *error = NULL;
+        gchar *locale = NULL;
 
        if (!g_utf8_validate(string, -1, NULL)) {
                const gchar *cur_locale = conv_get_current_locale();
-               utf8 = g_convert(string, -1, "utf-8", cur_locale, NULL, &length, &error);
-               if (error) {
-                       debug_print("Failed convertion to current locale: %s", error->message);
-                       g_error_free(error);
-                       error = NULL;
+               gchar* split = g_strstr_len(cur_locale, -1, ".");
+               if (split) {
+                   locale = ++split;
+               } else {
+                   locale = (gchar *) cur_locale;
+               }
+               debug_print("Try converting to UTF-8 from %s\n", locale);
+               if (g_ascii_strcasecmp("utf-8", locale) != 0) {
+                   utf8 = g_convert(string, -1, "utf-8", locale, NULL, &length, &error);
+                   if (error) {
+                           debug_print("Failed convertion to current locale: %s\n", error->message);
+                           g_clear_error(&error);
+                       }
+           }
+           if (!utf8) {
+               debug_print("Use iso-8859-1 as last resort\n");
                        utf8 = g_convert(string, -1, "utf-8", "iso-8859-1", NULL, &length, &error);
                        if (error) {
-                               debug_print("Charset detection failed");
+                               debug_print("Charset detection failed. Use text as is\n");
                                utf8 = g_strdup(string);
                                g_clear_error(&error);
                        }
@@ -69,36 +81,38 @@ 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;
+       const gchar *charset;
 
-       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;
+       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);
 }
@@ -112,13 +126,43 @@ static void lh_clear_viewer(MimeViewer *_viewer)
 
 static void lh_destroy_viewer(MimeViewer *_viewer)
 {
+       LHViewer *viewer = (LHViewer *)_viewer;
+
        debug_print("LH: destroy_viewer\n");
+       g_free(viewer);
+}
 
-       /* Just in case. */
-       lh_clear_viewer(_viewer);
+static void lh_print_viewer (MimeViewer *_viewer)
+{
+    debug_print("LH: print_viewer\n");
+    
+    LHViewer* viewer = (LHViewer *) _viewer;
+    lh_widget_print(viewer->widget);    
+}
 
-//     LHViewer *viewer = (LHViewer *)_viewer;
-//     lh_widget_destroy(viewer->widget);
+
+static gboolean lh_scroll_page(MimeViewer *_viewer, gboolean up)
+{
+       LHViewer *viewer = (LHViewer *)_viewer;
+       GtkAdjustment *vadj = gtk_scrolled_window_get_vadjustment(
+                                       GTK_SCROLLED_WINDOW(lh_widget_get_widget(viewer->widget)));
+
+       if (viewer->widget == NULL)
+               return FALSE;
+
+       return gtkutils_scroll_page(lh_widget_get_widget(viewer->widget), vadj, up);
+}
+
+static void lh_scroll_one_line(MimeViewer *_viewer, gboolean up)
+{
+       LHViewer *viewer = (LHViewer *)_viewer;
+       GtkAdjustment *vadj = gtk_scrolled_window_get_vadjustment(
+                                       GTK_SCROLLED_WINDOW(lh_widget_get_widget(viewer->widget)));
+
+       if (viewer->widget == NULL)
+               return;
+
+       gtkutils_scroll_one_line(lh_widget_get_widget(viewer->widget), vadj, up);
 }
 
 /***************************************************************/
@@ -136,6 +180,9 @@ MimeViewer *lh_viewer_create()
        viewer->mimeviewer.clear_viewer = lh_clear_viewer;
        viewer->mimeviewer.destroy_viewer = lh_destroy_viewer;
 
+       viewer->mimeviewer.scroll_page = lh_scroll_page;
+       viewer->mimeviewer.scroll_one_line = lh_scroll_one_line;
+
        viewer->vbox = gtk_vbox_new(FALSE, 0);
 
        GtkWidget *w = lh_widget_get_widget(viewer->widget);
@@ -147,3 +194,14 @@ MimeViewer *lh_viewer_create()
        return (MimeViewer *)viewer;
 }
 
+void lh_widget_statusbar_push(const gchar* msg)
+{
+       MainWindow *mainwin = mainwindow_get_mainwindow();
+       STATUSBAR_PUSH(mainwin, msg);
+}
+
+void lh_widget_statusbar_pop()
+{
+        MainWindow *mainwin = mainwindow_get_mainwindow();
+        STATUSBAR_POP(mainwin);
+}