Initial commit of litehtml_viewer
[claws.git] / src / plugins / litehtml_viewer / lh_viewer.c
1 /*
2  * Claws Mail -- A GTK+ based, lightweight, and fast e-mail client
3  * Copyright(C) 1999-2015 the Claws Mail Team
4  * == Fancy Plugin ==
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include <codeconv.h>
26 #include "lh_viewer.h"
27
28 static gchar *content_types[] = { "text/html", NULL };
29
30 MimeViewer *lh_viewer_create();
31
32 MimeViewerFactory lh_viewer_factory = {
33         content_types,
34         0,
35         lh_viewer_create
36 };
37
38 static GtkWidget *lh_get_widget(MimeViewer *_viewer)
39 {
40         debug_print("LH: get_widget\n");
41         LHViewer *viewer = (LHViewer *)_viewer;
42         return viewer->vbox;
43 }
44
45 static gchar *get_utf8_string(const gchar *string) {
46         gchar *utf8;
47         gsize length;
48         GError *error = NULL;
49
50         if (!g_utf8_validate(string, -1, NULL)) {
51                 const gchar *cur_locale = conv_get_current_locale();
52                 utf8 = g_convert(string, -1, "utf-8", cur_locale, NULL, &length, &error);
53                 if (error) {
54                         debug_print("Failed convertion to current locale: %s", error->message);
55                         g_error_free(error);
56                         error = NULL;
57                         utf8 = g_convert(string, -1, "utf-8", "iso-8859-1", NULL, &length, &error);
58                         if (error) {
59                                 debug_print("Charset detection failed");
60                                 utf8 = g_strdup(string);
61                                 g_error_free(error);
62                         }
63                 }
64         } else {
65                 utf8 = g_strdup(string);
66         }
67
68         return utf8;
69 }
70
71 static void lh_show_mimepart(MimeViewer *_viewer, const gchar *infole,
72                 MimeInfo *partinfo)
73 {
74         debug_print("LH: show_mimepart\n");
75         LHViewer *viewer = (LHViewer *)_viewer;
76
77         gchar *msgfile = procmime_get_tmp_file_name(partinfo);
78         debug_print("LH: msgfile '%s'\n", msgfile);
79
80         if (procmime_get_part(msgfile, partinfo) < 0) {
81                 debug_print("LH: couldn't get MIME part file\n");
82                 g_free(msgfile);
83                 return;
84         }
85
86         gchar *contents, *utf8;
87         gsize length;
88         GError *error = NULL;
89         if (!g_file_get_contents(msgfile, &contents, &length, &error)) {
90                 g_warning("LiteHTML viewer: couldn't read contents of file '%s': %s",
91                                 msgfile, error->message);
92                 g_error_free(error);
93                 return;
94         } else {
95                 utf8 = get_utf8_string(contents);
96                 g_free(contents);
97         }
98
99         g_free(msgfile);
100
101         lh_widget_open_html(viewer->widget, utf8);
102         g_free(utf8);
103 }
104
105 static void lh_clear_viewer(MimeViewer *_viewer)
106 {
107         debug_print("LH: clear_viewer\n");
108         LHViewer *viewer = (LHViewer *)_viewer;
109         lh_widget_clear(viewer->widget);
110 }
111
112 static void lh_destroy_viewer(MimeViewer *_viewer)
113 {
114         debug_print("LH: destroy_viewer\n");
115
116         /* Just in case. */
117         lh_clear_viewer(_viewer);
118
119 //      LHViewer *viewer = (LHViewer *)_viewer;
120 //      lh_widget_destroy(viewer->widget);
121 }
122
123 /***************************************************************/
124 MimeViewer *lh_viewer_create()
125 {
126         debug_print("LH: viewer_create\n");
127
128         LHViewer *viewer = g_new0(LHViewer, 1);
129         viewer->mimeviewer.factory = &lh_viewer_factory;
130         viewer->widget = lh_widget_new();
131
132         viewer->mimeviewer.get_widget = lh_get_widget;
133         viewer->mimeviewer.show_mimepart = lh_show_mimepart;
134
135         viewer->mimeviewer.clear_viewer = lh_clear_viewer;
136         viewer->mimeviewer.destroy_viewer = lh_destroy_viewer;
137
138         viewer->vbox = gtk_vbox_new(FALSE, 0);
139
140         GtkWidget *w = lh_widget_get_widget(viewer->widget);
141         gtk_box_pack_start(GTK_BOX(viewer->vbox), w,
142                         TRUE, TRUE, 1);
143
144         gtk_widget_show_all(viewer->vbox);
145
146         return (MimeViewer *)viewer;
147 }
148