Added support for local image attachments to the Litehtml plugin
[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 *utf8 = procmime_get_part_as_string(partinfo, TRUE);
90
91         if (utf8 == NULL) {
92                 g_warning("LH: couldn't get MIME part file\n");
93                 return;
94         }
95
96         lh_widget_set_partinfo(viewer->widget, partinfo);
97         lh_widget_open_html(viewer->widget, utf8);
98         g_free(utf8);
99 }
100
101 static void lh_clear_viewer(MimeViewer *_viewer)
102 {
103         debug_print("LH: clear_viewer\n");
104         LHViewer *viewer = (LHViewer *)_viewer;
105         lh_widget_clear(viewer->widget);
106 }
107
108 static void lh_destroy_viewer(MimeViewer *_viewer)
109 {
110         debug_print("LH: destroy_viewer\n");
111
112         /* Just in case. */
113         lh_clear_viewer(_viewer);
114
115 //      LHViewer *viewer = (LHViewer *)_viewer;
116 //      lh_widget_destroy(viewer->widget);
117 }
118
119 static void lh_print_viewer (MimeViewer *_viewer)
120 {
121     debug_print("LH: print_viewer\n");
122     
123     LHViewer* viewer = (LHViewer *) _viewer;
124     lh_widget_print(viewer->widget);    
125 }
126
127 /***************************************************************/
128 MimeViewer *lh_viewer_create()
129 {
130         debug_print("LH: viewer_create\n");
131
132         LHViewer *viewer = g_new0(LHViewer, 1);
133         viewer->mimeviewer.factory = &lh_viewer_factory;
134         viewer->widget = lh_widget_new();
135
136         viewer->mimeviewer.get_widget = lh_get_widget;
137         viewer->mimeviewer.show_mimepart = lh_show_mimepart;
138
139         viewer->mimeviewer.clear_viewer = lh_clear_viewer;
140         viewer->mimeviewer.destroy_viewer = lh_destroy_viewer;
141         
142         viewer->mimeviewer.print = lh_print_viewer;
143
144         viewer->vbox = gtk_vbox_new(FALSE, 0);
145
146         GtkWidget *w = lh_widget_get_widget(viewer->widget);
147         gtk_box_pack_start(GTK_BOX(viewer->vbox), w,
148                         TRUE, TRUE, 1);
149
150         gtk_widget_show_all(viewer->vbox);
151
152         return (MimeViewer *)viewer;
153 }
154
155 void lh_widget_statusbar_push(const gchar* msg)
156 {
157         MainWindow *mainwin = mainwindow_get_mainwindow();
158         STATUSBAR_PUSH(mainwin, msg);
159 }
160
161 void lh_widget_statusbar_pop()
162 {
163         MainWindow *mainwin = mainwindow_get_mainwindow();
164         STATUSBAR_POP(mainwin);
165 }