support image/x-eps format via pdf_viewer
[claws.git] / src / plugins / dillo / dillo_viewer.c
blob8a93511d73e9163d2dee0891fd0f4bbfdab260b5
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2023 the Claws Mail Team
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.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * 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, see <http://www.gnu.org/licenses/>.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #include "claws-features.h"
23 #endif
25 #include <unistd.h>
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.h>
30 #include <gtk/gtkx.h>
31 #include <gdk/gdkx.h>
33 #include "common/claws.h"
34 #include "common/version.h"
35 #include "plugin.h"
36 #include "file-utils.h"
37 #include "utils.h"
38 #include "mimeview.h"
39 #include "addr_compl.h"
41 #include "dillo_prefs.h"
43 #define PLUGIN_NAME (_("Dillo HTML Viewer"))
45 typedef struct _DilloViewer DilloViewer;
47 struct _DilloViewer
49 MimeViewer mimeviewer;
50 GtkWidget *widget;
51 GtkWidget *socket;
52 gchar *filename;
55 static MimeViewerFactory dillo_viewer_factory;
57 static GtkWidget *dillo_get_widget(MimeViewer *_viewer)
59 DilloViewer *viewer = (DilloViewer *) _viewer;
61 debug_print("dillo_get_widget\n");
63 return GTK_WIDGET(viewer->widget);
66 static gboolean socket_destroy_cb(GObject *object, gpointer data)
68 DilloViewer *viewer = (DilloViewer *) data;
69 debug_print("Destroyed dillo socket %p\n", viewer->socket);
70 viewer->socket = NULL;
71 return FALSE;
74 static gboolean load_images(DilloViewer *viewer)
76 MessageView *messageview = ((MimeViewer *)viewer)->mimeview
77 ? ((MimeViewer *)viewer)->mimeview->messageview
78 : NULL;
79 MsgInfo *msginfo = NULL;
80 gchar *ab_folderpath = NULL;
82 if (messageview == NULL)
83 return FALSE;
85 msginfo = messageview->msginfo;
87 if (msginfo == NULL)
88 return FALSE;
90 /* don't load remote images, period. */
91 if (dillo_prefs.local)
92 return FALSE;
94 /* don't do whitelisting -> load images */
95 if (!dillo_prefs.whitelist_ab)
96 return TRUE;
98 if (*dillo_prefs.whitelist_ab_folder != '\0' &&
99 strcasecmp(dillo_prefs.whitelist_ab_folder, _("Any")) != 0)
100 ab_folderpath = dillo_prefs.whitelist_ab_folder;
102 start_address_completion(ab_folderpath);
104 /* do whitelisting -> check sender */
105 if (found_in_addressbook(msginfo->from)) {
106 end_address_completion();
107 return TRUE;
110 end_address_completion();
111 return FALSE;
114 static void dillo_show_mimepart(MimeViewer *_viewer,
115 const gchar *infile,
116 MimeInfo *partinfo)
118 DilloViewer *viewer = (DilloViewer *) _viewer;
119 GdkWindow *gdkwin;
121 debug_print("dillo_show_mimepart\n");
123 if (viewer->filename != NULL) {
124 claws_unlink(viewer->filename);
125 g_free(viewer->filename);
128 #ifdef GDK_WINDOWING_X11
129 if (!GDK_IS_X11_DISPLAY(gdk_display_get_default())) {
130 debug_print("dillo viewer only works on X11\n");
131 return;
133 #else
134 debug_print("dillo viewer only works on X11\n");
135 return;
136 #endif
138 viewer->filename = procmime_get_tmp_file_name(partinfo);
140 if (!(procmime_get_part(viewer->filename, partinfo) < 0)) {
141 gchar *cmd;
143 if (viewer->socket)
144 gtk_widget_destroy(viewer->socket);
145 viewer->socket = gtk_socket_new();
146 debug_print("Adding dillo socket %p", viewer->socket);
147 gtk_container_add(GTK_CONTAINER(viewer->widget),
148 viewer->socket);
149 gtk_widget_realize(viewer->socket);
150 gtk_widget_show(viewer->socket);
151 g_signal_connect(G_OBJECT(viewer->socket), "destroy",
152 G_CALLBACK(socket_destroy_cb), viewer);
154 gdkwin = gtk_widget_get_window(viewer->socket);
155 cmd = g_strdup_printf("dillo %s%s-x %d \"%s\"",
156 (!load_images(viewer) ? "-l " : ""),
157 (dillo_prefs.full ? "-f " : ""),
158 (gint) GDK_WINDOW_XID(gdkwin),
159 viewer->filename);
161 execute_command_line(cmd, TRUE, NULL);
162 g_free(cmd);
166 static void dillo_clear_viewer(MimeViewer *_viewer)
168 DilloViewer *viewer = (DilloViewer *) _viewer;
170 debug_print("dillo_clear_viewer\n");
171 debug_print("Removing dillo socket %p\n", viewer->socket);
173 if (viewer->socket) {
174 gtk_widget_destroy(viewer->socket);
178 static void dillo_destroy_viewer(MimeViewer *_viewer)
180 DilloViewer *viewer = (DilloViewer *) _viewer;
182 debug_print("dillo_destroy_viewer\n");
184 if (viewer->socket) {
185 gtk_widget_destroy(viewer->socket);
188 g_object_unref(GTK_WIDGET(viewer->widget));
189 claws_unlink(viewer->filename);
190 g_free(viewer->filename);
191 g_free(viewer);
194 static MimeViewer *dillo_viewer_create(void)
196 DilloViewer *viewer;
198 debug_print("dillo_viewer_create\n");
200 viewer = g_new0(DilloViewer, 1);
201 viewer->mimeviewer.factory = &dillo_viewer_factory;
202 viewer->mimeviewer.get_widget = dillo_get_widget;
203 viewer->mimeviewer.show_mimepart = dillo_show_mimepart;
204 viewer->mimeviewer.clear_viewer = dillo_clear_viewer;
205 viewer->mimeviewer.destroy_viewer = dillo_destroy_viewer;
206 viewer->mimeviewer.get_selection = NULL;
207 viewer->widget = gtk_event_box_new();
209 gtk_widget_show(viewer->widget);
210 g_object_ref(viewer->widget);
212 viewer->filename = NULL;
214 return (MimeViewer *) viewer;
217 static gchar *content_types[] =
218 {"text/html", NULL};
220 static MimeViewerFactory dillo_viewer_factory =
222 content_types,
225 dillo_viewer_create,
228 gint plugin_init(gchar **error)
230 gchar *dillo_path = NULL;
231 if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
232 VERSION_NUMERIC, PLUGIN_NAME, error))
233 return -1;
235 if ((dillo_path = g_find_program_in_path("dillo")) == NULL) {
236 *error = g_strdup(_("Can't find the dillo executable in PATH. Is it installed?"));
237 return -1;
239 g_free(dillo_path);
241 dillo_prefs_init();
243 mimeview_register_viewer_factory(&dillo_viewer_factory);
245 return 0;
248 gboolean plugin_done(void)
250 mimeview_unregister_viewer_factory(&dillo_viewer_factory);
252 dillo_prefs_done();
253 return TRUE;
256 const gchar *plugin_name(void)
258 return PLUGIN_NAME;
261 const gchar *plugin_desc(void)
263 return _("This plugin renders HTML mail using the Dillo "
264 "web browser.\n"
265 "\n"
266 "Options can be found in /Configuration/Preferences/Plugins/Dillo Browser");
269 const gchar *plugin_type(void)
271 return "GTK3";
274 const gchar *plugin_licence(void)
276 return "GPL3+";
279 const gchar *plugin_version(void)
281 return VERSION;
284 struct PluginFeature *plugin_provides(void)
286 static struct PluginFeature features[] =
287 { {PLUGIN_MIMEVIEWER, "text/html"},
288 {PLUGIN_NOTHING, NULL}};
289 return features;