Fix leak on error variable
[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) 2019 the Claws Mail Team
4  *
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.
16  */
17
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #include "claws-features.h"
21 #endif
22
23 #include <codeconv.h>
24 #include "common/utils.h"
25 #include "mainwindow.h"
26 #include "statusbar.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 = NULL;
48         gsize length;
49         GError *error = NULL;
50         gchar *locale = NULL;
51
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, ".");
55                 if (split) {
56                     locale = ++split;
57                 } else {
58                     locale = (gchar *) cur_locale;
59                 }
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);
63                     if (error) {
64                             debug_print("Failed convertion to current locale: %s\n", error->message);
65                             g_clear_error(&error);
66                         }
67             }
68             if (!utf8) {
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);
71                         if (error) {
72                                 debug_print("Charset detection failed. Use text as is\n");
73                                 utf8 = g_strdup(string);
74                                 g_clear_error(&error);
75                         }
76                 }
77         } else {
78                 utf8 = g_strdup(string);
79         }
80
81         return utf8;
82 }
83
84 static void lh_show_mimepart(MimeViewer *_viewer, const gchar *infile,
85                 MimeInfo *partinfo)
86 {
87         debug_print("LH: show_mimepart\n");
88         LHViewer *viewer = (LHViewer *)_viewer;
89         gchar *string = procmime_get_part_as_string(partinfo, TRUE);
90         gchar *utf8 = NULL;
91
92         if (string == NULL) {
93                 g_warning("LH: couldn't get MIME part file\n");
94                 return;
95         }
96
97         gchar *charset = procmime_mimeinfo_get_parameter(partinfo, "charset");
98         if (charset != NULL && g_ascii_strcasecmp("utf-8", charset) != 0) {
99                 gsize length;
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);
103                 if (error) {
104                         g_warning("LH: failed mimepart conversion to UTF-8: %s", error->message);
105                         g_free(string);
106                         g_error_free(error);
107                         return;
108                 }
109                 debug_print("LH: successfully converted %" G_GSIZE_FORMAT " bytes\n", length);
110         } else {
111                 utf8 = string;
112         }
113
114         lh_widget_set_partinfo(viewer->widget, partinfo);
115         lh_widget_open_html(viewer->widget, utf8);
116         g_free(utf8);
117 }
118
119 static void lh_clear_viewer(MimeViewer *_viewer)
120 {
121         debug_print("LH: clear_viewer\n");
122         LHViewer *viewer = (LHViewer *)_viewer;
123         lh_widget_clear(viewer->widget);
124 }
125
126 static void lh_destroy_viewer(MimeViewer *_viewer)
127 {
128         LHViewer *viewer = (LHViewer *)_viewer;
129
130         debug_print("LH: destroy_viewer\n");
131         g_free(viewer);
132 }
133
134 static void lh_print_viewer (MimeViewer *_viewer)
135 {
136     debug_print("LH: print_viewer\n");
137     
138     LHViewer* viewer = (LHViewer *) _viewer;
139     lh_widget_print(viewer->widget);    
140 }
141
142 /***************************************************************/
143 MimeViewer *lh_viewer_create()
144 {
145         debug_print("LH: viewer_create\n");
146
147         LHViewer *viewer = g_new0(LHViewer, 1);
148         viewer->mimeviewer.factory = &lh_viewer_factory;
149         viewer->widget = lh_widget_new();
150
151         viewer->mimeviewer.get_widget = lh_get_widget;
152         viewer->mimeviewer.show_mimepart = lh_show_mimepart;
153
154         viewer->mimeviewer.clear_viewer = lh_clear_viewer;
155         viewer->mimeviewer.destroy_viewer = lh_destroy_viewer;
156         
157         viewer->mimeviewer.print = lh_print_viewer;
158
159         viewer->vbox = gtk_vbox_new(FALSE, 0);
160
161         GtkWidget *w = lh_widget_get_widget(viewer->widget);
162         gtk_box_pack_start(GTK_BOX(viewer->vbox), w,
163                         TRUE, TRUE, 1);
164
165         gtk_widget_show_all(viewer->vbox);
166
167         return (MimeViewer *)viewer;
168 }
169
170 void lh_widget_statusbar_push(const gchar* msg)
171 {
172         MainWindow *mainwin = mainwindow_get_mainwindow();
173         STATUSBAR_PUSH(mainwin, msg);
174 }
175
176 void lh_widget_statusbar_pop()
177 {
178         MainWindow *mainwin = mainwindow_get_mainwindow();
179         STATUSBAR_POP(mainwin);
180 }