LiteHtml viewer: implement keyboard scroll
[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         const gchar *charset;
92
93         if (string == NULL) {
94                 g_warning("LH: couldn't get MIME part file\n");
95                 return;
96         }
97
98         charset = procmime_mimeinfo_get_parameter(partinfo, "charset");
99         if (charset != NULL && g_ascii_strcasecmp("utf-8", charset) != 0) {
100                 gsize length;
101                 GError *error = NULL;
102                 debug_print("LH: converting mimepart to UTF-8 from %s\n", charset);
103                 utf8 = g_convert(string, -1, "utf-8", charset, NULL, &length, &error);
104                 if (error) {
105                         g_warning("LH: failed mimepart conversion to UTF-8: %s", error->message);
106                         g_free(string);
107                         g_error_free(error);
108                         return;
109                 }
110                 debug_print("LH: successfully converted %" G_GSIZE_FORMAT " bytes\n", length);
111         } else {
112                 utf8 = string;
113         }
114
115         lh_widget_set_partinfo(viewer->widget, partinfo);
116         lh_widget_open_html(viewer->widget, utf8);
117         g_free(utf8);
118 }
119
120 static void lh_clear_viewer(MimeViewer *_viewer)
121 {
122         debug_print("LH: clear_viewer\n");
123         LHViewer *viewer = (LHViewer *)_viewer;
124         lh_widget_clear(viewer->widget);
125 }
126
127 static void lh_destroy_viewer(MimeViewer *_viewer)
128 {
129         LHViewer *viewer = (LHViewer *)_viewer;
130
131         debug_print("LH: destroy_viewer\n");
132         g_free(viewer);
133 }
134
135 static void lh_print_viewer (MimeViewer *_viewer)
136 {
137     debug_print("LH: print_viewer\n");
138     
139     LHViewer* viewer = (LHViewer *) _viewer;
140     lh_widget_print(viewer->widget);    
141 }
142
143
144 static gboolean lh_scroll_page(MimeViewer *_viewer, gboolean up)
145 {
146         LHViewer *viewer = (LHViewer *)_viewer;
147         GtkAdjustment *vadj = gtk_scrolled_window_get_vadjustment(
148                                         GTK_SCROLLED_WINDOW(lh_widget_get_widget(viewer->widget)));
149
150         if (viewer->widget == NULL)
151                 return FALSE;
152
153         return gtkutils_scroll_page(lh_widget_get_widget(viewer->widget), vadj, up);
154 }
155
156 static void lh_scroll_one_line(MimeViewer *_viewer, gboolean up)
157 {
158         LHViewer *viewer = (LHViewer *)_viewer;
159         GtkAdjustment *vadj = gtk_scrolled_window_get_vadjustment(
160                                         GTK_SCROLLED_WINDOW(lh_widget_get_widget(viewer->widget)));
161
162         if (viewer->widget == NULL)
163                 return;
164
165         gtkutils_scroll_one_line(lh_widget_get_widget(viewer->widget), vadj, up);
166 }
167
168 /***************************************************************/
169 MimeViewer *lh_viewer_create()
170 {
171         debug_print("LH: viewer_create\n");
172
173         LHViewer *viewer = g_new0(LHViewer, 1);
174         viewer->mimeviewer.factory = &lh_viewer_factory;
175         viewer->widget = lh_widget_new();
176
177         viewer->mimeviewer.get_widget = lh_get_widget;
178         viewer->mimeviewer.show_mimepart = lh_show_mimepart;
179
180         viewer->mimeviewer.clear_viewer = lh_clear_viewer;
181         viewer->mimeviewer.destroy_viewer = lh_destroy_viewer;
182
183         viewer->mimeviewer.scroll_page = lh_scroll_page;
184         viewer->mimeviewer.scroll_one_line = lh_scroll_one_line;
185
186         viewer->vbox = gtk_vbox_new(FALSE, 0);
187
188         GtkWidget *w = lh_widget_get_widget(viewer->widget);
189         gtk_box_pack_start(GTK_BOX(viewer->vbox), w,
190                         TRUE, TRUE, 1);
191
192         gtk_widget_show_all(viewer->vbox);
193
194         return (MimeViewer *)viewer;
195 }
196
197 void lh_widget_statusbar_push(const gchar* msg)
198 {
199         MainWindow *mainwin = mainwindow_get_mainwindow();
200         STATUSBAR_PUSH(mainwin, msg);
201 }
202
203 void lh_widget_statusbar_pop()
204 {
205         MainWindow *mainwin = mainwindow_get_mainwindow();
206         STATUSBAR_POP(mainwin);
207 }