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