2 * Claws Mail -- A GTK+ based, lightweight, and fast e-mail client
3 * Copyright(C) 2019 the Claws Mail Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write tothe Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "claws-features.h"
24 #include "common/utils.h"
25 #include "mainwindow.h"
26 #include "statusbar.h"
27 #include "lh_viewer.h"
29 static gchar *content_types[] = { "text/html", NULL };
31 MimeViewer *lh_viewer_create();
33 MimeViewerFactory lh_viewer_factory = {
39 static GtkWidget *lh_get_widget(MimeViewer *_viewer)
41 debug_print("LH: get_widget\n");
42 LHViewer *viewer = (LHViewer *)_viewer;
46 static gchar *get_utf8_string(const gchar *string) {
52 if (!g_utf8_validate(string, -1, NULL)) {
53 const gchar *cur_locale = conv_get_current_locale();
54 gchar* split = g_strstr_len(cur_locale, -1, ".");
58 locale = (gchar *) cur_locale;
60 debug_print("Try converting to UTF-8 from %s\n", locale);
61 if (g_ascii_strcasecmp("utf-8", locale) != 0) {
62 utf8 = g_convert(string, -1, "utf-8", locale, NULL, &length, &error);
64 debug_print("Failed convertion to current locale: %s\n", error->message);
65 g_clear_error(&error);
69 debug_print("Use iso-8859-1 as last resort\n");
70 utf8 = g_convert(string, -1, "utf-8", "iso-8859-1", NULL, &length, &error);
72 debug_print("Charset detection failed. Use text as is\n");
73 utf8 = g_strdup(string);
74 g_clear_error(&error);
78 utf8 = g_strdup(string);
84 static void lh_show_mimepart(MimeViewer *_viewer, const gchar *infile,
87 debug_print("LH: show_mimepart\n");
88 LHViewer *viewer = (LHViewer *)_viewer;
89 gchar *string = procmime_get_part_as_string(partinfo, TRUE);
93 g_warning("LH: couldn't get MIME part file\n");
97 gchar *charset = procmime_mimeinfo_get_parameter(partinfo, "charset");
98 if (charset != NULL && g_ascii_strcasecmp("utf-8", charset) != 0) {
100 GError *error = NULL;
101 debug_print("LH: converting mimepart to UTF-8 from %s\n", charset);
102 utf8 = g_convert(string, -1, "utf-8", charset, NULL, &length, &error);
104 g_warning("LH: failed mimepart conversion to UTF-8: %s", error->message);
109 debug_print("LH: successfully converted %" G_GSIZE_FORMAT " bytes\n", length);
114 lh_widget_set_partinfo(viewer->widget, partinfo);
115 lh_widget_open_html(viewer->widget, utf8);
119 static void lh_clear_viewer(MimeViewer *_viewer)
121 debug_print("LH: clear_viewer\n");
122 LHViewer *viewer = (LHViewer *)_viewer;
123 lh_widget_clear(viewer->widget);
126 static void lh_destroy_viewer(MimeViewer *_viewer)
128 LHViewer *viewer = (LHViewer *)_viewer;
130 debug_print("LH: destroy_viewer\n");
134 static void lh_print_viewer (MimeViewer *_viewer)
136 debug_print("LH: print_viewer\n");
138 LHViewer* viewer = (LHViewer *) _viewer;
139 lh_widget_print(viewer->widget);
142 /***************************************************************/
143 MimeViewer *lh_viewer_create()
145 debug_print("LH: viewer_create\n");
147 LHViewer *viewer = g_new0(LHViewer, 1);
148 viewer->mimeviewer.factory = &lh_viewer_factory;
149 viewer->widget = lh_widget_new();
151 viewer->mimeviewer.get_widget = lh_get_widget;
152 viewer->mimeviewer.show_mimepart = lh_show_mimepart;
154 viewer->mimeviewer.clear_viewer = lh_clear_viewer;
155 viewer->mimeviewer.destroy_viewer = lh_destroy_viewer;
157 viewer->mimeviewer.print = lh_print_viewer;
159 viewer->vbox = gtk_vbox_new(FALSE, 0);
161 GtkWidget *w = lh_widget_get_widget(viewer->widget);
162 gtk_box_pack_start(GTK_BOX(viewer->vbox), w,
165 gtk_widget_show_all(viewer->vbox);
167 return (MimeViewer *)viewer;
170 void lh_widget_statusbar_push(const gchar* msg)
172 MainWindow *mainwin = mainwindow_get_mainwindow();
173 STATUSBAR_PUSH(mainwin, msg);
176 void lh_widget_statusbar_pop()
178 MainWindow *mainwin = mainwindow_get_mainwindow();
179 STATUSBAR_POP(mainwin);