2 * Claws Mail -- A GTK+ based, lightweight, and fast e-mail client
3 * Copyright(C) 1999-2015 the Claws Mail Team
5 * This file Copyright (C) 2009-2015 Salvatore De Paolis
6 * <iwkse@claws-mail.org> and the Claws Mail Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write tothe Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "claws-features.h"
26 #include "common/utils.h"
27 #include "mainwindow.h"
28 #include "statusbar.h"
29 #include "lh_viewer.h"
31 static gchar *content_types[] = { "text/html", NULL };
33 MimeViewer *lh_viewer_create();
35 MimeViewerFactory lh_viewer_factory = {
41 static GtkWidget *lh_get_widget(MimeViewer *_viewer)
43 debug_print("LH: get_widget\n");
44 LHViewer *viewer = (LHViewer *)_viewer;
48 static gchar *get_utf8_string(const gchar *string) {
54 if (!g_utf8_validate(string, -1, NULL)) {
55 const gchar *cur_locale = conv_get_current_locale();
56 gchar* split = g_strstr_len(cur_locale, -1, ".");
60 locale = (gchar *) cur_locale;
62 debug_print("Try converting to UTF-8 from %s\n", locale);
63 if (g_ascii_strcasecmp("utf-8", locale) != 0) {
64 utf8 = g_convert(string, -1, "utf-8", locale, NULL, &length, &error);
66 debug_print("Failed convertion to current locale: %s\n", error->message);
67 g_clear_error(&error);
71 debug_print("Use iso-8859-1 as last resort\n");
72 utf8 = g_convert(string, -1, "utf-8", "iso-8859-1", NULL, &length, &error);
74 debug_print("Charset detection failed. Use text as is\n");
75 utf8 = g_strdup(string);
76 g_clear_error(&error);
80 utf8 = g_strdup(string);
86 static void lh_show_mimepart(MimeViewer *_viewer, const gchar *infole,
89 debug_print("LH: show_mimepart\n");
90 LHViewer *viewer = (LHViewer *)_viewer;
92 gchar *msgfile = procmime_get_tmp_file_name(partinfo);
93 debug_print("LH: msgfile '%s'\n", msgfile);
95 if (procmime_get_part(msgfile, partinfo) < 0) {
96 debug_print("LH: couldn't get MIME part file\n");
101 gchar *contents, *utf8;
103 GError *error = NULL;
104 if (!g_file_get_contents(msgfile, &contents, &length, &error)) {
105 g_warning("LiteHTML viewer: couldn't read contents of file '%s': %s",
106 msgfile, error->message);
107 g_clear_error(&error);
110 utf8 = get_utf8_string(contents);
116 lh_widget_open_html(viewer->widget, utf8);
120 static void lh_clear_viewer(MimeViewer *_viewer)
122 debug_print("LH: clear_viewer\n");
123 LHViewer *viewer = (LHViewer *)_viewer;
124 lh_widget_clear(viewer->widget);
127 static void lh_destroy_viewer(MimeViewer *_viewer)
129 debug_print("LH: destroy_viewer\n");
132 lh_clear_viewer(_viewer);
134 // LHViewer *viewer = (LHViewer *)_viewer;
135 // lh_widget_destroy(viewer->widget);
138 static void lh_print_viewer (MimeViewer *_viewer)
140 debug_print("LH: print_viewer\n");
142 LHViewer* viewer = (LHViewer *) _viewer;
143 lh_widget_print(viewer->widget);
146 /***************************************************************/
147 MimeViewer *lh_viewer_create()
149 debug_print("LH: viewer_create\n");
151 LHViewer *viewer = g_new0(LHViewer, 1);
152 viewer->mimeviewer.factory = &lh_viewer_factory;
153 viewer->widget = lh_widget_new();
155 viewer->mimeviewer.get_widget = lh_get_widget;
156 viewer->mimeviewer.show_mimepart = lh_show_mimepart;
158 viewer->mimeviewer.clear_viewer = lh_clear_viewer;
159 viewer->mimeviewer.destroy_viewer = lh_destroy_viewer;
161 viewer->mimeviewer.print = lh_print_viewer;
163 viewer->vbox = gtk_vbox_new(FALSE, 0);
165 GtkWidget *w = lh_widget_get_widget(viewer->widget);
166 gtk_box_pack_start(GTK_BOX(viewer->vbox), w,
169 gtk_widget_show_all(viewer->vbox);
171 return (MimeViewer *)viewer;
174 void lh_widget_statusbar_push(const gchar* msg)
176 MainWindow *mainwin = mainwindow_get_mainwindow();
177 STATUSBAR_PUSH(mainwin, msg);
180 void lh_widget_statusbar_pop()
182 MainWindow *mainwin = mainwindow_get_mainwindow();
183 STATUSBAR_POP(mainwin);